[英]a href link for entire div in HTML/CSS
这是我试图在 HTML/CSS 中完成的任务:
我有不同高度和宽度的图像,但它们都在 180x235 以下。 所以我想要做的是创建一个带有border
和vertical-align: middle
的div
vertical-align: middle
它们vertical-align: middle
。 我已经成功地做到了,但现在我被困在如何正确地链接整个div
的 href 上。
这是我的代码:
<div id="parentdivimage" style="position:relative;width:184px;height:235px;border-width:2px;border-color:black;border-style:solid;text-align:center;">
<div id="childdivimage" style="position:absolute;top:50%;height:62px;margin-top:-31px;">
<img src="myimage.jpg" height="62" width="180">
</div>
</div>
请注意,为了方便复制粘贴这里,样式代码是内联的。
我在某处读到,我可以简单地在代码顶部添加另一个父 div,然后在其中执行 href。 但是,根据一些研究,它不会是有效的代码。
所以再次总结一下,我需要整个 div ( #parentdivimage
) 作为一个 href 链接。
2014 年 6 月 10 日更新:在 HTML5 中使用 a 中的 div 在语义上是正确的。
您需要在以下场景中进行选择:
<a href="http://google.com">
<div>
Hello world
</div>
</a>
这在语义上是不正确的,但它会起作用。
<div style="cursor: pointer;" onclick="window.location='http://google.com';">
Hello world
</div>
这在语义上是正确的,但它涉及使用 JS。
<a href="http://google.com">
<span style="display: block;">
Hello world
</span>
</a>
这在语义上是正确的并且按预期工作,但不再是 div。
为什么不去掉<div>
元素并用<a>
替换它呢? 正因为定位标记不是一个div并不意味着你不能与风格它display:block
,高度,宽度,背景,边框等,您可以使它看起来像一个div,但仍然像一个链接. 这样您就不会依赖于某些用户可能未启用的无效代码或 JavaScript。
这样做:
Parentdivimage 应该有指定的宽度和高度,它的位置应该是:
position: relative;
就在 parentdivimage 内,在 parent 包含的其他 div 旁边,您应该放置:
<a href="linkt.to.smthn.com"><span class="clickable"></span></a>
然后在css文件中:
.clickable {
height: 100%;
width: 100%;
left: 0;
top: 0;
position: absolute;
z-index: 1;
}
由于高度和宽度设置为 100%,span 标签将填充其父块,即 parentdiv。 由于将 z-index 设置为高于其他元素,因此 Span 将位于所有周围元素的顶部。 最后 span 将是可点击的,因为它位于“a”标签内。
脱离超现实主义梦想所说的,根据我的经验,最好设置锚标签的样式,但这确实取决于你在做什么。 下面是一个例子:
网址:
<div class="parent-div">
<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
</div>
然后是 CSS:
.parent-div {
width: 200px;
}
a {
display:block;
background-color: #ccc;
color: #000;
text-decoration:none;
padding:10px;
margin-bottom:1px;
}
a:hover {
background-color: #ddd;
}
你可以做两件事:
将#childdivimage
更改为span
元素,并将#parentdivimage
更改为锚标记。 这可能需要您添加更多样式以使事情看起来完美。 这是首选,因为它使用语义标记,并且不依赖于 javascript。
#parentdivimage
。 您必须通过修改此事件内的window.location
来重定向浏览器窗口。 这是 TheEasyWay TM ,但不会优雅地降级。使div
的id="childdivimag"
一个span
,在一个,而是和包装a
元素。 由于默认情况下span
和img
是内联元素,这仍然有效,而div
是块级元素,因此包含在a
时标记无效。
将display:block
放在锚元素上。 和/或zoom:1
;
但你真的应该这样做。
a#parentdivimage{position:relative; width:184px; height:235px;
border:2px solid #000; text-align:center;
background-image:url("myimage.jpg");
background-position: 50% 50%;
background-repeat:no-repeat; display:block;
text-indent:-9999px}
<a id="parentdivimage">whatever your alt attribute was</a>
这可以通过多种方式完成。 a. 使用嵌套在标签内。
<a href="link1.html">
<div> Something in the div </div>
</a>
湾使用内联 JavaScript 方法
<div onclick="javascript:window.location.href='link1.html' ">
Some Text
</div>
C. 在标签内使用 jQuery
HTML:
<div class="demo" > Some text here </div>
jQuery:
$(".demo").click( function() {
window.location.href="link1.html";
});
我要做的是在<a>
标签内放置一个跨度,将跨度设置为阻止,并为跨度添加大小,或者只是将样式应用于<a>
标签。 绝对处理<a>
标签样式中的定位。 a where
JavaScript 将捕获该事件a where
添加一个onclick event
,然后在 JavaScript 事件结束时返回 false 以防止href
默认操作和点击的冒泡。 这适用于启用或不启用 JavaScript 的情况,并且任何 AJAX 都可以在 Javascript 侦听器中处理。
如果您使用的是 jQuery,则可以将其用作侦听器并省略a
标记中的onclick
。
$('#idofdiv').live("click", function(e) {
//add stuff here
e.preventDefault; //or use return false
});
这允许您根据需要将侦听器附加到任何更改的元素。
我很惊讶到目前为止没有人提出这个简单的技巧! (不过,denu 做了类似的事情。)
如果你想要一个链接来覆盖整个div
,一个想法是创建一个空的<a>
标签作为第一个孩子:
<div class="covered-div">
<a class="cover-link" href="/my-link"></a>
<!-- other content as usual -->
</div>
div.covered-div {
position: relative;
}
a.cover-link {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
当使用<ul>
创建块部分或幻灯片并且您希望整个幻灯片是一个链接(而不是简单的幻灯片上的文本)时,这尤其<ul>
。 在<li>
的情况下,用<a>
包裹它是无效的,因此您必须将封面链接放在项目内并使用 CSS 将其扩展到整个<li>
块。
请注意,将它作为第一个孩子意味着它将使文本中的其他链接或按钮无法通过点击访问。 如果您希望它们可点击,那么您必须将其设为最后一个子项。
在原始问题的情况下:
<div id="parentdivimage" style="position:relative;width:184px;height:235px;border-width:2px;border-color:black;border-style:solid;text-align:center;">
<a class="cover-link" href="/my-link"></a> <!-- Insert this empty link here and use CSS to expand it over the entire div -->
<div id="childdivimage" style="position:absolute;top:50%;height:62px;margin-top:-31px;">
<img src="myimage.jpg" height="62" width="180">
</div>
<!-- OR: it can also be here if the childdivimage divs should have their own clickable links -->
</div>
带有<div>
标签的链接:
<div style="cursor: pointer;" onclick="window.location='http://www.google.com';">
Something in the div
</div>
带有<a>
标签的链接:
<a href="http://www.google.com">
<div>
Something in the div
</div>
</a>
我只是做
onClick="location.href='url or path here'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.