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">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<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;
<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>

Open in new window


Avatar of undefined
Last Comment
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

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 */
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of maccaj51
Flag of Afghanistan image


Many thanks, very thorough

HTML (HyperText Markup Language) is the main markup language for creating web pages and other information to be displayed in a web browser, providing both the structure and content for what is sent from a web server through the use of tags. The current implementation of the HTML specification is HTML5.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo