[SOLVED] Limit the bottom bar to the text and not the window width

Issue

This Content is from Stack Overflow. Question asked by Maman

I’ve this style for my titles on my website:

.heading-text>* {
  position: relative;
}

.heading-text h4 {
    font-size: 34px;
    margin-bottom: 30px;
    letter-spacing: -.7px;
    line-height: 40px;
}

.heading-text.heading-line h4:before {
    content: '';
    position: absolute;
    width: 100%;
    height: 15px;
    bottom: 5px;
    background-color: #fcf8e3;
    z-index: -1
}
<div class="heading-text heading-line">
  <h4>
    My super loooong title
  </h4>
</div>

<div class="heading-text heading-line">
  <h4>
    My short title
  </h4>
</div>

The issue I’m facing is the yellow bar is not fitting the text width.

Is there a way it can be adjust automatically ?

Thanks.



Solution

Try this:

.heading-text>* {
    position: relative;
}

.short-text {
    display: inline-block;
}

.heading-text h4 {
    font-size: 34px;
    margin-bottom: 30px;
    letter-spacing: -.7px;
    line-height: 40px;
}

.heading-text.heading-line h4:before {
    content: '';
    position: absolute;
    width: 100%;
    height: 15px;
    bottom: 5px;
    background-color: #fcf8e3;
    z-index: -1
}
<div class="heading-text heading-line">
  <h4>
    My super loooong title
  </h4>
</div>

<div class="heading-text heading-line">
  <h4 class="short-text">
    My short title
  </h4>
</div>


This Question was asked in StackOverflow by Maman and Answered by Rizeen It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?