1

what is the proper code for this?

in div style code. I know how to use float but only 2 divides. But in 4 divides, I don't know.

4 Answers 4

4

Just float them all left and if necessary add a right margin of -1px so that the borders overlap nicely. Here's an SSCCE, just copy'n'paste'n'run it:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2684578</title>
        <style>
            .box {
                float: left;
                width: 100px;
                height: 100px;
                margin-right: -1px;
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        <div class="box">box1</div>
        <div class="box">box2</div>
        <div class="box">box3</div>
        <div class="box">box4</div>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

As long as body is wide enough to fit.
3

Floating will still work for any number of div's, they'll line up next to each other until they fill the width of the container, at which point they will start to wrap to the next line.

Comments

2

Just add float: left for every div.

Comments

0

Also, if you don't want your 4 divs to wrap to the next line when the window gets resized you can place your 4 divs inside a parent div and set the width of that parent div.

Here is an example based on BalusC's code above:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2684578</title>
        <style>
            .box {
                float: left;
                width: 100px;
                height: 100px;
                margin-right: -1px;
                border: 1px solid black;
            }
            .parent {
                width: 404px;
                height: 100px;
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <div class="box">box1</div>
            <div class="box">box2</div>
            <div class="box">box3</div>
            <div class="box">box4</div>
        </div>
    </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.