Home Web Front-end CSS Tutorial Talk about grid layout in css (graphics and text)

Talk about grid layout in css (graphics and text)

Jan 13, 2021 pm 06:53 PM
css grid layout

Talk about grid layout in css (graphics and text)

(Learning video sharing: css video tutorial)

Grid layout can divide the web page into rows and columns of simple attributes, CSS page layout Technology allows us to pick up elements in a web page and control their position relative to the normal layout flow, surrounding elements, parent containers, or the main viewport/window.

Holy Grail Layout

Holy Grail Layout is a three-column layout with fixed width on both sides and adaptive middle:

css:

* {
        box-sizing: border-box;
    }
    html, body{
        width: 100%;
        height: 100%;
        margin: 0;
    }
    .container{
        width:100%;
    }
    .container:after{
        display: table;
        content:".";
        clear:both;
    }
    
    .container .cl{
        float:left;
        border: 1px solid red;
        height: 200px;
    }
    
    .main{
        width:100%;
        padding 0 290px 0 320px;
        background-color: blue;
    }
    .sub{
        width: 320px;
        margin-left:-100%;
        background-color: white;
    }
    .extra{
        width: 290px;
        margin-left:-290px;
        background-color: yellow;
    }

CSS
Copy after login

HTML:

<body>
<div class="container">
    <div class="cl main">
    </div>
    <div class="cl sub"></div>
    <div class="cl extra"></div>
</div>
</body>
Copy after login

Talk about grid layout in css (graphics and text)

The principle of holy grail layout is that when the child element is in a floating state, set a negative margin, and the child element will overlap the brothers above the elements.​

So if you want to divide the blue area into three areas again, I believe there are many ways. But can it be achieved through nesting? We can try it:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta http-equiv="window-target" content="_top">
<title>Writing to Same Doc</title>
<style type="text/css">
    * {
        box-sizing: border-box;
    }
    html, body{
        width: 100%;
        height: 100%;
        margin: 0;
    }
    .container{
        width:100%;
    }
    .container:after{
        display: table;
        content:".";
        clear:both;
    }
    
    .container .cl{
        float:left;
        border: 1px solid red;
        height: 200px;
    }
    
    .main{
        width:100%;
        padding: 0 290px 0 320px;
        background-color: blue;
    }
    .sub{
        width: 320px;
        margin-left:-100%;
        background-color: white;
    }
    .extra{
        width: 290px;
        margin-left:-290px;
        background-color: yellow;
    }
</style>
</head>
<body>
<div class="container">
    <div class="cl main">
        <div class="container">
            <div class="cl main"></div>
            <div class="cl sub"></div>
            <div class="cl extra"></div>
        </div>
    </div>
    <div class="cl sub"></div>
    <div class="cl extra"></div>
</div>

</body>
</html>
Copy after login

Talk about grid layout in css (graphics and text)

The principle of grid system

Talk about grid layout in css (graphics and text)

Assumption: Flowline The width of is W, the width of column is c, the width of Gutter is g, the width of Margin is m, and the number of grid columns is N

W = c*N g*(N-1) 2m; g The width of is usually twice that of m, so:

W = (c g) * N; record c g as C, we get:

W = C * N;

Most grid systems are variations of this formula.

Bootstrap’s grid system

Let’s take a look at common grid layout designs and design implementations in bootstrap. To properly use grid layout in BootStrap, columns must be placed in rows, and rows must be placed in containers. The container class has two main functions in layout:

  1. Provide width restrictions in different width intervals (responsive breakpoints). When the width changes, take a different width.

  2. Provide a padding to prevent internal content from touching the browser borders.

Padding is used in Bootstrap instead of the margin mentioned above. The size is 15px, as shown in the picture below, and the pink color is the padding size.

Talk about grid layout in css (graphics and text)

Row is a container of columns. The sum of columns in each row must be 12, but we can expand it by nesting. The left and right margins of Row are -15px, which is used to offset the padding in the container, as shown in the blue part of the figure below:

Talk about grid layout in css (graphics and text)

This design of row is mainly for the convenience of nesting. , will be mentioned later.

Colomn is the protagonist of the grid system. The left and right padding of each column is 15px. The negative margin of the row above offsets the padding of the container, so setting padding for each column is to prevent the content from directly touching the border. , and there are 30px card slots (Gutter) between different columns. As shown in the yellow part of the picture below:

Talk about grid layout in css (graphics and text)

Now think about the formula mentioned above: W = C * N;

row mentioned above The negative margin design is mainly for nesting. If you want to nest a column in a column, you must first put the nested column into the row, and put the row into the column as the container, without placing a container. As shown in blue in the figure below, it is the negative margin area of ​​the row placed in the column.

Talk about grid layout in css (graphics and text)

Now put the nested column into the row, as shown in the figure below, the upper column plays the role of container.

Talk about grid layout in css (graphics and text)

For more programming-related knowledge, please visit: Programming Learning! !

The above is the detailed content of Talk about grid layout in css (graphics and text). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

See all articles