search
HomeWeb Front-endHTML TutorialCSS垂直居中_html/css_WEB-ITnose

1.使用绝对定位居中

1 <div class="container">2     <div class="center absolute_center">绝对对位原理:元素在过度受限情况下,将margin设置为auto,浏览器会重算margin的值,<br />过度受限指的是同时设置top/bottom与height或者left/right与width。3     </div>4 </div>

绝对对位原理:元素在过度受限情况下,将margin设置为auto,浏览器会重算margin的值,过度受限指的是同时设置top/bottom与height或者left/right与width。

1 .absolute_center{ position:absolute; width:200px; height:200px; top:0; bottom:0; left:0; right:0; margin:auto; background:#518fca; overflow:auto; resize:both;/*用于设置了所有除overflow为visible的元素*/}

使用绝对定位要求元素必须设置明确高度。内容超过元素高度时需要设置overflow决定滚动条的出现

优点:支持响应式,只有这种方法在resize之后仍然垂直居中

缺点:没有显式设置overflow时,内容超过元素高度时会溢出,没有滚动条

2.负marginTop方式

已知元素高度后,使用绝对定位将top设置为50%,mergin-top设置为内容高度的一半(height + padding) / 2;内容超过元素高度时需要设置overflow决定滚动条的出现

原理:top:50%元素上边界位于包含框中点,设置负外边界使得元素垂直中心与包含框中心重合;

1 .negative_margin_top{  width:200px; height:200px; position:absolute; top:50%; left:0; right:0; margin:auto; margin-top:-100px; /*-(height+padding)/2*/ }    

优点:代码量少、浏览器兼容性高支持ie6 ie7

缺点:不支持响应式(不能使用百分比、min/max-width)

3.借助额外元素floater

元素高度已知,在center元素外插入一个额外元素floater,设置floater的height为50%;margin-bottom为center元素高度的一半(height + padding) / 2。内容超过元素高度时需要设置overflow决定滚动条的出现。

原理与2方法类似,floater的下边界是包含框的中心线,负下外边界保证center的中心线与包含框中心线重合。

1 <div class="container">2     <div class="floater"></div>3     <div class="center floater_center">元素高度已知,在center元素外插入一个额外元素floater,设置floater的height为50%;<br />margin-bottom为center元素高度的一半(height + padding) / 2。内容超过元素高度时需要设置overflow决定滚动条的出现。</div>4 </div>

1 .floater{ height:50%; margin-bottom:-100px;}2 .floater_center{height:200px; width:200px; margin:auto;}

优点:浏览器兼容性好,兼容旧版本IE

缺点:需要额外元素,不支持响应式(不能使用百分比、min/max-width)

4.table-cell方式

将center元素的包含框display设置为table,center元素的display设置为table-cell,vertical-align设置为middle。

原理:利用表布局特点,vertical-align设置为middle后,单元格中内容中间与所在行中间对齐

1 .container2{display:table; height:100%;}2 .table_cell{/*将cell垂直居中,如果外层div不为table则tablecell必须有高度*/display:table-cell;vertical-align:middle;}

优点:支持任意内容的可变高度、支持响应式

缺点:每一个需要垂直居中的元素都会需要加上额外标签(需要table、table-cell两个额外元素)

5.inline-block方式

将center元素display设置为inline-block,vertical-align设置为middle,为包含框设置after伪元素,将伪元素display设置为inline-block,vercial-align设置为middle,同时设置height为100%,撑开容器。

原理:为同一行的inline-block元素设置vertical-align:middle,该行内的inline-block元素会按照元素的垂直中心线对齐。

<div class="container">    <div class="center inline_block">生命里有着多少的无奈和惋惜,又有着怎样的愁苦和感伤?<br> 雨浸风蚀的落寞与苍楚一定是水,静静地流过青春奋斗的日子和触摸理想的岁月。    </div></div>

1 .container{display:block;}2 /*inline-block的前世今生*/3 .container:after{content: ''; display: inline-block;vertical-align: middle; height: 100%;}4 .inline_block{display:inline-block;vertical-align:middle;}

优点:支持响应式、支持可变高度

缺点:会加上额外标记

6.line-height方式

该方式只适用于情况比较简单的单行文本,将line-height设置与元素高度同高。

原理:如果line-height高度大于font-size,生于高度浏览器会平分到文字上下两端。

1 <div class="single_line">其实我们每个人的生活都是一个世界,即使最平凡的人也要为他生活的那个世界而奋斗。2 </div>

1 .single_line{height: 30px; font-size: 14px; line-height: 30px; border: 1px solid #518dca;}

优点:简单明了

缺点:只适用于单行文本,局限性大

7.弹性盒式布局

利用弹性盒式布局,将字元素的主轴、侧轴的排列方式都设置为居中对齐

原理:CSS弹性盒

1 <div class="container is-Flexbox">2     <div class="center">既要脚踏实地于现实生活,又要不时跳出现实到理想的高台上张望一眼。<br>在精神世界里建立起一套丰满的体系,引领我们不迷失不懈怠。<br>待我们一觉醒来,跌落在现实中的时候,可以毫无怨言地勇敢地承担起生活重担。<br>这是孙少平教给我的道理。 <br>只能永远把艰辛的劳动看做生命的必要,即使没有收获的指望,也心平气静地继续耕种。<br>3 要做到这一点,路还好长。4     </div>5 </div>    

1 .is-Flexbox {display: -webkit-box;display: -moz-box;display: -ms-flexbox;display:  -webkit-flex; display: flex; -webkit-align-items: center; align-items: center; -webkit-justify-content: center; justify-content: center;}

优点:真正的垂直居中布局

缺点:ie11才开始支持弹性布局

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
HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

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.

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment