单行垂直居中
单行垂直居中可以直接用line-height=width来做
hello world
这样文本hello world便实现了垂直居中,如果想让整个p在父级元素中都居中,则在外面嵌套一层p,并且通过里面p的margin来实现
hello world
.element { position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto 0;}
多行垂直居中
多行垂直居中的话用line-height就不行了。需要将p的display:table-cell,然后vertical-align:middle;
立即学习“前端免费学习笔记(深入)”;
你好时间你好时间你好时间你好时间
.twoClass{display:table-cell; width:200px; height:200px; vertical-align:middle;}
其实这种方法对于单行的垂直居中也是可行的。
水平居中
对于文本的水平居中,只要text-align:center;就可以了,如果将正个p居中,则需要将p的margin-left margin-right设为auto
你好时间
.element { position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto auto;}
这个demo实现了p和文本的水平垂直居中。
两端对齐
对于多行文本的两端对齐,只需要text-align:justify就可以了
hello world he hello world你好世界你好世界你好世界, he hello world he hello你好世界你好世界你好世界, world he hello world he hello world he
值得注意的是这个多行文本的最后一行并没有两端对齐。
如果想对最后一行做操作,可以使用text-align-last: justify; 但是存在兼容性问题。
单行的两端对齐
我好帅
没想到一个text-align-last: justify;就实现了(chrome),但是在IE浏览器下并没有效果。。。
下面这个是从网上找的几个a标签两端对齐
.demo{
text-align-last:justify;
line-height:0;
height:44px;
}
.demo a{
width:20%;
display:inline-block;
height:44px;
line-height:44px;
text-align:center;
}
模块内的元素之间为换行符
模块内的元素之间为空格符
模块内的元素之间为无分隔符,justify不起作用










