Table of Contents
下面是“方法-1”示例的HTML标签:
纯CSS实现等高列(1): 使用Margins, Paddings和 Overflow来实现
This is a sidebar
下面是这种实现的CSS规则:
HTML标签(与方法-1的html结构一样):
纯CSS实现等高列(2): 通过CSS伪类和定位实现等高列
好的,在看看CSS规则:
下面是方法-3的HTML:
纯CSS实现等高列(3): 使用表格实现等高列
This is a Sidebar.
CSS 部分:
Home Web Front-end HTML Tutorial 三种纯CSS方法实现等高列_html/css_WEB-ITnose

三种纯CSS方法实现等高列_html/css_WEB-ITnose

Jun 24, 2016 am 11:21 AM

在这篇文章里, 我会介绍三种使用纯css的方式来实现等高列的方法。在网页布局中设置列等高是比较常见的, 所以写这篇文章就是要总结下一些优雅的纯CSS解决方案。

插图自己弄得,不喜莫喷 哈哈。。

下面介绍的三种方法都只用到了CSS , 不涉及jQuery、JavaScript计算实现的方法,所以我把它这篇文章标题设为:三种纯CSS方法实现等高列。

方法-1: 使用Margins, Paddings和 Overflow来实现

第一种方法使用margins, paddings和overflow来迫使列的高度相等。这个方法需要为每个浮动元素设置一个足够大的底部填充。关键是设置在父容器overflow: hidden把多余的部分给它隐藏起来。

下面是“方法-1”示例的HTML标签:

<div class="main">            <div class="container clearfix">                <div class="content">                    <section>                        <h1 id="纯CSS实现等高列-使用Margins-Paddings和-Overflow来实现">纯CSS实现等高列(1): 使用Margins, Paddings和 Overflow来实现</h1>                        <hr>                        <p>第一种方法使用margins, paddings和overflow来迫使列的高度相等。这个方法需要为每个浮动元素设置一个足够大的底部填充。关键是设置在父容器overflow: hidden把多余的部分给它隐藏起来。下面是这种实现的CSS规则:</p>                        <p>这个方法很简单! 它可以扩展到更多的行和列的布局中,或者说是所有类型的网格布局。</p>                        <p>最后, 再在CSS规则中添加媒介查询,OK, 下面是最后的效果截图。</p>                    </section>                </div>                <div class="sidebar">                    <aside>                        <h2 id="This-is-a-sidebar">This is a sidebar</h2>                    </aside>                </div>            </div><!-- /.containter -->        </div><!-- /.main -->
Copy after login

下面是这种实现的CSS规则:

    <style> html, body { font-family: Microsoft JhengHei, SimHei; background-color: #29384D; font-size: 10px; width: 100%; height: 100%; position: relative; } .main .container { width: 80%; margin: 3em auto; overflow: hidden; } .content { width: 80%; float: left; margin-left: 2px; color: #464444; background-color: #FFFFFF; box-shadow: 2px 2px 3px #1f1d1d, -1px -1px 3px #1f1d1d; } .content h1 { font-size: 28px; color: #FF4A59; } .content p { font-size: 18px; font-family: SimHei; color: #29384D; } .sidebar { width: 18%; float: right; margin-right: 3px; color: #FFFFFF; background-color: #FF4A59; box-shadow: 2px 2px 3px #151414, -1px -1px 3px #100f0f; } .content, .sidebar { padding-bottom: 99999px; margin-bottom: -99999px; } section, aside { padding: 3em; } // 添加媒介查询 @media all and (max-width: 840px) { .main .container { padding: 0 3em; overflow: visible; } .content { float: none; width: 100%; } .sidebar { float: none; margin-right: 0; width: 100%; } .content, .sidebar { padding-bottom: 0; margin-bottom: 0; } .content { margin-bottom: 30px } } </style>
Copy after login

最后, 我还在CSS规则中添加媒介查询。

这个方法很简单! 它可以扩展到更多的行和列的布局中,或者说是所有类型的网格布局。

OK, 下面是最后的效果截图。

方法-2:通过CSS伪类和定位实现等高列

这个方法就是使用伪类:after。将父容器设置为相对定位(position: relative),再把容器的伪元素设置为绝对定位(position: absolute)和高度为100%(height: 100%)。然后还需要我们去调整容器伪元素所需的width、left和right。注意:这种方法也要给容器设置overflow: hidden来隐藏溢出。

HTML标签(与方法-1的html结构一样):

<div class="main">            <div class="container clearfix">                <div class="content">                    <section>                        <h1 id="纯CSS实现等高列-通过CSS伪类和定位实现等高列">纯CSS实现等高列(2): 通过CSS伪类和定位实现等高列</h1>                        <hr>                        <p>这个方法就是使用伪类:after。将父容器设置为相对定位(position: relative),再把容器的伪元素设置为绝对定位(position: absolute)和高度为100%(height: 100%)。然后还需要我们去调整容器伪元素所需的width、left和right。注意:这种方法也要给容器设置overflow: hidden来隐藏溢出。</p>                        <p>内容和侧边栏的左和右定位30 px是为了弥补父容器的填充。同样,最后给它添加媒介查询, 让它变成响应:</p>                    </section>                </div>                <div class="sidebar">                    <aside>                        <h2 id="This-is-a-sidebar">This is a sidebar</h2>                    </aside>                </div>            </div><!-- /.containter -->        </div><!-- /.main -->
Copy after login

好的,在看看CSS规则:

    <style> html, body { font-family: Microsoft JhengHei, SimHei; background-color: #FFFFFF; font-size: 10px; height: 100%; width: 100%; } .main .container { width: 80%; margin: 3em auto; position: relative; overflow: hidden; } .content { float: left; margin: 3px; width: 800px; color: #FFFFFF; background-color: #29384D; box-shadow: 2px 2px 4px #846363, 0px 0px 2px #8E7171; } .content h1 { font-size: 28px; } .content p { font-size: 18px; font-family: SimHei; color: #bea9a9; } .sidebar { float: right; margin: 3px; width: 230px; color: #FFFFFF; background-color: #FF4A59; box-shadow: 2px 3px 4px #846363; } .content:after, .sidebar:after { display: block; position: absolute; height: 100%; content: ""; background-color: #FF4A59; box-shadow: 2px 2px 4px #846363; } .content:after { } .sidebar:after { width: 230px; } section, aside { padding: 30px } @media all and (max-width: 840px) { .main .container { padding: 0 30px; overflow: visible; } .content { float: none; margin-bottom: 30px; } .sidebar { float: none; margin-right: 0; width: 100%; } .content:after, .sidebar:after { display: none } } </style>
Copy after login

内容和侧边栏的左和右定位30 px是为了弥补父容器的填充。

同样,最后也给它添加了媒介查询, 让它变成响应式的,下面是“方法-2”示例的截图:

方法-3:使用表格实现等高列

方法3使用表格来实现列的等高并不会真正使用表格(在HTML里没有用到表格元素), 而只是把CSS属性设置为表格元素的属性来显示。这个方法可能是最简单的解决方案。不过因为是用到了”表格”所有需要特别小心浏览器兼容性问题。尽管如此, 方法-3 仍然是一个优雅、简单、有效的解决方案。

下面是方法-3的HTML:

    <div class="main">        <div class="container">            <div class="table">                <div class="row">                    <div class="col content">                        <h1 id="纯CSS实现等高列-使用表格实现等高列"> 纯CSS实现等高列(3): 使用表格实现等高列</h1>                        <hr>                        <p>方法3使用表格来实现列的等高并不会真正使用表格(在HTML里没有用到表格元素), 而只是是把CSS属性设置为表格元素的属性来显示。这个方法可能是最简单的解决方案。不过因为是用到了"表格"所有需要特别小心浏览器兼容性问题。尽管如此, 它仍然是一个优雅、简单、有效的解决方案, 下面是方法-3的HTML:</p>                        <p>我们将设置父元素设置为display: table。我重置容器padding: 0,  以弥补在table的border-spacing属性。这是我们的CSS:</p>                        <p>最后是添加媒体查询,让它变成响应式的。</p>                    </div>                    <div class="col sidebar">                        <h2 id="This-is-a-Sidebar">This is a Sidebar.</h2>                    </div>                </div>            </div>        </div>    </div><!-- #main -->
Copy after login

CSS 部分:

    <style> html, body { font-family: Microsoft JhengHei, SimHei; background-color: #FF4A59; font-size: 10px; width: 100%; height: 100%; position: relative; } .main .container { padding: 0 0; } .table { width: 80%; margin: 2em auto; display: table; border-collapse: separate; border-spacing: 30px 0; } .row { display: table-row; } .col { display: table-cell; background-color: #fff; padding: 30px; } .col.content { width: 70%; background-color: #FFFFFF; box-shadow: 4px 4px 8px #443333; } .col.content h1 { font-size: 28px; color: #FF4A59; } .col.content p { font-size: 18px; font-family: SimHei; color: #29384D; } .col.sidebar { width: 25%; color: #FFFFFF; background-color: #29384D; box-shadow: 4px 4px 8px #443333; } @media all and (max-width: 840px) { /* demo 3 */ .main .container { padding: 0 30px; } .table { display: block; } .row { display: block; } .col { display: block; } .col.content { margin-bottom: 30px; width: 100%; } .col.sidebar { width: 100%; } } </style>
Copy after login

我们将父元素设置为display: table。重置容器padding: 0, 来弥补在table的border-spacing属性。

方法-3 示例效果截图:

OK , 以上就是总结的三种纯CSS实现等高列问题的解决方法。

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)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

See all articles