gpt4 book ai didi

html - CSS 位置固定为与相对父级相同的大小

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:13 25 4
gpt4 key购买 nike

我想要一种粘性部分,当用户滚动浏览页面时,它在顶部有一个固定位置。

如果我将宽度设置为粘性容器的 100%,它会溢出父 div 容器。即使我调整浏览器的大小,宽度也应该完全相同。

你可以在这里看到我的问题:https://jsfiddle.net/d49tyfo2/2/

body {
padding: 50px;
}
#d-header {
height: 400px;
position: relative;
padding-bottom: 55px;
background-color: blue;
margin-bottom: 0px !important;
box-shadow: 2px 5px 3px 0 rgba(0,0,0,0.16);
z-index: 1;
}

.tab-container {
position: absolute;
width: 100%;
bottom: 10px;
height: 55px;
letter-spacing: 1px;
}

.tabs {
background-color: orange;
color: white;
text-transform: uppercase;
width: 26.8%;
height: 50px;
line-height: 50px;
float: left;
text-align: center;
margin-left: 10px;
}

.date {
position: absolute;
font-size: 72px;
text-align: center;
left: 0;
right: 0;
bottom: 65px;
}

.header-sticky {
position: fixed;
top: 83px;
width: 100%;
height: 205px;
background-color: white;
box-shadow: 0 5px 4px 0 rgba(0,0,0,0.16),0 0px 0px 0 rgba(0,0,0,0.12);
border: 1px solid black;
}
<body>
<div class="myHeader" id="d-header">
<div class="special-headline-wrap" style="width: 526px;">
<h1 class="special js-done">HEADLINE</h1>
</div>
<p>Aenean lacinia nulla sed consectetur. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>

<div id="date-tab-wrapper" class="header-sticky">
<div class="date">2017</div>

<div class="tab-container">
<div class="tabs" style="margin-left: 9.1%;">Tab 1</div>
<div class="tabs">Tab 2</div>
<div class="tabs">Tab 3</div>
</div>
</div>
</div>
</body>

我在这里缺少什么?

最佳答案

position: fixed 的元素从文档流中移除,因此不受其父容器的约束。来自 CSS 技巧:

position: fixed - the element is removed from the flow of the document like absolutely positioned elements. In fact they behave almost the same, only fixed positioned elements are always relative to the document, not any particular parent, and are unaffected by scrolling.

关于html - CSS 位置固定为与相对父级相同的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46704835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com