flex 宽高相等 文字居中对齐
2019-01-04 本文已影响12人
9682f4e55d71
效果图
![](https://img.haomeiwen.com/i1924687/7cdbdd4c10b1f897.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>