Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text hidden behind CSS border shapes

I am trying to create a h1 style that is surrounding the text with a background color.

    h1.skew {
        padding-left: 10px;
        text-decoration: none;
        color: white;
        font-weight: bold;
        display: inline-block;
        border-right: 50px solid transparent;
        border-top: 50px solid #4c4c4c;
        height: 0;
        line-height: 50px;
    }
    <h1 class="skew">HELLO WORLD</h1>

https://jsfiddle.net/zo32q98n/

At this point the text appears beneath the background. How can I make the text appear inside the brown colored background?

like image 711
Fjott Avatar asked Jan 26 '17 13:01

Fjott


2 Answers

You can use pseudo-element for background and set z-index: -1 so it appears under the text.

h1.skew {
  padding-left: 10px;
  text-decoration: none;
  color: white;
  font-weight: bold;
  display: inline-block;
  position: relative;
  height: 0;
  line-height: 50px;
}
h1.skew:before {
  content: '';
  z-index: -1;
  border-right: 50px solid transparent;
  border-top: 50px solid #4c4c4c;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}
<h1 class="skew">HELLO WORLD</h1>
like image 179
Nenad Vracar Avatar answered Sep 28 '22 18:09

Nenad Vracar


Since the border is 50px tall, you can insert a negative margin of the same amount inside:

h1.skew::before {
  content: '';
  display: block;
  margin-top: -50px;
}

body {
  background: #ddd;
}
h1.skew {
  padding-left: 10px;
  text-decoration: none;
  color: white;
  font-weight: bold;
  display: inline-block;
  border-right: 50px solid transparent;
  border-top: 50px solid #4c4c4c;
  height: 0;
  line-height: 50px;
}
h1.skew::before {
  content: '';
  display: block;
  margin-top: -50px;
}
<h1 class="skew">HELLO WORLD</h1>
like image 41
Oriol Avatar answered Sep 28 '22 17:09

Oriol