html - Element styled with CSS Flex box appears outside parent element and doesn't vertically align with parent -


i have <section> element title, contains <div> holds text. need <div> appear in middle of <section> tag, , <section> should take rest of space under header. user, <div> should appear in centre of space under header.

my following code degree, appears off-centre. think thats's because applied height: 100vh <section>, makes element longer rest of page.

how achieve this? i'm trying create generic set of styles div.message can drop in when needed , appear in centre of area below header.

header {}    .content {    height: 100vh;  }    .message {    height: 100%;    display: flex;    flex-direction: row;    flex-wrap: nowrap;    justify-content: center;    align-content: stretch;    align-items: center;  }    .message .text {    font-size: 20px;    order: 0;    flex: 0 1 auto;    align-self: auto;  }
<header>    <h1>header area</h1>  </header>  <section class="content">    <h2>this section</h2>    <div class="message">      <p class="text">this section empty</p>    </div>  </section>

jsfiddle

here how recommend do, , responsive layout:

  • add wrapper, container (could use body)
  • make container flex column container header , content stack vertically
  • set flex-grow: 1 on content if take remaining space of parent
  • make content flex column container
  • set flex-grow: 1 on message if take remaining space of parent
  • make message flex row container (the default)
  • set justify-content: center; align-items: center; on message content centers

finally, need take h2 out of flow or else message won't fill entire parent's height, , if not, message won't center vertically in section

note, h2 positioned absolute content set flex row container, though choose use "column" make move obvious compared markup structure

updated fiddle

stack snippet

html, body {    margin: 0;  }  .container {    display: flex;    flex-direction: column;    height: 100vh;  }  header {}  .content {    position: relative;    flex-grow: 1;    display: flex;    flex-direction: column;  }  .content h2 {    position: absolute;    top: 0;    left: 0;    width: 100%;    text-align: center;  }  .message {    flex-grow: 1;    display: flex;    justify-content: center;    align-items: center;  }    .message .text {    font-size: 20px;  }      /* styles demo */  html, body {    margin: 0;  }  .container {  }  header {    border: 1px dotted red;    }  .content {    border: 1px dotted red;  }  .message,  .message .text {    border: 1px dotted red;  }
<div class="container">      <header>      <h1>header area</h1>    </header>      <section class="content">      <h2>this section</h2>      <div class="message">        <p class="text">this section empty</p>      </div>    </section>    </div>


based on how intend use message, set justify-content: center; align-items: center; content (and drop flex properties on message)

fiddle demo 2

stack snippet

html, body {    margin: 0;  }  .container {    display: flex;    flex-direction: column;    height: 100vh;  }  header {}  .content {    position: relative;    flex-grow: 1;    display: flex;    flex-direction: column;    justify-content: center;    align-items: center;  }  .content h2 {    position: absolute;    top: 0;    left: 0;    width: 100%;    text-align: center;  }  .message {  }    .message .text {    font-size: 20px;  }      /* styles demo */  html, body {    margin: 0;  }  .container {  }  header {    border: 1px dotted red;    }  .content {    border: 1px dotted red;  }  .message,  .text {    border: 1px dotted red;  }
<div class="container">      <header>      <h1>header area</h1>    </header>      <section class="content">      <h2>this section</h2>      <div class="message">        <p class="text">this section empty</p>      </div>    </section>    </div>


if message wrapper p, drop together.

fiddle demo 3


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -