[英]CSS - Force image to have same width and height of a span
I have put an image on a span like this: 我将图像放在这样的跨度上:
word_span.append('<a id="imagelink" href="'+ image.link +'" " target="_blank"><img src="' + image.src +'" border="2px" /></a>')
The css code for span element is like this: span元素的css代码如下:
span.m10 {
height:90px;
width:90px;
}
What I need to do is to force image to be contained in the dimensions of the span. 我需要做的是强制将图像包含在跨度的尺寸中。 Ι've noticed that the image is not displayed with the dimensions of the span, but keeps the proportion of the original size.
我注意到,图像没有显示跨度的尺寸,而是保持原始尺寸的比例。 It follows just the width of the span but not the height.
它仅遵循跨度的宽度,而不遵循高度。
Why might this happen? 为什么会发生这种情况?
Thank you in advance. 先感谢您。
The span
element is an inline element in HTML. span
元素是HTML中的内联元素。 You can not specify a width and height for an inline element. 您不能为内联元素指定宽度和高度。
With that said, you can make the span behave like an inline-block
like so 话虽如此,您可以使跨度像一个
inline-block
一样
.m10 {
display: inline-block;
width: 90px;
height: 90px;
}
Then you tell the image inside the span to use the full width available 然后,您告诉跨度内的图像使用可用的全宽
.m10 img {
width: 100%;
}
You might want to consider just letting the span
display as a block
element instead. 您可能要考虑只让
span
显示为block
元素。 IE does not support inline elements being displayed as inline-block
. IE不支持将内联元素显示为
inline-block
。 Though, then you might aswell change the span
to a div
and save you some time. 不过,您也可以将
span
更改为div
并节省一些时间。
Have you tried using something on the image in your CSS to constrain the image to be only 100% width of the parent? 您是否尝试过在CSS中的图像上使用某些东西来将图像限制为仅父图像宽度的100%? Such as:
如:
span.m10 > a > img{
max-width: 100%;
height: auto;
}
This would mean the image would never go beyond the parent container 这意味着图像永远不会超出父容器
To change image proportions use background for span (div) as described below 要更改图像比例,请使用背景作为跨度(div),如下所述
div
{
background-image:url('image.gif');
background-repeat:no-repeat;
background-size:100% 100%;
}
You have to make your span
display:block;
您必须使
span
display:block;
to make it behave like a block container. 使它的行为像一个块容器。
Try this 尝试这个
span.m10 {
height:90px;
width:90px;
display:block;
}
#imagelink {
display:block;
width:100%;
height:100%;
}
.m10 img {
width: 100%;
height:100%;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.