将大图像垂直居中放置在较小的 div 中
我有一个问题让我有点疯狂。我有一个小 div,其中包含一个较大的图像(没有固定尺寸),
我正在拍摄这个较大的图像,并为其指定 100% 的宽度以缩小以适合框边界,但我需要图像垂直居中显示在 div 内部而不是自上而下。
有没有简单的 CSS 修复方法?我一直在摸索,但运气不佳。
Currently: Intended:
-------- --------
| div /w | |overflow|
| image | --------
-------- | div w/ |
|overflow| | image |
| image | --------
-------- |overflow|
--------
I have a problem thats driving me a little insane. I have a small div that contains a larger image (of no fixed dimension)
Im taking this larger image, and giving it a width of 100% to scale down to fit within the boxes boundaries, but I need the image to show up vertically centered inside the div rather than just top down.
Is there an easy CSS fix for this? I've been poking around, but not having much luck.
Currently: Intended:
-------- --------
| div /w | |overflow|
| image | --------
-------- | div w/ |
|overflow| | image |
| image | --------
-------- |overflow|
--------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会尝试这个CSS:
I'd try this CSS:
我建议将图像粘贴在 div 中,然后将 div 居中。有关如何正确执行此操作的详细说明此处< /strong>。
I would suggest sticking the image in a div and then centering the div. There is a detailed description on how to do this right here.
在不进一步了解您的问题的情况下,您可以通过多种方法来解决它。
最有可能的是,如果您将图像设置为 div 的背景(而不是将其放置在 HTML 中),它将解决您的问题;
我喜欢的另一种方法是使用
position:absolute
将图像精确定位在我想要的位置。另外,如果您要使用带有
vertical-align: middle
的方法,请确保将其应用于图像,而不是包含的 div。Without knowing more about your problem, there are many ways you could approach it.
Most likely, if you set the image as the background of the div (instead of placing it in the HTML), it will solve your problem;
Another method I like is to use
position:absolute
to position the image exactly where I want it.Also, if you're going to use a method with
vertical-align: middle
, make sure you apply that to the image, not the containing div.