1

Lets say I have this simple html page:

<div class="main">
    <div class="header">
        <h1>HEADER</h1>
    </div>
    <div class="content">
        <div class="box">
        </div>
    </div>
</div>

My header is fixed and the content should be beneath it and with height 100% of what ever left of the body.
I've already done that with this style:

*{
    margin-left: 0;
    margin-top: 0;
}

body,
html {
    height: 100%;
    width: 100%;
    margin: 0;
}

.header {
    background-color: red;
    text-align: center;
    position: fixed;
    width: 100%;
}

.content {
    background-color: antiquewhite;
    padding-top: 38px;
}

h1 {
    margin-bottom: 0;
}

.main {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.content {
    flex: 1;
}

.box {
    width: 150px;
    height: 150px;
    background-color: yellow;
}

Here's how the page looks for now: https://elbargho.github.io/sudoku/centerdiv.html
now I'm trying to center the box div horizontally and vertically in relative to the full body - the header size
what I've tried to do:

  1. margin-top: 50% - for some reason the box went all the way down to the bottom
  2. setting the position of content div to relative, and of box div to absolute - the content div overlapped the fixed header

4 Answers4

1

You can set content class as

.content {
    /* flex: 1; */
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
}

*{
    margin-left: 0;
    margin-top: 0;
}

body,
html {
    height: 100%;
    width: 100%;
    margin: 0;
}

.header {
    background-color: red;
    text-align: center;
    position: fixed;
    width: 100%;
}

.content {
    background-color: antiquewhite;
    padding-top: 38px;
}

h1 {
    margin-bottom: 0;
}

.main {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.content {
    /*flex: 1; */
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
}

.box {
    width: 150px;
    height: 150px;
    background-color: yellow;
}
<div class="main">
    <div class="header">
        <h1>HEADER</h1>
    </div>
    <div class="content">
        <div class="box">
        </div>
    </div>
</div>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
1

This is probably what you need. Documented in the code.

* {
  margin-left: 0;
  margin-top: 0;
}

body,
html {
  height: 100%;
  width: 100%;
  margin: 0;
}

/* Modified */
.header {
  background-color: red;
  text-align: center;
  /* position: fixed; */
  position: sticky;
  width: 100%;
}

.content {
  background-color: antiquewhite;
  padding-top: 38px;
}

h1 {
  margin-bottom: 0;
}

/* Modified */
.main {
  display: flex;
  flex-direction: column;
  height: 100vh;
  align-items: center;
}

/* Modified */
.content {
  /*flex: 1;*/
  display: flex;
  align-items: center;
  height: inherit;
}

.box {
  width: 150px;
  height: 150px;
  background-color: yellow;
}
<div class="main">
  <div class="header">
    <h1>HEADER</h1>
  </div>
  <div class="content">
    <div class="box">
    </div>
  </div>
</div>
Gerard
  • 15,418
  • 5
  • 30
  • 52
0

Here solution:

.content {
    display: flex;
    flex: 1;
    justify-content: center;
    align-items: center;
}
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
0

One way is to use CSS Transform.

.box {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  /* horizontal center */
  margin: 0 auto; 
}

Check out this website for all CSS centering help: http://howtocenterincss.com/

Sudesh Ryan
  • 13
  • 1
  • 3