【发布时间】:2016-11-16 15:22:49
【问题描述】:
用'a'标签包围一个div会产生奇怪的结果,感觉有必要让'a'标签尽可能宽而不是里面的孩子大小,我把它读作与HTML5有关。
Edge 或多或少地给出了我正在寻找的结果,但在 Chrome 中,出于某种原因,“a”标签是页面的整个宽度。
我还想在 'a' 标记中包含 div 'margin'。所以我不能使用'onclick'方法。这可以在没有javascript的情况下实现吗? JSFiddle.
<a href="http://example.com" style="background-color: #CCC;">
<div style="width: 100px; height: 100px; background-color: #C00; margin: 0px 8px 8px 0px; display:block;"><h2>text</h2></div>
</a>
<a href="http://example.com" style="background-color: #CCC;">
<div style="width: 100px; height: 100px; background-color: #C00; margin: 0px 8px 8px 0px;"><h2>text</h2></div>
</a>
<a href="http://example.com" style="background-color: #CCC;">
<div style="width: 100px; height: 100px; background-color: #C00; margin: 0px 8px 8px 0px; display:inline-block;"><h2>text</h2></div>
</a>
<a href="http://example.com" style="background-color: #CCC; display: block;">
<div style="width: 100px; height: 100px; background-color: #C00; margin: 0px 8px 8px 0px;"><h2>text</h2></div>
</a>
【问题讨论】:
-
标签不关心宽度,子元素即 div 将默认显示 display: block。这就是为什么一些 div 是全宽的(你是一个元素随着它们一起移动)。你可以给 a 元素一个
display: inline-block;我不完全明白,你想用 a 标签上的边距实现什么。 -
感谢@ThijsBouwes 您的评论,这只是为了让 div 之间没有空白。因此,如果您错过单击 div,它仍会将您带到链接。这没什么大不了的,但我发现它很奇怪,如果我需要对每个“a”标签进行 javascript 处理以找到里面的子尺寸,它会减慢页面速度。
标签: html hyperlink tags margin