如何根据浏览器高度制作“边距:顶部”
How to make `margin: top;` based on browser height?
标题几乎说明了一切。我希望我的 HTML main_content
元素上的 CSS margin: top;
相对于浏览器窗口(百分比)(以便main_content
始终保持在浏览器窗口的底部。我怎样才能做到这一点?
我试过这个,但它不起作用。(body {height:100vh}
似乎没有使body
有任何高度,因为main_content
不会像它应该的那样粘在底部。
body {height:100vh}
#main_content {position:absolute; width:100%; display:block; left:0; bottom:0; text-align:center; padding:20px;}
<div>Extra Infomation </div>
<div id="main_content">
<p>here you can learn about me and my adventures</p>
</div>
(现在不要尝试这个)如果你去我的网站,你会看到"了解我和我的冒险"标题,连同"最近的活动",以及下面的其他内容,这就是我想要在浏览器窗口底部的部分,最好是"了解我和我的冒险"部分只是从页面底部伸出来。
给.main_content一个 100vh 的边距顶部(就在视口下方),然后使用 transformY 将其拉回:
.main_content {
text-align: center;
padding: 20px;
margin-top: 100vh;
transform: translateY(calc(-100% - 20px));
background:lightblue;
}
.below_content{
margin-top:-100px;
}
<div class="wrapper">
<div>Extra Infomation </div>
<div class="main_content">
<p>here you can learn about me and my adventures</p>
</div>
</div>
<div class="below_content">
This is content below the main content
</div>
所以如果
它是一个类,在main_content之前放一个.
,如果它是一个id,则放#
。
下面的 CSS 代码main_content ID 应该可以工作。
#main_content {
width: 100%;
height: 100px;
position: absolute;
bottom: 0;
left: 0;
}
你可以试试在这里 https://jsfiddle.net/xsdr00dn/
相关文章:
- 根据元素和容器大小计算边距
- 查找元素高度,包括边距
- 如何将CSS(特别是填充/边距)应用于select下拉菜单的选项或optgroup
- jquery返回边距为0的绝对中心
- 如何使用jQuery添加另一个对象的高度作为边距
- 如何根据主体高度动态更改元素边距顶部
- jQuery:粘滞的导航栏会更改边距
- 如何使边距不偏移我的网格
- 垂直与特定边距的顶部和底部对齐
- JQuery 使用 var 对边距顶部进行动画处理,不起作用
- 如何根据浏览器高度制作“边距:顶部”
- jQuery设置的边距顶部使内容跳跃.如何使其平滑动画化
- Javascript // 将顶部边距设置为网站高度的负一半
- 使用边距获取正文元素的左偏移量和顶部偏移量:自动
- 由于固定导航,在扇形框的顶部和底部添加边距
- 灯箱顶部边距在向下滚动一次后又向上滚动一次
- jQuery动画边距顶部
- 生成边距顶部大小取决于页面位置-固定的画布
- 为布局中的元素添加内边距'在顶部添加空白空间
- Safari文本跳跃/闪烁时,改变边距顶部