这是我的代码
html {
box-sizing: border-box;
}
* {
box-sizing: inherit;
margin: 0;
padding: 0;
}
header nav ul {
display: flex;
list-style: none;
justify-content: center;
align-items: center;
height: 3em;
background-color: #783F27;
}
.naviga {
border: solid medium;
border-radius: 0.4em;
margin: 0 0.5em;
width: 10em;
color: goldenrod;
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Learning</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<header>
<nav>
<ul>
<li><a class="naviga" href="">Menu</a></li>
<li><a class="naviga" href="">News</a></li>
<li><a class="naviga" href="">About</a></li>
<li><a class="naviga" href="">Contact</a></li>
</ul>
</nav>
</header>
</body>
</html>
即使我指定width: 10em
,<li>
元素也以min-content
(或在这种情况下相同的max-content
(的宽度显示。如果我指定height
,也会发生同样的情况。为什么会这样?
您应该更改li的宽度,而不是a标签的宽度,请参阅下面的示例
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Learning</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
box-sizing: border-box;
}
* {
box-sizing: inherit;
margin: 0;
padding: 0;
}
header nav ul {
display: flex;
list-style: none;
justify-content: center;
align-items: center;
height: 3em;
background-color: #783F27;
}
.naviga {
color: goldenrod;
}
.liClass {
border: solid goldenrod;
border-radius: 0.4em;
margin: 0 0.5em;
width: 20em;
}
</style>
</head>
<body>
<header>
<nav>
<ul>
<li class="liClass"><a class="naviga" href="">Menu</a></li>
<li class="liClass"><a class="naviga" href="">News</a></li>
<li class="liClass"><a class="naviga" href="">About</a></li>
<li class="liClass"><a class="naviga" href="">Contact</a></li>
</ul>
</nav>
</header>
</body>
</html>
我想,我还没有运行代码,但看起来这可能是问题所在?
html {
box-sizing: border-box;
}
* {
box-sizing: inherit;
margin: 0;
padding: 0;
}
您已经为整个文档定义了两次框大小。试着删除其中一个,我会测试你的代码,然后给你更多的细节。
我建议盒子尺寸:边框盒子;for*,并将其从html{}元素中删除
此外,像这样将display属性添加到navga的css中,它就可以工作了。
.naviga {
display: flex;
border: solid medium;
border-radius: 0.4em;
margin: 0 0.5em;
width: 200px;
color: goldenrod;
}
flex
属性设置灵活项的灵活长度。flex: 0 0 40%