【问题标题】:Getting a footer to the bottom of the page?将页脚放在页面底部?
【发布时间】:2017-02-12 23:53:55
【问题描述】:

所以我知道这里还有其他结果是针对这个问题的,但是我有一个相对“完成”的代码,如果我可以避免的话,我不想过多地弄乱它。

基本上,我网站上的所有内容看起来都符合我的要求,除了在较大的显示器上,页脚不会粘在屏幕底部,而且我的页脚和底部之间有这么大的丑陋差距的屏幕。

以下是我的索引和 css 文件。页脚元素在结束标记之间被挤来挤去,没有任何效果。我把它放在我的主要内容之外并尝试了 bottom: 0;与位置:绝对;它只是导致页脚的右端超出了我在容器中指定的宽度。

.header,
.navBar,
.pageTitle {
  margin: 0px;
  padding: 0px;
}
body {
  font-size: 20px;
  background-color: #006464;
}
html,
body,
#container {
  min-height: 100vh;
}
footer {
  background-color: #006400;
}
nav,
h1,
h2 {
  font-family: arial;
}
nav a:hover {
  background-color: white;
  color: black;
}
nav a {
  color: white;
}
h2 {
  text-align: center;
  background-color: white;
}
#container {
  width: 1000px;
  margin: auto;
  min-height: 100%;
}
#navBar {
  background-color: #228B22;
  padding: 10px;
}
#signUp {
  color: white;
  font-size: 20px;
  font-family: arial;
}
#welcomeFont {
  color: white;
  font-size: 25px;
  font-family: arial;
}
.header {
  background-color: #006400;
  padding: 20px;
}
.headerAnchor {
  text-decoration: none;
  color: black;
}
.table {
  background: #006400;
  max-width: 100%;
  border: 1px solid black;
  border-spacing: 10px;
  margin-left: auto;
  margin-right: auto;
}
.tableData {
  font-size: 19px;
  background: white;
  border: 1px solid black;
  padding: 8px;
  opacity: .75;
}
.resizeAbout {
  max-height: 50%;
  max-width: 50%;
  margin-top: 50px;
  margin-bottom: 50px;
}
.resizeHome {
  max-height: 50%;
  max-width: 50%;
  margin-top: 50px;
}
.resizePhotos {
  max-height: 40%;
  max-width: 40%;
}
.pageTitle {
  padding-bottom: 0px;
  box-shadow: 0px 8px 25px 0px;
}
.poetryAuthor {
  color: white;
  font-size: 15px;
  font-family: arial;
  font-style: italic;
}
.poetryCaptions {
  margin-top: 50px;
  color: white;
  font-size: 25px;
  font-family: arial;
}
<body>
  <div id="container">
    <header>
      <h1 class="header"><a class="headerAnchor" href="index.html">TheSingularEffect.Com</a></h1>
    </header>

    <nav id="navBar">
      <a href="index.html">Home</a>
      <a href="music.html">Music</a>
      <a href="photos.html">Photos</a>
      <a href="poetry.html">Poetry</a>
      <a href="about.html">About</a>
    </nav>



    <h2 class="pageTitle">
				Get the Full Effect!
		</h2>

    <img class="resizeHome" src="image/homepage.jpg" alt="home page image">
    <h3 id="welcomeFont">
				Welcome to the home of The Singular Effect!  
		</h3>
    <br>

    <form>
      <span id="signUp">Sign up for our newsletter!</span>
      <br>
      <input type="text" name="emailaddress" value="Email Address">
      <input type="submit" value="submit">
    </form>

  </div>

  <footer>
    &copy; 2016, Chris Hughes - SNHU
  </footer>


</body>

【问题讨论】:

  • position: fixed 工作吗?
  • 不,它确实工作了一段时间,但问题是它出现在页面加载时的屏幕上,有时会出现在我已经在屏幕上的内容之上。我只想让内容留在屏幕底部,在我碰巧在该页面上拥有的任何内容下方。

标签: html css position footer spacing


【解决方案1】:

您可以尝试使用 css-tables。我对其进行了测试,似乎可以按要求工作。如果您向其添加内容,页脚也会扩展。

在body下添加

margin:auto;
display:table;

及以下footer

display:table-row;
position:fixed;
width:1000px;
bottom: 0;

同样在这种情况下,您可能应该从#container 中删除margin,因为它已经在body 中定义。

我在哪里学到了窍门:http://colintoh.com/blog/display-table-anti-hero#sticky-footer

【讨论】:

  • 实际上这在 Firefox 上似乎不起作用,所以其他方式会更好。
  • 感谢您的努力。然而,当我做这一切的时候,结果是页脚没有到页面底部(仍然有很大的差距),页脚和我的内容底部之间不再有差距(它是现在只是停留在元素上),并且页脚不再与导航栏/页眉区域的宽度匹配。目标是让它坚持到底,即使没有足够的内容来填满大屏幕上的页面。
  • 做了一些改动,有什么改进吗?
  • 它确实有助于宽度。
【解决方案2】:
I added 1 more div to target all the body content except footer so I can set the height for that element. Here are code that works:

<!DOCTYPE html>
<! Must have tables, forms, multimedia, and links >
<head>
    <title>Home - The Singular Effect</title>
    <link rel="stylesheet" href="css/style.css">`enter code here`
    <style type="text/css">
        * {
               margin: 0px;
               padding: 0px;
            }

            body {
                font-size: 20px;
                background-color: #006464;
            }

            html, body, #container {
              height: 100%;
              margin: 0;
            }

            footer {
                margin-top: 50px;
                background-color: #006400;
                margin-bottom: 0px;
                bottom: 0;
            }


            nav, h1, h2 {
                font-family: arial;
            }

            nav a:hover {
                background-color: white;
                color: black;
            }

            nav a {
                color: white;
            }

            h2 {
                text-align: center;
                background-color: white;
            }

            #container {
                width: 1000px;
                margin: auto;
                min-height: 100%;

            }


            #navBar {
                background-color: #228B22;
                padding: 10px;

            }

            #signUp {
                color: white;
                font-size: 20px;
                font-family: arial;
            }


            #welcomeFont {
                color: white;
                font-size: 25px;
                font-family: arial;
            }


            .header {
                background-color: #006400;
                padding: 20px;
            }


            .headerAnchor {
                text-decoration: none;
                color: black;
            }

            .table {
                background: #006400;
                max-width: 100%;
                border: 1px solid black;
                border-spacing: 10px;
                margin-left: auto;
                margin-right: auto;
            }

            .tableData {
                font-size: 19px;
                background: white;
                border: 1px solid black;
                padding: 8px;
                opacity: .75;
            }


            .resizeAbout{
                max-height: 50%;
                max-width: 50%;
                margin-top: 50px;
                margin-bottom: 50px;

            }

            .resizeHome{
                max-height: 50%;
                max-width: 50%;
                margin-top: 50px;
            }

            .resizePhotos{
                max-height: 40%;
                max-width: 40%;

            }

            .pageTitle {
                padding-bottom: 0px;
                box-shadow: 0px 8px 25px 0px;
            }


            .poetryAuthor {
                color: white;
                font-size: 15px;
                font-family: arial;
                font-style: italic;
            }

            .poetryCaptions {
                margin-top: 50px;
                color: white;
                font-size: 25px;
                font-family: arial;
            }

            .bodyContetExceptFooter{
                padding: 20px;
              min-height: 90%;
              margin: 0 auto -50px;
            }
    </style>
</head>
<body>
    <div id="container">
    <div class="bodyContetExceptFooter">
        <header>
            <h1 class="header"><a class="headerAnchor" href="index.html">TheSingularEffect.Com</a></h1>
        </header>
        <div id="navBar">
        <nav>
                <a href="index.html">Home</a>
                <a href="music.html">Music</a>
                <a href="photos.html">Photos</a>
                <a href="poetry.html">Poetry</a>
                <a href="about.html">About</a>
            </nav>  
        </div>

        <div id="divContent">
            <h2 class="pageTitle">
                Get the Full Effect!
            </h2>

            <img class="resizeHome" src="image/homepage.jpg" alt="home page image">
            <h3 id="welcomeFont">
                Welcome to the home of The Singular Effect!
            </h3> <br>


            <form>
                <span id="signUp">Sign up for our newsletter!</span> <br>
                <input type="text" name="emailaddress" value="Email Address">
                <input type="submit" value="submit">
            </form>
        </div>
        </div>
        <footer>
            &copy; 2016, Chris Hughes - SNHU
        </footer>


    </div>

</body>
</html>

【讨论】:

    【解决方案3】:

    在body 上使用relative 定位,在footer 上定位absolute 并将其定位到left: 0; bottom 0;,还将width: 100%; 添加到footer 以填充body 的整个宽度。

    最后一件事是在正文中添加padding-bottom: 23px;,以避免footer在浏览器高度小于您的内容时隐藏内容。

    .header,
    .navBar,
    .pageTitle {
      margin: 0px;
      padding: 0px;
    }
    body {
      font-size: 20px;
      background-color: #006464;
      position: relative; /* added */
      padding-bottom: 23px; /* added, where 23px is the height of the footer */
    }
    html,
    body,
    #container {
      min-height: 100vh;
    }
    footer {
      background-color: #006400;
      position: absolute; /* added */
      bottom: 0; /* added */
      left: 0; /* added */
      width: 100%; /* added */
    }
    nav,
    h1,
    h2 {
      font-family: arial;
    }
    nav a:hover {
      background-color: white;
      color: black;
    }
    nav a {
      color: white;
    }
    h2 {
      text-align: center;
      background-color: white;
    }
    #container {
      width: 1000px;
      margin: auto;
      min-height: 100%;
    }
    #navBar {
      background-color: #228B22;
      padding: 10px;
    }
    #signUp {
      color: white;
      font-size: 20px;
      font-family: arial;
    }
    #welcomeFont {
      color: white;
      font-size: 25px;
      font-family: arial;
    }
    .header {
      background-color: #006400;
      padding: 20px;
    }
    .headerAnchor {
      text-decoration: none;
      color: black;
    }
    .table {
      background: #006400;
      max-width: 100%;
      border: 1px solid black;
      border-spacing: 10px;
      margin-left: auto;
      margin-right: auto;
    }
    .tableData {
      font-size: 19px;
      background: white;
      border: 1px solid black;
      padding: 8px;
      opacity: .75;
    }
    .resizeAbout {
      max-height: 50%;
      max-width: 50%;
      margin-top: 50px;
      margin-bottom: 50px;
    }
    .resizeHome {
      max-height: 50%;
      max-width: 50%;
      margin-top: 50px;
    }
    .resizePhotos {
      max-height: 40%;
      max-width: 40%;
    }
    .pageTitle {
      padding-bottom: 0px;
      box-shadow: 0px 8px 25px 0px;
    }
    .poetryAuthor {
      color: white;
      font-size: 15px;
      font-family: arial;
      font-style: italic;
    }
    .poetryCaptions {
      margin-top: 50px;
      color: white;
      font-size: 25px;
      font-family: arial;
    }
    <body>
      <div id="container">
        <header>
          <h1 class="header"><a class="headerAnchor" href="index.html">TheSingularEffect.Com</a></h1>
        </header>
    
        <nav id="navBar">
          <a href="index.html">Home</a>
          <a href="music.html">Music</a>
          <a href="photos.html">Photos</a>
          <a href="poetry.html">Poetry</a>
          <a href="about.html">About</a>
        </nav>
    
    
    
        <h2 class="pageTitle">
    				Get the Full Effect!
    		</h2>
    
        <img class="resizeHome" src="image/homepage.jpg" alt="home page image">
        <h3 id="welcomeFont">
    				Welcome to the home of The Singular Effect!  
    		</h3>
        <br>
    
        <form>
          <span id="signUp">Sign up for our newsletter!</span>
          <br>
          <input type="text" name="emailaddress" value="Email Address">
          <input type="submit" value="submit">
        </form>
    
      </div>
    
      <footer>
        &copy; 2016, Chris Hughes - SNHU
      </footer>
    
    
    </body>

    【讨论】:

      【解决方案4】:

      .header,
      .navBar,
      .pageTitle {
        margin: 0px;
        padding: 0px;
      }
      body {
        margin: 0px;
        padding: 0px;
        font-size: 20px;
        background-color: #006464;
      }
      html,
      body,
      #container {
        min-height: 100vh;
      }
      footer {
        background-color: #006400;
        position: absolute;
        bottom: 0px;
        width: 100%;
      }
      nav,
      h1,
      h2 {
        font-family: arial;
      }
      nav a:hover {
        background-color: white;
        color: black;
      }
      nav a {
        color: white;
      }
      h2 {
        text-align: center;
        background-color: white;
      }
      #container {
        width: 1000px;
        margin: auto;
        min-height: 100vh;
        position: relative;
      }
      #navBar {
        background-color: #228B22;
        padding: 10px;
      }
      #signUp {
        color: white;
        font-size: 20px;
        font-family: arial;
      }
      #welcomeFont {
        color: white;
        font-size: 25px;
        font-family: arial;
      }
      .header {
        background-color: #006400;
        padding: 20px;
      }
      .headerAnchor {
        text-decoration: none;
        color: black;
      }
      .table {
        background: #006400;
        max-width: 100%;
        border: 1px solid black;
        border-spacing: 10px;
        margin-left: auto;
        margin-right: auto;
      }
      .tableData {
        font-size: 19px;
        background: white;
        border: 1px solid black;
        padding: 8px;
        opacity: .75;
      }
      .resizeAbout {
        max-height: 50%;
        max-width: 50%;
        margin-top: 50px;
        margin-bottom: 50px;
      }
      .resizeHome {
        max-height: 50%;
        max-width: 50%;
        margin-top: 50px;
      }
      .resizePhotos {
        max-height: 40%;
        max-width: 40%;
      }
      .pageTitle {
        padding-bottom: 0px;
        box-shadow: 0px 8px 25px 0px;
      }
      .poetryAuthor {
        color: white;
        font-size: 15px;
        font-family: arial;
        font-style: italic;
      }
      .poetryCaptions {
        margin-top: 50px;
        color: white;
        font-size: 25px;
        font-family: arial;
      }
      <!DOCTYPE html>
      
      <head>
        <title>Home - The Singular Effect</title>
      
      </head>
      
      <body>
        <div id="container">
          <header>
            <h1 class="header"><a class="headerAnchor" hreF="index.html">TheSingularEffect.Com</a></h1>
          </header>
      
          <nav id="navBar">
            <a href="index.html">Home</a>
            <a href="music.html">Music</a>
            <a href="photos.html">Photos</a>
            <a href="poetry.html">Poetry</a>
            <a href="about.html">About</a>
          </nav>
      
      
      
          <h2 class="pageTitle">
      	    			Get the Full Effect!
      		    </h2>
      
          <img class="resizeHome" src="image/homepage.jpg" alt="home page image">
          <h3 id="welcomeFont">
          				Welcome to the home of The Singular Effect!
          		</h3> 
          <br>
      
          <form>
            <span id="signUp">Sign up for our newsletter!</span> 
            <br>
            <input type="text" name="emailaddress" value="Email Address">
            <input type="submit" value="submit">
          </form>
      
          <footer>
            &copy; 2016, Chris Hughes - SNHU
          </footer>
      
        </div>
      
      
      
      </body>

      【讨论】:

      • 复制粘贴到一个新页面,看看是不是你想要的。您对 #content div 为 100vh 然后声明 100% 最小高度有疑问。我将页脚粘贴到内容 div 中,然后将其设为内容 div 的绝对值。
      • 好的,所以我尝试了这个,它实际上是我最接近使我的网站正常工作的方法。这是唯一的问题:我有 4 个其他页面,其中 3 个页面的内容一直到页面底部,看起来它正在与页脚合并。我尝试在我的“关于”页面上添加一个额外的表格行,并且页脚随之展开,但它仍然粘在最后一个元素的底部。我尝试在页脚中添加上边距,但它没有做任何事情。
      • 更新:我能够通过在这两个页面的页脚和最后一个元素之间添加一个高度为 50px 的空
        来纠正这个问题。虽然这很有效,但感觉像是一种变通方法,我仍然不确定是什么导致页脚像这样撞到其他元素中。
      猜你喜欢
      • 1970-01-01
      • 2021-02-07
      • 2014-04-21
      • 2022-07-07
      • 2016-03-30
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多