How to put a <p> on the same line as <hr>

Asked

Viewed 1,081 times

4

It is possible to place the element <hr> in front of an element <p>?

 hr{
   border-top: 1px solid red;
}
<p>Colocar a tag <b>hr</b> em frente do <b>p</b> </p><hr>

I’ve tried to use display:inline in <p> and in <hr>

inserir a descrição da imagem aqui

  • 1

    Could you describe what the expected result would be? If possible, add an image illustrating what you need. By the way, in the title there is twice the tag <hr>.

  • @woss I put, thank you

  • 1

    Amadeu, what should happen if the <p> have more than one line?

  • @Woss in this case will be something fixed

3 answers

7


I’ll leave an example that will work for all cases, with any sentence size (as many words as you want), or with any font-size, or even in a <h1> or <p>

Notice I put the p and the hr within a container with display:flex, this will leave the elements side by side horizontal, then I put flex:1 in the hr, this will make it grow and occupy only the remaining space of the width of the container.

.meu-hr {
  display: flex;
  align-items: center;
}
.meu-hr hr {
  flex:1;
  border-color: red;
  margin-left: .5em;
}
<div class="meu-hr">
  <p>123</p><hr>
</div>

<div class="meu-hr">
  <h1>123456</h1><hr>
</div>

<div class="meu-hr">
  <p style="font-size:40px">123456789</p><hr>
</div>

3

I would use a container div with flexbox and apply flex: 1 in the hr for it to occupy the remaining width of the div where the text is not. And a margin-left to give a spacing. And instead of edge, would use height: 1px and the background color red. You also need to remove the standard borders of the hr, so that it looks like a red line 1 pixel high.

.paragrafo{
   display: flex;
   align-items: center;
}

.paragrafo hr{
   background: red;
   flex: 1;
   margin-left: 10px;
   height: 1px;
   border: none;
}
<div class="paragrafo">
   <p>Colocar a tag <b>hr</b> em frente do <b>p</b></p><hr>
</div>

1

 hr{
   margin-top:-25px;
   margin-left:213px;
   border-top: 1px solid red;
   }
<p>Colocar a tag <b>hr</b> em frente do <b>p</b> </p><hr>

  • 1

    what the problem is in the answer?

  • That’s right for me, but it would be interesting to know why you negatively

  • 3

    Well, I wasn’t even negative, but if he changes the <p> for <h1>, or simply change the font size. Or even if you put one more word in the sentence your answer no longer serves. It is virtually unsustainable because it uses fixed ditches, and qq change even if a font-size will already make it stop working... The user will have to make a CSS class for each different phrase they will use. It may even solve the author’s problem, but it is far from being an indicated way

  • What would be the right way?

  • @Amadeuantunes: I gave up a vote because I believe that static design solutions are also valid just don’t be the most flexible in multi-device times. But the way no longer correct, but more appropriate would be to manipulate the rendering layout with the CSS property display

Browser other questions tagged

You are not signed in. Login or sign up in order to post.