html 如何保持更改的弹性父项中绝对项目的一致性?

ny6fqffe  于 2022-12-16  发布在  其他
关注(0)|答案(2)|浏览(96)

基本上,我在一个flex-box里面的一些图像上面有一些横幅。图像根据CSS对齐,而flexbox是在它的 *flex-direction上:行 * 方向,则当Flex盒切换到 * Flex方向时,它们都在顶部聚集并彼此窒息:第 * 栏。

<html>
    <div class="locations">
        <div class="location">
            <img src="resources/images/norbert-toth-I1oL89qxefc-unsplash.jpg" />
            <div class="location-container>
                <h2>Herring Bone Library</h2>
                <p>141 Address St. <br>Detroit, MI <br>49449</p>
            </div>
        </div>
        <div class="location ">
            <img src="resources/images/kristen-colada-adams-wpCJlKxCNRA-unsplash.jpg" />
            <div class="location-container">
                <h2>Sunrise Botanical <br>(Downstairs)</h2>
                <p>2397 Hatchet Rd. <br>Suite 11 <br>Detroit, MI <br>49447</p>
            </div>
        </div>
        <div class="location">
            <img src="resources/images/daria-volkova-_IhXaHmTJr8-unsplash.jpg" />
            <div class="location-container">
                <h2>Black Cat Coffee</h2>
                <p>686 My Mind <br>Detroit, MI <br> 49448</p>
            </div>
        </div>
    </div>
</html>

本质上,通过添加CSS,带有类 location-container 的banner不会随flex-box改变其方向,并且它们仅在第一个父对象中显示在另一个的顶部。
下面是他们的CSS:

.locations {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 3rem 4rem;
    gap: 2rem;
    position: relative;
}

.location-container {
    position: absolute;
    background-color: #fffaf7;
    margin-left: 1rem;
    padding: 1rem;
    top: 1rem;
}

.location-container h2 {
    font-family: "Roboto Condensed", sans-serif;
    margin-bottom: 1rem;
} 

.location-container p {
    font-family: "Cabin", sans-serif;
}

.location img {
    width: 100%;
    max-height: 46rem;
}

@media only screen and (max-width: 1200px) {
    .locations {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        margin: 3rem 4rem;
        gap: 2rem;
        position: relative;
    }

    .location-container {
        position: absolute;
        background-color: #fffaf7;
        margin-left: 1rem;
        padding: 1rem;
        top: 1rem;
    }
}

预计一旦网页的宽度达到1200 px,flex-box将把方向更改为direction:列,文本容器将保留在它们的相对位置。
当父容器弯曲时,有没有办法将横幅保持在正确的位置?

rekjcdws

rekjcdws1#

在你的媒体1200px中,你仍然使用position:absolute。你需要设置它的location-container类为relative,一旦你在那个视口中。你还需要添加min-width到它,使每个location-container是相等的。

.locations {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 3rem 4rem;
    gap: 2rem;
    position: relative;
}

.location-container {
    position: absolute;
    background-color: #fffaf7;
    margin-left: 1rem;
    padding: 1rem;
    top: 1rem;
}

.location-container h2 {
    font-family: "Roboto Condensed", sans-serif;
    margin-bottom: 1rem;
} 

.location-container p {
    font-family: "Cabin", sans-serif;
}

.location img {
    width: 100%;
    max-height: 46rem;
}

@media only screen and (max-width: 1200px) {
    .locations {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        margin: 3rem 4rem;
        gap: 2rem;
        position: relative;
    }

    .location-container {
        position: relative;
        background-color: #fffaf7;
        margin-left: 1rem;
        padding: 1rem;
        top: 1rem;
        min-width:300px;
    }
}
<div class="locations">
    <div class="location">
        <div class="location-container">
            <h2>Herring Bone Library</h2>
            <p>141 Address St. <br>Detroit, MI <br>49449</p>
        </div>
    </div>
    <div class="location">
        <div class="location-container">
            <h2>Sunrise Botanical <br>(Downstairs)</h2>
            <p>2397 Hatchet Rd. <br>Suite 11 <br>Detroit, MI <br>49447</p>
        </div>
    </div>
    <div class="location">
        <div class="location-container">
            <h2>Black Cat Coffee</h2>
            <p>686 My Mind <br>Detroit, MI <br> 49448</p>
        </div>
    </div>
</div>
iaqfqrcu

iaqfqrcu2#

对于你的原始代码来说,这可能不是真的,但是第一个<div class="location-container>在class属性中缺少了一个结束的",这会妨碍你,需要修正(在代码片段中完成)。
您使absolute相对于.locations定位为location-container,结尾为s,但它们应该相对于.location,而不使用s
所以

  • .locations中删除position: relative
  • .location { position: relative }添加到CSS

你可以走了。
顺便说一句,我确实注意到了溢出的.location内容,但添加了一些不错的随机图片来显示效果。

UPDATE在深入查看您的代码后,我注意到除了 flexbox.locations { flex-direction: column }之外的所有属性都是不同的。没有必要再次定义其他属性。

经验法则:

  • 定义介质查询之外的所有属性,这些属性对于每个设备都保持不变。
  • 仅覆盖需要更改和/或添加新属性的属性。在这种情况下,仅需要更改flex-direction

我相应地调整了片段...

.locations {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 3rem 4rem;
    gap: 2rem;

/*    position: relative; /* REMOVE */
}

/* ADD */
.location {
    position: relative;
}

.location-container {
    position: absolute;
    background-color: #fffaf7;
    margin-left: 1rem;
    padding: 1rem;
    top: 1rem;
}

.location-container h2 {
    font-family: "Roboto Condensed", sans-serif;
    margin-bottom: 1rem;
}

.location-container p {
    font-family: "Cabin", sans-serif;
}

.location img {
    width: 100%;
    max-height: 46rem;
}

@media only screen and (max-width: 1200px) {
    .locations {
        flex-direction: column;
   }
}
<div class="locations">

    <div class="location">
        <img src="https://picsum.photos/1200/700?random=1">
        <div class="location-container">
            <h2>Herring Bone Library</h2>
            <p>141 Address St. <br>Detroit, MI <br>49449</p>
        </div>
   </div>

    <div class="location">
        <img src="https://picsum.photos/1200/700?random=2">
        <div class="location-container">
            <h2>Sunrise Botanical <br>(Downstairs)</h2>
            <p>2397 Hatchet Rd. <br>Suite 11 <br>Detroit, MI <br>49447</p>
        </div>
    </div>

    <div class="location">
        <img src="https://picsum.photos/1200/700?random=3">
        <div class="location-container">
            <h2>Black Cat Coffee</h2>
            <p>686 My Mind <br>Detroit, MI <br> 49448</p>
        </div>
    </div>
</div>

相关问题