DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Modify JSON Data in Postgres and Hibernate 6
  • Top 10 C# Keywords and Features

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • The End of “Good Enough Agile”
  • Event Driven Architecture (EDA) - Optimizer or Complicator
  1. DZone
  2. Coding
  3. Languages
  4. CSS Grid Solution to the Problems of Float and Flexbox

CSS Grid Solution to the Problems of Float and Flexbox

This article examines the use of CSS Grid and explains how it can be used in comparison to other tools when developing a website layout.

By 
Arnau Silvestre user avatar
Arnau Silvestre
·
May. 28, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.8K Views

Join the DZone community and get the full member experience.

Join For Free

Before going on to explain what CSS Grid is and what it is for, I will explain the current state of the tools we have when developing a layout for our website.

Let’s suppose we want to create the following layout:

Layout

We should use an HTML with each necessary section, for example:

HTML
 




x


 
1
<body>
2
<nav>Navbar</nav>
3
<main class="container">
4
    <section class="left">Section</section>
5
    <section class="center">Section</section>
6
    <section class="right">Section</section>
7
</main>
8
<footer>Footer</footer>
9
</body>



Using this HTML we will add the styles using float, flexbox, and CSS grid.

Float

When using float we are facing some of the main problems of creating a layout, since the float property is not designed to create out layouts, despite being used for that. As mentioned in the float specification, the use of float is limited to specifying that an element will float to the right or to the left of the container and the rest of its content will float around it. However, the use of floats to position elements in our layout can lead to a series of strange bugs that require “hacks” to solve them, as mentioned in this post about floats. To achieve the following layout, we should use a CSS like this (I have only added the necessary CSS, in the previous link the complete code appears):

CSS
 




xxxxxxxxxx
1
22


 
1
.container {
2
  text-align: center; /* necesario para la class .center */
3
}
4

          
5
.container:after {
6
  content: "";
7
  display: table;
8
  clear: both;
9
}
10

          
11
.left {
12
  float: left;
13
}
14

          
15
.center {
16
  margin: 0 auto;
17
  display: inline-block;
18
}
19

          
20
.right {
21
  float: right;
22
}



Just by looking at the resulting CSS, we can see how we need to apply “patches” to center the .center class and use clearfix hack to prevent the .container class from losing its size by having its children as floating elements.

To deal with all these hacks needed when designing, flexbox emerged, which provides a significant improvement over float.

Flexbox

Flexbox allows you to create out layouts that adapt to the size of the screen and that free us from the disadvantages of using float, such as the adjustment of our elements in certain screen sizes. Despite this, one of the disadvantages of Flexbox is that it only allows us to organize our elements horizontally or vertically, but not both at the same time, which greatly limits our possibilities. To create the following layout we should use a CSS like this:

CSS
 




xxxxxxxxxx
1
16


 
1
.container {
2
  display: flex;
3
  justify-content: space-between;
4
}
5

          
6
.left {
7
  background-color: #dc4d4d;
8
}
9

          
10
.center {
11
  background-color: yellow;
12
}
13

          
14
.right {
15
  background-color: #e8a2af;
16
}



In the previous code, we see how it is no longer necessary to specify anything in our three columns, we simply modify the properties of the class .container and we can place our elements comfortably. Despite not being appreciated in this example, and as mentioned above, Flexbox works very well in a single direction either vertical or horizontal, but if we want to work in a layout positioning element in both directions we face another common problem, not only in Flexbox but also using floats.

Using Unnecessary Tags

Another disadvantage when using both Flexbox and float is the fact that we have to group our elements in boxes, since we need a specific group of elements to be positioned together in the same area. In the case of Flexbox, assuming that we are working in a horizontal layout we should group all the elements we want vertically into a new parent tag, which will contain all the elements to be positioned vertically and specify the vertical direction there. Doing this forces us to add unnecessary HTML tags that do not provide any semantic value.

XML
 




xxxxxxxxxx
1
14


 
1
<body>
2
<nav></nav>
3
<main class="container">
4
    <section class="left"></section>
5
    <section class="center"></section>
6
    <section class="right"></section>
7
    <div class="container container--vertical">
8
        <section class="first"></section>
9
        <section class="second"></section>
10
        ...
11
    </div>
12
</main>
13
<footer></footer>
14
</body>


In the previous example we can see how we need to add a new <div> to encapsulate the elements that we want to position vertically and despite being a single extra element, this large scale can lead us to have hundreds of tags without any semantic value, so that we would have a complex html without semantics.

CSS Grid Solution

To solve the problems when using float or Flexbox appears CSS Grid, which does not force us to use “hacks” to achieve the expected behavior of our CSS and offers the possibility of designing a layout in both directions without the need to add extra and unnecessary elements to achieve it. Another advantage of using grid, and perhaps one of the most important, is the fact that it manages to separate our HTML from the CSS, we no longer need to modify or adapt our HTML to behave well with our CSS, with grid we get that HTML just be what it should be, markup.

CSS Grid

Given that most browsers support CSS grid, it has become an option to be taken into account more and more likely to replace Flexbox, which is its most recent competitor.

The main concepts of CSS grid are very similar to Flexbox: we have a container that includes a series of elements on which grid properties are applied.

We will start to apply the display property: grid to our .container, which will indicate that our container is a grid, but by not specifying anything else it will simply stack the divs one on top of the other.

CSS
 




xxxxxxxxxx
1


 
1
.container {
2
  display: grid;
3
}


Stacking divs

Link a fiddle.

grid-template-columns and grid-template-rows

Now we will specify the form of our layout. Since we want three columns and a single row, we will use the properties grid-template-columns and grid-template-rows, to which we pass the value we want for each of our rows or columns.

CSS
 




xxxxxxxxxx
1


 
1
.container {
2
  display: grid;
3
  grid-template-columns: 33.33% 33.33% 33.33%;
4
  grid-template-rows: 100%;
5
}


 Three sections in single row

grid-column and grid-row

We will continue positioning the elements: for this we can use the grid-column and grid-row properties. Assigning the beginning and end of this, for example, grid-column-start and grid-column-end, we can define in which line our columns and rows begin and end. This gives us a lot of flexibility when it comes to creating layouts, and in turn is a very simple tool to use. In the following image we will see how the lines are distributed, starting with the 1 (most browsers show a grid if we inspect an element with a grid display, which helps differentiate these lines):

Columns and rows

Image Source

CSS
 




xxxxxxxxxx
1
10


 
1
.container {
2
  ...
3
  grid-template-rows: 100px 100px; /* Usamos 100px como tamaño de las rows ya que es nuestro height fixo para secciones */
4
}
5

          
6
.left {
7
  grid-column-start: 1;
8
  grid-column-end: 3;
9
  /* también se puede resumir como grid-column: 1/3 */
10
}


Stacked sections

link a fiddle

 

repeat(), fr and grid-gap

Now we will start creating more elaborate layouts. To begin with, we will use the unit ab abbreviation of fraction that takes the assigned value from the total available space of the container. This unit is very useful as it prevents us from unnecessary calculations. We will also use the repeat () function that accepts the number of times to repeat as the first parameter and a value as the second parameter. Using this function will prevent us from repeating the columns or rows as for example grid-template-rows: 1fr 1fr 1fr.... Finally, we will use the grid-gap property, which is the abbreviation of grid-row-gapy grid-column- gap, in this order, we can assign a general value for both or a specific value to each one.

CSS
 




xxxxxxxxxx
1


 
1
.container {
2
  ...
3
  grid-template-column: repeat(3, 1fr);
4
  grid-template-row: repeat(3, auto);
5
  grid-gap: 8px;
6
}


 Layout

grid-template-areas

Added this, we will use one of the most useful and innovative properties of CSS grid, the property grid-template-areas. This allows us to define a “scheme” in which we can define which element we want in each cell of our grid. To each of the elements we will assign a grid-area property, which will be the name of the area we want to use. We have defined that we have 3 columns and 3 rows, so our template will be like this:

CSS
 




xxxxxxxxxx
1
22


 
1
.container {
2
  ...
3
  grid-template-areas: 
4
    "f f f"
5
    "s s s"
6
    "t t t";
7
}
8

          
9
.left {
10
  ...
11
  grid-area: f
12
}
13

          
14
.center {
15
  ...
16
  grid-area: s
17
}
18

          
19
.right {
20
  ...
21
  grid-area: t
22
}


Layout

Being able to design our layout, apart from being very fast and intuitive, allows us to completely modify the structure of the HTML without having to touch any of it, which is known as source order independence. Imagine that we want to change the layout for mobile:

Java
 




xxxxxxxxxx
1


 
1
@media screen and (max-width: 640px) {
2
  .container {
3
    ...
4
    grid-template-areas: 
5
      "t t t"
6
      "s s s"
7
      "f f f";
8
  }
9
}



Simply with this change in our CSS we have modified our entire layout completely, something that is impossible to do without CSS grid.

This incredible property allows us to modify our layouts very quickly, allowing us to create simple mockups very fast and we can move different elements of our layout to see how we like them more.

CSS
 




xxxxxxxxxx
1
11


 
1
.container {
2
  ...  
3
  grid-template-areas: 
4
    "t . f"
5
    "t s s"
6
    "t s s";
7
}
8

          
9
.section {
10
  // height: 100px;
11
}


Rearranged layout

I added a link to codepen in which I could modify the property grid-template-areas and thus be able to see how the layout changes.

When to Use CSS Grid

As we have already said, Flexbox opts for an approach based on single-direction containers while CSS grid opts for a bidirectional layout. As a result of this, Flexbox is better suited for the use of components, since most of the content of a component is usually in one direction since it is usually small-scale content, whereas if we require a more complex layout, with varying directions, we would use CSS grid.

It is also necessary to take into account the support of both browsers, because if we want to support IE browsers we should opt for Flexbox, since having more use has polyfills adapted for IE.

CSS Data Types Element Property (programming) HTML

Published at DZone with permission of Arnau Silvestre. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Modify JSON Data in Postgres and Hibernate 6
  • Top 10 C# Keywords and Features

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!