html - margin top with percentage doesn't match the parent div -


basically i'm trying position icon in center of inner div. in code have div inside div inside div. center text in innermost div tried using margin-top: 50% instead, text went further past halfway point. thought innermost div find halfway point in parent div , position accordingly. please explain why wrong. thx

    <html> <body> <div id="container" style="height:1000px;width:100%;background-color: green;position:absolute;">      <div id="inner" style="height:50%; width:50%; background-color: black;margin-top:50%;display:inline-block;">         <div id="inner" style="height:50%; width:50%; background-color: yellow"></div>     </div> </div> </body> 

margin-top:50% means top border of element @ vertical half, doesn't center element.

to center element it's best use relative positions , display:block elements, top:50%, left: 50% hich moves upper left corner center of parent, , in addition transform:translate(-50%, -50%), moves (up , left) half of own height , width.

in addition parent element has have defined height (which in case true). here fiddle complete code:

https://jsfiddle.net/49lvzszj/1/


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -