Table of Contents
0x00 min-content width adaptive
0x01 Set the style according to the number of sibling elements
:nth-child()
0x02 calc
0x03 Vertical centering
Absolute positioning-based solution
FlexBox-based solution
0x04 Footer that sticks to the bottom
Home Web Front-end CSS Tutorial CSS structure and layout

CSS structure and layout

Feb 18, 2017 pm 02:55 PM
css


0x00 min-content width adaptive

CSS3 new width attribute value width:min-content can set the width value of the container to the largest unbreakable line in the container The width (the widest word, picture, or box element with a fixed width)

figure{
    width:min-content;
    margin: auto;    
}
Copy after login

0x01 Set the style according to the number of sibling elements

We know the pseudo-element selector: only-child ,In fact, it can be equivalent to:first-child:last-child, that is, it is the first item and the last item at the same time, so logically it is unique. And :last-child is also a shortcut for :nth-last-child(1).

Then let’s think about a question, what does li:first-chidl:nth-last-child(4) represent? The result is _the first item in a list with exactly four list items_, ok , and then combine the sibling selector ~ to hit each item after it, you can achieve such a goal. When it contains exactly four list items, hit each item

li:first-child:nth-last-child(4),
li:first-child:nth-last-child(4) ~ li {
  
    background: red;
}
Copy after login

Combined with SASS, it will Simplify reuse

/*定义混合器*/
@mixin n-items($n){
    &:first-child:nth-last-child(#{$n}),
    &:first-child:nth-last-child(#{$n}) ~ &{
        @content;
    }
}

/*调用*/
li {
    @include n-items(4){
        /*属性与值写在这里*/
        background: red;
    }
}
Copy after login

:nth-child()

nth-child() is powerful in that it accepts an expression in the form of an+b, so you can naturally use its variant nth -child(n+4) In this form, it will select all child elements except the 1st, 2nd, and 3rd child elements.

ul li:first-child:nth-last-child(n+4),
ul li:first-child:nth-last-child(n+4) ~ li{
    /*当列表中至少包含四项时,命中包括该项之后的所有列表项*/
}
Copy after login

Of course, it’s more than that. How to play :nth-child() depends entirely on your imagination.

0x02 calc

Sometimes, if we need to implement a layout with a background width that fills the screen and a fixed content width, maybe we will design the DOM structure like this

<footer>
    <p class="wrapper">        
    </p>
</footer>
Copy after login

CSS style:

footer{
    background: #333;
}

.wrapper{
    max-width: 900px;
    margin: 1em auto;
    height: 200px;
}
Copy after login

After using the calc() method, we don’t have to be so troublesome. We only need three lines of code to achieve it:

footer{
  background:#333;
  padding:1em calc(50% - 50px);
 }
Copy after login

Using clac(), we can perform simple calculations in CSS Arithmetic operation makes the DOM structure very concise without any redundancy. Of course, the shortcomings are also obvious. The code here will only see the effect when the parent of the footer element exceeds 900 px.

The percentage in calc() is parsed based on its parent

But, for the first time, we learned about the magic trick of cacl() in CSS3.

0x03 Vertical centering

Absolute positioning-based solution

There is a very common phenomenon in CSS, and the real solution often comes from where we least expect it. For example, you can combine the positon:absolute and transform:translate() attributes to achieve vertical centering

Because the percentage in the translate() transformation function is converted based on the width and height of the element itself, so , you can completely eliminate the dependence on fixed sizes.

Example: DOM structure

<body>
    <p class="main">
        <h1>Am i center?</h1>
        <p>Center me ,please!</p>
    </p>
</body>
Copy after login

CSS code:

.main{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
Copy after login

CSS结构与布局

However, this method also has shortcomings:
1. In some browsers, this may cause blurry display because elements may be placed on half a pixel.
2. When absolute positioning is not suitable. And absolute positioning has too strong an impact on the entire layout.

FlexBox-based solution

There is no doubt that this is the best solution at present. Moreover, modern browser support for FlexBox is already quite high.

Using margin:auto for items based on FlexBox containers can be centered not only in the horizontal direction, but also in the vertical direction, even if no width is specified, because the width assigned to this element is equal to max-content.

CSS结构与布局

Another benefit of FlexBox is that the text can also be vertically centered. Just add align-items:center and justify-content:center to the elements using display:flex.

.main{
    background: deeppink;
    width: 50%;
    height: 50%;
    margin: auto;
    display: flex;
    align-items: center;
    justify-content: center;
}
Copy after login

CSS结构与布局

Sometimes, we expect the height of the header and footer to be determined by its internal factors, and the content area The height of the block can automatically shrink to fill all available space. Again, this is easy with FlexBox.

body {
    min-height: 100vh;
    display:flex;
    flex-flow: column;

    header{
        /*heaer style*/
    }
    
    .main{
        flex:1;
    }

    footer{
        /*footer style*/
    }
}
Copy after login

We give body a height of min-height:100vh so that it will at least occupy the height of the entire viewport, and then give main a flex value greater than 0.

Question: What if the footer is fixed at the bottom of the screen? How to ensure that the footer does not cover the content area when the page is scrolled to the end?

For this question, it is purely a personal idea. You can add a p#_footer after footer.

The DOM structure at this time is as follows:

<body>
    <header><header>
    <p class="main"></p>
    <footer></footer>
    <p id="_footer"></p>
</body>
Copy after login

As for p#_footer, there is no need to add any content and style to it, as long as its height is equal to the height of the footer. , and for this, you can easily do it using jQuery.

$('#_footer').height($('footer').height())
Copy after login

In this way, you don’t have to worry about the responsive layout. Although it is a bit hacky, it is a perfect solution. Bingo!


##More For related articles on CSS structure and layout, please pay attention to 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

Video Face Swap

Video Face Swap

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

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 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.

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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 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 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 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 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-

See all articles