이것은 작성하기가 훨씬 간단합니다.
a는 인라인 요소로 내용에 따라 너비가 늘어나고 높이를 설정하기 위해 인라인 블록으로 변환됩니다.
너비를 늘리려면 단어 수가 다르기 때문에 너비를 직접 지정할 수 없으며 패딩을 직접 엽니 다.
<!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>
* {
padding: 0;
margin: 0;
}
.nav {
height: 41px;
background-color: #FCFCFC;
/*上边框*/
border-top: 3px solid #FF8500;
/*下边框*/
border-bottom: 1px solid #EDEEF0;
}
a {
display: inline-block;
line-height: 41px;
text-decoration: none;
padding: 0 10px;
font-size: 12px;
color: #4C4C4C;
}
a:hover {
background: #eee;
}
.box {
width: 500px;
height: 300px;
border: 1px solid #CCC;
}
.box p {
padding-left: 20px;
background: pink;
}
</style>
</head>
<body>
<div class="nav">
<a href="#">设为首页</a>
<a href="#">手机新浪网</a>
<a href="#">移动客户端</a>
<a href="#">博客</a>
<a href="#">微博</a>
<a href="#">关注我</a>
</div>
<div class="box">
<p>hello world</p>
</div>
</body>
</html>