1

I struggle to make my header be 85% of the screen height and the following red bar being the rest 15%, please tell my what did I do wrong and how can I make this happen:)

Thank you for the responses, could you guys tell me why when I reduce the height the content of header overlaps with the red bar and how can i solve that?

Here is the code: https://jsfiddle.net/v0bk9u3v/

<header>
        <a href="#"><img src="#" class="logo"></a>
        <ul class="navbar">
            <li><a href="#">Lorem</a></li>
            <li><a href="#">Lorem</a></li>
            <li><a href="#">Lorem</a></li>
            <li><a href="#">Lorem</a></li>
        </ul>
        <div class="headline">
            <h1>Lorem</h1>
            <div class="desc">
                Lorem ipsum dolor sit amet, et denique molestiae sit. Alia mediocrem ei mei, recusabo repudiandae mel et.
            </div>
        </div>
        <a href="#" class="button b1">Lorem ipsum</a>
    </header>

<div class="lorem">
    <h2>Lorem ipsum</h2>
        <p>Lorem ipsum dolor sit amet, et denique molestiae sit. </p>
<a class="button" href="#">Lorem ipsum</a>
    </div>


*
{
  margin: 0;
  padding: 0;
}

.headline h1
{
    line-height: 85%;
    font-size: 7em;
    color: #ffffff;
    width: 5em;
    margin-left: 5vh;
    text-transform: uppercase;
    padding-top: 2em;
}

.navbar
{
    padding-top: 1em;
    padding-right: 3em;
    float: right;
    text-transform: uppercase;
    list-style-type: none;
    display: inline;
}

.navbar li
{
    float: left;
}

.navbar li a
{
    font-size: 0.9em;
    display: block;
    text-align: center;
    padding: 0.8em;
    text-decoration: none;
    color: #ffffff;
}

.logo
{
    width: 15em;
    padding: 1.5em 2em 2em 5vh;
    float: left;
}

.b1
{
    margin-left: 5vh;
    font-size: 0.9em;
    letter-spacing: 0.1em;
    padding: 1.3em 2.8em;
    margin-top: 1em;
}

.button
{

  z-index: 1;
  display: inline-block;
  color: #ffffff;
  text-decoration: none;
  text-transform: uppercase;
  border: solid 1px #ffffff;
  position: relative;
  transition: color 0.2s ease-in;
}

 .button:before
{
    z-index: -1;
    content: '';
    position: absolute;
    top: 0px;
    left: 0px;
    height: 100%;
    width: 100%;
    max-height: 0%;
    background: #ffffff;
    transition: all 0.1s ease-in;
}

  .button:hover
{
    color:#d00807;
}

  .button:hover:before
{
    max-height: 100%;
}

.desc
{
    color: #ffffff;
    width: 25em;
    font-size: 1.2em;
    padding: 2em 0em 2em 5vh;
}

header
{
    background-image: linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.1)),url(#);
    background-position: center;
    background-size: cover;
}

.lorem,.lorem h2,.lorem p,.lorem a
{
 overflow: hidden;   
}

.lorem
{
    background-color:#d00807;
    color: #ffffff;
}

.lorem h2
{
    font-size: 1.7em;
    text-transform: uppercase;
    display: inline-block;
    text-align: center;
    position: relative;
}

.lorem p
{
    font-size: 1em;
    display: inline-block;
    text-align: center;
    position: relative;
}

.lorem a
{
    font-size: 0.9em;
    letter-spacing: 0.1em;
    padding: 1.5em 1.5em;
    position: relative;
}

@media screen and (min-width: 1000px)
{
    .lorem
    {
    padding: 2.2em 2em;
    }

    .lorem h2
    {
        float: left;
        width: 10em;
        font-size: 2.3em;

    }

    .lorem a
    {
        float: right;
        margin-right: 1em;
        margin-top: 1em;
    }

    .lorem p
    {
        margin-left: 1em;
        font-size: 0.9em;
        padding-top: 1.5em;
        width: 26vw;
    }
}

@media screen and (max-width: 1000px) and (min-width: 750px)
{
    .lorem
    {
    padding: 2.2em 2em;
    }

    .lorem h2
    {
        font-size: 2em;

    }

    .lorem h2,.lorem p
    {
        display: block;
        text-align: left;
        width: 50vw;
    }

    .lorem a
    {
        float: right;
        margin-right: 1em;
        margin-top: -4em;
    }
}

@media only screen and (max-width: 750px)
{
    .lorem
    {
        padding: 5em 7em;
    }

    .lorem h2
    {
        padding-bottom: 0.3em;
        display: block;
        font-size: 1.5em;
        text-align: center;
    }

    .lorem p
    {
        font-size: 0.9em;
        display: block;
    }

    .lorem a
    {
        text-align: center;
        display:block; 
        margin-top: 1em;
    }

    .logo
    {
        width: 25em;
        display: block;
        margin: 0 auto;
        float:none;
    }

    .navbar
    {
        text-align: center;
        position: relative;
        justify-content: center;
        float: none;
        display: block;
    }

    .navbar li
    {
        float: none;
        display: inline-block;
    }

    .headline h1
    {
        font-size: 5.2em;
    }

    .desc
    {
        font-size: 1.1em;
    }

    .navbar li a
    {
        font-size: 1em;
    }
}
Feast
  • 39
  • 5
  • Possible duplicate of [Percentage Height HTML 5/CSS](http://stackoverflow.com/questions/1622027/percentage-height-html-5-css) – Pavel May 10 '17 at 11:08
  • @Ursu Grizzly check my answer, may be it will be resolve your issue. – LKG May 10 '17 at 11:17

3 Answers3

1

Add below code in css and this work for desktop you can use media queries for responsive.

body, html {
  height:100%;
}
header {
  height:calc(100% - 130px);
}
LKG
  • 4,152
  • 1
  • 11
  • 21
  • Thanks:) I have another question regarding my code: could you tell me why when I reduce the height the content of header overlaps with the red bar and how can i solve that? – Feast May 10 '17 at 11:27
  • You can use media queries for it – LKG May 10 '17 at 11:28
  • Thanks, coul you explain to me how does this work? I am talking about this code: body, html { height:100%; } header { height:calc(100% - 130px); } – Feast May 10 '17 at 11:56
  • header { height:calc(100% - 130px); } is reduce 130px in height of 100% which is the red part height. – LKG May 10 '17 at 12:22
  • I did :) I am new so I didn't knew how however it says that it does not display because my reputation is under 15. – Feast May 10 '17 at 12:52
1

Use vh for height of header and the red area. 100vh means 100% of the vertical height of the device.. So assign 85 vh (85%) to header and 15vh (15%) to red area only for desktop version like the below code:

@media(min-width: 768px){
header{height:85vh}
.lorem{height:15vh;}
}

* {
  margin: 0;
  padding: 0;
  box-sizing:border-box;
}

.headline h1 {
  line-height: 85%;
  font-size: 7em;
  color: #ffffff;
  width: 5em;
  margin-left: 5vh;
  text-transform: uppercase;
  padding-top: 2em;
}

.navbar {
  padding-top: 1em;
  padding-right: 3em;
  float: right;
  text-transform: uppercase;
  list-style-type: none;
  display: inline;
}

.navbar li {
  float: left;
}

.navbar li a {
  font-size: 0.9em;
  display: block;
  text-align: center;
  padding: 0.8em;
  text-decoration: none;
  color: #ffffff;
}

.logo {
  width: 15em;
  padding: 1.5em 2em 2em 5vh;
  float: left;
}

.b1 {
  margin-left: 5vh;
  font-size: 0.9em;
  letter-spacing: 0.1em;
  padding: 1.3em 2.8em;
  margin-top: 1em;
}

.button {
  z-index: 1;
  display: inline-block;
  color: #ffffff;
  text-decoration: none;
  text-transform: uppercase;
  border: solid 1px #ffffff;
  position: relative;
  transition: color 0.2s ease-in;
}

.button:before {
  z-index: -1;
  content: '';
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
  max-height: 0%;
  background: #ffffff;
  transition: all 0.1s ease-in;
}

.button:hover {
  color: #d00807;
}

.button:hover:before {
  max-height: 100%;
}

.desc {
  color: #ffffff;
  width: 25em;
  font-size: 1.2em;
  padding: 2em 0em 2em 5vh;
}

header {
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.1)), url(#);
  background-position: center;
  background-size: cover;
}

.lorem,
.lorem h2,
.lorem p,
.lorem a {
  overflow: hidden;
}

.lorem {
  background-color: #d00807;
  color: #ffffff;
}

.lorem h2 {
  font-size: 1.7em;
  text-transform: uppercase;
  display: inline-block;
  text-align: center;
  position: relative;
}

.lorem p {
  font-size: 1em;
  display: inline-block;
  text-align: center;
  position: relative;
}

.lorem a {
  font-size: 0.9em;
  letter-spacing: 0.1em;
  padding: 1.5em 1.5em;
  position: relative;
}
@media(min-width: 768px){
header{height:85vh}
.lorem{height:15vh;}
}
@media screen and (min-width: 1000px) {
  .lorem {
    padding: 2.2em 2em;
  }
  .lorem h2 {
    float: left;
    width: 10em;
    font-size: 2.3em;
  }
  .lorem a {
    float: right;
    margin-right: 1em;
    margin-top: 1em;
  }
  .lorem p {
    margin-left: 1em;
    font-size: 0.9em;
    padding-top: 1.5em;
    width: 26vw;
  }
}

@media screen and (max-width: 1000px) and (min-width: 750px) {
  .lorem {
    padding: 2.2em 2em;
  }
  .lorem h2 {
    font-size: 2em;
  }
  .lorem h2,
  .lorem p {
    display: block;
    text-align: left;
    width: 50vw;
  }
  .lorem a {
    float: right;
    margin-right: 1em;
    margin-top: -4em;
  }
}

@media only screen and (max-width: 750px) {
  .lorem {
    padding: 5em 7em;
  }
  .lorem h2 {
    padding-bottom: 0.3em;
    display: block;
    font-size: 1.5em;
    text-align: center;
  }
  .lorem p {
    font-size: 0.9em;
    display: block;
  }
  .lorem a {
    text-align: center;
    display: block;
    margin-top: 1em;
  }
  .logo {
    width: 25em;
    display: block;
    margin: 0 auto;
    float: none;
  }
  .navbar {
    text-align: center;
    position: relative;
    justify-content: center;
    float: none;
    display: block;
  }
  .navbar li {
    float: none;
    display: inline-block;
  }
  .headline h1 {
    font-size: 5.2em;
  }
  .desc {
    font-size: 1.1em;
  }
  .navbar li a {
    font-size: 1em;
  }
}
<header>
  <a href="#"><img src="#" class="logo"></a>
  <ul class="navbar">
    <li><a href="#">Lorem</a></li>
    <li><a href="#">Lorem</a></li>
    <li><a href="#">Lorem</a></li>
    <li><a href="#">Lorem</a></li>
  </ul>
  <div class="headline">
    <h1>Lorem</h1>
    <div class="desc">
      Lorem ipsum dolor sit amet, et denique molestiae sit. Alia mediocrem ei mei, recusabo repudiandae mel et.
    </div>
  </div>
  <a href="#" class="button b1">Lorem ipsum</a>
</header>

<div class="lorem">
  <h2>Lorem ipsum</h2>
  <p>Lorem ipsum dolor sit amet, et denique molestiae sit. </p>
  <a class="button" href="#">Lorem ipsum</a>
</div>
Sahil Dhir
  • 4,162
  • 1
  • 13
  • 33
  • Thanks:) I have another question regarding my code: could you tell me why when I reduce the height the content of header overlaps with the red bar and how can i solve that? How could I make the content the header to resize accordingly because now when you reduce height resizing the window it overlaps. – Feast May 10 '17 at 11:28
  • the overlapping is beacause of the font sizes of the h1 in the header , the 2em padding given to it.... just reduce it – Sahil Dhir May 10 '17 at 11:30
1

See if this helps you: https://jsfiddle.net/v0bk9u3v/

For the first block you were setting the height on the h1, I changed this and set it on the header selector @ line 7

header{
  height:85vh;
}  

I also explicity set the height for the lorem block @ line 123

.lorem{
    height:15vh;
}  

Finally to remove this stlying on mobile I added the following at lines 212 - 219

@media only screen and (max-width: 750px){

header{
  height:inherit;
}  

.lorem{       
  height:inherit;
    }  
  ....  
}

I noticed that at some viewport heights elemlents in your header are too high to fit in the 85% you are setting, so you may have to fine tune that.

Hope this helps

David Taiaroa
  • 25,157
  • 7
  • 62
  • 50