Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive 4 box grid from two to one columns

I'm a beginner in css and I have a little problem. I tested different methods to handle a responsive 4 div grid with css, and I failed.

I want to responsively arrange the 4 divs as an grid with 2 columns and, if the display is to narrow it should be floating to a one column layout.

Here is a sketch of the responsive grid:

responsive grid from two to one columns

like image 797
user3668869 Avatar asked May 23 '14 12:05

user3668869


People also ask

How do you create a grid with 4 columns of equal width?

If you really need the columns to be the exact same width you should use: grid-template-columns: repeat(3, minmax(0, 1fr)); minmax(0, 1fr) allows the grid tracks to be as small as 0 but as large as 1fr , creating columns that will stay equal.

How do I make my display grid responsive?

Building a Responsive Grid-View First ensure that all HTML elements have the box-sizing property set to border-box . This makes sure that the padding and border are included in the total width and height of the elements. Read more about the box-sizing property in our CSS Box Sizing chapter.

How many columns are present in responsive fluid grid system layout?

Material Design provides responsive layouts based on 4-column, 8-column, and 12-column grids, available for use across different screens, devices, and orientations. Each breakpoint range determines the number of columns, and recommended margins and gutters for each display size.

What is Col lg 4?

So, you can control the width of column using specifying numbers in . col-*, where * means the width of the column in numbers. col-md-4: This class is used when the device size is medium or greater than 768px and the maximum width of container is 720px and you want the width equal to 4 columns.


1 Answers

Here is a simple responsive grid with 4 div boxes in plain CSS and HTML it aranges from two to one columns when the browser width becomes smaller :

DEMO (resize the result window to see the effect)

Note that the max-width value on the #container is set to 450px so that 2 blocks + their margin can fit by width in two colmuns.

When the widow is smaller than 450px the width of the #container adapts to the window width and as the block can't fit anymore, they rearage to one column.

#container {
  text-align: center;
  max-width: 450px;
  margin: 0 auto;
}
.block {
  width: 200px;
  height: 200px;
  margin: 10px;
  display: inline-block;
  background: #00CC99;
}
<div id="container">
  <div class="block">1</div>
  <div class="block">2</div>
  <div class="block">3</div>
  <div class="block">4</div>
</div>
like image 80
web-tiki Avatar answered Sep 20 '22 19:09

web-tiki