gpt4 book ai didi

html - css 位置不适用于粘性

转载 作者:行者123 更新时间:2023-11-28 15:10:24 26 4
gpt4 key购买 nike

我在css文件中使用了overflow: hidden。
我是 CSS 和 Web 开发的新手。
所以我想知道如何在那个导航栏上使用粘性。
当我删除溢出属性时,背景发生变化。
以下是我的代码。
如果你能给我解决那个问题的代码,那将是一个很大的帮助。

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
legend,
label,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
}

article,
aside,
details,
figcaption,
footer,
figure,
header,
hgroup,
menu,
nav,
section {
display: block;
}

.header {
margin-top: 20px;
height: 40px;
width: 100%
}

.left_header a {
margin: 0 0 0 20px;
text-decoration: none;
color: green;
float: left;
}

.right_header {
margin: 5px 0 0 0;
float: right;
padding: 12px;
}

.navbar {
height: 30px;
}

ul {
list-style-type: none;
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
margin: 0;
padding: 0;
overflow: hidden;
position: sticky;
top: 0;
}

li {
float: left;
}

li a {
display: block;
color: black;
text-align: center;
padding: 15px 20px;
text-decoration: none;
}

li a:hover {
background-color: green;
color: white;
}

.footer {
height: 200px;
}
<!DOCTYPE html>
<html lang="en-US">

<head>
<title>The Only One Stop for Laptops &amp; their Accessories!</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="favicon.ico" />
<link rel="stylesheet" type="text/css" href="styles/style.css" />
<style>
.content {
height: 700px;
}
</style>
</head>

<body style="font-family: verdana;">

<header class="header">
<div class="left_header">
<h1><b><i><a href="index.html">TechTops</a></i></b></h1>
</div>
<div class="right_header">
The Only One Stop for Laptops &amp; their Accessories!
</div>
</header>

<br>

<nav class="navbar">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="#">Laptops</a></li>
<li><a href="#">Accessories</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</nav>

<div class="content">

</div>

<hr>

<footer class="footer">

</footer>

<hr>

</body>

</html>

最佳答案

请试试这个

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,
code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,
fieldset,form,legend,label,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,
figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{
margin:0;
padding:0;
border:0;
vertical-align: baseline;
}
article,aside,details,figcaption,footer,figure,header,hgroup,menu,nav,section{
display: block;
}

.header {
margin-top: 20px;
height: 40px;
width: 100%
}
.left_header a {
margin: 0 0 0 20px;
text-decoration: none;
color: green;
float: left;
}
.right_header {
margin: 5px 0 0 0;
float: right;
padding: 12px;
}
.navbar {
position: -webkit-sticky;
position: sticky;
top: 0;
}
ul {
list-style-type: none;
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
margin : 0;
padding: 0;
}
ul:after{ content: ""; height: 0px; overflow: hidden; clear: both; display: block; }
li {
float: left;
}
li a {
display: block;
color: black;
text-align: center;
padding: 15px 20px;
text-decoration: none;
}
li a:hover {
background-color: green;
color: white;
}
.footer {
height: 200px;
}

关于html - css 位置不适用于粘性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51696019/

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