dev-vsg
dev-vsg

Reputation: 97

Build line-through style with flexbox and pseudo elements

I'm building a line-through header that can span multiple lines. Using the sample code below, is it possible to write my CSS in such a way that the left and right divs are not needed? Where they could be added as pseudo-classes to my header class?

CodePen

.container {
  box-sizing: border-box;
  display: flex;
  place-content: center space-evenly;
  align-items: center;
}

.line {
  flex: 1;
  height: 2px;
  background: black;
}

.header {
  font-size: 50px;
  margin: 0 30px;
  text-align: center;
}

.header-broken:after {
  content: '';
  display: -webkit-inline-flex;
  display: -ms-inline-flexbox;
  display: inline-flex;
  width: 50px;
  height: 5px;
  flex: auto;
  width: 100%;
  height: 2px;
  background: black;
}
<div class="container">
  <div class="line"></div>
  <div class="header">Normal Title<br>fdasfsaf</div>
  <div class="line"></div>
</div>

Upvotes: 4

Views: 571

Answers (2)

Janki Gandhi
Janki Gandhi

Reputation: 349

You can also try this.

HTML:

<div class="container">
  <div class="header">
     <h1>Normal Title
        <br>fdasfsaf
     </h1>
  </div>
</div>

CSS:

.container {
        display: flex;
        text-align: center;
    }

    .header {
        flex: 1;
    }

    .header h1 {
        font-size: 50px;
        margin: 0 30px;
        text-align: center;
        background-color: #fff;
        display: inline-block;
    }

    .header:after {
        content: '';
        border-bottom: 1px solid #000;
        display: block;
        margin-top: -58px;
    }

Upvotes: 0

Stickers
Stickers

Reputation: 78706

It can be done with just one div, see the example below, add some margin to the pseudo elements as needed for spacing.

.container {
  display: flex;
  text-align: center;
}

.container:before,
.container:after {
  content: "";
  flex: 1;
  background: linear-gradient(black, black) center / 100% 1px no-repeat;
}
<div class="container">
  Normal Title<br>fdasfsaf
</div>

Upvotes: 1

Related Questions