Avatar of maccaj51
maccaj51Flag for Afghanistan asked on

Css & Jquery: Width, Min Width, Max Width

Hi Experts,

I am pretty sure I know the answer to this already, but thought I'd get a set of extra eyes on it.

I have the code below and I'm trying to get the grids to fill the container within the css width parameters that I have set.

At the moment it happens like this:
1) When the container becomes to small it kicks a grid to the next row
2) The grids then get smaller to try and stay on the rows they are currently on
3) They then stay at min width until they all cascading down one by one.

Ideally I would like them to fill the container across as much as possible until one jumps to next row... but then they all go back to max width and process happens again.

Now I am sure this will have to be a jquery window load and resize function... but was wondering whether anyone could point out an easier way or help down the path to creating this function as best possible

Many Thanks in advance

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Grid</title>
<style type="text/css">
body {
	margin: 0px;
}
#container {
	width: 100%;
	background-color: #033;
	overflow: auto;
}
#container .grid {
	display: inline;
	float: left;
	height: 240px;
	background-color: #0F6;
	min-width: 240px;
	max-width: 320px;
	width: 25%;  /* Starting at 25% as this would create 4 across on popular screen resolutions */
}
#container #grid1 {
	background-color: #c42;
}
#container #grid2 {
	background-color: #3C6;
}
#container #grid3 {
	background-color: #CC6;
}
#container #grid4 {
	background-color: #993;
}
#container #grid5 {
	background-color: #633;
}
#container #grid6 {
	background-color: #C96;
}
</style>
</head>
<body>
<div id="container">
  <div class="grid" id="grid1"> </div>
  <div class="grid" id="grid2"> </div>
  <div class="grid" id="grid3"> </div>
  <div class="grid" id="grid4"> </div>
  <div class="grid" id="grid5"> </div>
  <div class="grid" id="grid6"> </div>
</div>
</body>
</html>

Open in new window

jQueryHTMLCSS

Avatar of undefined
Last Comment
maccaj51

8/22/2022 - Mon
Scott Fell

Are you familiar with media queries in your css? https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries

As the screen goes smaller, you want to use a lower min width and height.  You can have 3 or 4 media queries.

@media (max-width: 600px) {
  #container .grid {
 /* display: inline;   this should be a block */
      float: left;
      height: 240px;  /* change for each size */
      background-color: #0F6;
      min-width: 240px;  /* change for each size */
      max-width: 320px;  /* change for each size */
      width: 25%;  /* Starting at 25% as this would create 4 across on popular screen resolutions */
}
}
ASKER CERTIFIED SOLUTION
Kyle Hamilton

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
maccaj51

Many thanks, very thorough
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy