Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS how to vertically align text within a pseudo element

I have created a pseudo element to sit over an unordered list, the css is as follows:

ul.pricing-column {
  width: 200px;
  height: 400px;
  background: red;
  position: relative;
  margin: 50px;
}

.pricing-column.featured::after {
  content: "Most Popular";
  background: #52BDE6 none repeat scroll 0% 0%;
  color: #FFF;
  width: 80px;
  border-radius: 100%;
  position: absolute;
  top: -10px;
  left: -10px;
  z-index: 1000;
  text-transform: uppercase;
  font-weight: 700;
  font-size: 0.9em;
  line-height: 0.9em;
  padding: 10px;
  display: inline-block;
  height: 80px;
  text-align: center;
}
<ul class="pricing-column featured">
</ul>

However, with this, the text inside the pseudo-element sits at the top of my element - is there a way to center it vertically?

Here is a fiddle

like image 849
Sam Skirrow Avatar asked Oct 15 '15 16:10

Sam Skirrow


People also ask

How do you vertically align text in an element?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .

How do you align pseudo elements?

You need to position the :before pseudo-element absolutely by using the well known centering technique (use the top, left, and transform properties). Here, -25px will offset the text above the circle. Note that for the <span> element, we set the position property to its "relative" value and the display to "block".


3 Answers

Easiest way? Add padding top. Little more difficult but better way, use flexbox.

These properties will do

display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
text-align: center;

http://jsfiddle.net/6dqxt2r3/4/

like image 109
Steyn Avatar answered Oct 22 '22 11:10

Steyn


If you want to center it verticaly use top: calc(50% - 40px); 40px is half of the element

Updated fiddle

EDIT:

Sorry, update use display: inline-flex; and align-items: center;

Fiddle

like image 34
user4675957 Avatar answered Oct 22 '22 11:10

user4675957


As long as you can change how you position the pseudo element then this will work.

Change position: absolute; to position: relative; and adjust the top and left values accordingly.

To center the text apply display: table-cell;, text-align: center; and vertical-align: middle.

.pricing-column.featured::after {
    content: "Most Popular";
    background: #52BDE6 none repeat scroll 0% 0%;
    color: #FFF;
    width: 80px;
    border-radius: 100%;
    position: relative;
    top: -35px;
    left: -90px;
    z-index: 1000;
    text-transform: uppercase;
    font-weight: 700;
    font-size: 0.9em;
    line-height: 0.9em;
    padding: 10px;
    display: table-cell;
    height: 80px;
    text-align: center;
    vertical-align: middle;
}

http://jsfiddle.net/hungerstar/6dqxt2r3/5/

like image 45
hungerstar Avatar answered Oct 22 '22 10:10

hungerstar