CSS实现水平垂直的几种方法
目录
- 水平居中
- 垂直居中
- 水平垂直居
一、水平居中
内联元素和块级元素实现水平居中的方法不一样,其中块级元素又分定宽和不定宽两种情况
- 内联元素水平居中
父元素为块级元素,在父元素设置 text-align: center; 就可
代码:
<!-- HTML---><div class="container"> <p>我是内联元素</p></div><!-- CSS --><style>.container{ width: 100%; height: 100px; background-color: darkseagreen; /* 父元素是块级元素,直接设置text-align属性 */ text-align: center; }</style>效果:
image.png
- 块级元素水平居中(定宽)
块级定宽,谁居中谁 margin: 0 auto;
代码:
<!-- HTML --><div class="container"> <div class="first_box"></div></div><!-- CSS --><style>.container{ width: 100%; height: 100px; background-color: darkseagreen; } .first_box{ width: 50px; height: 50px; background-color: rosybrown; /* 块级定宽,谁居中谁margin: 0 auto; */ margin: 0 auto; }</style>效果:
image.png
- 块级元素水平居中(不定宽)
块级不定宽,子元素设display: inline;,父元素设text-align: center;
代码:
<!-- HTML --><div class="container"> <div class="second_box">我是不定宽的块级元素</div></div><!-- CSS --><style>.container{ width: 500px; height: 100px; background-color: darkseagreen; text-align: center; }.second_box{ background-color: rosybrown; display: inline; }</style>效果:
image.png
- 使用定位属性实现水平居中
父元素设置position: relative;,子元素设置position: absolute;、left: 50%;、margin-left: -元素宽度一半
代码:
<!-- HTML --><div class="container1"> <div class="third_box"></div></div><!-- CSS --><style>.container1{ width: 500px; height: 100px; background-color: darkseagreen; position: relative; } .third_box{ width: 50px; height: 50px; background-color: rosybrown; position: absolute; left: 50%; margin-left: -25px; }</style>效果
image.png
- 使用flex布局实现水平居中
只要在父元素设置display: flex;、justify-content: center;
代码:
<!-- HTML --><div class="container2" > <div></div></div><!-- CSS --><style>.container2{ width: 500px; height: 100px; background-color: darkseagreen; display: flex; justify-content: center; } .container2 div{ width: 50px; height: 50px; background-color: rosybrown; }</style>效果:
image.png
flex布局详见阮一峰的flex布局教程 语法篇
二、垂直居中
实现垂直居中,亦分内联元素和块级元素,其中块级元素同分定高和不定高两种情况
- 内联元素垂直居中
单行元素,仅需设置很高等于盒子高就可;若为多行元素,则需给父元素设置display: table-cell;、vertical-align: middle;
单行代码:
<!-- HTML --><div class="container"> <p>单行行内元素</p></div><!--CSS--><style>.container{ width: 500px; height: 200px; background-color: darkseagreen; } .container p{ line-height: 200px; }</style>效果:
image.png
多行代码:
<!-- HTML --><div class="container"> <p>我是多行行内元素我是多行行内元素我是多行行内元素我是多行行内 元素我是多行行内元素我是多行行内元素我是多行行内元素我是多行行内元素我是多行行内元素</p></div><!--CSS--><style>.container{ width: 500px; height: 200px; background-color: darkseagreen; display: table-cell; vertical-align: middle; }</style>效果:
image.png
- 块级元素垂直居中(定高)
定宽时,使用定位实现垂直居中。父元素设置position: relative;,子元素设置display:absolute;、left: 50%;、margin-top: -一半高度
代码:
<!-- HTML --><div class="container"> <div></div></div><!-- CSS --><style>.container{ width: 500px; height: 200px; background-color: darkseagreen; position: relative; } .container div{ width: 50px; height: 50px; background-color: rosybrown; position: absolute; top: 50%; margin-top: -25px; }</style>效果:
image.png
- 块级元素居中(不定高)
不定高时,需要使用到 transform: translateY(-50%);
代码:
<!-- HTML --><div class="container"> <div>不定高度</div></div><!-- CSS --><style> .container{ width: 500px; height: 200px; background-color: darkseagreen; position: relative; } .container div{ width: 50px; background-color: rosybrown; position: absolute; top: 50%; transform: translateY(-50%); }</style>效果:
image.png
- 使用flex实现垂直居中
代码:
<!-- HTML --><div class="container"> <div>不定高度</div></div><!-- CSS --><style>.container{ width: 500px; height: 200px; background-color: darkseagreen; display: flex; align-items: center; } .container div{ width: 50px; background-color: rosybrown; }</style>效果:
image.png
flex布局详见阮一峰的flex布局教程 语法篇
三、水平垂直居中
- 利用定位实现水平垂直居中
父元素设置position: relative;,子元素设置position: absolute;
代码:
<!-- HTML --><div class="container"> <div></div></div><!-- CSS --><style>.container{ width: 500px; height: 200px; background-color: darkseagreen; position: relative; } .container div{ width: 50px; height: 50px; background-color: rosybrown; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; }</style>效果:
- 利用flexbox实现水平垂直居中
父元素设置position: relative;,子元素设置position: absolute;
代码:
<!-- HTML --><div class="container"> <div></div></div><!-- CSS --><style>.container{ width: 500px; height: 200px; background-color: darkseagreen; display: flex; justify-content: center; align-items: center; } .container div{ width: 50px; height: 50px; background-color: rosybrown; }</style>效果:
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » CSS实现水平垂直的几种方法
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » CSS实现水平垂直的几种方法