Posted in css grid
7144
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
174
This Month
633
This Year
1038

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

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

<blend-mode>

The <blend-mode> CSS data type describes how colors should appear when elements overlap. It is used in the background-blend-mode and mix-blend-mode properties.
<blend-mode> css reference