CSS实现水平居中的4种思路_html/css_WEB-ITnose
× 目录 [1]text-align [2]margin [3]absolute [4]flex
前面的话
水平居中是经常遇到的问题。看似方法较多,条条大路通罗马。但系统梳理下,其实都围绕着几个思路展开。本文将介绍关于水平居中的4种思路
思路一:在父元素中设置text-align:center实现行内元素水平居中
将子元素的display设置为inline-block,使子元素变成行内元素
[注意]若要兼容IE7-浏览器,可使用display:inline;zoom:1;来达到inline-block的效果
<style>.parent{text-align: center;} .child{display: inline-block;}</style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div></div>
思路二:在本身元素设置margin: 0 auto实现块级元素水平居中
【1】将子元素的display为table,使子元素成为块级元素,同时table还具有包裹性,宽度由内容撑开
[注意]若要兼容IE7-浏览器,可把child的结构换成
<style>.child{ display: table; margin: 0 auto;}</style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div></div>
【2】若子元素定宽,则可以使用绝对定位的盒模型属性,实现居中效果;若不设置宽度时,子元素被拉伸
<style>.parent{ position: relative;}.child{ position: absolute; left: 0; right: 0; margin: 0 auto; width: 50px;}</style>
<div class="parent" style="background-color: gray;height: 20px;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
思路三: 通过绝对定位的偏移属性实现水平居中
【1】配合translate()位移函数
translate函数的百分比是相对于自身宽度的,所以left:50%配合translateX(-50%)可实现居中效果
[注意]IE9-浏览器不支持
<style>.parent{ position: relative;}.child{ position: absolute; left: 50%; transform:translateX(-50%);}</style>
<div class="parent" style="background-color: gray;height: 20px;"> <div class="child" style="background-color: lightblue;">DEMO</div></div>
【2】配合relative
relative的偏移属性是相对于自身的,因为子元素已经被设置为absolute,所以若使用relative,则需要增加一层
[注意]该方法全兼容,但是增加了html结构
<style>.parent{ position: relative;}.childWrap{ position: absolute; left: 50%;}.child{ position: relative; left: -50%;}</style>
<div class="parent" style="background-color: gray;height: 20px;"> <div class="childWrap"> <div class="child" style="background-color: lightblue;">DEMO</div> </div> </div>
【3】配合负margin
margin的百分比是相对于包含块的,所以需要增加一层
[注意]虽然全兼容,但需要增加页面结构及定宽处理,所以限制了应用场景
<style>.parent{ position: relative;}.childWrap{ position: absolute; left: 50%;}.child{ width:50px; margin-left:-50%;}</style>
<div class="parent" style="background-color: gray;height: 20px;"> <div class="childWrap"> <div class="child" style="background-color: lightblue;">DEMO</div> </div> </div>
思路四: 使用弹性盒模型flex实现水平居中
[注意]IE9-浏览器不支持
【1】在伸缩容器上设置主轴对齐方式jusify-content:center
<style>.parent{ display: flex; justify-content: center;}</style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
【2】在伸缩项目上设置margin: 0 auto
<style>.parent{display: flex;}.child{margin: 0 auto;}</style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>

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



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 article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

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.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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

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