CSS structure and layout
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; }
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; }
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; } }
: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{ /*当列表中至少包含四项时,命中包括该项之后的所有列表项*/ }
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>
CSS style:
footer{ background: #333; } .wrapper{ max-width: 900px; margin: 1em auto; height: 200px; }
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); }
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>
CSS code:
.main{ position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
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.
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; }
0x04 Footer that sticks to the bottom
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*/ } }
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>
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())
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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

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.

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.

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 the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

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-
