css-让div永远在最底部

本文深入探讨了CSS中固定定位的使用方法,详细解释了如何将元素固定在浏览器窗口的特定位置,无论页面如何滚动,该元素的位置保持不变。通过实际案例,展示了固定定位在网页布局中的应用技巧。

 

 

<div class="a"></div>

.a{
position:fixed;
bottom:0;

}

 

转载于:https://www.cnblogs.com/hwaggLee/p/5137201.html

使用CSS将块固定在页面底部有多种方法,以下是几种常见的方式: ### 使用fixed固定定位 通过`fixed`固定定位,可将元素永远固定在页面底部,即使有滚动条也不受影响。示例代码如下: ```html <template> <div class="container"> <header style="background: #f9cc9d">Header</header> <main style="background: #c3d08b">Content</main> <footer style="background: #2b93f5">Footer</footer> </div> </template> <style scoped lang="scss"> .container { height: 100vh; background: #f1f3f4; header, main, footer { height: 80px; line-height: 80px; width: 100%; } footer { height: 60px; line-height: 60px; position: fixed; bottom: 0; left: 0; } } </style> ``` 这种方式中,`position: fixed`将`footer`元素固定在页面底部,`bottom: 0`和`left: 0`确保它位于底部且左对齐 [^1]。 ### 使用绝对定位 使用子绝父相的方式,父元素设置`position: relative`,子元素设置`position: absolute`。示例如下: ```html <template> <div class="container"> <header style="background: #f9cc9d">Header</header> <main style="background: #c3d08b">Content</main> <footer style="background: #2b93f5">Footer</footer> </div> </template> <style scoped lang="scss"> .container { position: relative; height: 100vh; background: #f1f3f4; header, main, footer { height: 80px; line-height: 80px; width: 100%; } footer { height: 60px; line-height: 60px; position: absolute; bottom: 0; left: 0; } } </style> ``` 在这个例子中,`.container`作为父元素设置了`position: relative`,`footer`作为子元素设置`position: absolute`和`bottom: 0`,使其固定在容器底部 [^1]。 ### 使用相对定位和padding-bottom `footer`使用相对定位`bottom: 0`,页面主体`page`容器设置一个大于等于`footer`高度的`padding-bottom`,将`footer`的高度计算在`page`容器中,使`footer`始终固定在页面底部 [^2]。 ### 使用Flexbox布局 通过Flexbox布局,可将元素固定在底部。示例代码如下: ```css body { display: flex; min-height: 100vh; flex-direction: column; } header, footer { line-height: 100px; height: 100px; } footer { margin-top: auto; } ``` 这里,`body`设置为Flex容器,`flex-direction: column`使子元素垂直排列,`margin-top: auto`将`footer`推到页面底部 [^3]。 ### 使用负边距 示例代码如下: ```css html, body, #sticker { height: 100%; } body > #sticker { height: auto; min-height: 100%; } #stickerCon { padding-bottom: 40px; } #footer { margin-top: -40px; height: 40px; width: 100%; text-align: center; line-height: 40px; color: #ABA498; font-size: 12px; background: #fafafa; border-top: 1px solid #E7E7E7; } ``` 在这个方法中,通过设置负边距和`padding-bottom`来实现元素固定在底部 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值