简体   繁体   English

如何在图片中将div居中? (使用flex但不使用background-img)

[英]How to center a div inside an image? (using flex but not background-img)

As usual I am desesperately trying everything with css for hours ;) if it's not clear enough I am sorry, I try to make it as understandable as posisble. 像往常一样,我拼命地尝试使用css进行所有操作;)如果还不够清楚,我很抱歉,我尝试使其变得可理解。

I have a blured image and would like to put a div inside it, and center it. 我的图像模糊,我想在其中放一个div并将其居中。 the best would be to have the two elements next to each other 最好的办法是使两个元素彼此相邻

<parent/>
<child/>

because the parent is blured and the child isn't. 因为父母模糊了,孩子却没有。 I tried this 我试过了

<parent class="blured">
    <child class="notblured"/>
</parent>

.blured{
    -moz-filter: blur(5px);
    -ms-filter: blur(5px);
    filter: blur(5px);
}

.notblured{
    -moz-filter: blur(0) !important;
    -ms-filter: blur(0) !important;
    filter: blur(0) !important;
}

child{
    margin: auto;
    width: 19em;
}
parent{
    display: flex;
    justify-content: center;
    align-items:center;
}

But this doesn't work, both are blured + my image isn't really the way I want it 但这不起作用,两者都模糊了+我的图像并不是我想要的样子

So I would prefer to have them next to each others and then I can just center it manually, but it's not responsive then... 因此,我希望将它们彼此并排放置,然后我可以手动将其居中,但那时它没有响应...

child{
    padding: 1.5em;
    text-align:center;
    width: 19em;
    padding-top:0.5em;
    position: absolute;
    margin-left: auto;
    margin-right: auto;
    top: 6em;
    left: 0;
    right: 0;
}

Any suggestion? 有什么建议吗?

Use an outer element as parent for both, and center the child with the following CSS: 将外部元素都用作父元素,并使用以下CSS将子元素居中:

 .blured { -moz-filter: blur(5px); -ms-filter: blur(5px); filter: blur(5px); } .outer{ position: relative; width: 500px; height: 300px; } .child { position: absolute; left: 50%; top: 50%; width: 150px; height: 100px; background: blue; transform: translate(-50%, -50%); } .parent { position: absolute; left: 0; top: 0; height: 100%; width: 100%; background: red; } 
 <div class="outer"> <div class="parent blured"> </div> <div class="child"> </div> </div> 

Put the image with the filter blur on a pseudo element :before or :after , that way the filter does not affect the child elements. 将带有滤镜模糊的图像放在伪元素:before:after ,这样滤镜就不会影响子元素。 Needs some z-index too to work with this exact approach. 也需要一些z-index才能使用此精确方法。

 .parent { position: relative; display: flex; justify-content: center; align-items:center; min-height: 100vh; } .parent:before { content: ''; position: absolute; height: 100%; width: 100%; background-image: url('https://picsum.photos/200/300'); background-size: cover; -moz-filter: blur(5px); -ms-filter: blur(5px); filter: blur(5px); z-index: 1; } .child { position: relative; color: white; background: green; min-height: 200px; min-width: 300px; z-index: 9; } 
 <div class="parent blured"> <div class="child">CHILD</div> </div> 

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

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