简体   繁体   English

HTML-如何使<a>边框可点击?</a>

[英]html - how to make <a> clickable in border?

Say I have a <a class="myclass" href="foo.htm">Click Here</a> and in css something like this: 假设我有一个<a class="myclass" href="foo.htm">Click Here</a>并且在CSS中是这样的:

.myclass
{
border: 2px solid #000;
padding: 1em;
}

so the <a> looks like a button but it only operates when clicked on the text, not in the whole box. 所以<a>看起来像一个按钮,但它仅文字点击时,而不是在整个盒子操作。 How can I make so that the box also "catches" onClick? 如何使框也能“捕捉” onClick?

Block will not work well unless you float the element and give it a fixed width. 除非您浮动元素并为其设置固定宽度,否则块将无法正常工作。 I think "inline-block" would work better. 我认为“内联阻止”会更好。

.myclass{
    display: inline-block;
    border: 2px solid #000;
    padding: 1em;
}

You can see it in action here: http://jsfiddle.net/2tmzL/ 您可以在此处查看其运行情况: http : //jsfiddle.net/2tmzL/

Browser support for inline-block is pretty good: http://caniuse.com/inline-block 浏览器对inline-block的支持非常好: http : //caniuse.com/inline-block

将锚定标签包裹在另一个容器元素周围

<a class=".." href=".."><div>Click here</div><a>

< a > is an inline , you have to transform it to a block, try this <a>是一个内联,您必须将其转换为一个块,尝试这个

.myclass:
{
  display:block;
  border: 2px solid #000;
  padding: 1em;
}

您需要为元素设置css属性display:block或inline-block(取决于大小写...)。

I seem to be able to click the entire link. 我似乎可以单击整个链接。 Make sure you remove : after .myclass. 确保删除.myclass之后。 Also if it's still not working you may like to try adding display:block; 另外,如果仍然无法正常工作,您可能想尝试添加display:block;

Alternatively in html5 you can wrap the a tag around a block element. 另外,在html5中,您可以将标记包装在block元素周围。 This will work in older html though it's not correct. 这将在较旧的html中运行,尽管不正确。

.myclass
{
    border: 2px solid #000;
    padding: 1em;
    display:block;
}

The issue is that a's are inline elements, and padding doesn't work the way we expect with inline elements. 问题是a是内联元素,而填充不符合我们对内联元素的期望。 Change the a's to a block level element, and everything should work as you expect (note the removal of the ":" in the CSS declaration, that shouldn't be there): 将a更改为块级元素,一切都会按预期工作(请注意,在CSS声明中删除了“:”,但不应存在):

.myclass {
   display: block;
   border: 2px solid #000;
   padding: 1em;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM