I'm looking to have a <span> inside a <p> be underlined and fill the remainder space of the <p> tag

Viewed 54

I'd like the underline of the <span class="line-sm"></span> to fill in the remainder of the <p> space and have a border-bottom.

Here's a mockup, with the red line being what I want the span to be:

Mockup of what I'm looking for

If the window shrinks, the span will also shrink. Sort of like "contain" for image backgrounds.

Here is the HTML I have:

  <p>
    <strong>What ACT score do you want? <span class="line-sm"></span></strong> 
  </p>

Any ideas if this is possible without the use of tables or some crazy workaround?

Thanks!

3 Answers

You can use flexbox to allow the span to grow to fit its parent. I'd recommend closing the strong tag before the span in this case.

p {
  display: flex;
}

p .line-sm {
  flex: 1;
  margin-left: 10px;
  border-bottom: 1px solid red;
}
<p>
  <strong>What ACT score do you want?</strong>
  <span class="line-sm"></span>
</p>


If you want to ensure the span doesn't collapse so far that it disappears, you could add a min-width to it:

p {
  display: flex;
}

p .line-sm {
  flex: 1;
  min-width: 200px;
  margin-left: 10px;
  border-bottom: 1px solid red;
}
<p>
  <strong>What ACT score do you want?</strong>
  <span class="line-sm"></span>
</p>

In case you don`t want to use flexbox, try adding some pseudo elements:

CSS:

.line {
  position: relative;
  display: block;    
}

.line::before {
  display: block;
  position: absolute;
  width: 100%;
  bottom: 0;
  border-bottom: solid red 2px;
  content: '';
  z-index: 0;
}

.text {
  position: relative;
  display: inline-block;
  background: white;
  /* make some space with padding after text label */
  padding-right: 5px;
  z-index: 1;
}

HTML:

<p>
  <span class="line">
    <span class="text">
      <strong>What ACT score do you want?</strong>
    </span>
  </span>
  <span class="line">
    <span class="text">
      Another line
    </span>
  </span>
</p>

See codepen example

You can use the :after pseudo-class to display the line after your content.

span.line-sm:after {
  margin: 0 0 0 10px;
  background-color: #000;
  content: "";
  display: inline-block;
  height: 1px;
  position: relative;
  vertical-align: middle;
  width: 50%;
}
<p>
  <strong>What ACT score do you want? <span class="line-sm"></span></strong>
</p>

Related