简体   繁体   English

如何在div内垂直和水平居中图像?

[英]How to center an image inside div both vertically and horizontally?

.spotlight-area img {
    width: 100px; 
    height: 100px; 
    margin-top: 30%; 
    margin-left: 40%; 
}

spotlight-area will be a div with random width and height. spotlight-area将是一个随机宽度和高度的div。 The image I'm using is 100px to 100px (resized). 我正在使用的图像是100px100px (调整大小)。 I've tried to center it with margin-top and margin-left but it looks like a crappy workaround. 我试图以margin-top和margin-left为中心,但它看起来像一个糟糕的解决方法。

What would you suggest? 你会建议什么?

Ps. PS。 It's a play button going to appear on top of videos container divs. 这是一个播放按钮,将出现在视频容器div的顶部。

Check this article out by Chris Coyier on CSS-Tricks. 请查看Chris Coyier关于CSS-Tricks的文章。 I think it might help out. 我认为这可能会有所帮助。 I ran into the same problem a few days ago and his answer worked for me. 几天前我遇到了同样的问题,他的回答对我有用。

Centering an image horizontally and vertically 水平和垂直居中图像

Basically the idea is, if your image has a set width and height. 基本上这个想法是,如果你的图像有一个设定的宽度和高度。 You can absolute position it from the left and top 50% and then do negative margins equal to half of the width and height to bring it back dead center. 您可以从左侧和顶部50%绝对定位它,然后将负边距等于宽度和高度的一半以使其返回死点。 They're are other ways to vertically center an image if it doesn't have set dimensions. 如果图像没有设置尺寸,它们是垂直居中图像的其他方式。

vertical-align: middle;
text-align: center;

It will help you... 它会帮助你......

clck on this link 点击这个链接

Mybe这将起作用:

   .spotlight-area { text-align: center; }

I guess this will work, Try and let know... 我想这会奏效,试着让我们知道......

 .spotlight-area img {
        width: 100px; 
        height: 100px; 
        text-align: center;
    }

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

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