如何让子盒子在大盒子中沉底显示?

如何让子盒子在大盒子中沉底

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/game.css">
</head>
<body>
    <!-- 游戏盒子 -->
    <div id="gameBox">
        <!-- 大地盒子 -->
        <div id="land">
        </div>
    </div>
</body>
</html>

【方法1】 绝对定位

#gameBox {
    width: 480px;
    height: 640px;
    border: 1px solid #000;
    background-image: url(../images/background.png);
    /* 子绝父相 */
    position: relative;
}

/* 大地盒子 */
#land {
    width: 480px;
    height: 80px;
    background-color: red;
    /* 一个绝对定位元素的定位参考点是祖先元素中距离自己最近的已经定位的元素 */
    position: absolute;
    bottom: 0;
    left: 0;
}

【方法2】 flex布局

/* 设置父元素弹性盒子 */
display: flex;
/* 轴朝向调成纵轴逆向 */
flex-direction: column-reverse;

#gameBox {
    width: 480px;
    height: 640px;
    border: 1px solid #000;
    background-image: url(../images/background.png);
    /* 弹性盒子 */
    display: flex;
    /* 轴朝向调成纵轴逆向 */
    flex-direction: column-reverse;

}

/* 大地盒子 */
#land {
    width: 480px;
    height: 80px;
    background-color: red;
}
 <!-- 游戏盒子 -->
    <div id="gameBox">
        <!-- 大地盒子 -->
        <div id="land">
        </div>
        <div style="width:100%;height:80px;background: pink;">第二个</div>
        <div style="width:100%;height:80px;background: rgb(191, 54, 77);">第三个</div>
        <div style="width:100%;height:80px;background: rgb(13, 147, 78);">第四个</div>
        <div style="width:100%;height:80px;background: rgb(140, 19, 162);">第五个</div>
        <div style="width:100%;height:80px;background: rgb(122, 142, 85);">第六个</div>
    
    </div>

在这里插入图片描述

【方法3】 margin-top:auto;

父盒子 display:flex;
子盒子 margin-top:auto;

#gameBox {
    width: 480px;
    height: 640px;
    border: 1px solid #000;
    background-image: url(../images/background.png);
    /* 弹性盒子 */
    display: flex;

}

/* 大地盒子 */
#land {
    width: 480px;
    height: 80px;
    background-color: red;
    margin-top: auto;
}

【方法3】 verticle-align:center;

父盒子
display: table-cell;
vertical-align: bottom;

#gameBox {
    width: 480px;
    height: 640px;
    border: 1px solid #000;
    background-image: url(../images/background.png);
    display: table-cell;
    vertical-align: bottom;

}

/* 大地盒子 */
#land {
    width: 480px;
    height: 80px;
    background-color: red;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-footer是Element UI库的一个组件,用于在页面底部添加一个固定的页脚。它的使用方式如下: ```html <template> <div> <el-header>Header</el-header> <el-main>Main Content</el-main> <el-footer>Footer</el-footer> </div> </template> ``` 在上面的例,el-header、el-main和el-footer分别代表页面的头部、主体和底部。el-footer会被固定在页面底部,无论页面内容的高度是多少。 如果你想让el-footer沉底,即当页面内容不足以撑满整个屏幕时,让el-footer始终保持在页面底部,可以使用CSS的flex布局来实现。具体的代码如下: ```html <template> <div class="container"> <div class="content"> <!-- 页面内容 --> </div> <el-footer class="footer">Footer</el-footer> </div> </template> <style> .container { display: flex; flex-direction: column; min-height: 100vh; } .content { flex: 1; } .footer { margin-top: auto; } </style> ``` 上面的代码,我们创建了一个flex容器(class为container),设置了它的`min-height`为`100vh`,确保容器至少占满整个屏幕高度。然后,将页面内容放在一个div(class为content),并设置其`flex`属性为1,让它占满剩余的空间。最后,将el-footer的`margin-top`属性设置为`auto`,使其自动填充剩余的空间,从而将el-footer沉底。 这样,无论页面内容的高度是多少,el-footer都会保持在页面底部。希望对你有帮助!如有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值