whatamidoingwithmylife
whatamidoingwithmylife

Reputation: 1186

CSS Quickie on positioning

I would love to know how to make div or any element for that matter appear on a new line if it's width is over max of the parent's width.

For example let's say I have one parent div and three child divs, let's say parent's width is 300px and each child has specified width of 33% (so each child has width of around 100px), and parent has display: inline-flex, now if I add 4th child with width of 33% I want it to appear on a new line rather than all 4 of the child divs getting resized.

So basically (let's say letter A represents each child div) if I add 4th child div I want them to be displayed like this:

A A A

A

instead of like this:

A A A A

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="parent">
        <div class="child"></div>
        <div class="child"></div>
        <div class="child"></div>
        <div class="child"></div>
    </div>
</body>
</html>

CSS:

.parent {
    height: 100px;
    width: 400px;
    border: 1px solid red;
    display: inline-flex;
}

.child {
    width: 33%;
    border: 1px solid black;
    height: 30px;
}

Upvotes: 0

Views: 53

Answers (3)

add to parent flex-wrap without that property you will not be able to jump of line.

PD: You are going to have a problem with the children. they will have a margin of 1px that will be very difficult to remove by CSS. I solve it by removing the blanks of the children by HTML

    .parent {
        height: 100px;
        width: 400px;
        border: 1px solid red;
        display: inline-flex;
        /* wrap line */
        flex-wrap: wrap;
        position:relative;
    }
    .child {
       width: 33.3%;
       border: 1px solid black;
       height: 30px;
       box-sizing: border-box;
}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div class="parent">
            <div class="child">
            </div><div class="child">
            </div><div class="child">
            </div><div class="child">
            </div><div class="child">
            </div><div class="child">
            </div><div class="child">
            </div><div class="child"></div>
        </div>
    </body>
    </html>

Upvotes: 1

Johnsackson
Johnsackson

Reputation: 449

The Best practice is to have display:flex to the parent and have flex elements inside. So I suggest

CSS:

.parent {
    height: 100px;
    width: 400px;
    border: 1px solid red;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
}

.child {
    -ms-flex: 0 0 33.333333%;
    flex: 0 0 33.333333%;
    max-width: 33.333333%;
    border: 1px solid black;
}

HTML:

<div class="parent">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>

Upvotes: 1

Gautam Naik
Gautam Naik

Reputation: 9358

Add flex-wrap:wrap to parent so that the children can go to next line, if width is not sufficient

.parent {
    height: 100px;
    width: 400px;
    border: 1px solid red;
    display: inline-flex;
    flex-wrap: wrap;
}

.child {
    width: 33%;
    border: 1px solid black;
    height: 30px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="parent">
        <div class="child"></div>
        <div class="child"></div>
        <div class="child"></div>
        <div class="child"></div>
    </div>
</body>
</html>

Upvotes: 1

Related Questions