水平居中

简介: 方案一 位置 属性名 值 意义父div text-align center 让子元素居中子div display inline-block 可以将对象呈递为内联对象,但是对象的内容作为块对象呈递。

方案一

位置 属性名 意义
父div text-align center 让子元素居中
子div display inline-block 可以将对象呈递为内联对象,但是对象的内容作为块对象呈递。inline-block的宽度是其中文字的宽度。
子div text-align left 让子元素中的文字不继承父div 的  text-align:center属性, 设置为默认的text-align:left
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平居中--方案一</title>
    <style type="text/css">
    /*优点:兼容IE低版本浏览器*/
    .parent{ text-align: center; }
    .child{display: inline-block;
            text-align: left;}
    </style>
</head>
<body>
    <div class="parent" style="width: 400px;height: 100px;background: red;">
        <div class="child" style="width: 80px;height: 100px;background: green;">
            <h1>DEMO</h1>
        </div>
    </div>
</body>
</html>

 

image

 

 

 

方案二

位置 属性名 意义 兼容性
子div display table table的表现上和block非常像,但是它和block元素有区别,『宽度也是跟着内容走』 IE8以上
子div margin 0 auto 上下为0  左右为『自动』  
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平居中table_margin</title>
    <style type="text/css">
    .child{display: table;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div class="parent" style="width: 400px;height: 100px;background: red;">
    <div class="child" style="height: 100px;background: green;text-align: center">DEMO</div>
</div>
</body>
</html>

image

 

 

 

方案三---绝对定位+transform

位置 属性名 意义 兼容性
父div position relative 相对定位,将父元素设置为一个参照物  
子div position absolute 绝对定位,宽度由内容决定  
子div left 50% 把子div的左边缘设置在其父元素左边缘向右 父元素50%宽度的位置:  
子div transform 0 auto 上下为0  左右为『自动』 css3新增,IE老版本不支持
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平居中absolute_transform</title>
    <style type="text/css">
    .parent{
        position: relative;
    }
    .child{
        position: absolute; /* absolute元素默认没有宽度,宽度由内容决定*/
        left: 50%;  /*以父元素为参照物,向左移动父类元素宽度的50%*/
        transform: translateX(-50%);  /*css3新增 以自身的宽度的50%向左边移动 */
    }
    </style>
</head>
<body>
<div class="parent" style="width: 400px;height: 100px;background: #403f42;">
    <div class="child" style="width: 200px;height: 100px;background: red;color: white;">
        DEMO
    </div>
</div>
</body>
</html>

image

缺点: transform 属性为css3新增,IE老版本(IE6,7,8)中无法使用。

 

 

 

方案四

位置 属性名 意义 兼容性
父div display flex 父元素的display 为 flex时,子元素自然就成为了flex item。flex 默认情况下,宽度是auto的。  
父div justify-content center

项目位于容器的中心。

 
或者        
子div margin 0 auto    
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平居中flex_justify-content</title>
    <style type="text/css">
    .parent{
        display: flex;    /* 当父类元素是display为flex时,其中的子元素就会变为 flex item,子元素的宽度就为文字的实际宽度  */
        justify-content: center;
    }
    </style>
</head>
<body>
<div class="parent" style="width: 400px;height: 100px;background: #403f42;">
    <div class="child" style="height: 100px;background: red;color: white;">
        DEMO
    </div>
</div>
</body>
</html>

image

 

 

 

或者,在子元素中设置 margin 属性:上下为0 ,左右自动(居中)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平居中flex_justify-content</title>
    <style type="text/css">
    .parent{
        display: flex;    /* 当父类元素是display为flex时,其中的子元素就会变为 flex item,子元素的宽度就为文字的实际宽度  */
    }
    .child{
        margin: 0 auto;
    }
    </style>
</head>
<body>
<div class="parent" style="width: 400px;height: 100px;background: #403f42;">
    <div class="child" style="height: 100px;background: red;color: white;">
        DEMO
    </div>
</div>
</body>
</html>

效果相同

image

 

缺点:  flex 在IE低版本中不支持。

开始做,坚持做,重复做
相关文章
|
10天前
背景图像 - 水平或垂直平铺
背景图像 - 水平或垂直平铺。
11 2
|
10月前
水平居中 #31
水平居中 #31
37 0
|
C#
C#中,让按钮文字垂直水平都居中
C#中,让按钮文字垂直水平都居中
109 0
|
前端开发
CSS水平居中+垂直居中+水平/垂直居中的方法总结
CSS水平居中+垂直居中+水平/垂直居中的方法总结
180 0
CSS水平居中+垂直居中+水平/垂直居中的方法总结
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(3,此节不全)
通过本章认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法。
61 0
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(2)
内边距会影响盒子的可见框的大小,元素的背景会延伸到内边距,盒子的大小由内容区、内边距和边框共同决定。外边距指的是当前盒子与其他盒子之间的距离,他不会影响可见框的大小,而是会影响到盒子的位置。
144 0
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(2)
|
前端开发
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(1)
网页是一个多层的结构,设置样式样式,也是一层一层的设置,最终我们看到的最上面的一层。
90 0
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(1)
|
前端开发
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(4)
通过本章认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法
111 0
html+css实战111-行内元素的垂直外边距
html+css实战111-行内元素的垂直外边距
129 0
|
移动开发 安全 weex
基于 Flex 实现两端对齐垂直布局
一般来说布局我们都是水平布局,最多搞个垂直居中。而且对于一些 float 、 position 好像本身就不太适合垂直布局。 正好前段时间用 weex 做了一个页面,weex 天生基于 flex 。且 weex 中 flex-direction 默认值为 column,flex-direction 定义了 flex 容器中 flex 成员项的排列方向。 页面分为三部分(header,section,footer),有一些优化的点: 类似两端对齐,但是最好可以让 section 偏上一点 尽可能一屏显示,如果不够那么出滚动条。 那么我们来看看有什么方案可以实现。
476 0
基于 Flex 实现两端对齐垂直布局

热门文章

最新文章