如何将div元素保留在页面底部

时间:2019-05-29 12:25:29

标签: html css

我需要在页面底部具有一个两列的div元素,以用作页脚。问题是我尝试使用position:fixedbottom:0使元素显示在底部。此方法有效,但仅将#column div中的一个降到底部,另一个消失。我将不胜感激。

代码:

/* ~ Copyright (c) Macleay Netball Association (made by Tom Carpenter for Year 11 IPT Assessment Task #2.) 2019. */

html>body {
  background: none;
  padding: 0;
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: 'Trebuchet MS', sans-serif;
  /* Trebuchet MS as main font throughout website, sans-serif acting as a backup */
}

#wrapper {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  position: absolute;
  background: #1B315E;
}

::-webkit-scrollbar {
  display: none;
}

* {
  box-sizing: border-box;
  /* Alignment of DropDown Navigation Menu and Slideshow */
}

.navigation {
  background: #444;
  overflow: hidden;
  top: 0;
}

.menuDropDown {
  overflow: hidden;
  float: left;
}

.menuDropDown>#menuButton {
  font-size: 12px;
  padding: 16px 16px;
  /* Will result in final height of navigation */
  margin: 0;
  outline: none;
  border: none;
  font: inherit;
  background: inherit;
  color: #FFF;
}

#menuButton:hover,
.navigation>.menuDropDown:hover>#menuButton {
  border: none;
  outline: none;
  background: #666;
  color: #1B315E;
}

.menuDropDown:hover>.menuContent {
  display: block;
}

.menuContent {
  display: none;
  position: absolute;
  background: none;
  width: 100%;
  left: 0;
  max-height: 85vh;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.menuRow:after {
  display: table;
  clear: both;
  content: "";
}

.menuColumn {
  width: 25%;
  height: 100px;
  overflow-y: auto;
  padding: 5px;
  float: left;
  background: #666;
}

.menuColumn>a {
  float: none;
  display: block;
  text-align: left;
  text-decoration: none;
  font-size: 14px;
  padding: 8px;
  color: #1B315E;
}

.menuColumn>a.current {
  background: #777;
}

.menuColumn>a:hover {
  background: #888;
}

.logo {
  float: right;
  padding-right: 10px;
  width: 60px;
  height: 50px;
}

.main {
  width: 100%;
  height: calc(100vh - 43px);
  float: left;
  background: none;
  position: absolute;
  overflow: auto;
  padding: 8px;
}

.mainHeader {
  color: #FFF;
  text-align: center;
}

.mainImg {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

.mainBody {
  color: #FFF;
  padding: 8px;
}

.row:after {
  content: "";
  clear: both;
  display: table;
}

.column {
  float: left;
  width: 33.33%;
  padding: 8px;
}

#column {
  float: left;
  width: 50%;
  padding: 8px;
  background: #888;
  bottom: 0;
}

.sponsers {
  float: left;
  width: 20%;
  padding: 8px;
  -webkit-filter: grayscale(100%);
  filter: grayscale(100%);
}

.sponsers:hover {
  -webkit-filter: grayscale(80%);
  filter: grayscale(80%);
}

.slideshow {
  /* Slideshow Container containing Images, Sliders and Captions */
  position: relative;
  margin: auto;
  max-width: 1000px;
}

.slides {
  display: none;
}

.slideshowImages {
  width: 100%;
}

.slideButton {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  background: #888;
  color: #FFF;
  border: none;
  cursor: pointer;
  padding: 16px 32px;
  font-size: 18px;
  text-align: center;
  border-radius: 10px;
  font-weight: bold;
}

.mnaHeader {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}

.slideButton:hover {
  background: #666;
}

.previous,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: #FFF;
  font-weight: bold;
  font-size: 18px;
  transition: 0.3s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

.previous:hover,
.next:hover {
  background: rgba(0, 0, 0, 0.8);
}

table {
  width: 100%;
  font-size: 16px;
  color: #FFF;
}

.tableCaption {
  font-weight: bold;
  font-size: 18px;
  color: #33CC33;
}

table,
th,
td {
  border-collapse: collapse;
  border: 2px solid #888;
}

th,
td {
  padding: 8px;
  text-align: left;
}

#headerTable {
  width: 33.33%;
  background: #888;
  font-size: 18px;
  color: #33CC33;
}

.linkProp {
  color: #FFF;
  text-decoration: underline;
}

.newsPanel {
  width: 100%;
  background: #888;
  height: 135px;
}

.imgNewsPreview {
  width: 215px;
  height: 135px;
  padding: 4px;
  float: left;
}

.newsHeaderPreview {
  color: #FFF;
  margin-top: 0px;
}

.newsBodyPreview {
  color: #FFF;
  margin-top: -12px;
}

.caption {
  color: #33CC33;
  text-align: center;
  position: absolute;
  font-size: 20px;
  width: 100%;
  padding: 8px 12px;
  bottom: 16px;
}

.collapsible {
  background: #888;
  color: #FFF;
  font-weight: bold;
  width: 100%;
  padding: 18px;
  cursor: pointer;
  font-size: 16px;
  outline: none;
  border: none;
  text-align: left;
}

.active,
.collapsible:hover {
  background: #666;
}

.collapsible:after {
  content: '\002B';
  color: #FFF;
  font-weight: bold;
  float: right;
  margin-left: 4px;
}

.active:after {
  content: '\2212';
}

.contentCollapsible {
  padding: 0px 18px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
  background: #F8F8F8;
}

.fade {
  -webkit-animation-name: fade;
  /* To work on Safari (Apple) */
  -webkit-animation-duration: 1s;
  /* To work on Safari (Apple) */
  animation-name: fade;
  animation-duration: 1s;
}

.buttonReturn {
  position: absolute;
  margin-left: 20px;
  margin-top: 20px;
  background: #888;
  color: #FFF;
  border: none;
  cursor: pointer;
  font-size: 36px;
  text-align: center;
  border-radius: 50%;
  font-weight: bold;
}

.videoProp {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  height: 400px;
}

@-webkit-keyframes fade {
  from {
    opacity: 0.4
  }
  to {
    opacity: 1
  }
}

@keyframes fade {
  from {
    opacity: 0.4
  }
  to {
    opacity: 1
  }
}

@media screen and (max-width: 600px) {
  /* Stacks navigation menu acting for the site to be responsive */
  .menuColumn {
    width: 100%;
    height: auto;
  }
}

@media screen and (max-width: 800px) {
  /* Stacks Image Header on Top - acting as responsive */
  .column,
  #column {
    width: 100%;
  }
}

@media screen and (max-width: 600px) {
  /* Responsive Sponser Images */
  .sponsers {
    width: 33.33%;
  }
}

@media screen and (max-width: 800px) {
  /* Increases video resolution as screen reduces */
  .videoProp {
    width: 100%;
  }
}

@media screen and (max-width: 600px) {
  /* Reduces font-size as screen resolution lowers */
  .newsBodyPreview {
    font-size: 14px;
  }
}

@media screen and (max-width: 300px) {
  /* Reduces font-sizes within the slideshow <div> */
  .previous,
  .next,
  .caption {
    font-size: 14px;
  }
}
<!DOCTYPE HTML>
<!--
	~ Copyright (c) Macleay Netball Association (made by Tom Carpenter for Year 11 IPT Assessment Task #2.) 2019.
-->
<html lang="en-AU">

<head>
  <title>Contact Us &#8212; Macleay Netball Association</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0">
  <link rel="stylesheet" href="../style.css" type="text/css">
  <!-- Internal Stylesheet -->
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
  <!-- External Font Stylesheet -->
  <link rel="shortcut icon" href="../Assets/Img/mnaHeader.png">
  <!-- Tab Browser Icon (Favicon) -->
</head>

<body>
  <div id="wrapper">
    <div class="navigation">
      <div class="menuDropDown">
        <button id="menuButton" title="Navigation Menu"><i class="fas fa-bars"></i> Menu</button>
        <div class="menuContent">
          <div class="menuRow">
            <div class="menuColumn">
              <a title="Home" href="../index.html"><i class="fas fa-home"></i> Home</a>
              <a title="Association Contacts" href="contacts.html"><i class="fas fa-address-book"></i> Association Contacts</a>
            </div>
            <div class="menuColumn">
              <a title="Weekly Draw" href="weekly-draw.html"><i class="fas fa-user-friends"></i> Weekly Draw</a>
              <a title="News" href="#"><i class="far fa-newspaper"></i> News</a>
            </div>
            <div class="menuColumn">
              <a title="Quick Links" href="links.html"><i class="fas fa-bookmark"></i> Quick Links</a>
              <a title="Representative Teams" href="representative-teams.html"><i class="fas fa-certificate"></i> Representative Teams</a>
            </div>
            <div class="menuColumn">
              <a title="Documentation" href="documentation.html"><i class="fas fa-file"></i> Documentation</a>
              <a class="current" title="Contact Us" href="contact-us.html"><i class="fas fa-phone"></i> Contact Us</a>
            </div>
          </div>
        </div>
      </div>
      <a href="../index.html"><img src="../Assets/Img/mnaHeader.png" alt="Macleay Netball Association" title="Macleay Netball Association" class="logo"></a>
      <!-- Macleay Netball Association Main Logo -->
    </div>
    <!-- End of Navigation -->
    <div class="main">
      <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3417.0548484369647!2d152.83521681537312!3d-31.080401281517187!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x6b9ddf84d6da904b%3A0xfc941d1ded8f7a25!2sPam+Guyer+Netball+Centre!5e0!3m2!1sen!2sau!4v1559129788713!5m2!1sen!2sau"
        width="100%" height="400px" frameborder="0" style="border:0" allowfullscreen></iframe>
      <!-- Google Maps Embed -->
      <br>

      <div class="row">
        <div id="column">1</div>
        <div id="column">2</div>
      </div>
      <!-- End of Footer -->
    </div>
    <!-- End of Main -->
  </div>
  <!-- End of Wrapper -->
</body>

</html>

如果我将position:fixed bottom:0放在#column CSS代码中,则会附带一些图像。仅第二列下降,第一列被隐藏。

enter image description here

4 个答案:

答案 0 :(得分:1)

实现此目标的最佳方法是考虑响应速度并在不同的屏幕尺寸上更轻松地操作,这是将两个div元素包装在另一个div中,您将其固定在底部,然后调整两个div以占用其父对象的50% 。通过将div放入.row包装器中,您已经完成了一半的工作。

最好的解决方案可能是使用flex来调整其父包装中的div,如下所示:

此外,您不能两次使用相同的ID。

<div class="row">
    <div>1</div>
    <div>2</div>
 </div>

<style>
    .row {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;

        display: flex;
    }
    .row > div {
        flex: 1;
    }
</style>

然后,在移动设备上,您只需更改弯曲方向,页脚就会变成一栏而不是两栏。像这样:

<style>
    @media only screen and (max-width: 768px) {
        .row {
            flex-direction: column;
        }
    }
</style>

答案 1 :(得分:0)

向想要的页脚元素添加id,删除类并使用下面的css设置元素的样式。

#footer {
clear: both;
}
<div id="footer">
<div id="column">1</div>
<div id="column">2</div>
</div> <!-- End of Footer -->

答案 2 :(得分:0)

12都固定在底部。仅float: left在这里无效,并且它们位于完全相同的位置。 21的前面,而后者在2后面不可见

改为执行此操作。

<div class="row footer">
  <div id="column">1</div>
  <div id="column">2</div>
</div>
.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}

/* ~ Copyright (c) Macleay Netball Association (made by Tom Carpenter for Year 11 IPT Assessment Task #2.) 2019. */

html>body {
  background: none;
  padding: 0;
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: 'Trebuchet MS', sans-serif;
  /* Trebuchet MS as main font throughout website, sans-serif acting as a backup */
}

#wrapper {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  position: absolute;
  background: #1B315E;
}

::-webkit-scrollbar {
  display: none;
}

* {
  box-sizing: border-box;
  /* Alignment of DropDown Navigation Menu and Slideshow */
}

.navigation {
  background: #444;
  overflow: hidden;
  top: 0;
}

.menuDropDown {
  overflow: hidden;
  float: left;
}

.menuDropDown>#menuButton {
  font-size: 12px;
  padding: 16px 16px;
  /* Will result in final height of navigation */
  margin: 0;
  outline: none;
  border: none;
  font: inherit;
  background: inherit;
  color: #FFF;
}

#menuButton:hover,
.navigation>.menuDropDown:hover>#menuButton {
  border: none;
  outline: none;
  background: #666;
  color: #1B315E;
}

.menuDropDown:hover>.menuContent {
  display: block;
}

.menuContent {
  display: none;
  position: absolute;
  background: none;
  width: 100%;
  left: 0;
  max-height: 85vh;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.menuRow:after {
  display: table;
  clear: both;
  content: "";
}

.menuColumn {
  width: 25%;
  height: 100px;
  overflow-y: auto;
  padding: 5px;
  float: left;
  background: #666;
}

.menuColumn>a {
  float: none;
  display: block;
  text-align: left;
  text-decoration: none;
  font-size: 14px;
  padding: 8px;
  color: #1B315E;
}

.menuColumn>a.current {
  background: #777;
}

.menuColumn>a:hover {
  background: #888;
}

.logo {
  float: right;
  padding-right: 10px;
  width: 60px;
  height: 50px;
}

.main {
  width: 100%;
  height: calc(100vh - 43px);
  float: left;
  background: none;
  position: absolute;
  overflow: auto;
  padding: 8px;
}

.mainHeader {
  color: #FFF;
  text-align: center;
}

.mainImg {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

.mainBody {
  color: #FFF;
  padding: 8px;
}

.row:after {
  content: "";
  clear: both;
  display: table;
}

.column {
  float: left;
  width: 33.33%;
  padding: 8px;
}

#column {
  float: left;
  width: 50%;
  padding: 8px;
  background: #888;
  bottom: 0;
}

.sponsers {
  float: left;
  width: 20%;
  padding: 8px;
  -webkit-filter: grayscale(100%);
  filter: grayscale(100%);
}

.sponsers:hover {
  -webkit-filter: grayscale(80%);
  filter: grayscale(80%);
}

.slideshow {
  /* Slideshow Container containing Images, Sliders and Captions */
  position: relative;
  margin: auto;
  max-width: 1000px;
}

.slides {
  display: none;
}

.slideshowImages {
  width: 100%;
}

.slideButton {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  background: #888;
  color: #FFF;
  border: none;
  cursor: pointer;
  padding: 16px 32px;
  font-size: 18px;
  text-align: center;
  border-radius: 10px;
  font-weight: bold;
}

.mnaHeader {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}

.slideButton:hover {
  background: #666;
}

.previous,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: #FFF;
  font-weight: bold;
  font-size: 18px;
  transition: 0.3s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

.previous:hover,
.next:hover {
  background: rgba(0, 0, 0, 0.8);
}

table {
  width: 100%;
  font-size: 16px;
  color: #FFF;
}

.tableCaption {
  font-weight: bold;
  font-size: 18px;
  color: #33CC33;
}

table,
th,
td {
  border-collapse: collapse;
  border: 2px solid #888;
}

th,
td {
  padding: 8px;
  text-align: left;
}

#headerTable {
  width: 33.33%;
  background: #888;
  font-size: 18px;
  color: #33CC33;
}

.linkProp {
  color: #FFF;
  text-decoration: underline;
}

.newsPanel {
  width: 100%;
  background: #888;
  height: 135px;
}

.imgNewsPreview {
  width: 215px;
  height: 135px;
  padding: 4px;
  float: left;
}

.newsHeaderPreview {
  color: #FFF;
  margin-top: 0px;
}

.newsBodyPreview {
  color: #FFF;
  margin-top: -12px;
}

.caption {
  color: #33CC33;
  text-align: center;
  position: absolute;
  font-size: 20px;
  width: 100%;
  padding: 8px 12px;
  bottom: 16px;
}

.collapsible {
  background: #888;
  color: #FFF;
  font-weight: bold;
  width: 100%;
  padding: 18px;
  cursor: pointer;
  font-size: 16px;
  outline: none;
  border: none;
  text-align: left;
}

.active,
.collapsible:hover {
  background: #666;
}

.collapsible:after {
  content: '\002B';
  color: #FFF;
  font-weight: bold;
  float: right;
  margin-left: 4px;
}

.active:after {
  content: '\2212';
}

.contentCollapsible {
  padding: 0px 18px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
  background: #F8F8F8;
}

.fade {
  -webkit-animation-name: fade;
  /* To work on Safari (Apple) */
  -webkit-animation-duration: 1s;
  /* To work on Safari (Apple) */
  animation-name: fade;
  animation-duration: 1s;
}

.buttonReturn {
  position: absolute;
  margin-left: 20px;
  margin-top: 20px;
  background: #888;
  color: #FFF;
  border: none;
  cursor: pointer;
  font-size: 36px;
  text-align: center;
  border-radius: 50%;
  font-weight: bold;
}

.videoProp {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  height: 400px;
}

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}
@-webkit-keyframes fade {
  from {
    opacity: 0.4
  }
  to {
    opacity: 1
  }
}

@keyframes fade {
  from {
    opacity: 0.4
  }
  to {
    opacity: 1
  }
}

@media screen and (max-width: 600px) {
  /* Stacks navigation menu acting for the site to be responsive */
  .menuColumn {
    width: 100%;
    height: auto;
  }
}

@media screen and (max-width: 800px) {
  /* Stacks Image Header on Top - acting as responsive */
  .column,
  #column {
    width: 100%;
  }
}

@media screen and (max-width: 600px) {
  /* Responsive Sponser Images */
  .sponsers {
    width: 33.33%;
  }
}

@media screen and (max-width: 800px) {
  /* Increases video resolution as screen reduces */
  .videoProp {
    width: 100%;
  }
}

@media screen and (max-width: 600px) {
  /* Reduces font-size as screen resolution lowers */
  .newsBodyPreview {
    font-size: 14px;
  }
}

@media screen and (max-width: 300px) {
  /* Reduces font-sizes within the slideshow <div> */
  .previous,
  .next,
  .caption {
    font-size: 14px;
  }
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<div id="wrapper">
  <div class="navigation">
    <div class="menuDropDown">
      <button id="menuButton" title="Navigation Menu"><i class="fas fa-bars"></i> Menu</button>
      <div class="menuContent">
        <div class="menuRow">
          <div class="menuColumn">
            <a title="Home" href="../index.html"><i class="fas fa-home"></i> Home</a>
            <a title="Association Contacts" href="contacts.html"><i class="fas fa-address-book"></i> Association Contacts</a>
          </div>
          <div class="menuColumn">
            <a title="Weekly Draw" href="weekly-draw.html"><i class="fas fa-user-friends"></i> Weekly Draw</a>
            <a title="News" href="#"><i class="far fa-newspaper"></i> News</a>
          </div>
          <div class="menuColumn">
            <a title="Quick Links" href="links.html"><i class="fas fa-bookmark"></i> Quick Links</a>
            <a title="Representative Teams" href="representative-teams.html"><i class="fas fa-certificate"></i> Representative Teams</a>
          </div>
          <div class="menuColumn">
            <a title="Documentation" href="documentation.html"><i class="fas fa-file"></i> Documentation</a>
            <a class="current" title="Contact Us" href="contact-us.html"><i class="fas fa-phone"></i> Contact Us</a>
          </div>
        </div>
      </div>
    </div>
    <a href="../index.html"><img src="../Assets/Img/mnaHeader.png" alt="Macleay Netball Association" title="Macleay Netball Association" class="logo"></a>
    <!-- Macleay Netball Association Main Logo -->
  </div>
  <!-- End of Navigation -->
  <div class="main">
    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3417.0548484369647!2d152.83521681537312!3d-31.080401281517187!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x6b9ddf84d6da904b%3A0xfc941d1ded8f7a25!2sPam+Guyer+Netball+Centre!5e0!3m2!1sen!2sau!4v1559129788713!5m2!1sen!2sau"
      width="100%" height="400px" frameborder="0" style="border:0" allowfullscreen></iframe>
    <!-- Google Maps Embed -->
    <br>

    <div class="row footer">
      <div id="column">1</div>
      <div id="column">2</div>
    </div>
    <!-- End of Footer -->
  </div>
  <!-- End of Main -->
</div>
<!-- End of Wrapper -->

答案 3 :(得分:0)

首先id对于一个元素必须是唯一的,不应在两个元素上设置相同的ID。

在元素上设置位置fixed时,其位置是相对于视口,而不是相对于页面流。

您的两个元素都有bottom: 0,这是相对于浏览器窗口的,这就是为什么第一个元素被第二个元素覆盖的原因(未被隐藏)。您可以将它们分组在同一个父级中并放置父级,因此您的元素可以按预期堆叠。看一下下面的代码:

body {
    min-height: 200vh;
}

#column {
    display: block;
    padding 15px;
    width: 100%;
    position: fixed;
    bottom: 0;
    background: #e5e5e5;
}

#column div {
    display: block;
    padding: 10px;
    width: 100%;
    background: #9b009b;
    color: #fff;
    margin-bottom: 10px;
}
<div id="column">
    <div>first col</div>
    <div>second col</div>
</div>