Blogger Information
Blog 6
fans 0
comment 0
visits 4144
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
四种元素对齐方案总结-2018年8月16日
cousin的博客
Original
580 people have browsed it

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>四种元素对齐方式</title>

</head>

<body>

<h3>元素对齐方式</h3>

1.子元素是单行行内元素

<style>

.box1{

width: 200px;

height: 200px;

background-color: #ffff0a;

text-align: center;/*水平居中*/

}

.box1 a{

line-height: 200px;/*垂直居中:在行内子元素设置行高与父元素等高*/

}

</style>

<div class="box1">

<a href="">php中文网</a>

</div>


2.子元素是多行的内联文本

<style>

.box2{

width: 200px;

height: 200px;

background-color: lightgreen;

text-align: center;/*水平居中*/

display:table-cell;/*垂直居中,先合并单元格*/

vertical-align: middle;/*垂直居中*/

}

</style>

<div class="box2">

<span>php中文网</span><br>

<style>www.php.cn</style>

</div>


3.子元素是块元素<br>

<style>

.box3{

width: 200px;

height: 200px;

background-color: lightgreen;

display:table-cell;/*垂直居中,先合并单元格*/

vertical-align: middle;/*垂直居中*/

}

.box3 .child{

width: 100px;

height: 100px;

background-color: lightcoral;

/*margin-left: auto;

margin-right: auto;*/

margin: :auto;/*水平居中,左右外边距自动适应容器*/

}

</style>

<div class="box3">

<div class="child"></div>

</div>

<br>

4.子元素是不定宽的块元素

<style>

.box4{

width: 200px;

height: 200px;

background-color: lightgreen;

text-align: center;

display: table-cell;;

vertical-align: bottom;/*底部居中*/

}


.box4 li{

display: inline;/*将块元素转化为行内元素*/

}

</style>

<div class="box4">

<ul>

<li><a href="">1</a></li>

<li><a href="">2</a></li>

<li><a href="">3</a></li>

<li><a href="">4</a></li>

<li><a href="">5</a></li>

</ul>

</div>

<div>总结:元素的对齐也是经常要用到的布局方式,如果多行含内元素垂直居中,先要设置display:table-cell先和并单元格然后设置vertical-align,如果是块元素垂直居中通过display:inline先转化为行内元素才行,如果是水平居中不管是行内元素还是块元素都是设置text-align</div>

</body>

</html>


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post