gpt4 book ai didi

css position sticky 不适用于 bootstrap 4 类行

转载 作者:行者123 更新时间:2023-11-28 10:08:03 25 4
gpt4 key购买 nike

我无法在网站上固定菜单。当页面内容有一个带有 Bootstrap 4 ROW 类的 DIV 时,这个 DIV 会覆盖菜单。这是 html 代码:

<html>
<head>
<script type="text/javascript"
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<main class="main-container">
<h4>
Scroll page down to see the HEADER fixed on top
</h4>
<header class="main-header">HEADER</header>
<div class="main-content">
<h4>MAIN CONTENT TITLE</h4>
<div class="row"><!-- if delete this DIV, works fine -->
<div class="col">
left col
</div>
<div class="col">
right col
</div>
</div><!-- row -->
</div><!-- main-content -->
<footer class="main-footer">FOOTER</footer>
</main>
</body>
</html>

这里是 CSS 代码:

.main-container{ max-width:600px; margin:0 auto; border:solid 10px green; padding:10px; margin-top:40px;}
.main-container *{padding:10px;background:#aaa; border:dashed 5px #000;}
.main-container * + *{margin-top:20px;}
.main-header{ height:50px; background:#aaa; border-color:red;}
.main-content{ min-height:1000px;}
.main-header{position:-webkit-sticky; position:sticky; top:0;}

https://jsfiddle.net/WRobynson/qhdfbjmv/10/

拜托,有人可以帮助我吗?

谢谢

最佳答案

在 main-header 中添加 z-index。

.main-container{ max-width:600px; margin:0 auto; border:solid 10px green; padding:10px; margin-top:40px;}
.main-container *{padding:10px;background:#aaa; border:dashed 5px #000;}
.main-container * + *{margin-top:20px;}
.main-header{ height:50px; background:#aaa; border-color:red; z-index:100;}
.main-content{ min-height:1000px;}
.main-header{position:-webkit-sticky; position:sticky; top:0;}
<html>
<head>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<main class="main-container">
<header class="main-header">HEADER</header>
<div class="main-content">
<h4>MAIN CONTENT TITLE</h4>
<div class="row">
<div class="col">
left col
</div>
<div class="col">
right col
</div>
</div><!-- row -->
</div><!-- main-content -->
<footer class="main-footer">FOOTER</footer>
</main>
</body>
</html>

关于css position sticky 不适用于 bootstrap 4 类行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57747889/

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