我目前正在为学校制作一个网站,在该网站上我有一个带有链接的侧边栏。我希望它在侧面的任何地方都可见,所以我使用了position:fixed。但现在它不再保留在其父级的空间内,而是从页面的最顶部开始。
h1 {
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 250%;
color: #000000;
position: relative;
z-index: 10;
}
h2 {
text-align: left;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 180%;
color: #591500;
position: relative;
z-index: 10;
}
body {
background-size: 600% 100%;
height: 100%;
width: 1200px;
position: relative;
z-index: 3;
}
article {
width: 900px;
float: left;
}
nav {
background-color: black;
width: 1200px;
height: 110px;
position: relative;
z-index: 10;
}
aside {
width: 300px;
float: left;
text-align: center;
overflow-y: hidden;
height: 3000px;
position: relative;
z-index: 4;
}
footer {
width: 1200px;
float: left;
height: 25px;
color: white;
}
header.intro {
font-size: 100%;
color: black;
position: relative;
z-index: 10;
}
header#intro {
background-image: url("../images/banner4.jpg");
background-size: cover;
font-size: 350%;
color: white;
width: 1200px;
height: 300px;
position: relative;
z-index: 10;
}
header#intro h1 {
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 225%;
color: black;
position: relative;
z-index: 10;
}
li {
display: inline-block;
}
li a {
display: inline-block;
background-color: #FFDA45;
border: black solid 1px;
color: white;
text-decoration: none;
padding: 20px;
width: 235px;
margin-top: 15px;
margin-bottom: 15px;
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 200%;
}
li a:hover {
border: red solid 5px;
background-color: #E8B53F;
}
div#wrapper {
max-width: 1200px;
margin-left: auto;
margin-right: auto;
}
.middle {
text-align: center;
float: left;
border-style: solid;
border-width: 10px;
border-color: #000033;
width: 110px;
padding: 1em;
margin-left: 2em;
margin-right: auto;
position: fixed;
top: 0px;
overflow-y: hidden;
position: fixed;
z-index: 2;
margin-top: 2em;
}
.btn {
display: block;
width: 90px;
height: 90px;
background: #f1f1f1;
margin: 10px;
border-radius: 30%;
box-shadow: 0 5px 15px -5px #00000070;
color: #3498db;
overflow: hidden;
position: relative;
}
.btn i {
line-height: 90px;
font-size: 26px;
transition: 0.2s linear;
}
.btn:hover i {
transform: scale(1.3);
color: #f1f1f1;
}
.btn::before {
content: "";
position: absolute;
width: 120%;
height: 120%;
background: #3498db;
transform: rotate(45deg);
left: -110%;
top: 90%;
}
.btn:hover::before {
animation: aaa 1s 1;
top: -10%;
left: -10%;
}
@keyframes aaa {
0% {
left: -110%;
top: 90%;
}
50% {
left: 10%;
top: -30%;
}
100% {
top: -10%;
left: -10%;
}
}
/* dit stijlblad is gemaakt in 2019 */
<link href="styles/eersteStijl.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<body>
<header id="intro">
<h1>The Last of Us information page</h1>
</header>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="TLOU part II.html">TLOU part II</a></li>
<li><a href="Video's.html">Video's</a></li>
<li><a href="Music.html">Music</a></li>
</ul>
</nav>
<article>
article
</article>
<aside>
<section class="middle">
<a class="btn" href="https://nl-nl.facebook.com/TLOUPS/" target="_blank">
<i class="fab fa-facebook-f"></i>
</a>
<a class="btn" href="https://twitter.com/naughty_dog" target="_blank">
<i class="fab fa-twitter"></i>
</a>
<a class="btn" href="https://en.wikipedia.org/wiki/The_Last_of_Us" target="_blank">
<i class="fab fa-wikipedia-w"></i>
</a>
<a class="btn" href="https://www.instagram.com/_the.last.of.us/" target="_blank">
<i class="fab fa-instagram"></i>
</a>
<a class="btn" href="https://www.youtube.com/watch?v=OQpdSVF_k_w" target="_blank">
<i class="fab fa-youtube"></i>
</a>
</section>
</aside>
<footer>copyright</footer>
可以看出,侧边栏超出了页面顶部,有没有办法让它不这样做并粘在页面内?我希望侧边栏完全相同,除非它位于顶部,然后我希望将其隐藏。我尝试了隐藏溢出,但似乎不起作用。
我似乎也看不到侧边栏,除非我处于全屏状态,这不是问题,但可能很高兴知道是否有人检查了代码。
我知道您只是在学习,但为了我的理智,我将显式大小调整规则更改为 flex,并删除了一些其他规则只是为了可读性,但不应影响固定对象的最终目标;如果你愿意,你仍然可以使用你的方式。
解决此问题的一种方法是使用sticky
的值position https://developer.mozilla.org/en-US/docs/Web/CSS/position财产。您必须针对较小的屏幕进行一些响应式调整,但我没有包括它,因为我不确定您想要什么。
Sticky:元素按照文档的正常流程定位,然后相对于其最近的滚动祖先和包含块(最近的块级祖先)进行偏移,包括表格相关的元素,基于top、right、底部和左侧。偏移量不会影响任何其他元素的位置。
h1 {
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 250%;
color: #000000;
}
h2 {
text-align: left;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 180%;
color: #591500;
}
body {
height: 100%;
}
article {
background-color: red;
flex: 1 1 100%;
}
nav {
background-color: black;
}
main {
display: flex;
flex: 1;
}
footer {
height: 25px;
color: white;
background-color: black;
}
header.intro {
font-size: 100%;
color: black;
}
header#intro {
background-image: url("../images/banner4.jpg");
background-size: cover;
font-size: 350%;
color: white;
}
header#intro h1 {
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 225%;
color: black;
}
ul {
margin: 0;
}
li {
display: inline;
}
li a {
background-color: #FFDA45;
border: black solid 1px;
color: white;
text-decoration: none;
margin-top: 15px;
margin-bottom: 15px;
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
}
li a:hover {
border: red solid 5px;
background-color: #E8B53F;
}
.middle {
height: 500px;
top: 0;
position: sticky;
}
.btn {
display: block;
width: 90px;
height: 90px;
background: #f1f1f1;
margin: 10px;
border-radius: 30%;
box-shadow: 0 5px 15px -5px #00000070;
color: #3498db;
overflow: hidden;
position: relative;
}
.btn i {
line-height: 90px;
font-size: 26px;
transition: 0.2s linear;
}
.btn:hover i {
transform: scale(1.3);
color: #f1f1f1;
}
.btn::before {
content: "";
position: absolute;
width: 120%;
height: 120%;
background: #3498db;
transform: rotate(45deg);
left: -110%;
top: 90%;
}
.btn:hover::before {
animation: aaa 1s 1;
top: -10%;
left: -10%;
}
@keyframes aaa {
0% {
left: -110%;
top: 90%;
}
50% {
left: 10%;
top: -30%;
}
100% {
top: -10%;
left: -10%;
}
}
<link href="styles/eersteStijl.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<header id="intro">
<h1>The Last of Us information page</h1>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="TLOU part II.html">TLOU part II</a></li>
<li><a href="Video's.html">Video's</a></li>
<li><a href="Music.html">Music</a></li>
</ul>
</nav>
</header>
<main>
<article>
article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long
text
<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long
long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with
long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article
with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long
text
<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long
long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with
long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article
with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long
text
<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long
long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with
long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article
with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long
text
<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long
long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with
long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article
with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long
text
<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long
long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with
long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article with long long text<br> article
with long long text<br> article with long long text<br>
</article>
<aside class="middle">
<a class="btn" href="https://nl-nl.facebook.com/TLOUPS/" target="_blank">
<i class="fab fa-facebook-f"></i>
</a>
<a class="btn" href="https://twitter.com/naughty_dog" target="_blank">
<i class="fab fa-twitter"></i>
</a>
<a class="btn" href="https://en.wikipedia.org/wiki/The_Last_of_Us" target="_blank">
<i class="fab fa-wikipedia-w"></i>
</a>
<a class="btn" href="https://www.instagram.com/_the.last.of.us/" target="_blank">
<i class="fab fa-instagram"></i>
</a>
<a class="btn" href="https://www.youtube.com/watch?v=OQpdSVF_k_w" target="_blank">
<i class="fab fa-youtube"></i>
</a>
</aside>
</main>
<footer>copyright</footer>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)