简体   繁体   English

我怎样才能在这个 div 中居中这个图像?

[英]How can I center this image inside of this div?

How can I center this image that I have in this div in a way that it won't move the 'line' div?如何以不会移动“line”div的方式将我在这个div中的这个图像居中? I want the line to be touching the top of the square too.我希望这条线也能接触到正方形的顶部。

 .black { background: black; } .square { width: 200px; height: 500px; margin: 37px auto; border-radius: 2px; } .image { height: 60px; width: 60px; } .line { width: 4px; height: 500px; background-color: red; }
 <div class="container"> <div class="square black"> <img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg"> <div class="wrapper"> <div class="line"></div> <div class="rectangle"></div> </div> </div>

Here is one way to prevent it from disrupting the flow layout of your container:这是防止它破坏容器的流布局的一种方法:

you can make the container a position of relative, and the image a position of absolute, positioned off the top and left by 50%, then transform it so that the center of the image is in the center position.您可以将容器设置为相对位置,将图像设置为绝对位置,从顶部和左侧定位 50%,然后对其进行变换,使图像的中心位于中心位置。

You could also just make the image a background-image of the div instead of using an image element, which may be easier to manipulate.您也可以只使图像成为 div 的背景图像,而不是使用图像元素,这可能更容易操作。

 .black { background: black; } .square { position: relative; width: 200px; height: 500px; margin: 37px auto; border-radius: 2px; } .image { height: 60px; width: 60px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .line { width: 4px; height: 500px; background-color: red; }
 <div class="container"> <div class="square black"> <img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg"> <div class="wrapper"> <div class="line"></div> <div class="rectangle"></div> </div> </div> </div>

I'm not sure I understand your exact desired end goal.我不确定我是否了解您所期望的最终目标。 But, if I understand correctly, you could create a flex parent to justify the image, and then position the line absolutely within that.但是,如果我理解正确,您可以创建一个 flex 父级来证明图像的合理性,然后将线绝对定位在其中。 See -看 -

 .black { background: black; } .square { width: 200px; height: 500px; margin: 37px auto; border-radius: 2px; overflow: hidden; display: flex; align-items: center; justify-content: center; position: relative; } .image { height: 60px; width: 60px; } .line { width: 4px; background-color: red; position: absolute; left: 0; top: 0; bottom: 0 }
 <div class="square black"> <div class="line"></div> <img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg"> </div>

You can just use these css for .square and .image您可以将这些 css 用于 .square 和 .image

        .square {
            width: 200px;
            height: 500px;
            margin: 37px auto;
            border-radius: 2px;

            position: relative;
        }

        .image {
            height: 60px;
            width: 60px;

            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }

You can easily center a image by using CSS position absolute.您可以使用 CSS 绝对位置轻松居中图像。 By making the position of square black class "absolute" and apply to properties "top: 45%;"通过将方形黑色类的位置设置为“绝对”并应用于属性“顶部:45%;” and "left: 47%" .和“左:47%”。 By applying this your problem will be definitely solve.通过应用这个,你的问题肯定会得到解决。

 .black { background: black; } .square { display: flex; align-item: center; justify-content: center; width: 200px; height: 500px; border-radius: 2px; } .image { height: 60px; width: 60px; }
 <div class="container"> <div class="square black"> <img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg"> </div> </div>

 .black { background: black; } .square { position: absolute; top: 45%; left: 47%; width: 200px; height: 500px; margin: 37px auto; border-radius: 2px; } .image { height: 60px; width: 60px; position: absolute; top:50%; left:50%; } .line { width: 4px; height: 500px; background-color: red; }
 <div class="container"> <div class="square black"> <img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg"> <div class="wrapper"> <div class="line"></div> <div class="rectangle"></div> </div> </div>

 .black { background: black; } .square { width: 200px; height: 500px; margin: 37px auto; border-radius: 2px; } .image { height: 60px; width: 60px; } .line { width: 4px; height: 500px; background-color: red; }
 <div class="container"> <div class="square black"> <img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg"> <div class="wrapper"> <div class="line"></div> <div class="rectangle"></div> </div> </div>

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

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