Jorje12
Jorje12

Reputation: 423

Div with 100% height becomes shorter than 100% when viewport is vertically resized

I have a div which has height:100%; but when I resize the window vertically this happens (look at the nav-bar on the left):

enter image description here

Here is the jsfiddle: https://jsfiddle.net/uygxjbr2/2/

This is the faulty element:

.nav {
  /* position: fixed; */
  width: 250px;
  height: 100%;
  min-height: 100%;
}

.nav ul {
  list-style-type: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding: 40% 0;
  height: 100%;
  color: #fff;
  background-color: #40404a;
}

I've tried using /* position: fixed; */ but that destroys the whole page design.

Also, another issue that I've noticed is that when I vertically resize the window the top of the .upper-page disappears, making it unscrollable.

What can I do ?

Upvotes: 2

Views: 101

Answers (4)

bron
bron

Reputation: 1548

The easy way is a view height 100% and an outer page-wrapper with min-height 100%

<body>
  <div class="page-wrapper">
    <div class="nav">
      ....
    </div>
    <div id="page">
      ....
    </div>
  </div>
</body>


/* keep padding and border in the elements */
* {
  box-sizing: border-box;
}
/* the view height is 100% */
html, body {
  height: 100:
}
/* remove default margin of body */
body {
  margin: 0;
}
/* the main wrapper with a min-height 100% */
/* or, if using bootstrap: body > .container { these 3 lines } */
.page-wrapper {
  max-width: 100%;
  min-height: 100%;
  margin: auto;
}
/* below all your css */
.nav {
  width: 250px;
  ...
}
#page {
  width: 100%;
  ...
}

Upvotes: 0

54ka
54ka

Reputation: 3589

Add this styles to your CSS. I think that will be enough to solve the problem.

.nav {
    position: fixed;
    width: 250px;
    height: 100%;
    min-height: 100%;
}

@media (min-width: 900px) {
    #page {
        margin-left: 250px;
    }
}

@media (max-width: 900px) {
    .nav {
        width: 100%;
        z-index: 1;
        min-height: auto;
        height: auto;
    }
}

Example:

* {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

html {
    height: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    font-family: sans-serif;
}

body {
    height: 100%;
    margin: 0;
    padding: 0;
    display: flex;
    background-color: #28282f;
}

/* Chrome, Firefox, Opera, Safari 10.1+ */
::placeholder {
    color: #cfcfd5;
    opacity: 1;
    /* Firefox */
}

/* Internet Explorer 10-11 */
:-ms-input-placeholder {
    color: #cfcfd5;
}

/* Microsoft Edge */
::-ms-input-placeholder {
    color: #cfcfd5;
}

[list]::-webkit-calendar-picker-indicator {
    display: none;
}

a,
a:hover,
a:focus,
a:visited {
    color: #fff;
    text-decoration: none;
}

.nav {
    position: fixed;
    width: 250px;
    height: 100%;
    min-height: 100%;
}

.nav ul {
    list-style-type: none;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    padding: 40% 0;
    height: 100%;
    color: #fff;
    background-color: #40404a;
}

.nav ul li {
    width: 100%;
    background-color: #646474;
    padding: 15px;
    border: 1px solid #28282f;
}

.nav ul li:hover {
    background-color: #898999;
    cursor: pointer;
}

.nav ul .active {
    background-color: #898999;
}

#page {
    position: relative;
    width: 100%;
    color: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

span {
    display: inline-block;
}

.upper-page,
.middle-page,
.lower-page {
    position: relative;
    margin: 0 auto;
    width: 90%;
    max-width: 1000px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #40404a;
    padding-bottom: 10px;
}

.lower-page {
    margin-top: 20px;
}

.title,
.info {
    background-color: #4c4c58;
}

.current-settings,
.change-settings,
.logs {
    background-color: #646474;
}

.title,
.wrapper-title {
    font-size: 20px;
    font-weight: bold;
    width: 100%;
    max-width: 1000px;
    padding: 10px;
    text-align: left;
}

.wrapper-title {
    padding-top: 40px;
    padding-left: 25px;
    font-size: 24px;
}

.wrapper {
    width: 100%;
    max-width: 1000px;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    padding-bottom: 10px;
}

.current-settings,
.change-settings,
.logs {
    width: 45%;
    max-width: 600px;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px;
}

.upper-page .current-settings,
.upper-page .change-settings {
    height: 560px;
}

.middle-page .current-settings,
.middle-page .change-settings {
    height: 650px;
}

.settings {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
}

.settings,
.change-settings {
    font-size: 14px;
    padding: 10px;
}

.settings span {
    padding: 20px 0;
    letter-spacing: 0.5px;
}

.change-settings .info {
    padding: 10px;
    width: 100%;
    letter-spacing: 0.2px;
}

.change-settings input {
    margin: 5px 0;
    padding: 10px;
    width: 100%;
    color: #fff;
    background-color: #52525f;
    border: none;
    font-size: 16px;
    letter-spacing: 0.5px;
}

.inputs {
    display: flex;
    flex-direction: row;
    width: 100%;
    justify-content: space-around;
    align-items: center;
}

.inputs .color {
    margin-left: 10px;
    padding: 5px;
    height: 38px;
    border: none;
    width: 80px;
}

.change-settings .wrong {
    color: #e11422;
    font-weight: 700;
}

.change-settings .correct {
    color: #14e16d;
    font-weight: 700;
}

.change-settings label {
    padding: 5px 0;
    font-weight: bold;
    letter-spacing: 0.5px;
}

.change-settings .button {
    display: inline-block;
    width: 50%;
    margin: 0 auto;
    margin-top: 20px;
    background-color: #28282f;
}

.change-settings .button:hover {
    background-color: #4c4c58;
    cursor: pointer;
}

.logs {
    width: 95%;
    max-width: none;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    line-height: 25px;
}

.middle-page .user-settings {
    height: auto;
}

.user-title {
    text-align: center;
}

@media (max-width: 900px) {
    body {
        flex-direction: column;
        justify-content: flex-start;
    }

    .nav {
        width: 100%;
    }

    .nav ul {
        width: 100%;
        flex-direction: row;
        justify-content: center;
        padding: 0;
    }

    .nav ul li {
        padding: 10px 5px;
        font-size: 14px;
    }

    #page {
        padding: 50px 0;
    }

    .wrapper {
        flex-direction: column;
    }

    .wrapper-title {
        padding: 40px 5% 0;
    }

    .upper-page .current-settings,
    .upper-page .change-settings,
    .middle-page .current-settings,
    .middle-page .change-settings {
        width: 90%;
        margin: 10px 0;
        height: auto;
    }

    .logs {
        width: 90%;
        max-width: 600px;
    }

    .settings span {
        padding: 5px 0;
    }

    .change-settings .info {
        padding: 10px;
    }

    .change-settings input {
        margin: 5px 0;
        padding: 10px;
        font-size: 14px;
    }

    .change-settings label {
        padding: 5px 0;
    }
}

@media (min-width: 900px) {
    #page {
        margin-left: 250px;
    }
}

@media (max-width: 900px) {
    .nav {
        width: 100%;
        z-index: 1;
        min-height: auto;
        height: auto;
    }
}
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="scripts.js" defer></script>

<div class="nav">
    <ul>
        <a href="/display">
            <li class="active">Display Settings</li>
        </a>
        <a href="/network">
            <li>Network Settings</li>
        </a>
        <a href="/user">
            <li>Create/Change User</li>
        </a>
    </ul>
</div>
<div id="page">
    <div class="upper-page">
        <span class="wrapper-title">Display Settings</span>
        <div class="wrapper">
            <div class="current-settings">
                <span class="title">Current Settings</span>
                <div class="settings">
                    <span>Brightness: %ph_brightness% (&#37;)</span>
                    <span>Idle Messsage: %ph_message%</span>
                    <span>Timer 1 (%ph_GPIO_1%): %ph_timer_1%</span>
                    <span>Timer 2 (%ph_GPIO_2%): %ph_timer_2%</span>
                    <span>Timer 3 (%ph_GPIO_3%): %ph_timer_3%</span>
                </div>
            </div>
            <div class="change-settings">
                <span class="title">Change Settings</span>
                <span class="info">
                    Here you can change the display's settings.
                </span>
                <form method="post" class="settings">
                    <label for="brightness">Brightness: </label>
                    <input type="text" name="brightness" id="brightness" placeholder="Brightness" />
                    <label for="idle">Idle Message: </label>
                    <div class="inputs">
                        <input type="text" name="idle" id="idle" placeholder="Idle Message" />
                        <input type="color" list="idleColor" name="idleColor" class="color" />
                        <datalist id="idleColor">
                            <option value="#ffffff"></option>
                            <option value="#ff0000"></option>
                            <option value="#ff7f00"></option>
                            <option value="#00ff00"></option>
                            <option value="#0000ff"></option>
                            <option value="#00ffff"></option>
                            <option value="#8f00ff"></option>
                        </datalist>
                    </div>
                    <label for="timer1">Timer 1 (%ph_GPIO_1%): </label>
                    <div class="inputs">
                        <input type="text" name="timer1" id="timer1" placeholder="Timer 1" />
                        <input type="color" list="timer1Color" name="timer1Color" class="color" />
                        <datalist id="timer1Color">
                            <option value="#ffffff"></option>
                            <option value="#ff0000"></option>
                            <option value="#ff7f00"></option>
                            <option value="#00ff00"></option>
                            <option value="#0000ff"></option>
                            <option value="#00ffff"></option>
                            <option value="#8f00ff"></option>
                        </datalist>
                    </div>
                    <label for="timer2">Timer 2 (%ph_GPIO_2%): </label>
                    <div class="inputs">
                        <input type="text" name="timer2" id="timer2" placeholder="Timer 2" />
                        <input type="color" list="timer2Color" name="timer2Color" class="color" />
                        <datalist id="timer2Color">
                            <option value="#ffffff"></option>
                            <option value="#ff0000"></option>
                            <option value="#ff7f00"></option>
                            <option value="#00ff00"></option>
                            <option value="#0000ff"></option>
                            <option value="#00ffff"></option>
                            <option value="#8f00ff"></option>
                        </datalist>
                    </div>
                    <label for="timer3">Timer 3 (%ph_GPIO_3%): </label>
                    <div class="inputs">
                        <input type="text" name="timer3" id="timer3" placeholder="Timer 3" />
                        <input type="color" list="timer3Color" name="timer3Color" class="color" />
                        <datalist id="timer3Color">
                            <option value="#ffffff"></option>
                            <option value="#ff0000"></option>
                            <option value="#ff7f00"></option>
                            <option value="#00ff00"></option>
                            <option value="#0000ff"></option>
                            <option value="#00ffff"></option>
                            <option value="#8f00ff"></option>
                        </datalist>
                    </div>
                    <input class="button" type="submit" name="save_display" value="Save Settings" />
                </form>
            </div>
        </div>
    </div>

    <div class="lower-page">
        <div class="wrapper-title">Logs</div>
        <div class="wrapper">
            <div class="logs">%ph_logs%</div>
        </div>
    </div>
</div>

Upvotes: 1

Warden330
Warden330

Reputation: 1042

Okay so if i understood correctly your aim is that the Navbar on the side is always full body-size in height. First of all: dont add style to the html itself, always use the body for that. height does not work like width with % it only works if you have a relation, so 100% works if the parent element has a fixed height, so 100% on body and html wont work correctly. Therefore i suggest the following changes:

html {
  /*height: 100%; <--- remove this! */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-family: sans-serif;
}
body {
  /*height: 100%; <--- change this to the code below:*/
  min-height: 100vh;
  margin: 0;
  padding: 0;
  display: flex;
  background-color: #28282f;
}
.nav {
  width: 250px;
   /*height: 100%; <--- remove this! */
  min-height: 100%;
}

With this your body is always the same height as your screen and the navbar will always be 100% of the body. This, atleast in my tests, removed the gap below the navbar.

Upvotes: 1

Siddharth Subramanian
Siddharth Subramanian

Reputation: 51

The gap below .nav is because your right div is overflowing in height. Instead of using height: 100% on your body (as per your code), give it a min-height: 100%. This should fix the overflowing issue and also make sure the nav element takes full height.

Upvotes: 0

Related Questions