Posted in css grid
8586
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
224
This Month
905
This Year
2480

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 here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
In this case my anchor this week becomes driving almost 2hrs outside of Atlanta to one of my favorite hard core gyms in the world.. MetroFlex aka The Dungeon. The gym owners turn the heat way up so it becomes a fun sweat box and the gym members just watch from afar and leave me alone. I happily drive myself long distances to find MY ANCHOR. Our anchor allows us to have balance, focus and be as productive as possible. And if you're in the middle of a heavy set and your headphones start to fall off your head, like mine did here.. well.. f*ck the headphones. Let em break and fall. You can always get a new pair, but the iron ain't ever gonna lift itself.
Unknown
Random CSS Property

@media

The @media CSS at-rule can be used to apply part of a style sheet based on the result of one or more media queries. With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.
@media css reference