Home Web Front-end HTML Tutorial CSS垂直居中_html/css_WEB-ITnose

CSS垂直居中_html/css_WEB-ITnose

Jun 24, 2016 am 11:22 AM

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>
Copy after login

绝对对位原理:元素在过度受限情况下,将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的元素*/}
Copy after login

使用绝对定位要求元素必须设置明确高度。内容超过元素高度时需要设置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*/ }    
Copy after login

优点:代码量少、浏览器兼容性高支持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>
Copy after login

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

优点:浏览器兼容性好,兼容旧版本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;}
Copy after login

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

缺点:每一个需要垂直居中的元素都会需要加上额外标签(需要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>
Copy after login

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;}
Copy after login

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

缺点:会加上额外标记

6.line-height方式

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

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

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

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

优点:简单明了

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

7.弹性盒式布局

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

原理:CSS弹性盒

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

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;}
Copy after login

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

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

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
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.

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.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

See all articles