Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS <hr> right aligned next to text

I need a h2 that has a heavy stroke to the right of it. Like so:

I'm struggling with the best, responsive way to accomplish it. Not to mention that it's in a custom WP theme, so I don't want to create a ton of on page markup that the client will break immediately :)

like image 676
bradmagnus Avatar asked Oct 25 '13 06:10

bradmagnus


People also ask

Can you style an HR tag in CSS?

In older HTML specifications the HR tag was just a horizontal rule and did not provide the semantic meaning it does now. Today it does not provide a visible break, but should be styled using CSS. This gives more control to the designer to make the HR tag match the site's theme.

How do I align the right HR?

The alignment of the horizontal rule can be specified by applying the text-align property to the HR element. The alignment of the horizontal rule can be specified by applying the margin property to the HR element.


1 Answers

What you need is a single element and an :after pseudo. P.S It's responsive.

Demo

Explanation: Here, the main part is to use overflow: hidden; on the element, and than am creating a virtual element using an :after pseudo with content property, and am positioning it absolute to the parent element which am setting to relative

<h2>Hello World</h2>

h2 {
  font-size: 20px;
  font-family: Arial;
  position: relative;
  overflow: hidden;
}


h2:after {
  display: inline-block;
  content: "";
  height: 4px;
  background: #f00;
  position: absolute;
  width: 100%;
  top: 50%;
  margin-top: -2px;
  margin-left: 10px;
}
like image 167
Mr. Alien Avatar answered Sep 27 '22 18:09

Mr. Alien