Posted in css grid
473
12:37 am, December 7, 2021

Using CSS Grid instead of Float's for a 3 column simple list

I have been in the habit of just using float:left and then width 31% with margin 1% for list items, and it seems to work fine usually. 

But i thought i probably should update the defaults in by brain to use the css grid for these kind of items, so i will document this here. 

You can see the margins are much neater, and makes it much easier to move things around in the grid.

HTML

<!-- The old float layout -->
<h2>The old Float Layout</h2>
<p>Now that i look at this code it seems quite simple and neat.</p>
<div class='f-container'>
  <div class='f-item'>Item 1</div>
  <div class='f-item'>Item 2</div>
  <div class='f-item'>Item 3</div>
</div>
<p>Now lets have a look at this same layout with css grid, and see what is improved. </p>
<h2>The New CSS Grid layout</h2>
<p>You can see that the code is a bit neater here, and the grid is all specified in the container. You can specify the width of each item, and then also add a px gap still, and the items go all the way to the edge of the container, so its definately better. </p>
<p>It seems that grid's are much neater and easier to control the layout. I will try and start using this rather than floats!</p>
<div class='g-container'>
  <div class='g-item'>Item 1</div>
  <div class='g-item'>Item 2</div>
  <div class='g-item'>Item 3</div>
</div>

<p>So if i wanted to make the middle item 50% i could just change that in the container grid, rather than having to add a class to the item, or selecting it with n-th. </p>
<div class='g-container-2'>
  <div class='g-item'>Item 1</div>
  <div class='g-item'>Item 2</div>
  <div class='g-item'>Item 3</div>
</div>

CSS

/* The old float layout */
.f-container {
	overflow:auto;
  	clear: both;
  	width:100%;
  	margin-bottom:20px;
}
.f-item {
  	width:31%;
  	margin:0 1%;
	float:left;
  	border-radius:5px;
  	background:teal;
  	padding:30px;
}
/* The old float layout */

/* The New CSS Grid layout */
.g-container {
	display: grid;
  	grid-template-columns: 33.33% 33.33% 33.33%;
  	grid-template-rows: 100%;  
  	grid-gap: 10px;
  	margin-bottom:20px;
}
.g-item {
  	border-radius:5px;
  	background:purple;
  	padding:30px;  
}
/* The New CSS Grid layout */

/* Different Grid Item Widths */
.g-container-2 {
	display: grid;
  	grid-template-columns: 25% 50% 25%;
  	grid-template-rows: 100%;  
  	grid-gap: 10px;
  	margin-bottom:20px;
}
/* Different Grid Item Widths */

The old Float Layout

Now that i look at this code it seems quite simple and neat.

Item 1
Item 2
Item 3

Now lets have a look at this same layout with css grid, and see what is improved.

The New CSS Grid layout

You can see that the code is a bit neater here, and the grid is all specified in the container. You can specify the width of each item, and then also add a px gap still, and the items go all the way to the edge of the container, so its definately better.

It seems that grid's are much neater and easier to control the layout. I will try and start using this rather than floats!

Item 1
Item 2
Item 3

So if i wanted to make the middle item 50% i could just change that in the container grid, rather than having to add a class to the item, or selecting it with n-th.

Item 1
Item 2
Item 3

View Statistics
This Week
13
This Month
165
This Year
165

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!

❤👩‍💻🕹

Random Quote
Treasure every moment that you have and treasure it more because you shared it with someone special, special enough to spend your time...and remember that time waits for no one.
Unknown