Search code examples
htmlcsscss-shapescss-transforms

How to place text on a diagonal line?


Text on a diagonal line

Hey guyz, I guess you got my concept by seeing the above picture. I'm unable to place diagonal line behind the text and it should get masked by the content placed on it. I wanted it in pure css. Background should be visible through the text.


Solution

  • You can use a rotated pseudo element. Make it 1px wide and make the lines with top/bottom borders :

    body {
      padding: 0;
      margin: 0;
      background-image: url('https://farm7.staticflickr.com/6083/6055581292_d94c2d90e3.jpg');
      background-size: cover;
    }
    
    div {
      position: relative;
      width: 150px;
      margin: 130px auto;
      padding: 10px 0;
    }
    
    div:before {
      content: '';
      position: absolute;
      top: -120px;
      left: 50%;
      width: 1px;
      height: 100%;
      border-top: 120px solid #000;
      border-bottom: 120px solid #000;
      -webkit-transform: rotate(8deg);
      -ms-transform: rotate(8deg);
      transform: rotate(8deg);
    }
    <div>
      <h1>Title</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec luctus condimentum mi sit amet iaculis. Aliquam erat volutpat. Maecenas eleifend commodo rutrum.</p>
    </div>