Table of Contents
前面的话
思路一: text-align + line-height实现单行文本水平垂直居中
思路二: text-align + vertical-align
思路三: margin + vertical-align
思路四: 使用absolute
思路五: 使用flex
Home Web Front-end HTML Tutorial CSS实现水平垂直同时居中的5种思路_html/css_WEB-ITnose

CSS实现水平垂直同时居中的5种思路_html/css_WEB-ITnose

Jun 24, 2016 am 11:19 AM

× 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐 [4]absolute [5]flex

前面的话

  水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路

 

思路一: text-align + line-height实现单行文本水平垂直居中

<style>.test{    text-align: center;    line-height: 100px;}</style>
Copy after login

<div class="test" style="background-color: lightblue;width: 200px;">测试文字</div>   
Copy after login

思路二: text-align + vertical-align

【1】在父元素设置text-align和vertical-align,并将父元素设置为table-cell元素,子元素设置为inline-block元素

  [注意]若兼容IE7-浏览器,将结构改为

结构来实现table-cell的效果;用display:inline;zoom:1;来实现inline-block的效果

<style>.parent{    display: table-cell;    text-align: center;    vertical-align: middle;}.child{    display: inline-block;}</style>
Copy after login

<div class="parent" style="background-color: gray; width:200px; height:100px;">  <div class="child" style="background-color: lightblue;">测试文字</div></div> 
Copy after login

【2】若子元素是图像,可不使用table-cell,而是其父元素用行高替代高度,且字体大小设为0。子元素本身设置vertical-align:middle

<style>.parent{    text-align: center;    line-height: 100px;    font-size: 0;}.child{    vertical-align: middle;}</style>
Copy after login

<div class="parent" style="background-color: gray; width:200px; ">  <img class="child lazy"  src="/static/imghw/default1.png"  data-src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/img1.gif"     style="max-width:90%" alt="test"></div>  
Copy after login

思路三: margin + vertical-align

  要想在父元素中设置vertical-align,须设置为table-cell元素;要想让margin:0 auto实现水平居中的块元素内容撑开宽度,须设置为table元素。而table元素是可以嵌套在tabel-cell元素里面的,就像一个单元格里可以嵌套一个表格

  [注意]若兼容IE7-浏览器,需将结构改为

结构

<style>.parent{    display:table-cell;    vertical-align: middle;}.child{    display: table;    margin: 0 auto;}</style>
Copy after login

<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  <div class="child" style="background-color: lightblue;">测试文字</div></div>  
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

思路四: 使用absolute

【1】利用绝对定位元素的盒模型特性,在偏移属性为确定值的基础上,设置margin:auto

<style>.parent{    position: relative;}.child{    position: absolute;    top: 0;    left: 0;    right: 0;    bottom: 0;    height: 50px;    width: 80px;    margin: auto;}</style>
Copy after login

<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  <div class="child" style="background-color: lightblue;">测试文字</div></div>  
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

【2】利用绝对定位元素的偏移属性和translate()函数的自身偏移达到水平垂直居中的效果

  [注意]IE9-浏览器不支持

<style>.parent{    position: relative;}.child{    position: absolute;    top: 50%;    left: 50%;    transform: translate(-50%,-50%);}</style>
Copy after login

<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  <div class="child" style="background-color: lightblue;">测试文字</div></div>  
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

【3】在子元素宽高已知的情况下,可以配合margin负值达到水平垂直居中效果

<style>.parent{    position: relative;}.child{    position: absolute;    top: 50%;    left: 50%;    width: 80px;    height: 60px;    margin-left: -40px;    margin-top: -30px;}</style>
Copy after login

<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  <div class="child" style="background-color: lightblue;">测试文字</div></div>  
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

思路五: 使用flex

  [注意]IE9-浏览器不支持

【1】在伸缩项目上使用margin:auto

<style>.parent{    display: flex;}.child{    margin: auto;}</style>
Copy after login

<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  <div class="child" style="background-color: lightblue;">测试文字</div></div>  
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

【2】在伸缩容器上使用主轴对齐justify-content和侧轴对齐align-items

<style>.parent{    display: flex;    justify-content: center;    align-items: center;}</style>
Copy after login

<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  <div class="child" style="background-color: lightblue;">测试文字</div></div>  
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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
1240
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