Vertical alignment of div
Posted By : Shivam Gupta | 01-Jun-2016
In this blog I want to discuss that how to align any div vertically centered, then there are different ways of doing this, which is given below -
1) Equal top -bottom padding: In this way at first we get outer div height, then inner div height. After then the top-bottom padding would be (outer-div-height - inner-div- height/2).
HTML: -
<div id="outer"> <div id="inner"></div> </div>
CSS: -
#outer{ width: 200px; height: 200px; } #inner{ width: 50px; height: 50px; padding: 75px 0px 75px 0px; }
<div id="outer"> <div id="inner"></div> </div>
#inner{ width: 50px; height: 50px; position: relative; top: 50%; transform: translateY(-50%); }
<div id="outer"> <div id="inner"></div> </div>
#outer{ height: 300px; width: 200px; display: flex; justify-content: center; flex-direction: column; resize: vertical; overflow: auto; } #inner { width: 30px; height: 30px; background: black; color: white; resize: vertical; overflow: auto; }
<div id="outer"> <div id="inner"></div> </div>
#outer {display: table;} #inner { display: table-cell; vertical-align: middle; }
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shivam Gupta
Shivam is a versatile UI developer with html, css, scss, js, angularjs technologies. He likes chess, cricket and reading book.