[英]Centering a responsive image
On a page, I have an image that I want centered on the screen (horizontally). 在页面上,我有一个想要水平放置在屏幕上的图像。
It's responsive, so that it resizes as the screen gets too small. 它具有响应能力,因此可以在屏幕过小时调整大小。
<img class="img-responsive" src="~/Content/Images/marketing/combined-displays.png" />
I was using around it, but W3C gets cross with this, and wants me to use styles instead. 我一直在使用它,但是W3C对此表示怀疑,并希望我改用样式。
So, I read up, and found this to try center images: 因此,我阅读了一下,发现它可以尝试居中显示图像:
P.blocktext {
margin-left: auto;
margin-right: auto;
width: 6em
}
I then change the 然后,我更改
to this: 对此:
<p class="blocktext">
<img....>
</p>
But then the image goes tiny, in the middle of the screen. 但随后,图像在屏幕中间变得很小。
So I removed the width, but then it doesn't centre at all. 所以我删除了宽度,但是它根本没有居中。
How can I make this class center my image in the middle of the screen? 如何使课程在屏幕中间居中显示我的图像?
You could change the display
type of the image to block
and align it to the center by margin: 0 auto
declaration. 您可以将图像的
display
类型更改为block
并通过margin: 0 auto
将其与中心对齐margin: 0 auto
声明。
Besides, Twitter bootstrap has a built-in class called .center-block
that exactly do the same: 此外,Twitter引导程序具有一个名为
.center-block
的内置类,该类完全相同:
<img src="http://placehold.it/150x150" class="img-responsive center-block">
And .img-responsive
would handle the rest: .img-responsive
将处理其余部分:
.img-responsive { /* Taken from Twitter bootstrap source */
display: block;
max-width: 100%;
height: auto;
}
Simple css solution is 简单的CSS解决方案是
img {
display:block;
margin:0 auto;
max-width:100%;
}
max-width:100%; 最大宽度:100%; makes sure that the img isnt wider than the parent
确保img不比父级宽
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.