text-align: center”>
关于用css实现文本和图片垂直水平居中
一直相信好记性不如烂笔头,最近遇到很多用到垂直居中的,整理一下以便日后查阅。
一、文本垂直水平居中
1、水平居中:
文字水平居中没什么好说的,用text-align: center;即可很容易的实现。
立即学习“前端免费学习笔记(深入)”;
2、垂直居中:
1)简单的单行文本
利用line-height==height,使得单行文本垂直居中。
12 垂直水平居中3
登录后复制
1 p{2 width: 200px;3 height: 200px4 text-align: center;5 line-height: 200px;6 background:#1AFF00;7 }
登录后复制
简单点来说,用p标签就可以,就像这样
垂直水平居中
登录后复制
1 p{2 width: 200px;3 height: 200px;4 text-align: center;5 line-height: 200px;6 background:#00ABFF;7 }
登录后复制
效果如下:
立即学习“前端免费学习笔记(深入)”;
2)多行文本
利用表格元素的特性,块级父元素display: table;内联元素display: table-cell;vertical-align: middle;
(内联)
1 <p2 国际《儿童权利公约》界定的儿童系指18岁以下的任何人3
登录后复制
1 p{ 2 width: 200px; 3 height: 200px; 4 display: table; 5 background:#1AFF00; 6 } 7 span{ 8 display: table-cell; 9 vertical-align: middle;10 }
登录后复制
(块级)
12
国际《儿童权利公约》界定的儿童系指18岁以下的任何人
3
登录后复制
定位+transform: translateY(-50%);
1 p{ 2 position: relative; 3 width: 200px; 4 height: 200px; 5 background: #00ABFF; 6 } 7 p{ 8 position: absolute; 9 top: 50%; 10 left: 0; 11 width: 200px; 12 height: 64px; 13 transform: translateY(-50%);14 }
登录后复制
定位+margin负值
1 p{ 2 position: relative; 3 width: 200px; 4 height: 200px; 5 background:#1AFF00; 6 } 7 p{ 8 position: absolute; 9 top: 50%; 10 left: 0; 11 width: 200px; 12 height: 64px; 13 margin-top: -32px; 14 }
登录后复制
定位+margin: auto;
1 p{ 2 position: relative; 3 width: 200px; 4 height: 200px; 5 background:#00ABFF; 6 } 7 p{ 8 position: absolute; 9 top: 0; 10 left: 0; 11 right: 0; 12 bottom: 0; 13 margin: auto; 14 width: 200px; 15 height: 64px; 16 }
登录后复制
两者都是定宽定高,父元素用padding值,此值为父元素高度减去子元素高度的一半
1 p{ 2 width: 200px; 3 height: 64px; 4 padding: 68px 0; 5 background:#1AFF00; 6 } 7 p{ 8 width: 200px; 9 height: 64px; 10 }
登录后复制
两者都是定宽定高,父元素用overflow: hidden;(css hack)子元素用margin值,此值为父元素高度减去子元素高度的一半
p{ width: 200px; height: 200px; overflow: hidden; background:#00ABFF;}p{ width: 200px; height: 64px; margin:68px auto;}
登录后复制
效果如下:
立即学习“前端免费学习笔记(深入)”;
二、图片垂直水平居中
立即学习“前端免费学习笔记(深入)”;
1、水平居中
1)img在css初始设置中是inline-block,行内块元素,此时若是要水平居中用text-align:center;
2)给img元素设置display:block;转换为块级元素,要想水平居中用margin:0 auto;
立即学习“前端免费学习笔记(深入)”;
2、垂直居中
立即学习“前端免费学习笔记(深入)”;
用这张图片做示范
1
登录后复制
line-height==height vertical-align: middle;
p{ width: 198px; height: 198px; text-align: center; line-height: 198px; border: 1px solid #8900FF;}img{ vertical-align: middle;}
登录后复制
display: table-cell;vertical-align: middle;
p{ display: table-cell; vertical-align: middle; width: 198px; height: 198px; border: 1px solid #8900FF;}img{ display: block; margin: 0 auto;}
登录后复制
display: table-cell;vertical-align: middle; text-align: center;
p{ display: table-cell; vertical-align: middle; text-align: center; width: 198px; height: 198px; border: 1px solid #8900FF;}
登录后复制
定位+display: block;transform: translate(-50%,-50%);
p{ position: relative; width: 198px; height: 198px; border: 1px solid #8900FF;}img{ position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); display: block;}
登录后复制
定位+margin负值
p{ position: relative; width: 198px; height: 198px; border: 1px solid #8900FF;}img{ position: absolute; top: 50%; left: 50%; margin: -75px 0 0 -75px;}
登录后复制
定位+margin: auto;
p{ position: relative; width: 198px; height: 198px; border: 1px solid #8900FF;}img{ position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; display: block;}
登录后复制
overflow: hidden;margin值
p{ width: 198px; height: 198px; overflow: hidden; border: 1px solid #8900FF;}img{ 8 margin: 25px;}
登录后复制
padding值
p{ 2 padding: 25px; 3 width: 148px; 4 height: 148px; 5 border: 1px solid #8900FF; 6 }
登录后复制
用table的属性+vertical-align: middle;实现
1
登录后复制
p{ display: table; width: 198px; height: 198px; text-align: center; border: 1px solid #8900FF;}span{ display:table-cell; vertical-align: middle;}
登录后复制
用background实现
1
登录后复制
1 p{2 width: 198px;3 height: 198px;4 border: 1px dashed #8900FF;5 background: url(https://www.php.cn/faq/1.jpg) no-repeat center center;6 }
登录后复制
效果如下:
原文来自:http://www.cnblogs.com/Ni-F/p/6937931.html 感谢作者分享!
以上就是关于用css实现文本和图片垂直水平居中的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2902057.html