"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Achieve Fluid Width and Even Spacing for Four DIVs in a Container?

How to Achieve Fluid Width and Even Spacing for Four DIVs in a Container?

Posted on 2025-02-06
Browse:223

How to Achieve Fluid Width and Even Spacing for Four DIVs in a Container?

Fluid Width with Equally Spaced DIVs

Question:

You have a fluid width container DIV with four DIVs, each with fixed dimensions. How can you float box 1 to the left, box 4 to the right, and space boxes 2 and 3 evenly between them, maintaining fluid spacing as the browser window size changes?

**Answer:

Refer to the provided JSFiddle for a complete example: http://jsfiddle.net/thirtydot/EDp8R/

Explanation:

This solution utilizes several techniques to achieve the desired layout:

  • text-align: justify;: This aligns the DIVs horizontally, distributing space between them.
  • .stretch: A span with width set to 100%. It expands to fill the remaining width of the container.
  • display:inline-block; *display:inline; zoom:1;: Ensures inline-block behavior for IE6/7.
  • font-size: 0; line-height: 0;: Prevents vertical space caused by inline-block in IE6.

By combining these techniques, the DIVs are positioned fluidly, evenly spaced and floating as desired, regardless of the container width.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3