CSS元素不会覆盖页面的100%

时间:2018-04-23 01:12:53

标签: html css

首先,这个 IS 是一项大学任务,尽管看起来很基本,但我在过去的3个小时里一直试图解决这个问题,似乎没有任何工作。

所以我的侧面导航栏设置为height: 100; 但是,它没有明显的原因,它靠近页面的底部。调整窗口大小足以将导航栏推过页脚,使页脚悬挂在页面最后一个元素的正下方

HTML:

<!DOCTYPE html>
<html lang=en>
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" type="text/css" media="screen and (max-width: 850px)" href="css/style2.css" />
    <link rel="stylesheet" type="text/css" media="screen and (max-width: 450px)" href="css/style3.css" />
    <!-- <script src="js/main.js"></script> -->
    <title>CS15020: Web Development Tools</title>
</head>

<body>
<header class="text element-color">
    <h1>David’s assignment 2 for CS15020</h1>
</header>

<nav>
    <a href="">Email me</a><br />
    <a href="prac2.html">My CV (no CSS)</a><br />
    <a href="prac2_centered.html">My CV (with CSS)</a><br />
    <a href="..">Homepage</a><br />
    <a href="" target="_blank">My website</a><br />
    <br /> <!-- dirty way of making media queries work -->
    <a href="http://validator.w3.org/check?uri=referer">Valid HTML5</a><br />
    <a href="http://jigsaw.w3.org/css-validator/check/referer">Valid CSS</a>
</nav>

<main>
<br />
Please enter your details below:
<form id="userDetails" action="/">
<p>
<input type="text" id="users_name" placeholder="Name" />
<br />
<select name="grade">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>

<input type="button" name="send" onclick="process()" value="Submit" />
</form>

<p id="out"></p>
<img src="images/web.jpg" alt="web image"></img>
<p id="dateModified"></p>

<script>
function process() {
        var txt = "";
        var hoursOut = "";
        var name = document.getElementById("users_name").value;

        switch(document.forms['userDetails'].grade.selectedIndex){
        case 0:
            txt="1/5 (20%); Wow you must really suck at life...";
            break;
        case 1:
            txt="2/5 (40%); Meh...";
            break;
        case 2: 
            txt="3/5 (60%); Well... at least you know something, right?";
            break;
        case 3:
            txt="4/5 (80%); It's getting good!";
            break;
        case 4:
            txt="5/5 (100%); Awesome bro, keep it goin'!";
            break;
        }

    var hours = (new Date()).getHours();

    if (hours >= 12 && hours < 18) {
    hoursOut = "afternoon";
    } else if (hours < 12 && hours >= 5) {
    hoursOut = "morning";
    } else if (hours >= 18 && hours <= 23) {
    hoursOut = "evening";
    } else hoursOut = "night";


    var finalOut = document.getElementById("out");
    finalOut.innerHTML = "Good " + hoursOut + ", " + name + "! You expected grade " + txt;
    }

    var mod = new Date(document.lastModified);
    var modFormatted = mod.toLocaleString();
    document.getElementById("dateModified").innerHTML = "Last modified: " + modFormatted;
</script>
</main>

<div id="footer" class="text element-color">
    CS15020 - WEB DEVELOPMENT TOOLS
</div>
</body>

CSS:

main {
    margin-left: 15%;
    min-height:100%;
    margin-bottom: 1%;
    overflow: hidden;
}

body
{
    margin: 0;
    background-color: #F8E0E0;
}

.text {
    font-family: Arial, Helvetica, sans-serif;
}

.element-color {
    color: #818181;
}

header {
    background-color: #111;
    width: 100%;
}

header h1 {
    margin: 0;
    padding-top: 2px;
    padding-bottom: 4px;
    padding-left: 5px;
}

nav {
    position: absolute;
    display: block;
    padding-top: 5px;
    padding-left: 0;
    width: 12%;
    height: 100%;
    background-color: #111;
    border-top: 2px solid #818181;
}

nav a {
    text-decoration: none;
    color: #818181;
    padding: 6px 8px 6px 16px;
}

nav a:hover{
    color: white;
}

img {
    max-width: 100%;
    height: auto;
}

#footer { 
    width: 100%;
    position: relative;
    bottom: 0;
    text-align: center;
    margin: 0;
    background-color: #111;
    color: #818181;

}

CSS#2:

main {
    margin-left: 140px;
    margin-bottom: 1%;
    overflow: hidden;
}

body
{
    margin:0;
    background-color: #F8E0E0;
}

.text {
    font-family: Arial, Helvetica, sans-serif;
}

.element-color {
    color: #818181;
}

header {
    background-color: #111;
    width: 100%;
    /*border-bottom: 2px solid white;*/
}

header h1 {
    margin: 0;
    padding-top: 2px;
    padding-bottom: 4px;
    padding-left: 5px;
    font-size: 100%;
}

nav {
    position: absolute;
    padding-top: 5px;
    padding-left: 0%;
    width: 16%;
    height: 100%;
    background-color: #111;
    border-top: 2px solid #818181;
}

nav a {
    text-decoration: none;
    font-size: 85%;
    color: #818181;
    padding: 6px 8px 6px 16px;
}

nav a:hover{
    color: white;
}

img {
    max-width: 100%;
    height: auto;
}

#footer { 
    width: 100%;
    position: relative;
    bottom: 0;
    text-align: center;
    margin: 0;
    background-color: #111;
    color: #818181;

}

CSS#3:

main {
    z-index: 1;
    margin-left: 5%;
    padding-top: 15%;
    margin-bottom: 1%;
    overflow: hidden;
}

body
{
    margin:0;
    background-color: #F8E0E0;
}

.text {
    font-family: Arial, Helvetica, sans-serif;
}

.element-color {
    color: #818181;
}

header {
    background-color: #111;
    /*border-bottom: 2px solid white;*/
}

header h1 {
    margin: 0;
    padding-top: 2px;
    padding-bottom: 4px;
    padding-left: 5px;
    font-size: 100%;
}

nav {
    z-index: 2;
    position: fixed;
    overflow: hidden;
    display: block;
    padding-bottom: 5px;
    width: 100%;
    height: auto;
    border-top: 2px solid #818181;
}

nav a {
    text-decoration: none;
    font-size: 85%;
    /*color: #818181;*/
    padding: 6px 8px 6px 16px;
}

br { 
    display: none;
}

nav a:hover{
    color: white;
}

img {
    max-width: 100%;
    height: auto;
}

#footer { 
    width: 100%;
    position: absolute;
    bottom: 0;
    text-align: center;
    margin: 0;
    background-color: #111;
    color: #818181;

}

CSS表#2和#3,纯粹仅用于媒体查询

更新:你实际上可以看到整件事here;我现在已将高度更改为固定。

3 个答案:

答案 0 :(得分:1)

通过 Flex 解决了可滚动内容问题。

.outer {
  display: flex;
}

.nav {
  flex: 1;
  background: black;
}

.main {
  flex: 4;
}

main {
  margin-left: 15%;
  min-height: 100%;
  margin-bottom: 1%;
  overflow: hidden;
}

body {
  margin: 0;
  background-color: #F8E0E0;
}

.text {
  font-family: Arial, Helvetica, sans-serif;
}

.element-color {
  color: #818181;
}

header {
  background-color: #111;
  width: 100%;
  height: 40px;
}

header h1 {
  margin: 0;
  padding-top: 2px;
  padding-bottom: 4px;
  padding-left: 5px;
}

nav a {
  text-decoration: none;
  color: #818181;
}

nav a:hover {
  color: white;
}

img {
  max-width: 100%;
  height: auto;
}

#footer {
  width: 100%;
  bottom: 0;
  text-align: center;
  margin: 0;
  background-color: #111;
  color: #818181;
}
<body>
  <header class="text element-color">
    <h1>David’s assignment 2 for CS15020</h1>
  </header>
  <div class="outer">
    <div class="nav">
      <nav>
        <a href="">Email me</a><br />
        <a href="prac2.html">My CV (no CSS)</a><br />
        <a href="prac2_centered.html">My CV (with CSS)</a><br />
        <a href="..">Homepage</a><br />
        <a href="" target="_blank">My website</a><br />
        <br />
        <!-- dirty way of making media queries work -->
        <a href="http://validator.w3.org/check?uri=referer">Valid HTML5</a><br />
        <a href="http://jigsaw.w3.org/css-validator/check/referer">Valid CSS</a>
      </nav>
    </div>
    <div class="main">
      <main>
        <br /> Please enter your details below:
        <form id="userDetails" action="/">
          <p>
            <input type="text" id="users_name" placeholder="Name" />
            <br />
            <select name="grade">
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>

            <input type="button" name="send" onclick="process()" value="Submit" />
        </form>

        <p id="out"></p>
        <img src="images/web.jpg" alt="web image">
        <p id="dateModified"></p>

        <br /> Please enter your details below:
        <form id="userDetails" action="/">
          <p>
            <input type="text" id="users_name" placeholder="Name" />
            <br />
            <select name="grade">
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>

            <input type="button" name="send" onclick="process()" value="Submit" />
        </form>

        <p id="out"></p>
        <img src="images/web.jpg" alt="web image">
        <p id="dateModified"></p>

        <br /> Please enter your details below:
        <form id="userDetails" action="/">
          <p>
            <input type="text" id="users_name" placeholder="Name" />
            <br />
            <select name="grade">
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>

            <input type="button" name="send" onclick="process()" value="Submit" />
        </form>

        <p id="out"></p>
        <img src="images/web.jpg" alt="web image">
        <p id="dateModified"></p>

        <br /> Please enter your details below:
        <form id="userDetails" action="/">
          <p>
            <input type="text" id="users_name" placeholder="Name" />
            <br />
            <select name="grade">
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>

            <input type="button" name="send" onclick="process()" value="Submit" />
        </form>

        <p id="out"></p>
        <img src="images/web.jpg" alt="web image">
        <p id="dateModified"></p>

        <br /> Please enter your details below:
        <form id="userDetails" action="/">
          <p>
            <input type="text" id="users_name" placeholder="Name" />
            <br />
            <select name="grade">
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
            </select>

            <input type="button" name="send" onclick="process()" value="Submit" />
        </form>

        <p id="out"></p>
        <img src="images/web.jpg" alt="web image" />
        <p id="dateModified"></p>
      </main>
    </div>
  </div>
  <div id="footer" class="text element-color">
    CS15020 - WEB DEVELOPMENT TOOLS
  </div>
</body>

答案 1 :(得分:1)

尝试将CS​​S中的 nav's 位置从absolute更改为fixed(第一个)。它对我有用。

EDITED

 header {
    background-color: #111;
    width: 100%;
    position: fixed;
}

nav {
    position: fixed;
    display: block;
    padding-top: 5px;
    padding-left: 0;
    width: 12%;
    height: 100%;
    margin-top: 42px;
    background-color: #111;
    border-top: 2px solid #818181;
}

答案 2 :(得分:0)

如果另一个元素推动它,固定位置可能会更好地工作,尽管这可能不适合您想要的最终产品。

兼容性不是很好,但您可以尝试将高度设置为100vh。这将把它设置为用户视口的100 1/100,这将是整个屏幕。