简体   繁体   English

HTML / CSS大小链接到图像

[英]HTML/CSS Size Link to Image

I'm trying to make a link with an image like this 我正在尝试与这样的图像建立链接

<a href="http://test.com">
    <img src="image.png" />
</a>

It's showing up fine, but the clickable area of the link is larger than the image (a 50px circle). 它显示的很好,但是链接的可点击区域大于图像( 50px圆圈)。 I would like to only allow the area of the image to be clickable. 我只希望图像的区域是可单击的。 I tried to do this with the following css, but to no avail: 我尝试使用以下CSS执行此操作,但无济于事:

a {
    max-width: 50px;
    border-radius: 50%;
}

Changing the width and max-width seems to have no effect. 更改宽度和最大宽度似乎无效。 What would be the best way to do this? 最好的方法是什么?

I'm trying to do this for the circular contact buttons on the bottom of this site . 我正在尝试为此站点底部的圆形接触按钮执行此操作

Two things, the image on your website is not embedded into an anchor as you described. 两件事,您网站上的图像未如您所描述的那样嵌入到锚点中。 And the link you provide is missing the www. 并且您提供的链接缺少www. possibly calling a page that cannot respond or does not exist. 可能会调用无法响应或不存在的页面。

a solution: 一个解法:

#contact a { display: inline-block; }

the a tag is taking the entire width of the parent tag as it's been rendering as a block , if you want force it to render only the content of it's child, you can use inline-block . a标签正在整个width父标签,因为它一直呈现为block ,如果你想强迫它仅渲染的是孩子的内容,您可以使用inline-block

Make the image as a background to that div shape ( circle ) , you can get circle by css3 radius . 将图像作为该div形状(圆形)的背景,可以通过css3半径获得圆形。 The disadvantage of this is that the image will no longer be imbedded as an HTML img . 这样做的缺点是,图像将不再作为HTML img嵌入。 Then It will work just fine , i think 那我认为它会很好

You can give your link a class, such as .social-link, and apply the following code: 您可以为链接提供一个类,例如.social-link,并应用以下代码:

.social-link{
  max-width: 50px;
  display: block;
  margin: 0 auto;
  border-radius: 50%;
}

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

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