简体   繁体   English

如何使标签边框环绕图像

[英]How to make an a tag border wrap around an image

I need the border to be applied to the a tag but when I do this the border doesn't wrap around the image how can I make this happen? 我需要将边框应用于a标签,但是当我这样做时边框不会围绕图像缠绕,如何使这种情况发生?

<div>
  <a href="#">
    <img src="https://cdn.pixabay.com/photo/2015/04/23/17/41/javascript-736400_960_720.png">
  </a>
</div>

css: CSS:

a{
  border: 5px solid blue;
}

https://jsfiddle.net/b7qdcub1/ https://jsfiddle.net/b7qdcub1/

You have to apply display: block to your a tag so that it behaves as a block level element. 你必须申请display: block给你a标记,以便它表现为一个块级元素。 Also give it a fixed width and a height to encompass the image inside it. 还给它固定的widthheight以包围其中的图像。

Refer code: 参考代码:

 a { border: 5px solid blue; display: block; width: 720px; height: 720px; } 
 <div> <a href="#"> <img src="https://cdn.pixabay.com/photo/2015/04/23/17/41/javascript-736400_960_720.png"> </a> </div> 

I'm guessing you are trying to clip image to its bounds. 我猜您正在尝试将图像裁剪到其边界。 Try this 尝试这个

a{
border: 5px solid blue;
overflow: hidden;
}
    a{
  border: 5px solid blue;
  display:inline-block;
}

If you want to know about Why anchor tag does not take height and width of its containing element Read This Answer . 如果您想知道为什么定位标记不包含其包含元素的高度和宽度,请 阅读此答案 Instead of border property I'm using css outline . 我使用的不是CSS边框属性,而是CSS轮廓

NB: There are lots of technique you can solve this this. 注意:有很多技术可以解决这个问题。 like Set the border for img element or set display inline-block on anchor tag. 例如,为img元素设置边框或在锚点标签上设置inline-block显示。 Check this Fiddle for more solution 检查此小提琴以获取更多解决方案

 div a{ outline:5px solid blue; } div a img{ vertical-align:bottom; /* Need this to clear white-space */ } 
 <div> <a href="#"> <img src="https://cdn.pixabay.com/photo/2015/04/23/17/41/javascript-736400_960_720.png"> </a> </div> 

Just put a border on the div. 只需在div上放置边框即可。

 div { border:solid;} 
 <div> <a href="#"> <img src="https://cdn.pixabay.com/photo/2015/04/23/17/41/javascript-736400_960_720.png"> </a> </div> 

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

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