使用固定位置时如何降低 content/element
How to bring content/element down when using position fixed
如何在使用固定在导航栏上的位置时向下移动内容?自从我使用 position:fixed,我的页面就到了顶部。我可以使用 margin-bottom,但我的导航栏并不完全位于屏幕顶部。 (是的,我在 * 中有 margin:0 和 padding:0)。
Link: https://codepen.io/Altinzzz/pen/ZEWawxV
代码
html
<div class="topnav" id="myTopnav">
<a href="websitecar.html"> Home</a>
<a href="aboutus.html" class="active"> About Us</a>
<a href="contactus.html">Contact</a>
<a href="#about">About</a>
</div>
<div class = "introduction">
<span class = "introductionText"> Welcome </span>
<span class = "introductionText2"> About us </span>
</div>
css
*{
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.introduction{
text-align:center;
padding: 0;
}
.introduction span{
display: block;
animation-fill-mode: forwards;
}
.introductionText{
font-size: 80px;
background: white;
color:black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2{
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}
在简介中添加 padding-top
当您使用 position:fixed
时,它会将元素移出流程。因此,页面的其余部分从顶部开始,就好像固定元素根本不存在一样。这意味着对其应用边距或任何其他 CSS 不会影响任何其他元素。
相反,您需要在页面的其余部分为其添加 space,例如在下一个元素的顶部添加填充或边距,例如
.introduction{
text-align:center;
padding: 50px 0 0;
}
为了便于维护,最好为所有页面添加外部容器以添加此 space,尤其是当每个页面中的第一个元素不相同时,例如
.page-container{
padding-top: 50px;
}
而 HTML 类似于:
<div class="topnav" id="myTopnav"> ... </div>
<div class="page-container">
... whatever elements you have...
</div>
响应式设计的注意事项:请注意,如果导航换行,那么固定填充是不够的 - 在这种情况下,您可能需要查看媒体查询或使用汉堡菜单保持导航栏相同的高度来解决这个问题。
工作示例:
* {
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.page-container {
padding-top: 50px;
}
.introduction {
text-align: center;
padding: 0;
}
.introduction span {
display: block;
animation-fill-mode: forwards;
}
.introductionText {
font-size: 80px;
background: white;
color: black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2 {
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}
.contentforscroll {
background: lightblue;
height: 800px;
}
<div class="topnav" id="myTopnav">
<a href="websitecar.html"> Home</a>
<a href="aboutus.html" class="active"> About Us</a>
<a href="contactus.html">Contact</a>
<a href="#about">About</a>
</div>
<div class="page-container">
<div class="introduction">
<span class="introductionText"> Welcome </span>
<span class="introductionText2"> About us </span>
</div>
<div class="contentforscroll">
<p>More content to make the page scroll so you see the fixed element</p>
</div>
</div>
请给 body 顶部填充(大于 topnav 的高度)
并且 topnav 应该保持在顶部,所以应该添加 top: 0
;
请检查以下内容
*{
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
body {
padding-top: 50px;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
top: 0;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: black;
color: white;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
.introduction{
text-align:center;
padding: 0;
}
.introduction span{
display: block;
animation-fill-mode: forwards;
}
.introductionText{
font-size: 80px;
background: white;
color:black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2{
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}
@keyframes text {
0%{
color: black;
margin-bottom: -80px;
letter-spacing: -10px;
}
25%{
margin-bottom: -80px;
letter-spacing: 25px;
}
80%{
letter-spacing: 0px;
}
100%{
letter-spacing: 0;
margin-bottom: -3px;
}
}
<div class="topnav" id="myTopnav">
<a href="websitecar.html"> Home</a>
<a href="aboutus.html" class="active"> About Us</a>
<a href="contactus.html">Contact</a>
<a href="#about">About</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<div class = "introduction">
<span class = "introductionText"> Welcome </span>
<span class = "introductionText2"> About us </span>
</div>
感谢大家的推荐。三个都正确。
添加padding-top:50px; .introduction 有效,添加 padding-top: 50px; 也是如此到正文然后把 top: 0;, inside topnav.
如何在使用固定在导航栏上的位置时向下移动内容?自从我使用 position:fixed,我的页面就到了顶部。我可以使用 margin-bottom,但我的导航栏并不完全位于屏幕顶部。 (是的,我在 * 中有 margin:0 和 padding:0)。
Link: https://codepen.io/Altinzzz/pen/ZEWawxV
代码
html
<div class="topnav" id="myTopnav">
<a href="websitecar.html"> Home</a>
<a href="aboutus.html" class="active"> About Us</a>
<a href="contactus.html">Contact</a>
<a href="#about">About</a>
</div>
<div class = "introduction">
<span class = "introductionText"> Welcome </span>
<span class = "introductionText2"> About us </span>
</div>
css
*{
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.introduction{
text-align:center;
padding: 0;
}
.introduction span{
display: block;
animation-fill-mode: forwards;
}
.introductionText{
font-size: 80px;
background: white;
color:black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2{
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}
在简介中添加 padding-top
当您使用 position:fixed
时,它会将元素移出流程。因此,页面的其余部分从顶部开始,就好像固定元素根本不存在一样。这意味着对其应用边距或任何其他 CSS 不会影响任何其他元素。
相反,您需要在页面的其余部分为其添加 space,例如在下一个元素的顶部添加填充或边距,例如
.introduction{
text-align:center;
padding: 50px 0 0;
}
为了便于维护,最好为所有页面添加外部容器以添加此 space,尤其是当每个页面中的第一个元素不相同时,例如
.page-container{
padding-top: 50px;
}
而 HTML 类似于:
<div class="topnav" id="myTopnav"> ... </div>
<div class="page-container">
... whatever elements you have...
</div>
响应式设计的注意事项:请注意,如果导航换行,那么固定填充是不够的 - 在这种情况下,您可能需要查看媒体查询或使用汉堡菜单保持导航栏相同的高度来解决这个问题。
工作示例:
* {
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.page-container {
padding-top: 50px;
}
.introduction {
text-align: center;
padding: 0;
}
.introduction span {
display: block;
animation-fill-mode: forwards;
}
.introductionText {
font-size: 80px;
background: white;
color: black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2 {
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}
.contentforscroll {
background: lightblue;
height: 800px;
}
<div class="topnav" id="myTopnav">
<a href="websitecar.html"> Home</a>
<a href="aboutus.html" class="active"> About Us</a>
<a href="contactus.html">Contact</a>
<a href="#about">About</a>
</div>
<div class="page-container">
<div class="introduction">
<span class="introductionText"> Welcome </span>
<span class="introductionText2"> About us </span>
</div>
<div class="contentforscroll">
<p>More content to make the page scroll so you see the fixed element</p>
</div>
</div>
请给 body 顶部填充(大于 topnav 的高度)
并且 topnav 应该保持在顶部,所以应该添加 top: 0
;
请检查以下内容
*{
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
body {
padding-top: 50px;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
top: 0;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: black;
color: white;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
.introduction{
text-align:center;
padding: 0;
}
.introduction span{
display: block;
animation-fill-mode: forwards;
}
.introductionText{
font-size: 80px;
background: white;
color:black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2{
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}
@keyframes text {
0%{
color: black;
margin-bottom: -80px;
letter-spacing: -10px;
}
25%{
margin-bottom: -80px;
letter-spacing: 25px;
}
80%{
letter-spacing: 0px;
}
100%{
letter-spacing: 0;
margin-bottom: -3px;
}
}
<div class="topnav" id="myTopnav">
<a href="websitecar.html"> Home</a>
<a href="aboutus.html" class="active"> About Us</a>
<a href="contactus.html">Contact</a>
<a href="#about">About</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<div class = "introduction">
<span class = "introductionText"> Welcome </span>
<span class = "introductionText2"> About us </span>
</div>
感谢大家的推荐。三个都正确。
添加padding-top:50px; .introduction 有效,添加 padding-top: 50px; 也是如此到正文然后把 top: 0;, inside topnav.