css how to realize fixed element relative to parent element position
css how to realize fixed element relative to parent element position
css fixed 相对父元素定位
absolute bug ❌ / fixed bug ❌
absolute 滚动 bug / fixed 滚动 bug
添加 transform: translate(0, 0); 使 fixed 相对父元素定位
.assemble-creative-toggle-card-box {
box-sizing: border-box;
position: relative;
display: flex;
align-items: flex-start;
justify-content: space-between;
flex-flow: row wrap;
// width: calc(325px + 16px);
width: 325px;
height: 185px;
overflow-y: scroll;
// overflow-y: auto;
background: #F7F7F7;
border: 1px solid #EEEEEE;
padding: 10px;
// fixed 参考
// transform: translateY(0);
// z-index: 1;
// transform: translate(0, 0);
/* hidden scrollbar */
&::-webkit-scrollbar {
display: none;
width: 0 !important;
height: 0 !important;
background: transparent;
}
.creative-toggle-card-header {
// margin-top: 10px;
// position: fixed;
// position: absolute;
position: sticky;
height: 0;
transform: translate(-10px, -10px);
top: 0;
right: 0;
display: flex;
align-items: flex-start;
justify-content: space-between;
width: 100%;
z-index: 2;
}
}
sticky bug ❌
sticky 不滚动时候,占用高度空间 bug ❌
solution ✅ 🚀
.awesome-fixed-solution {
position: sticky;
height: 0;
// ✅ height 设置为 0, 防止 sticky 没有滚动的时候占用空间,保持与滚动的时候一致性!
transform: translate(-10px, -10px);
// ✅调整偏移量,等价于本来 absolute 的 top, right 的负值
}
<style lang="scss">
.assemble-creative-toggle-card-box {
box-sizing: border-box;
position: relative;
display: flex;
align-items: flex-start;
justify-content: space-between;
flex-flow: row wrap;
// width: calc(325px + 16px);
width: 325px;
height: 185px;
overflow-y: scroll;
// overflow-y: auto;
background: #F7F7F7;
border: 1px solid #EEEEEE;
padding: 10px;
// fixed 参考
// transform: translateY(0);
// z-index: 1;
// transform: translate(0, 0);
/* hidden scrollbar */
&::-webkit-scrollbar {
display: none;
width: 0 !important;
height: 0 !important;
background: transparent;
}
.creative-toggle-card-header {
// margin-top: 10px;
// position: fixed;
// position: absolute;
position: sticky;
height: 0;
transform: translate(-10px, -10px);
top: 0;
right: 0;
display: flex;
align-items: flex-start;
justify-content: space-between;
width: 100%;
z-index: 2;
.toggle-switch {
margin-top: 2px;
margin-left: 4px;
width: 14px;
height: 14px;
border-radius: 2px;
}
.tags-box {
display: flex;
align-items: flex-start;
justify-content: flex-end;
margin-top: 4px;
margin-right: 4px;
.recommended-box {
height: 20px;
line-height: 20px;
// font-size: 12px;
color: #FFFFFF;
margin-right: 4px;
.recommended-tag {
padding: 2px 6px;
display: inline-block;
height: 17px;
line-height: 17px;
background: #E6A23C;
border-radius: 2px;
text-align: center;
font-size: 12px;
}
.unrecommended-tag {
padding: 2px 6px;
display: inline-block;
height: 17px;
line-height: 17px;
background: #999999;
border-radius: 2px;
text-align: center;
font-size: 12px;
}
}
.score-box {
margin-right: -20px;
// ✅ 右侧 item 对齐偏移量
// box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
height: 20px;
line-height: 20px;
background: #FCF5EB;
border-radius: 2px;
border: 1px solid #F7E3C4;
.score-text {
display: inline-block;
height: 17px;
line-height: 17px;
font-size: 12px;
color: #E6A23C;
// width: 24px;
text-align: center;
padding: 2px 4px;
font-size: 12px;
}
.separate-line {
box-sizing: border-box;
display: inline-block;
width: 1px;
height: 10px;
background: #F7E3C4;
border-radius: 1px;
padding: 0;
margin: 0;
}
}
}
}
.creative-toggle-card-body {
box-sizing: border-box;
// width: 150px;
width: 147px;
height: 80px;
background: rgba(0, 0, 0, 0.5);
border-radius: 4px;
// height: 100%;
max-height: 80px;
overflow-y: hidden;
margin-bottom: 5px;
&:nth-of-type(2n) {
margin-right: 5px;
}
&:nth-last-of-type(1) {
margin-bottom: 0;
}
&:nth-last-of-type(2) {
margin-bottom: 0;
}
.img-cover-url {
box-sizing: border-box;
width: 100%;
// height: auto;
height: 100%;
}
.assemble-video {
position: relative;
.tt-video-player {
position: absolute;
left: 50%;
top: 50%;
box-sizing: border-box;
width: 32px;
height: 32px;
transform: translateX(-50%) translateY(-50%);
}
}
}
.creative-toggle-card-fotter {
//
}
}
</style>
MDN position
refs
https://developer.mozilla.org/en-US/docs/Web/CSS/position
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14971511.html
未经授权禁止转载,违者必究!