[CSS] How to center text vertically
Author | Message |
---|---|
Ordo | # Posted two hours ago |
Hello. I have a text contained within a <div> that I want to center both horizontally, which I managed to do with text-align: center; , and also vertically something that I haven't managed to do yet.I tried using padding with a percentage but the problem is that the text doesn't stay centered when the amount of text changes.Is there any equivalent to text-align for vertically centering text inside a <div> ?Thanks ! |
|
Ads | |
Rel0ad | # Posted one hour ago |
Sigh... http://www.siteraw.com/html-css/how-to-create-a-website/flexbox-layouts | |
Phoenix | # Posted 37 minutes ago |
There is more than one way to align an element on the vertical axis.Method 1 - FlexboxAs Rel0ad said, it is covered in depths in the SiteRaw tutorial. .container { Method 2 - Transform & translateAlternatively, you could also use this method which will work on most browsers. .container { Method 3 - Table cellIn some cases, you will need to ensure that the html / body element's height is set to 100%. .parent { Method 4 - Absolute positioningThis approach assumes that the text has a known height (20 pixels in the demo) so I only recommend it as a last resort. We use a negative margin to vertically center our content. .container > p { Method 5 - Line heightThis is the least flexible method, for educational purposes only. .container { |
|
Ordo | # Posted 5 minutes ago |
Thanks, it works as intended. I'll remember the flexbox model from now on. |