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

maccaj51Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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 */
}
}
0
Kyle HamiltonData ScientistCommented:
if you use media queries you can specify the width of each grid item based on screen width. no need for jquery.

here's an example:

<!DOCTYPE html >
<html>
<head>
<meta charset="UTF-8" />
<title>Grid</title>
<style type="text/css">
body {
	margin: 0px;
}
#container {
	width: 100%;
	background-color: #033;
	overflow: auto;
}
.grid {
	
	height: 240px;
	float:left;
	width:18%;
-webkit-box-sizing:border-box;
   -moz-box-sizing:border-box;
    -ms-box-sizing:border-box;
		box-sizing:border-box;
	margin-right:2.5%;
	margin-bottom:2.5%;
	background-color: #0F6;

}
.grid:nth-of-type(5n) {
	margin-right: 0;
}
.grid:nth-of-type(5n) + .grid{
	clear:left;
}


#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;
}


/* ipad and below */ 
@media screen and (max-width: 768px){

  .grid{
    width:23.125%;
  }
  .grid:nth-of-type(5n) {
    margin-right: 2.5%;
  }
  .grid:nth-of-type(5n) + .grid{
    clear:none;
  }
  .grid:nth-of-type(4n) {
    margin-right: 0;
  }

  .grid:nth-of-type(4n) + .grid{
    clear: left;
  }


}
/* below ipad */
@media (max-width: 767px) {




}
/* phone and below */
@media screen and (max-width: 640px){
  

}

/* iphone landscape */ 
@media screen and (max-width: 480px){
  .grid{
    width:31.66666666666667%;
  }
  
  
  .grid:nth-of-type(5n) {
    margin-right: 2.5%;
  }
  .grid:nth-of-type(5n) + .grid{
    clear:none;
  }
  .grid:nth-of-type(4n) {
    margin-right: 2.5%;
  }
  .grid:nth-of-type(4n) + .grid{
    clear:none;
  }
  .grid:nth-of-type(3n) {
    margin-right: 0;
  }

  .grid:nth-of-type(3n) + .grid{
    clear:left;
  }




}

/* iphone portrait */ 
@media screen and (max-width: 320px){
   
  .grid{
    width:48.75%;
  }
  .grid:nth-of-type(even) {
    margin-right: 0 !important;
  }

  .grid:nth-of-type(even) + .grid{
    clear:left;
  }
  .grid:nth-of-type(odd) {
    margin-right: 2.5% !important;
  }
  .grid:nth-of-type(odd) + .grid{
    clear:none;
  }


}

/* above ipad */
@media (min-width: 769px) {
  .grid:nth-of-type(5n) {
    margin-right: 0;
  }
  .grid:nth-of-type(5n) + .grid{
      clear:left;
    }
  .grid {
    margin-right: 2.5%;
    margin-bottom: 2.5%;
  }
}


</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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maccaj51Author Commented:
Many thanks, very thorough
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.