开发者

Centering floated divs with fluid widths

开发者 https://www.devze.com 2023-03-27 01:57 出处:网络
I have the following divs on a page which contain products for sale: Container div. Centered on screen with 750px width.

I have the following divs on a page which contain products for sale:

Container div. Centered on screen with 750px width. Div with image (450px) floated left. Div with text (fluid width to allow for text overflow) floated left.

I would like the divs centered within the container, but cannot specify a pixel width for the text div as some pages have more text than others.

What's the best way to go abo开发者_运维技巧ut this?

Thanks Gary


In general, the procedure for centering block-level elements (like divs) is to set margin:0 auto, but you'll never get anything to center that has been floated. We need more information about the specific effect you're trying to achieve: do you need the entire div to center, or will just the contents of the div do? If the former, you'll need to set a hard width on the div, or it will expand to fill the entire width of its container. The only way to prevent this is with a float, which is out because you want it centered. If it's the latter, then you can just set text-align:center on your div and everything inside it will be centered.

0

精彩评论

暂无评论...
验证码 换一张
取 消