简体   繁体   English

为什么将 float:left/right 添加到我的 CSS 会将另一个 div 移动到顶部?

[英]Why does adding float:left/right to my CSS moves another div to the top?

Desired behavior: the center image of the restaurant to be centered horizontally, with a margin-top of 100px.期望的行为:餐厅的中心图像水平居中, margin-top像素。

Specific problem: When I add the float property to the left and right images (the arrow and the magnifying glass), the center image moves all the way to the top of its container.具体问题:当我将float属性添加到左右图像(箭头和放大镜)时,中心图像一直移动到其容器的顶部。

Reproducer: Here is a link to my CodePen: https://codepen.io/annabaker/pen/gObmWWx转载者:这是我的 CodePen 的链接: https ://codepen.io/annabaker/pen/gObmWWx

HTML: HTML:

<div class="restaurant-widget">
    <div class="left-arrow">
        <img src="https://i.imgur.com/8T03wEO.png">
    </div>
    <div class="search">
        <img src="https://i.ibb.co/zQX1b7N/zoom-32.png">
    </div>
    <div class="restaurant">
       <img src="https://i.imgur.com/4FeEerb.jpg">
    </div>
    <div class="header">
        THE WATER CLUB
    </div>
</div>

CSS: CSS:

.restaurant-widget {
    color: #4c4c4c;
    box-shadow: 1px 1px 16px rgba(0,0,0,.58);
    border-radius:1px;
    /* this centers the actual widget itself, provided width is defined */
    margin: auto;
    width: 500px;
    height: 900px;
}

.left-arrow img {
    margin-top: 50px;
    margin-left: 50px;
    float: left;
 }

.search img {
    margin-top: 50px;
    margin-right: 50px;
    float: right;
 }

.restaurant img {
    /* this makesthe restaurant image circular */
    border-radius: 50%;
    border: 5px solid grey;
    box-shadow: 1px 1px 16px rgba(0,0,0,.58);
    display: block;
    margin-top: 100px;
    margin-left: auto;
    margin-right: auto;
}

That's due to "collapsing margins" (you can google that for details...): The top margin of .restaurant img is "merged" with the margin of .restaurant-widget , resulting in a margin outside of the container.这是由于“折叠边距”(您可以在谷歌上搜索详细信息...): .restaurant img的顶部边距与.restaurant-widget的边距“合并”,导致容器的边距。

To avoid that, you can add padding-top: 1px to the container ( .restaurant-widget )为避免这种情况,您可以将padding-top: 1px添加到容器 ( .restaurant-widget )

Everytime you use float , you remove the element from its normal flow in the page and put it in another context .每次使用float ,都会从页面中的正常流中删除该元素并将其放在另一个上下文中 You can imagine the floating element really "floating" above the elements in the basic context.您可以想象浮动元素真的“浮动”在基本上下文中的元素之上。 As all HTML in a page is rendered line by line, the elements were been created everyone in its context (based on its display properties).由于页面中的所有 HTML 都逐行呈现,因此每个人都在其上下文中创建了元素(基于其display属性)。

The positioning of you central image when the others are not floated occurs the way it occurs because of the fact that the other images are in the same context as the central image, and so central image occupy that spot.当其他图像没有浮动时,您的中心图像的定位发生的方式与它发生的方式相同,因为其他图像与中心图像处于相同的上下文中,因此中心图像占据该位置。 When you do apply float , you remove the other images from its context, so central image can now occupy a new spot on the page.当您应用float ,您会从其上下文中删除其他图像,因此中央图像现在可以在页面上占据一个新位置。 About this topic of contexts, I recommend you a further reading here: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context关于上下文这个主题,我建议您在此处进一步阅读: https : //developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context

There are some ways to achieve what you want.有一些方法可以实现你想要的。 CSS Flexbox and CSS Grid are, probably, the best and the simpler ways. CSS FlexboxCSS Grid可能是最好且更简单的方法。 I redid your code using CSS Grid, here's a Codepen: https://codepen.io/alac1984/pen/WNbpYmb我使用 CSS Grid 重新编写了您的代码,这是一个 Codepen: https ://codepen.io/alac1984/pen/WNbpYmb

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

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