css - How to automatically center a div element that is inside another div element -
let's suppose following use case: working example plese @ link
the html code:
<div class="container"> <div class="centerelem"> width of div variable </div> </div>
the css style:
.container { width: 500px; /*this can change */ } .container .centerelem{ margin-right: auto /*unfortunately, works if set width*/ margin-left: auto /*unfortunately, works if set width*/ }
right works this:
||the width of div variable| |
and make working this, without knowing width of inner div element:
| |the width of div variable| |
it's bit annoying because code in question doesn't match code in jsfiddle,.
use display: inline-block
on .subcon
, combined text-align: center
on .container
, have.
like so: http://jsfiddle.net/thirtydot/euytq/66/
or code in question: http://jsfiddle.net/thirtydot/euytq/67/
Comments
Post a Comment