Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line right after text

Tags:

html

css

I'd like to have a line that starts right after my text on the same line, I've tried with the following simple code

<html><body>My Text<hr/></body></html>

It seems that <hr> is not an option because it is always on a new line and I'd like the line to start at the right of my text.

Any help ?

like image 404
Nikko Avatar asked Feb 01 '11 09:02

Nikko


People also ask

How do I stop text from going to the next line?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).

How do you put a line after text in CSS?

A line-break can be added in HTML, using only CSS, by employing the pseudo-class ::after or ::before . In the stylesheet, we use these pseudo-classes, with the HTML class or id, before or after the place where we want to insert a line-break. In myClass::after : Set the content property to "\a" (the new-line character).


2 Answers

Using FlexBox Property this can be achieved easily.

.mytextdiv{
  display:flex;
  flex-direction:row;
   align-items: center;
}
.mytexttitle{
  flex-grow:0;
}

.divider{
  flex-grow:1;
  height: 1px;
  background-color: #9f9f9f;
}
<div class="mytextdiv">
  <div class="mytexttitle">
    My Text
  </div>
  <div class="divider"></div>
</div>
like image 108
Deepak Avatar answered Sep 23 '22 06:09

Deepak


The <hr> has default styling that puts it on a new line. However that default styling can be over-ridden, in the same way as it can for any other element. <hr> is in essence nothing more than an empty <div> with a default border setting.

To demonstrate this, try the following:

<div>Blah blah<hr style='display:inline-block; width:100px;' />dfgdfg</div>

There are a number of ways to override the styling of <hr> to acheive your aim.

You could try using display:inline-block; along with a width setting, as I have above. The down-side of this approach is that it requires you to know the width you want, though there are ways around this - width:100%;, and the whole line in a container <div> that has overflow:hidden; might do the trick, for example:

<div style='overflow:hidden; white-space:nowrap;'>Blah blah<hr style='display:inline-block; width:100%;' /></div>

Another option would be to use float:left;. You'd need to apply this to all the elements in the line, and I dislike this option as I find that float tends to cause more problems than it solves. But try it and see if it works for you.

There are various other combinations of styles you can try - give it a go and see what works.

like image 43
Spudley Avatar answered Sep 25 '22 06:09

Spudley