These two solutions require only two nested elements.
First one . Relative and absolute positioning if the content is static (center manually).
.black { position:relative; min-height:500px; background:rgba(0,0,0,.5); } .message { position:absolute; margin: 0 auto; width: 180px; top: 45%; bottom:45%; left: 0%; right: 0%; }
https://jsfiddle.net/GlupiJas/5mv3j171/
or for fluid design β to accurately use the content center below:
.message { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
https://jsfiddle.net/GlupiJas/w3jnjuv0/
You need to set the min-height if the content exceeds 50% of the window height. You can also manipulate this media request stream for mobile and tablet devices. But only if you play with a responsive design.
I gues. You can go further and use a simple JavaScript / JQuery script to control the minimum height or fixed height, if there is any need.
Seckound - If the content is fluid , you can also use the css and table-cell cls properties with vertical alignment and text alignment:
display:table;
and
display:table-cell; vertical-align: middle; text-align: center;
It works and scales perfectly, it is often used as a flexible web design solution using grid layouts and media queries that manipulate the width of the object.
.black { display:table; height:500px; width:100%; background:rgba(0,0,0,.5); } .message { display:table-cell; vertical-align: middle; text-align: center; }
https://jsfiddle.net/GlupiJas/4daf2v36/
I prefer a desktop solution to accurately center the content, but in some cases, relative absolute positioning will do the job better, especially if we donβt want to keep the exact proportion of the content.