4

I made a gradient background and I want to centralize this block of text. My objetive is to create a header that centralizes in the middle of the screen no matter the resolution of the viewport.

So I made this header an absolute position and used this centralization method I found online. It centralized perfectly, the problem is, the gradient background turns white (looks like the header is above the background on the body, I don't know). I've already tried using position fixed, but the problem persists, other types of position don't centralize.

* {
  margin: 0px;
  padding: 0px;
}

body {
  background: linear-gradient(20deg, #B7B0F6, #B1D5F9);
}

header {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%)
}
<header>
  <h1>Hello!</h1>
</header>

You can run the code here: https://jsfiddle.net/Jhugorn/dsknqp7x/1/ If you take out the header in the CSS, the background appears just fine. How can I make the background appear and centralize this element at the same time?

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Vitor Lima
  • 73
  • 6

1 Answers1

2

Add some height to the body to see the background:

* {
  margin: 0px;
  padding: 0px;
}

body {
  background: linear-gradient(20deg, #B7B0F6, #B1D5F9);
  min-height: 100vh;
}

header {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);
}
<header>
  <h1>Hello!</h1>
</header>

Your body contain no in-flow element so its height is equal to 0 (same thing for the html height) and this will make the background with a size of 0 thus you will see nothing.

You are not obliged to give a height of 100vh. Even a small padding can be enough due to background propagation. The visual won't be exactly the same but you will hardly notice this in this case.

* {
  margin: 0px;
  padding: 0px;
}

body {
  background: linear-gradient(20deg, #B7B0F6, #B1D5F9);
  padding:5px;
}

header {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);
}
<header>
  <h1>Hello!</h1>
</header>

A small padding on the html too is fine:

* {
  margin: 0px;
  padding: 0px;
}

body {
  background: linear-gradient(20deg, #B7B0F6, #B1D5F9);

}
html {
  padding:2px;
}

header {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);
}
<header>
  <h1>Hello!</h1>
</header>

A big padding will make things look different!

* {
  margin: 0px;
  padding: 0px;
}

body {
  background: linear-gradient(20deg, #B7B0F6, #B1D5F9);

}
html {
  padding:40px;
}

header {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%);
}
<header>
  <h1>Hello!</h1>
</header>

You can check this answer to better understand how the propagation is done and how it works with gradient.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • And that explanation about the element without inflow height gave me a lot of insight on how CSS understands elements, this helped me very much, thank you! – Vitor Lima Feb 16 '19 at 23:29