水平+垂直 居中

简介: 方案一 DOCTYPE html> 水平居中和垂直居中,并且父容器的宽度高度都是未知的,里面的子容器大小也是不一定的 .parent{ background: #bebebe; height: 300px; w...

方案一

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平居中和垂直居中,并且父容器的宽度高度都是未知的,里面的子容器大小也是不一定的</title>
    <style type="text/css">
    .parent{
        background: #bebebe;
        height: 300px;
        width: 700px;

        /* 水平居中*/
        text-align: center;

        /* 垂直居中*/
        display: table-cell;
        vertical-align: middle;
    }

    .child{
        background: #404040;
        height: 50px;
        width: 50px;
        color:white;
        /* 水平居中 */
        display: inline-block;
    }
    </style>
</head>
<body>
<!--

这个方案的优点:
1。兼容性比较高。

-->
<div class="parent">
    <div class="child">DEMO</div>
</div>
</body>
</html>

image

 

方案二

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平居中和垂直居中 absolute_transform</title>
    <style type="text/css">
    .parent{
        background: #bebebe;
        height: 400px;
        width: 600px;

        position: relative;
    }
    .child{
        background: #404040;
        height: 50px;
        color: white;

        position: absolute;
        left: 50%;
        top: 50%;

        transform: translate(-50%,-50%);
    }
    </style>
</head>
<body>
<div class="parent">
    <div class="child">DEMO</div>
</div>
</body>
</html>

image

 

 

方案三

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>水平居中和垂直居中flex_justify-content_align-items</title>
    <style type="text/css">
    .parent{
        background: #bebebe;
        height: 400px;
        width: 600px;

        display: flex;
        justify-content: center;
        align-items: center;
    }
    .child{
        background: #404040;
        color: white;


    }
    </style>
</head>
<body>
<div class="parent">
    <div class="child"><H1>DEMO</H1></div>
</div>
</body>
<!--
缺点: 兼容性是个问题。

-->
</html>

image

开始做,坚持做,重复做
相关文章
|
4天前
背景图像 - 水平或垂直平铺
背景图像 - 水平或垂直平铺。
9 2
|
10月前
水平居中 #31
水平居中 #31
37 0
水平渐近线与铅直渐近线
水平渐近线与铅直渐近线
|
C#
C#中,让按钮文字垂直水平都居中
C#中,让按钮文字垂直水平都居中
107 0
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(2)
内边距会影响盒子的可见框的大小,元素的背景会延伸到内边距,盒子的大小由内容区、内边距和边框共同决定。外边距指的是当前盒子与其他盒子之间的距离,他不会影响可见框的大小,而是会影响到盒子的位置。
144 0
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(2)
|
前端开发
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(4)
通过本章认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法
111 0
|
前端开发
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(1)
网页是一个多层的结构,设置样式样式,也是一层一层的设置,最终我们看到的最上面的一层。
88 0
认识盒子模型,盒子模型的边框、内外边距、水平布局、垂直布局、设置浮动、处理高度塌陷的基本方法(1)
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 偏上一点 尽可能一屏显示,如果不够那么出滚动条。 那么我们来看看有什么方案可以实现。
470 0
基于 Flex 实现两端对齐垂直布局
|
前端开发
这一次,彻底搞懂垂直水平居中
这一次,彻底搞懂垂直水平居中
130 0