Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

什么是clearfix?

clearfix 是一种用于使每个浮子子元素都放置在父母内部的技术。它使父元素拉伸以修复其内部的漂浮子元素。父元素在没有clearfix的情况下崩溃。

当您发现带有浮动元素的布局时,您可能会发现父容器无法扩展以使子元素适合。这导致了布局和一些视觉问题。让我们与此博客中的不同示例讨论Clearfix及其重要性。

目录:

为什么我们需要Clearfix?

当父容器内部的子元素漂浮时,父容器会忘记孩子的身高。这导致以下问题:

  • 您可以看到父容器倒塌到零高。
  • 非浮动元素可能会重叠父容器。
  • 页面的一致性会突破。

没有clearfix的示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Without Clearfix</title>
    <style>
        .container {
            background-color: lightgray;
            border: 2px solid black;
            padding: 10px;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: blue;
            float: left;
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box"></div>
        <div class="box"></div>
    </div>
</body>
</html>

输出:

没有clearfix的示例没有clearfix的示例

问题: 您可以看到容器不会包装浮动的 。盒子 元素,这会导致容器塌陷。

使用clearfix解决此问题的方法

您可以使用诸如 溢出属性,添加空的clearfix元素或使用clearfix pseudo-lement 修复浮动元素。让我们在下面讨论这些方法。

方法1:使用溢出属性

您可以使用 溢出:隐藏 父元素上的属性使子元素适合父容器。这种方法在漂浮的子元素上强制执行这种机制。如果您使用 浮点:左 属性从正常流中删除,这使父容器忽略其高度。

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clearfix with Overflow</title>
    <style>
        .container {
            background-color: lightgray;
            border: 2px solid black;
            padding: 10px;
            overflow: hidden;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: blue;
            float: left;
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box"></div>
        <div class="box"></div>
    </div>
</body>
</html>

输出:

使用溢出属性使用溢出属性

解释: 您可以使用 溢出:隐藏 属性创建一个灰色容器,其中有两个蓝色盒子漂浮在左侧。此方法避免了带有浮动子元素的父容器的崩溃。

方法2:使用clearfix伪元素

您可以使用 ::后 伪元素使浮点元素在父元素内部。这是清除浮子的最建议方法。

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clearfix with ::after</title>
    <style>
        .container::after {
            content: "";
            display: table;
            clear: both;
        }
        .container {
            background-color: lightgray;
            border: 2px solid black;
            padding: 10px;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: blue;
            float: left;
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box"></div>
        <div class="box"></div>
    </div>
</body>
</html>

输出:

使用clearfix伪元素使用clearfix伪元素

解释: 您可以使用 ::后 要添加一个伪元素,以使浮点元素进入父容器内。蓝色漂浮的元素适合于父容器的左侧左侧。

方法3:添加一个空的clearfix元素

您可以使用空的

清晰:两者 到浮动元素的末端,使其适合于父容器。

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clearfix with Empty Div</title>
    <style>
        .clear {
            clear: both;
        }
        .container {
            background-color: lightgray;
            border: 2px solid black;
            padding: 10px;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: blue;
            float: left;
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box"></div>
        <div class="box"></div>
        <div class="clear"></div>
    </div>
</body>
</html>

输出:

添加一个空的clearfix元素添加一个空的clearfix元素

说明:在此代码中, 班级 。清除 用于适合父级内的子元素。这 清晰:两者 样式可确保父容器在浮动的蓝色盒子上包裹

结论

上述方法用于使父元素拉伸以使用clearfix修复其内部的浮动子元素。您可以使用 溢出属性,添加一个空的clearfix元素,或使用clearfix pseudo-lement 修复浮动元素。这使您的布局稳定且视觉上吸引人。

什么是clearfix? – 常见问题解答

1。什么是clearfix?

clearfix是一种CSS技术,用于将浮动子元素适合父元素

2。为什么需要一个clearfix?

当子元素浮动时,父容器经常崩溃,因为它无法识别浮动元素的高度。 ClearFix解决了此问题。

3. clearfix如何工作?

它添加了一个清除机制 ::后 或溢出以确保父容器扩展以适合其漂浮的孩子

4.实施ClearFix的常见方法是什么?

您可以使用 溢出:隐藏 在父母上,添加一个 ::后 伪元素,或插入空清除

在容器内部。

5。哪种clearfix方法更好?

这取决于您的用例。 溢出:隐藏 很简单,但可能会切断溢出的内容,而 ::后 灵活且广泛使用。



Related Posts

如何在Chromebook上禁用触摸屏:简单步骤

如何在Chromebook上禁用触摸屏:简单步骤

带有触摸屏支持的Chromebook因其用户友好的设计…

通过新推出的Urban’s Harmonic Soundbar 2080体验在家的电影声音

通过新推出的Urban’s Harmonic Soundbar 2080体验在家的电影声音

具有80W强大的低音,2.1频道家庭影院系统,功能强大…

它是什么以及如何避免失去条纹 – apoptimist 2.0 – 技术,工具和加密​​货币

随着社交媒体在我们的生活中继续发挥关键作用,Snapc…

HarperCollins很自豪地宣布所有在《新鲜工作》创始人的回忆录的出版

HarperCollins很自豪地宣布所有在《新鲜工作》创始人的回忆录的出版

该帖子最初发表在此网站上 由哈珀出版 商业 精装本|回…

如何在2025年与Tiktok客户支持联系

如何在2025年与Tiktok客户支持联系

关键要点 Tiktok是创作者简短视频内容的流行平台,…

洛雷克斯的摄像机可以为您观看镜头,节省数百小时

洛雷克斯的摄像机可以为您观看镜头,节省数百小时

在城市中,有一波新的有组织犯罪浪潮,犯罪分子越来越多地…