如何使用显示flex
代替float: left
and float: right
?
见下图
我这样尝试过:
body {
padding: 0;
margin: 0;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
a {
text-underline: none;
}
.bg {
background: #eee;
}
.header {
display: flex;
background: #af2c2c;
}
.logo h2 {
color: #fff;
font-family: "Times New Roman", Times, serif
}
.menu_hang {
background-position: -70px -92px;
background-image: url(https://static.toiimg.com/photo/52121907.cms);
background-size: 200px;
width: 30px;
height: 24px;
opacity: .8;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="wrapper">
<div id="bg">
<header>
<div class="header">
<div>
<i class="menu_hang"></i>
</div>
<div class="logo">
<h2>HIM</h2>
</div>
<div>
<a href="">share</a>
</div>
</div>
</header>
</div>
</div>
</body>
</html>
这是我的代码https://plnkr.co/edit/Jlx5tzSB3CKQRVGrFSfh?p=preview
使用后display flex
。我面临两个问题
菜单是隐藏的。
份额anchor tag
显示在顶部
display: block
(或inline-block
)阻止。flex
容器内部居中,可以使用align-items: center;
。share
基本上,要将链接移到右侧,我们需要告诉浏览器我们希望.logo
容器占用整个空间。我们可以通过做到这一点flex-grow: 1
。这意味着“重要”元素是.logo
,它将占用空间。全部一起:
body {
padding: 0;
margin: 0;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
a {
text-decoration: none;
}
.bg {
background: #eee;
}
.header {
display: flex;
align-items: center;
background: #af2c2c;
}
.logo {
flex-grow: 1;
}
.logo h2 {
color: #fff;
font-family: "Times New Roman", Times, serif
}
.menu_hang {
display: block;
background-position: -70px -92px;
background-image: url(https://static.toiimg.com/photo/52121907.cms);
background-size: 200px;
width: 30px;
height: 24px;
opacity: .8;
}
.share-container {
margin-right: 10px;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<div id="wrapper">
<div id="bg">
<header>
<div class="header">
<div>
<i class="menu_hang"></i>
</div>
<div class="logo">
<h2>HIM</h2>
</div>
<div class="share-container">
<a href="">share</a>
</div>
</div>
</header>
</div>
</div>
https://plnkr.co/edit/5SYSWCHzHAe3ED5K?p=info&preview
顺便说一句:没有text-underline
CSS属性。你的意思可能是text-decoration: none
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句