Elements with a height equal to their width with just html/css -


i had idea making elements high wide css , html. requirement text inside aligned in center, horizontally , vertically across multiple lines.

so based on other peoples thoughts on ( http://www.mademyday.de/css-height-equals-width-with-pure-css.html )

i made following html:

<span class='box'>  <span class='height'></span>  <span class='content'>this longer text demonstrate multiple lines being centered.</span> </span> 

accompanied following css:

.box {     /* width */     width: 25%;      display: table; } .height {     display: table-cell;      /* height item become %-age of width of .box, 1:1 ratio */     padding-top: 100%; } .content {     display: table-cell;      vertical-align: middle;     text-align: center; } 

here's fiddle: http://jsfiddle.net/cjxb7/.

this seems work fine, however, when resizing browser ( longer tekst, browser big small ) elements don't keep ratio.

i've tried in major browsers. , seem have caveat.

what missing? have way tables work?

please let me know if need more go on. thanks!

try this:

@import url(http://fonts.googleapis.com/css?family=open+sans:400, 700); body {     font-family:'open sans', sans-serif;     font-size: 1.2em;     font-weight: 700; } .box {     width: 25%;     height: 0;     position: relative;     padding-bottom: 25%;     overflow: hidden;     float: left;     background-color: #9c9;     color: #fff;     text-shadow: 1px 1px #666;     box-shadow: 0px 0px 1px 1px #666;     border: 2px solid;     margin: 1%;     position: relative; } .box.ratio2_1 {     padding-bottom: 12.5%; } .box > div {     transform: translate(0%, -50%);     -webkit-transform: translate(0%, -50%);     -moz-transform: translate(0%, -50%);     -o-transform: translate(0%, -50%);     -ms-transform: translate(0%, -50%);     top: 50%;     position: absolute;     text-align: center;     width: 100%; } 

working fiddle

source


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -