search
HomeWeb Front-endCSS TutorialAnalysis of CSS3 grid

Analysis of CSS3 grid

Jun 21, 2018 pm 05:45 PM
css3new featuresgrid

In this article, we will look at some new CSS3 properties to make working with grids easier with HTML and CSS. But first let’s discuss a little history of HTML and CSS grids, and understand why it was so difficult before.

1. A brief history of grids

Once upon a time, our layouts were a mess. Tables and frames are the primary tools for creating multi-column layouts. While they get the job done (and are actually pretty bad at it).

Set your sights to today. HTML and CSS have become very complex, and web design is growing in popularity and sophistication every day. The old layout methods we used to use are clearly out. However, a legacy issue surfaced: multi-column layout.

To complicate things further, our page width is no longer static. Responsiveness is all the rage, so we tend to favor percentage-based column widths. A simple grid based on a fixed 960 pixel width no longer works – we need a fluid grid.

There is a problem with the method of using floats to solve columns in the CSS2 specification. To prevent parent elements from breaking your layout, we need to add a clearfix. Through this method, the height collapse problem of the parent element is corrected (the floating element breaks away from the standard flow, and the parent element will think that the floating resource does not exist). We mostly accept this approach, but many still consider it a hack.

The method via inline-box is less common, but still exists. Inline elements will remain on one line, in their natural order. When a row is full, subsequent elements are naturally folded to the next row. But because it behaves like text, it behaves like text. This means you have to avoid whitespace elements (spaces, tabs, newlines...) between HTML elements. Inline-block is not designed for this, and it does not work very well.

Of these two methods, the floating method is more reliable. That's why it's more popular and ranked first. However, after creating multiple columns, we find that we need to compress the content again because we need some padding distance. This leads to the final question: What is the box model? To put it simply, the actual size of an element includes: height/width, padding, and side width. The outer border does not make the box larger, it just adds space between itself and other elements. So when you set the width to, say, 25%, the actual width of the box is much larger than that, which means there isn't enough room for four elements in one row.

There are different solutions to this annoying problem: negative margins, nested elements – that’s all I know. They all require additional CSS or DOM elements and count as hacks. Let's face it, there is no good way to solve grids in CSS2.

Today, however, CSS3 provides good support, and the specification has added several new features specifically for grids. What are these characteristics? How do we use them? Let's take a look.

2. box-sizing: border-box

One of the problems that has been solved is the nature of the extended box model. This problem can be solved by setting the value of box-sizing to border-box. By reducing the width of the content, the distance between the sides and the padding is also calculated into the width attribute.

HTML

<p class="row">
  <p class="column">Col one</p>
  <p class="column">Col two</p>
  <p class="column">Col three</p>
  <p class="column">Col four</p>
</p>

CSS

.row:after {
  clear: both;
  content: &#39;&#39;;
  display: block;
}
.column {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  float: left;
  min-height: 8em;
  overflow: hidden;
  padding: 2em;
  width: 25%;
}
.column:nth-child(1) { background-color: #9df5ba; }
.column:nth-child(2) { background-color: #9df5d7; }
.column:nth-child(3) { background-color: #9df5f5; }
.column:nth-child(4) { background-color: #9dd7f5; }

Effect


While this works great, we still need to use Float, we still need to clear the float. Furthermore, we can only use padding as space for the element, margins no longer play a role. This means there is no actual space between the fast elements, but rather its contents. While this is very useful for many designs, it still feels like a small mistake.

1.Firefox 1

2.Chrome 1

3.IE 8
4.Opera 7
5.Safari 3

3. width: calc( Percent – ​​Distance)

Another great option is to use the calc() function. It allows us to calculate the true width of an element - in different units - without relying on JavaScript!

HTML

<p class="row">
  <p class="column">Col one</p>
  <p class="column">Col two</p>
  <p class="column">Col three</p>
  <p class="column">Col four</p>
</p>

CSS

.row { margin-left: -1em; }</p> <p>.row:after {
 clear: both;
 content: &#39;&#39;;
 display: block;
}
.column {
 float: left;
 margin-left: 1em;
 min-height: 8em;
 padding: 1em;
 width: -webkit-calc(25% - 3em);
 width: -moz-calc(25% - 3em);
 width: calc(25% - 3em);
}
.column:nth-child(1) { background-color: #9df5ba; }
.column:nth-child(2) { background-color: #9df5d7; }
.column:nth-child(3) { background-color: #9df5f5; }
.column:nth-child(4) { background-color: #9dd7f5; }

Effect:


The ability to recalculate the actual size is a Great option, but unfortunately we still need floats and we also need negative margins for the column's container. Ditto, a great choice, but still somewhat flawed.

1.Firefox 4

2.Chrome 19

3.IE 9
4.Opera ?
5.Safari 6 (appears to be a little buggy)

4. Flexbox

The flex layout box is an element with specific configuration behavior-a bit like a table. is this real? Yes, that's right. The behavior of the table is actually quite nice, as its display changes based on its content. But table layouts are no longer used, so table tags are not an option.

At first, the telescoping box looked complicated. There are many attributes that are difficult to understand, especially for someone like me who is not good at speaking in English. Luckily, Chris Coyier wrote a great guide on retractable boxes that I have to mention.

HTML

<p class="row">
  <p class="column">Col one</p>
  <p class="column">Col two</p>
  <p class="column">Col three</p>
  <p class="column">Col four</p>
</p>

CSS

.row {
    display: -webkit-flex;
    -webkit-flex-direction: row;
    -webkit-flex-wrap: nowrap;
    -webkit-justify-content: space-between;</p>
<p>    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-between;
}
.column {
    margin: 0.5em;
    min-height: 8em;
    padding: 1em;
    width: 25%;
}
.column:nth-child(1) { background-color: #9df5ba; }
.column:nth-child(2) { background-color: #9df5d7; }
.column:nth-child(3) { background-color: #9df5f5; }
.column:nth-child(4) { background-color: #9dd7f5; }

Effect:


5. Conclusion

Although CSS3 brings many new features and fixes some historical issues, in my opinion, flex box layout is the only non-hack way to create a flexible grid with CSS. Unfortunately, however, browser support is mediocre. Nonetheless, other methods enrich the presentation, so they are an improvement, and they have good browser support.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

The beautification and function of div imitation checkbox form style

CSS page has left, middle and right columns Implementation of layout

The above is the detailed content of Analysis of CSS3 grid. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How much specificity do @rules have, like @keyframes and @media?How much specificity do @rules have, like @keyframes and @media?Apr 18, 2025 am 11:34 AM

I got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?

Can you nest @media and @support queries?Can you nest @media and @support queries?Apr 18, 2025 am 11:32 AM

Yes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.

Quick Gulp Cache BustingQuick Gulp Cache BustingApr 18, 2025 am 11:23 AM

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

In Search of a Stack That Monitors the Quality and Complexity of CSSIn Search of a Stack That Monitors the Quality and Complexity of CSSApr 18, 2025 am 11:22 AM

Many developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have

Datalist is for suggesting values without enforcing valuesDatalist is for suggesting values without enforcing valuesApr 18, 2025 am 11:08 AM

Have you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of

Front Conference in ZürichFront Conference in ZürichApr 18, 2025 am 11:03 AM

I'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited

Building a Full-Stack Serverless Application with Cloudflare WorkersBuilding a Full-Stack Serverless Application with Cloudflare WorkersApr 18, 2025 am 10:58 AM

One of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details

Creating Dynamic Routes in a Nuxt ApplicationCreating Dynamic Routes in a Nuxt ApplicationApr 18, 2025 am 10:53 AM

In this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor