Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically centering content of :before/:after pseudo-elements

I'm trying to achieve something similar to this picture:

enter image description here

I have an image (as part of a slideshow) wrapped in a div, and with :before and :after pseudo-elements, I display two controls to move onto the next (>>) or previous (<<) images of the slideshow.

So far, I have this:

div {   position: relative; }  div:before {   display:block;   height: 100%;   content: "stuff";   position:absolute;   top: 0; left: 0;   text-align: center; } 

I can't, however, center the content of the pseudo-elements, the text appears like this:

enter image description here

Is this possible to achieve? If not, what would be the most semantic workaround? I do not want to center the element itself, only its content. I'd prefer to have the element stretched to 100% height.

Edit: http://jsfiddle.net/rdy4u/

Edit2: Also, the img is liquid/fluid, the height of the div/img are unknown, and the width is set to 800px and max-width to 80%.

like image 204
dcastro Avatar asked Jan 25 '13 14:01

dcastro


People also ask

How do you center align a pseudo element?

Using margin:auto to center. As long as the element has a width declared you can use the absolute centering method. To use this method the right and left properties must be set to 0 for margin: auto to be effective. This method can be expanded to implement horizontal centering as well.

How do I center my vertical content?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do I center text vertically 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 I center HTML content vertically?

For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.


2 Answers

Assuming your element is not an <img> (because pseudo elements are not allowed on self-closing elements), let's suppose it's a <div>, so a way could be:

div {     height: 100px ;     line-height: 100px; } div:before, div:after {     content: "";     ...     display: inline-block;     vertical-align: middle;     height: ...;     line-height: normal; } 

If you cannot change the line-height of the div, another way is:

div {     position: relative; } div:before, div:after {     position: absolute;     display: block;     top: 50%;     -webkit-transform: translateY(-50%);     -moz-transform: translateY(-50%);     -ms-transform: translateY(-50%);     transform: translateY(-50%);     content: "";     width: ... } 

Otherwise, just place the indicators as a background in center position.

like image 183
Fabrizio Calderan Avatar answered Sep 24 '22 04:09

Fabrizio Calderan


Using flex in the pseudo element's css it is rather easy:

.parent::after {   content: "Pseudo child text";   position: absolute;   top: 0;   right: 0;   width: 30%;   height: 100%;   border: 1px solid red;   display:flex;   flex-direction:row;   align-items: center;   justify-content: center; } 

see https://jsfiddle.net/w408o7cq/

like image 39
david Avatar answered Sep 24 '22 04:09

david