css 如何将图片固定到文章的底部?

omqzjyyz  于 2022-12-30  发布在  其他
关注(0)|答案(3)|浏览(167)

我几乎完成了一个小网站,但我有一个统一性的问题。所以有一个图像位于底部的每篇文章在我的代码,但我目前已经得到了它的设置,图像是直接在下面的段落之前。这成为一个问题,当某些段落比其他短,导致一些图像在它们应该在底部的时候在它们下面留下空白。我怎么才能解决这个问题呢?我"我试过将文章设置为position: relative,将图片设置为position: absolute,但是这只是将图片向右移动并水平拉伸。非常感谢帮助!如果需要,我可以分享完整的代码,但是它太长了,所以我把它精简到了与问题直接相关的部分。
代码示例:超文本:

<div class="games">
            <article>
            <div class="upper-section">
                <h2>Hollow Knight</h2>
                <p class="tags">Difficult, Souls-like</p>
                <hr>
            </div>
                <p>"Forge your own path in Hollow Knight! An epic action adventure through a vast ruined kingdom of insects and heroes. Explore twisting caverns, battle tainted creatures and befriend bizarre bugs, all in a classic, hand-drawn 2D style."</p>
                <img src="hollow-knight.png">
            </article>
            <article>
            <div class="upper-section">
                <h2>Celeste</h2>
                <p class="tags">Difficult, Amazing Soundtrack</p>
                <hr>
            </div>
                <p>"Help Madeline survive her inner demons on her journey to the top of Celeste Mountain, in this super-tight platformer from the creators of TowerFall. Brave hundreds of hand-crafted challenges, uncover devious secrets, and piece together the mystery of the mountain."</p>
                <img src="celeste.png">
            </article>
            <article>
            <div class="upper-section">
                <h2>Ori and the Blind Forest/Will of the Wisps</h2>
                <p class="tags">Amazing Soundtrack, Metroidvania</p>
                <hr>
            </div>
                <p>"Ori and the Blind Forest tells the tale of a young orphan destined for heroics, through a visually stunning action-platformer crafted by Moon Studios for PC."</p>
                <img src="ori.png">
            </article>
        </div>

以及CSS:

body {
    margin: 0;
    padding-top: 150;
    padding-bottom: 0;
    background-color: rgb(206, 238, 197);
}

article {
    background-color: #67aa69;
    text-align: center;
    width: 30%;
    padding: 2;
    position: relative;
    border-radius: 10%;
    border-style: solid;
    border-color: #67aa69;
    border-width: 5px;
    
}

.section {
    text-align: center;
    margin-top: 50px;
    font-family: 'Andika', sans-serif;
}

.games {
    display: flex;
    flex-direction: row;
    justify-content: space-evenly;
    margin-top: 50px;
    margin-bottom: 50px;
    min-height: 450px;
}

img {
    border-bottom-left-radius: 50px;
    border-bottom-right-radius: 50px;
    width: 100%;
    height: 250px;
}
enyaitl3

enyaitl31#

尝试避免使用位置相对和绝对,除非你真的需要,而不是在您的情况下,只是添加以下到您的文章在css
第一个月

body {
    margin: 0;
    padding-top: 150;
    padding-bottom: 0;
    background-color: rgb(206, 238, 197);
}

article {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    
    background-color: yellow;
    text-align: center;
    width: 30%;
    padding: 2;
    border-radius: 10%;
    border-style: solid;
    border-color: #67aa69;
    border-width: 5px;
    
}

.section {
    text-align: center;
    margin-top: 50px;
    font-family: 'Andika', sans-serif;
}

.games {
    background: red;
    display: flex;
    flex-direction: row;
    justify-content: space-evenly;
    margin-top: 50px;
    margin-bottom: 50px;
    min-height: 450px;
}

img {
    border-bottom-left-radius: 50px;
    border-bottom-right-radius: 50px;
    width: 100%;
    height: 250px;
}
<div class="games">
            <article>
            <div class="upper-section">
                <h2>Hollow Knight</h2>
                <p class="tags">Difficult, Souls-like</p>
                <hr>
            </div>
                <p>"Forge your own path in Hollow Knight! An epic action adventure through a vast ruined kingdom of insects and heroes. Explore twisting caverns, battle tainted creatures and befriend bizarre bugs, all in a classic, hand-drawn 2D style."</p>
                <img src="hollow-knight.png">
            </article>
            <article>
            <div class="upper-section">
                <h2>Celeste</h2>
                <p class="tags">Difficult, Amazing Soundtrack</p>
                <hr>
            </div>
                <p>"Help Madeline survive her inner demons on her journey to the top of Celeste Mountain, in this super-tight platformer from the creators of TowerFall. Brave hundreds of hand-crafted challenges, uncover devious secrets, and piece together the mystery of the mountain."</p>
                <img src="celeste.png">
            </article>
            <article>
            <div class="upper-section">
                <h2>Ori and the Blind Forest/Will of the Wisps</h2>
                <p class="tags">Amazing Soundtrack, Metroidvania</p>
                <hr>
            </div>
                <p>"Ori and the Blind Forest tells the tale of a young orphan destined for heroics, through a visually stunning action-platformer crafted by Moon Studios for PC."</p>
                <img src="ori.png">
            </article>
        </div>

有一个链接你可能会发现有用的https://ishadeed.com/article/less-absolute-positioning-modern-css/
好运

1yjd4xko

1yjd4xko2#

您的方向正确!还可以尝试将left: 0;bottom: 0;添加到绝对定位的<img>

article {
    position: relative;
}

img {
    position: absolute;
    left: 0;
    bottom: 0;
}
q3aa0525

q3aa05253#

尝试在"upper-section" div中添加段落并添加高度:55%用于"上段"分部
超文本:

<article>
        <div class="upper-section">
            <h2>Hollow Knight</h2>
            <p class="tags">Difficult, Souls-like</p>
            <hr>
            <p>"Forge your own path in Hollow Knight! An epic action adventure through a vast ruined kingdom of insects and heroes. Explore twisting caverns, battle tainted creatures and befriend bizarre bugs, all in a classic, hand-drawn 2D style."</p>
        </div>    
        <img src="hollow-knight.png">
    </article>

CSS:

.upper-section {
      height: 55%;
   }

相关问题