我想让flex控制它子元素div里的span 例子如下
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,minimum-scale=1, user-scalable=no" />
- <title>头部</title>
- <style type="text/css">
- html{
- font-size: 100px;
- }
- *{
- margin: 0;
- padding: 0;
- }
- img{
- display:block;
- }
- .header{
- width: 3.2rem;
- height:0.42rem;
- display: flex;
- background-color: yellow;
- align-items: center;
- justify-content: space-between;
- box-sizing: border-box;
- padding:0 0.15rem;
- }
- </style>
- </head>
- <body>
- <div class="header">
- <div class="right">
- <span style="font-size: 0.05rem;">1111</span>
- </div>
- <div class="left">
- <span style="font-size: 0.05rem;">1111</span>
- </div>
- </div>
- </body>
- </html>
运行结果
子元素为span
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <style type="text/css">
- html{
- font-size: 10px;
- }
- .box {
- display: flex;
- align-items: center;
- padding: 4rem 2rem;
- color: white;
- background-color: black;
- }
- .item {
- flex-grow: 1;
- height: 6rem;
- line-height: 60px;
- text-align: center;
- border: 1px solid white;
- background-color: #ff0000;
- }
- </style>
- </head>
- <body>
- <div class="box">
- <span class="item">元素</span>
- <span class="item">元素</span>
- <span class="item">元素</span>
- <span class="item">元素</span>
- </div>
- </body>
总之,flex只能对它子元素起作用,不论是块级还是行内。 但是一定不能对它的子元素的子元素起作用。