为什么底部内边距和底部边距无助于添加这些链接之间的垂直间距?
我有一个 div,里面有链接。 我用
标签将它们排列在一起,因为我不知道如何使用 CSS 添加垂直间距。 我尝试向样式规则添加底部边距和底部填充,但它似乎没有任何效果(为什么?)。 我可以添加另一个
标签来进一步分隔它们,但我必须假设有一种更好的方法可以使用我无法弄清楚的 CSS 来实现此目的。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body
{
height: 100%;
margin: 0;
padding: 0;
font-weight:normal;
font-size:12pt;
font-family: Verdana, Arial, Helvetica, serif, sans-serif;
background:lime;
}
#linksouter
{
margin: 0;
padding: 0;
border-style:solid;
border-width:0px;
position:absolute;
top: 0px;
left: 0px;
width: 80px;
background: blue;
text-align:left;
}
#linksinner
{
margin: 80px 0 0 .5em;
width:100%;
background:fuchsia;
display:inline;
height:100%;
}
#linksinner a
{
color:red;
text-decoration: none;
background:yellow;
}
</style>
</head>
<body>
<div id="linksouter">
<div id="linksinner">
<a href="#">1</a><br />
<a href="#">1</a><br />
<a href="#">1</a><br />
<a href="#">1</a><br />
<a href="#">1</a><br />
</div>
</div>
</body>
</html>
I have a div with links in it. And I'm lining them up one over the other with <br>
tags because I couldn't figure out how to add vertical spacingn with CSS. I tried adding a bottom margin and a bottom padding to the a style rule but it didn't seem to have any effect (why?). I could add another <br>
tag to separate them more but I have to assume there's a nicer way to do this with CSS that I just haven't been able to figure out.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body
{
height: 100%;
margin: 0;
padding: 0;
font-weight:normal;
font-size:12pt;
font-family: Verdana, Arial, Helvetica, serif, sans-serif;
background:lime;
}
#linksouter
{
margin: 0;
padding: 0;
border-style:solid;
border-width:0px;
position:absolute;
top: 0px;
left: 0px;
width: 80px;
background: blue;
text-align:left;
}
#linksinner
{
margin: 80px 0 0 .5em;
width:100%;
background:fuchsia;
display:inline;
height:100%;
}
#linksinner a
{
color:red;
text-decoration: none;
background:yellow;
}
</style>
</head>
<body>
<div id="linksouter">
<div id="linksinner">
<a href="#">1</a><br />
<a href="#">1</a><br />
<a href="#">1</a><br />
<a href="#">1</a><br />
<a href="#">1</a><br />
</div>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
垂直边距和填充仅适用于块级元素,例如
div
和p
。a
是一个内联元素,因此它不起作用。为了执行您想要的操作,您需要在链接中添加以下样式:
只有这样,顶部和底部的边距和分页才能正确应用
编辑:如果您这样做,您还可以获得去掉
标签Vertical margin and padding only works on block level elements like
div
andp
.a
is an inline element so it wont work.In order to do what you want you need to add the following style to your links:
only then will margin and paging for top and bottom get applied correctly
EDIT: if you do it this way you can also get rid of the
<br/>
tags要添加垂直间距,您可以执行以下操作:
边距不会对
元素产生任何影响,因为它们是内联的。 另一种解决方案是制造它们:
这意味着他们尊重您的利润,然后您就不需要您的
。To add vertical spacing, you can do this:
Margins won't have any effect on
<a>
elements because they're inline. An alternative solution is to make them:which would mean they respected your margins, and you then wouldn't need your
<br>
s.链接不能定义边距,因为默认情况下它们是“内联”元素。 内联元素不能应用边距或宽度规则。 要允许内联元素应用这些内容,您需要向规则添加另一个属性。
试试这个:
我认为对于您想要做的事情,您应该使用一个列表:然后
您可以将边距或填充规则应用于
标记。 如果您不想显示项目符号点,请使用:
Links can't have margins defined because by default they are "inline" elements. Inline elements can not have margin or width rules applied. To allow inline elements to have these things get applied, you need to add another property to your rule.
Try this:
I think for what you're looking to do, you should be using a list though:
You can then apply your margin or padding rules to the
<li />
tags. If you don't want bullet points to show up use:您需要为锚标记而不是 div 提供填充或边距。 但实际上不要这样做,而是这样做:
并给 p 一个 padding-bottom 或 padding-top 。
You need to give a padding or margin to the anchor-tags, not the divs. But actually don't, do this instead:
and give the p's a padding-bottom or padding-top.