Search code examples
htmlcssflexboxcentering

Why is the title element taking up ALL the verticle space in the container?


Main title in the header is taking up ALL the vertical space and moving the h2 out of the center to the right which is supposed to be under it and not connected besides for being in the same container.

https://codepen.io/ychalfari/pen/JVYoNW

https://codepen.io/ychalfari/pen/mgVdLr

These are a couple of the things I have tried but even just having the title in a <p> tag is taking up all the vertical space.

div class ="header-wrap">
    <header>
      <div class="span-wrap">
        <span id="my">My</span> 
        <span id="journey">Journey</span> 
        <span id="of">of </span>
        <span id="learning">Learning</span></div>
      <h2>Documenting the Learning Process in a Fun Interactive way! </h2>
    </header></div>  

Here is the css

 header-wrap, header{

  display:flex;
  color:white;  
  background-color:red;
  height: 100vh;
  width:100vw;}

h2 {

  font-size:25px;
  letter-spacing:2px;
  font-family:'Raleway';
  align-self:flex-end;}

.span-wrap{

  display:flex;
  justify-content:center;}


#my{
  font-size:55px;
  position:relative;
  top:30px;
}
span{
  max-height: 65px;
  display:block;
}
#journey{
  top:80px;
  font-size:55px;
  position:relative;
}
#of{
   top:120px;
  font-size:45px;
  position:relative;
  margin: 0 35px;
  border: solid;
  padding: 1px 15px;
  max-height:60px;}
#learning {
   top:185px;
  font-size:55px;
  position:relative;  
}

What I expect is <h2>Documenting the Learning Process in a Fun Interactive way! </h2> to be centered at the bottom of the div unaffected by the "Journey of Learning" part.


Solution

  • You have to use a column flexbox here by adding flex-direction: column to your header, and subsequently:

    • add flex: 1 to the span-wrap (occupies the available free space after placing the h2 thereby pushing it to the bottom)
    • change to align-self: center for h2 element

    See demo below:

    html body {
      font-family: 'Raleway';
      margin: 0;
      padding: 0;
      min-height: 100%;
      width: 100%;
    }
    
    ul li {
      display: none;
    }
    
    header-wrap,
    header {
      display: flex;
      color: white;
      background-color: red;
      height: 100vh;
      width: 100vw;
      flex-direction:column; /* added */
    }
    
    h2 {
      font-size: 25px;
      letter-spacing: 2px;
      font-family: 'Raleway';
      align-self: center; /* changed */
    }
    
    .span-wrap {
      display: flex;
      justify-content: center;
      flex: 1; /* added */
    }
    
    #my {
      font-size: 55px;
      position: relative;
      top: 30px;
    }
    
    span {
      max-height: 65px;
      display: block;
    }
    
    #journey {
      top: 80px;
      font-size: 55px;
      position: relative;
    }
    
    #of {
      top: 120px;
      font-size: 45px;
      position: relative;
      margin: 0 35px;
      border: solid;
      padding: 1px 15px;
      max-height: 60px;
    }
    
    #learning {
      top: 185px;
      font-size: 55px;
      position: relative;
    }
    <div class="header-wrap">
      <header>
        <div class="span-wrap">
          <span id="my">My</span> <span id="journey">Journey</span> <span id="of">of </span><span id="learning">Learning</span></div>
        <h2>Documenting the Learning Process in a Fun Interactive way! </h2>
      </header>
    </div>
    <nav>
      <ul>
        <li>Arrays</li>
        <li>Objects</li>
        <li>Apps</li>
        <li>Design</li>
      </ul>
    </nav>
    <section>
    </section>