页脚不显示或粘在底部 - 隐藏在导航后面,z-index 不工作
footer not showing or sticking to bottom - hidden behind nav, z-index not working
我有页脚,但没有显示。这是一个example。所以我想要一个粘性页脚(页脚不是 'fixed',而是留在底部),到目前为止它被导航隐藏(我认为)。我已经尝试将 z-index
更改为高于导航,但它仍然隐藏。代码:
HTML
<!-- Navigation -->
<div class="nav">
<div class="nContainer">
<ul class="pull-left">
<li class="active"><a>home</a></li>
<li><a href="#">about us</a></li>
<li><a href="#">contact us</a></li>
</ul>
<ul class="pull-right">
<li><a href="#">sign up</a></li>
<li><a href="#">log in</a></li>
</ul>
</div>
</div>
<!-- Collapse -->
<div class="collapse">
<h1 style="position: relative; top: 50%; transform: translateY(-50%);">Lorem ipsum dolor sit amet.</h1>
</div>
<!-- Content -->
<div class="content">
<div class="container">
<h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor...</p>
</div>
</div>
<div class="footer">
<p class="copyright">© 2015 Lorem Ipsum</p>
</div>
CSS
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
html {
position: relative;
min-height: 100%;
}
.nav {
margin-bottom: 20px;
margin-left: 0;
background-color: #fff;
height: 60px;
width: 100%;
position: fixed;
box-shadow: 0px 5px 5px #696969;
z-index: 2;
}
.nav > li > a {
display: block;
}
.pull-right {
float: right;
}
.pull-left {
float: left;
}
.nav a {
font-family: 'ABeeZee', sans-serif;
color: #b3b3ff;
padding: 14px 10px;
-webkit-transition: color 0.2s ease-in-out;
-moz-transition: color 0.2s ease-in-out;
-o-transition: color 0.2s ease-in-out;
transition: color 0.2s ease-in-out;
}
.nav a:hover {
color: #5a5a5a;
}
.nav li {
display: inline;
}
.nav .active a {
color: #5a5a5a;
cursor: default;
}
.nContainer {
margin-top: 20px;
}
.collapse {
background-image: url('http://placehold.it/1280x720');
background-size: cover;
background-repeat: no-repeat;
height: 600px;
position: fixed;
left: 0;
right: 0;
z-index: -1
text-align: center;
}
.collapse h1 {
color: #fff;
font-family: 'Shift', sans-serif;
font-size: 72px;
}
.content {
background-color: #fff;
position: relative;
top: 600px;
z-index: 1;
}
.content h1 {
color: #393c3d;
font-size: 24px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.container {
width: 90%;
}
.container {
margin-right: auto;
margin-left: auto;
*zoom: 1;
}
.container:before,
.container:after {
display: table;
line-height: 0;
content: "";
}
.container:after {
clear: both;
}
.container h1 {
margin-top: 4%;
font-family: 'Oswald', sans-serif;
font-weight: 700;
}
.container p {
font-family: 'Lato', sans-serif;
}
.footer {
height: 50px;
width: 100%;
bottom: 0;
z-index: 10;
}
看到这个fiddle
为了使 z-index
正常工作,您应该首先使用 css 定位来放置 div。
所以,试试这个 CSS 作为您的页脚
.footer {
height: 50px;
width: 100%;
bottom: 0;
position: fixed;
z-index: 10;
}
用以下
替换您的CSS代码
.footer {
height: 50px;
width: 100%;
bottom: 0;
position: fixed.
}
:fixed
不需要 z-index
勾选这个Fiddle
.footer {
height: 20px;
width: 100%;
bottom: 0;
position: fixed;
z-index: 10;
background-color: #fff;
padding: 20px 0;
}
你的描述不是很清楚-你想让它留在底部,但不是固定的?
我假设您希望它出现在 内容 的底部(即不固定在浏览器的底部)——在这种情况下,您只需必须将页脚元素移动到内容元素中。
<!-- Content -->
<div class="content">
<div class="container">
<h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor sit amet, cons...</p>
</div>
<div class="footer">
<p class="copyright">© 2015 Lorem Ipsum</p>
</div>
</div>
试试这个
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
html {
min-height: 100%;
}
.nav {
margin-bottom: 20px;
margin-left: 0;
background-color: #fff;
height: 60px;
width: 100%;
position: fixed;
box-shadow: 0px 5px 5px #696969;
z-index: 1;
}
.nav > li > a {
display: block;
}
.pull-right {
float: right;
}
.pull-left {
float: left;
}
.nav a {
font-family: 'ABeeZee', sans-serif;
color: #b3b3ff;
padding: 14px 10px;
-webkit-transition: color 0.2s ease-in-out;
-moz-transition: color 0.2s ease-in-out;
-o-transition: color 0.2s ease-in-out;
transition: color 0.2s ease-in-out;
}
.nav a:hover {
color: #5a5a5a;
}
.nav li {
display: inline;
}
.nav .active a {
color: #5a5a5a;
cursor: default;
}
.nContainer {
margin-top: 20px;
}
.collapse {
background-image: url('http://placehold.it/1280x720');
background-size: cover;
background-repeat: no-repeat;
height: 600px;
position: center bottom;
left: 0;
right: 0;
text-align: center;
}
.collapse h1 {
color: #fff;
font-family: 'Shift', sans-serif;
font-size: 72px;
}
.content {
background-color: #fff;
position: relative;
}
.content h1 {
color: #393c3d;
font-size: 24px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.container {
width: 90%;
margin-right: auto;
margin-left: auto;
}
.container:before,
.container:after {
display: table;
line-height: 0;
content: "";
}
.container:after {
clear: both;
}
.container h1 {
margin-top: 4%;
font-family: 'Oswald', sans-serif;
font-weight: 700;
}
.container p {
font-family: 'Lato', sans-serif;
}
.footer{
position: relative;
padding-top: 20px;
height: 50px;
z-index: 2;
bottom: 0;
}
<!-- Navigation -->
<div class="nav">
<div class="nContainer">
<ul class="pull-left">
<li class="active"><a>home</a></li>
<li><a href="#">about us</a></li>
<li><a href="#">contact us</a></li>
</ul>
<ul class="pull-right">
<li><a href="#">sign up</a></li>
<li><a href="#">log in</a></li>
</ul>
</div>
</div>
<!-- Collapse -->
<div class="collapse">
<h1 style="position: relative; top: 50%; transform: translateY(-50%);">Lorem ipsum dolor sit amet.</h1>
</div>
<!-- Content -->
<div class="content">
<div class="container">
<h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor...</p>
</div>
</div>
<div class="footer">
<p class="copyright">© 2015 Lorem Ipsum</p>
</div>
我有页脚,但没有显示。这是一个example。所以我想要一个粘性页脚(页脚不是 'fixed',而是留在底部),到目前为止它被导航隐藏(我认为)。我已经尝试将 z-index
更改为高于导航,但它仍然隐藏。代码:
HTML
<!-- Navigation -->
<div class="nav">
<div class="nContainer">
<ul class="pull-left">
<li class="active"><a>home</a></li>
<li><a href="#">about us</a></li>
<li><a href="#">contact us</a></li>
</ul>
<ul class="pull-right">
<li><a href="#">sign up</a></li>
<li><a href="#">log in</a></li>
</ul>
</div>
</div>
<!-- Collapse -->
<div class="collapse">
<h1 style="position: relative; top: 50%; transform: translateY(-50%);">Lorem ipsum dolor sit amet.</h1>
</div>
<!-- Content -->
<div class="content">
<div class="container">
<h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor...</p>
</div>
</div>
<div class="footer">
<p class="copyright">© 2015 Lorem Ipsum</p>
</div>
CSS
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
html {
position: relative;
min-height: 100%;
}
.nav {
margin-bottom: 20px;
margin-left: 0;
background-color: #fff;
height: 60px;
width: 100%;
position: fixed;
box-shadow: 0px 5px 5px #696969;
z-index: 2;
}
.nav > li > a {
display: block;
}
.pull-right {
float: right;
}
.pull-left {
float: left;
}
.nav a {
font-family: 'ABeeZee', sans-serif;
color: #b3b3ff;
padding: 14px 10px;
-webkit-transition: color 0.2s ease-in-out;
-moz-transition: color 0.2s ease-in-out;
-o-transition: color 0.2s ease-in-out;
transition: color 0.2s ease-in-out;
}
.nav a:hover {
color: #5a5a5a;
}
.nav li {
display: inline;
}
.nav .active a {
color: #5a5a5a;
cursor: default;
}
.nContainer {
margin-top: 20px;
}
.collapse {
background-image: url('http://placehold.it/1280x720');
background-size: cover;
background-repeat: no-repeat;
height: 600px;
position: fixed;
left: 0;
right: 0;
z-index: -1
text-align: center;
}
.collapse h1 {
color: #fff;
font-family: 'Shift', sans-serif;
font-size: 72px;
}
.content {
background-color: #fff;
position: relative;
top: 600px;
z-index: 1;
}
.content h1 {
color: #393c3d;
font-size: 24px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.container {
width: 90%;
}
.container {
margin-right: auto;
margin-left: auto;
*zoom: 1;
}
.container:before,
.container:after {
display: table;
line-height: 0;
content: "";
}
.container:after {
clear: both;
}
.container h1 {
margin-top: 4%;
font-family: 'Oswald', sans-serif;
font-weight: 700;
}
.container p {
font-family: 'Lato', sans-serif;
}
.footer {
height: 50px;
width: 100%;
bottom: 0;
z-index: 10;
}
看到这个fiddle
为了使 z-index
正常工作,您应该首先使用 css 定位来放置 div。
所以,试试这个 CSS 作为您的页脚
.footer {
height: 50px;
width: 100%;
bottom: 0;
position: fixed;
z-index: 10;
}
用以下
替换您的CSS代码.footer {
height: 50px;
width: 100%;
bottom: 0;
position: fixed.
}
:fixed
勾选这个Fiddle
.footer {
height: 20px;
width: 100%;
bottom: 0;
position: fixed;
z-index: 10;
background-color: #fff;
padding: 20px 0;
}
你的描述不是很清楚-你想让它留在底部,但不是固定的?
我假设您希望它出现在 内容 的底部(即不固定在浏览器的底部)——在这种情况下,您只需必须将页脚元素移动到内容元素中。
<!-- Content -->
<div class="content">
<div class="container">
<h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor sit amet, cons...</p>
</div>
<div class="footer">
<p class="copyright">© 2015 Lorem Ipsum</p>
</div>
</div>
试试这个
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
html {
min-height: 100%;
}
.nav {
margin-bottom: 20px;
margin-left: 0;
background-color: #fff;
height: 60px;
width: 100%;
position: fixed;
box-shadow: 0px 5px 5px #696969;
z-index: 1;
}
.nav > li > a {
display: block;
}
.pull-right {
float: right;
}
.pull-left {
float: left;
}
.nav a {
font-family: 'ABeeZee', sans-serif;
color: #b3b3ff;
padding: 14px 10px;
-webkit-transition: color 0.2s ease-in-out;
-moz-transition: color 0.2s ease-in-out;
-o-transition: color 0.2s ease-in-out;
transition: color 0.2s ease-in-out;
}
.nav a:hover {
color: #5a5a5a;
}
.nav li {
display: inline;
}
.nav .active a {
color: #5a5a5a;
cursor: default;
}
.nContainer {
margin-top: 20px;
}
.collapse {
background-image: url('http://placehold.it/1280x720');
background-size: cover;
background-repeat: no-repeat;
height: 600px;
position: center bottom;
left: 0;
right: 0;
text-align: center;
}
.collapse h1 {
color: #fff;
font-family: 'Shift', sans-serif;
font-size: 72px;
}
.content {
background-color: #fff;
position: relative;
}
.content h1 {
color: #393c3d;
font-size: 24px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.container {
width: 90%;
margin-right: auto;
margin-left: auto;
}
.container:before,
.container:after {
display: table;
line-height: 0;
content: "";
}
.container:after {
clear: both;
}
.container h1 {
margin-top: 4%;
font-family: 'Oswald', sans-serif;
font-weight: 700;
}
.container p {
font-family: 'Lato', sans-serif;
}
.footer{
position: relative;
padding-top: 20px;
height: 50px;
z-index: 2;
bottom: 0;
}
<!-- Navigation -->
<div class="nav">
<div class="nContainer">
<ul class="pull-left">
<li class="active"><a>home</a></li>
<li><a href="#">about us</a></li>
<li><a href="#">contact us</a></li>
</ul>
<ul class="pull-right">
<li><a href="#">sign up</a></li>
<li><a href="#">log in</a></li>
</ul>
</div>
</div>
<!-- Collapse -->
<div class="collapse">
<h1 style="position: relative; top: 50%; transform: translateY(-50%);">Lorem ipsum dolor sit amet.</h1>
</div>
<!-- Content -->
<div class="content">
<div class="container">
<h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor...</p>
</div>
</div>
<div class="footer">
<p class="copyright">© 2015 Lorem Ipsum</p>
</div>