HTML a 元素不会扩展到父 li 尺寸
我有一个水平导航栏,并且我的 a 元素没有扩展到宽度和宽度。父 li 元素的高度。
如何修复我的 CSS,使 a 元素的宽度和宽度一样大?与外部/父 li 元素一样高?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
<!--
#navbar {
width: 450px;
height: 40px;
background-color: RGB(112,112,112);
list-style: none;
}
#navbar li {
width: 112.5px;
height: 40px;
display: inline;
float: left;
}
#navbar li a {
color: white;
width: 112.5px;
height: 40px;
}
#navbar a:link {
background-color: red;
}
#navbar a:visited {
background-color: purple;
}
#navbar a:active {
background-color: green;
}
#navbar a:hover {
background-color: blue;
}
-->
</style>
</head>
<body>
<ul id="navbar">
<li><a href="">home</a></li>
<li><a href="">contact us</a></li>
<li><a href="">about us</a></li>
<li><a href="">other</a></li>
</ul>
</body>
</html>
I have a horizontal nav bar, and my a elements are not expanding to be the width & height of the parent li element.
How can I fix my CSS so that the a elements are as wide & tall as the outer/parent li element?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
<!--
#navbar {
width: 450px;
height: 40px;
background-color: RGB(112,112,112);
list-style: none;
}
#navbar li {
width: 112.5px;
height: 40px;
display: inline;
float: left;
}
#navbar li a {
color: white;
width: 112.5px;
height: 40px;
}
#navbar a:link {
background-color: red;
}
#navbar a:visited {
background-color: purple;
}
#navbar a:active {
background-color: green;
}
#navbar a:hover {
background-color: blue;
}
-->
</style>
</head>
<body>
<ul id="navbar">
<li><a href="">home</a></li>
<li><a href="">contact us</a></li>
<li><a href="">about us</a></li>
<li><a href="">other</a></li>
</ul>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过使所有
a
元素display:block
但是为什么,sdleihssirhc?为什么?
因为默认情况下
元素是内联元素,这意味着(除许多其他因素之外)您无法为其指定宽度或高度。如果您尝试,浏览器将忽略您。
By making all
a
elementsdisplay:block
But why, sdleihssirhc? Why?
Because the
<a>
element is an inline element by default, which means (among many, many other things) you cannot give it a width or a height. If you try, the browser will ignore you.使所有
a
元素display: inline-block
。它结合了两全其美 - 您可以将它们并排放置,无需
float
,并为它们提供任何width
和height
> 你选择。您甚至可以使用vertical-align
来确定它如何与周围的文本对齐!Make all
a
elementsdisplay: inline-block
.It combines the best of both worlds - you can put them side-by-side without the need for
float
, and give them whateverwidth
andheight
you choose. You can even usevertical-align
to determine how it should be aligned with surrounding text!我缩进了额外的线条以帮助美观。正如 sdleihssirhc 所提到的,
a
元素必须设置为display:block
。还要注意,通过在顶部添加一些填充(并从块的整体高度中减去),我们也有一些垂直居中。I have indented the additional lines to help with appearance. The
a
elements must be set todisplay:block
as mentioned by sdleihssirhc. Notice also that by adding some padding to the top ( and subtracting from the overall height of the block) we have some vertical centering as well.按如下方式修改 CSS:
Modify your CSS as follows: