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
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment