search
HomeWeb Front-endHTML Tutorialcss float_html/css_WEB-ITnose

我在项目的实践中遇到了这样的一个问题

<div class="main">        <p>aaaa</p>        <p>bbbb</p>    </div>

* {  padding: 0;  margin:0;}p {  padding-left: 30px;}.main {	background: green;}

上面的效果是这样的

我想让父元素div 里面的两个p元素通过float到同一行 

我添加了这样一句话

.mian p {            float:right;        }

我理所应当 异想天开的以为会是这样的结果

结果是这个样子的

为了更好的说明这个问题,也是我刚在疑惑的地方 我又将页面父div下面增加了一个div

<div class="main">        <p>aaaa</p>        <p style="clear:right">bbbb</p>    </div>    <div>cccc</div>

也就是我总体的想实现是这样的一个布局

结果我通过上面的那一点css 是这样的一个效果

我在实际遇到这个问题是在上面那个父div并没有设置背景色 我通过审查元素看到那个div的高度变成0了  我也不是很明白为什么下面的div会挤上去 应为两个div是块级元素,不会向上浮动的

原来float会造成一个塌陷的问题 当我们没有为父元素设置高度的时候 ,并且父元素只包含浮动元素的时候 就会产生塌陷这种问题 就是父元素的高度变成0 

通过查找资料和学习我发现

可以为父元素设置overflow:hidden

这样的情况下父元素就会自动扩展来包含他里面的浮动元素了,并且下面的div也自动的到下面去了

 

其实如果我们可以通过简单的clear设置来消除float的设置

clear:both 清除两边的浮动

clear:right 清除右边的浮动

clear:left 清除左边的浮动

 

例如上面的问题 我可以下面那个div 也就是并没有设置float的元素设置clear:both 这样他就不会飘上去了

上面的我并没有为上面的div设置overflow:hidden  只是让下面那个div clear:both

 

还有一种方法是在清除浮动元素之后通过css伪类:after添加一个不可见的内容 在它的上面clear:both 

上面加黑的就是两种清除浮动的方式

 

也解决了我的问题 拿来分享出来

 

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
How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment