Vertically aligning CSS :before and :after content
Vertically aligning CSS :before and :after content
16824-Apr-2023
Updated on 25-Apr-2023
Home / DeveloperSection / Forums / Vertically aligning CSS :before and :after content
Vertically aligning CSS :before and :after content
Aryan Kumar
25-Apr-2023To vertically align :before and :after pseudo-elements, you can use the vertical-align property. Here's an example:
css:
In this example, we're using the :before and :after pseudo-elements to add some content before and after the div element. We're setting the content property to some text and the display property to inline-block so that the pseudo-elements behave like inline elements and can be vertically aligned using vertical-align.
By setting vertical-align: middle;, we can center the pseudo-elements vertically within the div element. You can adjust the value of vertical-align to achieve the desired vertical alignment. Other possible values for vertical-align include top, bottom, baseline, and numeric values such as 50%.
Note that vertical-align only works on inline-level and table-cell elements, so you may need to adjust the display property of the element to make it work properly.