Tips for clearing floats between css hierarchical elements
如果让父元素包住浮动的子元素?主要有以下几种方法:
1:让父元素也一起浮动:经测试该方法可行,而且四周全部包住,非常严实.但是如果当前的子元素有多个父级的话,
那么每个父级元素都要设置浮动,甚至要一直写到
,所以该方法pass。2.给父元素设置溢出隐藏属性:同样也可实现要求,但这种方法有很多兼容性问题,大家课后可多打开几个浏览器测试
3.给当前的父元素中添加一个空的块元素,这个新元素只做一件事,清除它周围的浮动元素
经测试也能实现要求,这种方法是最简单最直观,而且没有任何兼容问题,但却有一个致命的问题,
那就是给当前的子元素列表中添加了一个无用的元素,当我们用js和php等脚本语言进行数据遍历的时候会非常麻烦
因为这个元素并不是我们需要的,必须单独的进行处理,那么有没有一种二全其美的方案:即保证兼容性,又不用添加新元素呢?
有,用我们之前学过的伪元素来解决。
4. 给父元素添加:after伪类,在后面添加一个空元素,直接设置它的属性即可;
<html> <head> <meta charset="UTF-8"> <title>7.层次元素之间的清除浮动的技巧</title> <style type="text/css"> .parent { background-color: lightskyblue; border: 2px solid red; /*让父元素一起浮动,让它包住浮动的子元素*/ /*float:left;*/ /*给父元素设置内容溢出隐藏功能*/ /*overflow: hidden;*/ } .parent:after { /*1.添加一个空元素*/ content: ''; /*2.默认为行内元素,所以要转为块元素*/ display: block; /*3.直接清除二边的浮动*/ clear: both; /*如果ie下显示有问题,可以加上*/ /*z-index: 1;*/ /*可能你看过有的地方是这样写的:*/ /*添加一个空格*/ /*content: '\0020';*/ /*然后让这个空格高度为0,这样就根本不会显示出来*/ /*height: 0;*/ } .child { width: 200px; height: 200px; background-color: lightgreen; /*设置提示文本的水平垂直居中*/ text-align: center; line-height: 200px; /*当前父元素没有声明宽高,它是由子元素的宽高撑起来的*/ /*下面我们将子元素进行浮动,你将会看到非常有意思的事情发生*/ float:left; /*结果你会发现,子元素浮动之后,脱离了文档流,它与原来的父级元素就没有了关联, 所以父级直接压缩了,无法再包住子元素*/ } .clear { clear: both; } </style> </head> <body> <div> <div>子元素</div> <!-- <div></div> --> </div> </body> </html>
The above is the detailed content of Tips for clearing floats between css hierarchical elements. For more information, please follow other related articles on the PHP Chinese website!

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.

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.

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

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.

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-

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