I have a css class defined so I can make a div to use all the browser's viewport, the rule is the following:
.fullscreenDiv { background-color: #e8e8e8; width: 100%; height: auto; bottom: 0px; top: 0px; left: 0; position: absolute; }
Now I want the text inside the div to be in the exact center of the screen so, vertical align center and horizontal align middle, but I can't seem to find the proper way to do so.
It only needs to work on webkit based browsers.
I already tried to add a P element inside with display
set to table-cell
(a common way of centering text) without luck.
Any suggestions?
If you want to center something horizontally in CSS you can do it just by, using the text-align: center; (when working with inline elements) or margin: 0 auto; (when working with block element).
To just center the text inside an element, use text-align: center; This text is centered.
To center text in CSS, use the text-align property and define it with the value “center.” Let's start with an easy example. Say you have a text-only web page and want to center all the text. Then you could use the CSS universal selector (*) or the type selector body to target every element on the page.
To move the inner div container to the centre of the parent div we have to use the margin property of style attribute. We can adjust the space around any HTML element by this margin property just by providing desired values to it. Now here comes the role of this property in adjusting the inner div.
The accepted answer works, but if:
use this:
.centered { position: fixed; /* or absolute */ top: 50%; left: 50%; /* bring your own prefixes */ transform: translate(-50%, -50%); }
More information about centering content in this excellent CSS-Tricks article.
.center{ height: 100vh; display: flex; justify-content: center; align-items: center; }
Another great guide about flexboxs from CSS Tricks; http://css-tricks.com/snippets/css/a-guide-to-flexbox/
The standard approach is to give the centered element fixed dimensions, and place it absolutely:
<div class='fullscreenDiv'> <div class="center">Hello World</div> </div> .center { position: absolute; width: 100px; height: 50px; top: 50%; left: 50%; margin-left: -50px; /* margin is -0.5 * dimension */ margin-top: -25px; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With