搜索
首页web前端html教程CSS实现水平垂直同时居中的5种思路_html/css_WEB-ITnose

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

前面的话

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

 

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

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

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

 

思路二: 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>

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

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

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

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

 

思路三: 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>

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

 

思路四: 使用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>

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

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

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

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

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

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

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

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

 

思路五: 使用flex

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

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

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

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

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

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

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

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
HTML,CSS和JavaScript:示例和实际应用HTML,CSS和JavaScript:示例和实际应用May 09, 2025 am 12:01 AM

HTML、CSS和JavaScript在网页开发中的作用分别是:1.HTML用于构建网页结构;2.CSS用于美化网页外观;3.JavaScript用于实现动态交互。通过标签、样式和脚本,这三者共同构筑了现代网页的核心功能。

如何在标签上设置lang属性?为什么这很重要?如何在标签上设置lang属性?为什么这很重要?May 08, 2025 am 12:03 AM

设置标签的lang属性是优化网页可访问性和SEO的关键步骤。1)在标签中设置lang属性,如。2)在多语言内容中,为不同语言部分设置lang属性,如。3)使用符合ISO639-1标准的语言代码,如"en"、"fr"、"zh"等。正确设置lang属性可以提高网页的可访问性和搜索引擎排名。

HTML属性的目的是什么?HTML属性的目的是什么?May 07, 2025 am 12:01 AM

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外观和互动,使网站互动,响应式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,类,类型,类型,和dissabledtransfransformformformformformformformformformformformformformformforment

您如何在HTML中创建列表?您如何在HTML中创建列表?May 06, 2025 am 12:01 AM

toCreateAlistinHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulleTedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,customeizableWithTheTtheTthetTheTeTeptTributeFordTributeForderForderForderFerentNumberingSnumberingStyls。

HTML行动:网站结构的示例HTML行动:网站结构的示例May 05, 2025 am 12:03 AM

HTML用于构建结构清晰的网站。1)使用标签如、、定义网站结构。2)示例展示了博客和电商网站的结构。3)避免常见错误如标签嵌套不正确。4)优化性能通过减少HTTP请求和使用语义化标签。

您如何将图像插入HTML页面?您如何将图像插入HTML页面?May 04, 2025 am 12:02 AM

toinsertanimageIntoanhtmlpage,usethetagwithsrcandaltattributes.1)usealttextforAcccessibilityandseo.2)instementRcsetForresponSiveImages.3)applylazyloadingWithLoadingWithLoading =“ lazy” tooptimizeperformance.4)tooptimizeperformance.4)

HTML的目的:启用Web浏览器可以显示内容HTML的目的:启用Web浏览器可以显示内容May 03, 2025 am 12:03 AM

HTML的核心目的在于让浏览器理解并展示网页内容。1.HTML通过标签定义网页结构和内容,如、到、等。2.HTML5增强了多媒体支持,引入了和标签。3.HTML提供了表单元素,支持用户交互。4.优化HTML代码可提升网页性能,如减少HTTP请求和压缩HTML。

为什么HTML标签对Web开发很重要?为什么HTML标签对Web开发很重要?May 02, 2025 am 12:03 AM

htmltagsareessentialforwebdevelopmentastheyandendenhancewebpages.1)heSdefinElayout,语义和互动性。2)SemantictagsiCtagSimproveCacsibilitieAndseo.3)pose poseriblesibilityAndseoandseo.3)poser

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。