美文网首页
flex 宽高相等 文字居中对齐

flex 宽高相等 文字居中对齐

作者: 9682f4e55d71 | 来源:发表于2019-01-04 12:11 被阅读12次

效果图

flex_width_eq_height.png

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            /* height: 300px; */
            /* for demo purposes */
            color: white;
        }

        flex-container {
            display: flex;
            /* primary flex container */
            flex-direction: row;
            /* horizontal alignment of flex items
                                      (default value; can be omitted) */
            align-items: stretch;
            /* will apply equal heights to flex items
                                      (default value; can be omitted) */
            height: 100%;
            width: 100%;
        }

        flex-item {
            display: flex;
            /* nested flex container */
            flex-direction: column;
            /* vertical alignment of flex items */
            justify-content: center;
            /* center flex items vertically */
            align-items: center;
            /* center flex items horizontally */
        }

        flex-item:first-child {
            flex: 3;
            /* consume 3x more free space than sibling */
            background-color: #a333c8;
        }

        flex-item:last-child {
            flex: 1;
            background-color: #db2828;
        }
        flex-item:last-child #bg{
            background-color: black;
            display: flex;
            width: 100%;
            height: 0;
            padding-top: 100%;
            align-items: stretch;
        }
        #text{
            position: absolute;
            border: 1px solid green;
        }
    </style>

</head>

<body>
    <flex-container>
        <flex-item>
            <!-- also flex container -->
            <p>Text Text Text</p>
            <p>Text Text Text</p>
            <!-- <p>Text Text Text</p> -->
            <p>Text Text Text</p>
        </flex-item>
        <flex-item>
            <!-- also flex container -->
            <div id='bg'></div>
            <div id='text'>Forward</div>
        </flex-item>
    </flex-container>
</body>

</html>

相关文章

  • flex 宽高相等 文字居中对齐

    效果图

  • [小结]css垂直居中方式小结

    文字垂直居中例如: 块元素垂直居中于父元素(也叫负边距居中,宽高都适用) 或者可以水平对齐 块元素(容器)水平居中

  • CSS 垂直居中的几种实现思路方法

    上下padding相等 绝对定位实现垂直居中(定宽高用负margin,不定宽高用transform) vertic...

  • flex 布局文字对齐

    图片和文字的中线对齐一直是一个头疼的问题。之前一直用flex布局做垂直居中对齐。 常用属性: flex布局多个子元...

  • html元素居中

    一、不定宽高元素居中 table absolute, transform css3 flex :before和di...

  • 面试题 -- 水平垂直居中

    水平居中 元素的水平垂直居中 1.定位 2.用translate设置宽、高 3.flex 4.table-cell

  • html,css基础知识

    1.怎么让一个不定宽高的 DIV,垂直水平居中? 使用Flex 只需要在父盒子设置:display: flex; ...

  • css简单样式-1

    margin:0px auto(外边距:上下为零左右相等-居中) width:宽 height:高 backgr...

  • CSS居中

    水平居中 文字的水平居中 容器的水平居中 垂直居中 行高与容器高设为相等 当文本行高等于父容器高度时, 就可以实现...

  • css –“left:50%,transform:transla

    最近了解flex布局,关于居中对齐的,根据之前的居中对齐有:margin: 0 auto 现在介绍另一种方法: 这...

网友评论

      本文标题:flex 宽高相等 文字居中对齐

      本文链接:https://www.haomeiwen.com/subject/rduhrqtx.html