html - CSS - how to make DIV with wrapped floats inside only be as large as it needs to be to hold the floats? -



html - CSS - how to make DIV with wrapped floats inside only be as large as it needs to be to hold the floats? -

i have several floats , div around them. div within div, , supposed horizontally centered within it. thing inner div not fixed-width, , can't be. here code:

div.outer { text-align: center; } div.inner { display: inline-block; width: auto; } div.floatdiv { display: block; float: left; width: 270px; height: 400px; margin: 5px; background-color: gray; text-align: center; } div.clearboth { clear: both; }

and html:

<?xml version="1.0" encoding="utf-8"?> <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <link href="main.css" rel="stylesheet" type="text/css" static="screen" /> </head> <body> <div class="outer"> <div class="inner"> <div class="floatdiv"> text. </div> <div class="floatdiv"> text. </div> <div class="floatdiv"> text. </div> <div class="floatdiv"> text. </div> <div class="clearboth"> </div> </div> </div> </body>

this works provided browser window wide plenty floats remain in 1 line. if window narrowed, lastly of floats jumps next line of floats - "inner" div becomes wide containing block "outer" , inner not centered in outer anymore.

http://jsfiddle.net/byszs/

i can't think of improve suggestion here, utilize media queries.

here's implementation of code: http://jsfiddle.net/thirtydot/byszs/4/

@media (max-width: 1123px) { div.inner { max-width: 843px; } } @media (max-width: 843px) { div.inner { max-width: 563px; } } @media (max-width: 563px) { div.inner { max-width: 280px; } }

it's cumbersome , doesn't (automatically) back upwards more elements, works!

well, works in browsers back upwards media queries.

if need back upwards other browsers, suggest using lightweight respond.js. although then, said might utilize javascript (which have advantage of supporting number of elements automatically) instead of media queries.

html css

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -