Building off of a previous question, I'm trying to add bigger blocks to my grid layout. In the last question, I needed to have a div span multiple rows. The problem now is that I need a div to span multiple rows and columns.
If I have a five-element row, how could I put bigger elements in the middle of it? (as float
puts it naturally on the side).
Here's an example snippet:
#wrapper{
width: 516px;
}
.block{
display: inline-block;
width: 90px;
height: 50px;
margin: 5px;
background-color: red;
}
.bigger{
height: 110px;
}
.larger{
height: 110px;
width: 190px;
}
<div id="wrapper">
<div class="block"></div>
<div class="block bigger"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block larger"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
I don't want to use display: grid
for the wrapper element, as Can I Use states this is a pretty on-the-edge technology right now. I was hoping for a non-grid, non-table solution.
Here's what I would like to have from the snippet above
Defining columns in HTML More columns can be added by adding more divs with the same class. The following syntax is used to add columns in HTML. <div class="row"> tag is used to initialize the row where all the columns will be added. <div class="column" > tag is used to add the corresponding number of columns.
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.
I understand that you are seeking an answer that doesn't involve HTML Tables or CSS Grid Layout. You mention that you don't want Grid because of weak browser support.
However, around the same time you posted your question, most major browsers released new versions which provide full support for Grid Layout (see details below).
CSS Grid makes your layout simple. There is no need to alter your HTML, add nested containers or set a fixed height on the container (see my flexbox answer on this page).
#wrapper {
display: grid; /* 1 */
grid-template-columns: repeat(5, 90px); /* 2 */
grid-auto-rows: 50px; /* 3 */
grid-gap: 10px; /* 4 */
width: 516px;
}
.tall {
grid-row: 1 / 3; /* 5 */
grid-column: 2 / 3; /* 5 */
}
.wide {
grid-row: 2 / 4; /* 6 */
grid-column: 3 / 5; /* 6 */
}
.block {
background-color: red;
}
<div id="wrapper">
<div class="block"></div>
<div class="block tall"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block wide"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
How it works:
grid-template-columns
property sets the width of explicitly defined columns. In this case, the grid is instructed to create a 90px width column, and repeat the process 5 times. grid-auto-rows
property sets the height of automatically generated (implicit) rows. Here each row is 50px tall.grid-gap
property is a shorthand for grid-column-gap
and grid-row-gap
. This rule sets a 10px gap between grid items. (It doesn't apply to the area between items and the container.).tall
item to span from row lines 1 to 3 and column lines 2 to 3.*.wide
item to span from row lines 2 to 4 and column lines 3 to 5.** In a five-column grid there are six column lines. In a three-row grid there are four row lines.
Browser Support for CSS Grid
Here's the complete picture: http://caniuse.com/#search=grid
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With