您现在的位置是:网站首页> 编程资料编程资料
CSS中的垂直和水平居中完全指南浅析CSS实现水平垂直同时居中的5种思路CSS解决页面图片水平垂直居中问题的方法全面总结使用CSS实现水平垂直居中效果的方法CSS定位“十字架”之水平垂直居中CSS设置未知大小图片在已知大小容器水平垂直居中Flexbox制作CSS布局实现水平垂直居中的简单实例
2021-09-05
1150人已围观
简介 这篇文章主要介绍了CSS中的垂直和水平居中,几乎囊括了各种板式居中的需求,非常推荐!需要的朋友可以参考下
- .link {
- padding-top: 30px;
- padding-bottom: 30px;
- }
居中一直是CSS中被抱怨的典型。为什么实现起来这么辛苦?所以有人被嘲笑。我觉得问题不是没有办法做到,只是视情况而定,有很多不同方式,但是很难弄清楚应该用何种方式。因此我写了这篇文章,希望能把他变得容易点。
水平居中
内联元素(inline or inline-*)居中?
你可以让他相对父级块级元素居中对齐
- .center-children {
- text-align: center;
- }
块级元素(block level)居中?
你可以通过设置margin-left和margin-right为auto让它居中(同时还要设置width,否则它就会承满整个容器,无法看出居中效果),如。
- .center-me {
- margin: 0 "width: auto; height: auto; float: none;" id="8_nwp">"text-decoration: none;" mpid="8" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f3e358418dc3f42b&k=auto&k0=auto&kdi0=0&luki=3&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2bf4c38d4158e3f3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5106%2Ehtml&urlid=0" id="8_nwl">"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">auto;
- }
如果有很多块级元素呢?
如果你有很匀块级元素需要水平居中成一行,你最好使用一个不同的display类型。这是一个使用inline-block和flex的例子。
"inline-block-center"> -
- I'm an element that is block-like with my siblings and we're centered in a row.
-
- I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
-
- I'm an element that is block-like with my siblings and we're centered in a row.
"flex-center"> -
- I'm an element that is block-like with my siblings and we're centered in a row.
-
- I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
-
- I'm an element that is block-like with my siblings and we're centered in a row.
- body {
- background: #f06d06;
- font-size: 80%;
- }
- main {
- background: white;
- margin: 20px 0;
- padding: 10px;
- }
- main div {
- background: black;
- color: white;
- padding: 15px;
- max-width: 125px;
- margin: 5px;
- }
- .inline-"width: auto; height: auto; float: none;" id="5_nwp">"text-decoration: none;" mpid="5" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f3e358418dc3f42b&k=block&k0=block&kdi0=0&luki=2&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2bf4c38d4158e3f3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5106%2Ehtml&urlid=0" id="5_nwl">"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">block-center {
- text-align: center;
- }
- .inline-block-center div {
- display: inline-block;
- text-align: left;
- }
- .flex-center {
- display: flex;
- justify-content: center;
- }
垂直居中
垂直居中在CSS中有点棘手
内联元素(inline or inline-*)居中,像文本和链接那样的?
它是一行的吗?
有时侯元素可以表现像垂直居中,只是因为它们有相等的上下padding
如果padding因为某些原因不能用,而且文本不会换行的情况下,你可以使用line-height,让其与height相等去对齐文本。
- .center-text-trick {
- height: 100px;
- line-height: 100px;
- whitewhite-space: nowrap;
- }
它是多行的?
上下等padding的方式也可以让多行居中,但是如果这方法没用,你可以让这些文字的容器按table cell模式显示,然后设置文字的vertical-align属性对齐,就像talbe那样
- I'm vertically centered multiple lines of text in a real table cell.
I'
m vertically centered multiple lines of text in a CSS-created table "width: auto; height: auto; float: none;" id="4_nwp">"text-decoration: none;" mpid="4" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f3e358418dc3f42b&k=layout&k0=layout&kdi0=0&luki=1&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2bf4c38d4158e3f3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5106%2Ehtml&urlid=0" id="4_nwl">"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">layout.- body {
- background: #f06d06;
- font-size: 80%;
- }
- table {
- background: white;
- width: 240px;
- border-collapse: separate;
- margin: 20px;
- height
相关内容
- 更改上传按钮的样式示例jquery实现的随机显示不同样式的网站右下角广告窗口带关闭按钮悬浮特效五款漂亮的纯CSS3动画按钮的实例教程
- 设计适用于打印的CSS样式CSS控制漂亮的网页打印效果示例代码将XHTML CSS页面转换为打印机页面CSS实现强制浏览器分页将XHTML CSS页面转换为打印机页面详解CSS3中@media的实际使用CSS3的media query学习攻略详解CSS3中Media Queries的相关使用CSS media queries CSS3 Media Queries(响应式布局可以让你定制不同的分辨率和设备)彻底弄明白CSS3的Media Queries(跨平台设计)
- CSS中使用clearfix清除浮动的方法CSS3 清除浮动的方法示例CSS清除浮动float的三种方法小结清除css浮动的三种方法小结详解css清除浮动float的七种常用方法总结和兼容性处理CSS清除浮动方法小结CSS清除浮动的常用方法优缺点分析html+css 清除浮动的相关技巧心得浅谈CSS中overflow清除浮动的用法css 盒模型 文档流 几种清除浮动的方法实例详解
- 用CSS实现textArea中的placeholder换行功能css控制文字自动换行的实现方法CSS控制文本超出指定宽度显示省略号和文本不换行效果的实现CSS超出文本指定宽度用省略号代替和文本不换行CSS自动换行、强制不换行、强制断行、超出显示省略号CSS强制换行对齐的实现方法CSS实现连续字符换行的方法CSS代码使纯英文数字自动换行的简单实现
- 简单介绍CSS3中Media Query的使用css3中transform属性实现的4种功能详解CSS3.0(Cascading Style Sheet) 层叠级联样式表纯CSS3实现div按照顺序出入效果CSS3实现列表无限滚动/轮播效果css3 利用transform-origin 实现圆点分布在大圆上布局及旋转特效CSS3实现的侧滑菜单CSS3实现的3D隧道效果用CSS3画一个爱心css3 实现文字闪烁效果的三种方式示例代码六种css3实现的边框过渡效果
- CSS中垂直居中的简单实现方法css实现元素水平垂直居中常见的两种方式实例详解CSS水平垂直居中的几种方法总结css让容器水平垂直居中的7种方式利用CSS3的flexbox实现水平垂直居中与三列等高布局CSS设置文字图片垂直居中的方法总结CSS定位“十字架”之水平垂直居中CSS实现同一行的图片和文字垂直居中对齐的方法CSS垂直居中实现方法大全CSS实现垂直居中的4种思路详解
- 利用CSS3实现气泡效果的教程CSS3 仿微信聊天小气泡实例代码HTML5-WebSocket实现聊天室示例HTML5仿手机微信聊天界面纯CSS实现聊天框小尖角、气泡效果利用html5的websocket实现websocket聊天室纯CSS实现右侧底部悬浮效果(悬浮QQ、微信、微博、邮箱等联系方式)如何弹出QQ临时对话框实现不添加好友在线交谈效果CSS3制作气泡对话框的实例教程可自定义箭头样式的CSS3气泡提示框html5+css3气泡组件的实现
- 深入理解CSS中的属性模块
- CSS中使用table-cell法来达到居中效果CSS居中效果之transform的使用CSS中使用负margin值来调整居中位置仅使用CSS做到完全居中的超级攻略
- CSS居中效果之transform的使用css3 transform属性详解CSS中使用负margin值来调整居中位置仅使用CSS做到完全居中的超级攻略
点击排行
本栏推荐
