jenlee123
jenlee123

Reputation: 153

Why isn't flex-wrap wrapping equally?

I am trying to make a website well responsive (in snippet). It is not behaving as I would expect though. From some reason at the width from 600 to 900 (or ipad), the pictures form a weird shape. I don't know what is it caused by. What I would expect to happen is that the pictures would form 3 rows with 2 pictures each and one picture at the bottom. Any idea how to fix that?

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

.row {
  display: flex;
}

img[src$="jpg"] {
  width: 350px;
  height: 250px;
  display: block;
}

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 50px;
}

.column1 {
  margin: 20px;
  position: relative;
  height: 0%;
}

.column2 {
  margin: 20px;
  position: relative;
}

.text {
  margin-right: 60%;
}

.text h1 {
  font-family: muli, sans-serif;
  font-size: 3.5rem;
  font-weight: 100;
}

.column2 .cover,
.column1 .cover {
  bottom: 0px;
  position: absolute;
  opacity: 0.8;
  font-family: muli, sans-serif;
  font-weight: 500;
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
}

.dark {
  background-color: #0E4C92;
}

.light {
  background-color: #00B5FD;
}

.icon {
  position: absolute;
  right: -30px;
  top: 8%;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  box-shadow: 0 0 2px 1px gray;
}

@media (max-width: 960px) {
  .row {
    justify-content: center;
    flex-wrap: wrap;
  }
  .text h1 {
    left: 0px;
  }
  .icon {
    width: 60px;
    height: 60px;
    right: -30px
  }
  img[src$="jpg"] {
    width: 250px;
    height: 250px;
    display: block;
  }
  .text {
    margin-right: 0%;
  }
}
<div class="container">

  <div class="text">
    <h1>Solutions for<br> medical<br> applications</h1>
  </div>

  <div class="row1 row">

    <div class="column2">
      <img class="icon" src="Datový zdroj 2.svg" alt="My Happy SVG" />
      <a href="https://www.resinex.co.uk/contacts.html"><img src="Fluids.jpg" alt=""></img>
      </a>
      <div class="cover dark">
        <h2>Fluids</h2>
      </div>
    </div>
    <div class="column1">
      <img class="icon" src="Datový zdroj 5.svg" alt="My Happy SVG" />
      <a href="https://www.resinex.co.uk/contacts.html"><img src="medical.jpg" alt=""></a>
      <div class="cover dark">
        <h2>Medical device</h2>
      </div>
    </div>
    <div class="column2">
      <img class="icon" src="Datový zdroj 6.svg" alt="My Happy SVG" />
      <a href="https://www.resinex.co.uk/contacts.html"><img src="SurgicalTools.jpg" alt=""></a>
      <div class="cover dark">
        <h2>Surgical tools and instruments</h2>
      </div>
    </div>
  </div>

  <div class="row2 row">
    <div class="column1">
      <img class="icon" src="Datový zdroj 1.svg" alt="My Happy SVG" />
      <a href="https://www.resinex.co.uk/contacts.html"><img src="DrugDevice.jpg" alt=""></a>
      <div class="cover light">
        <h2>Drug delivery device</h2>
      </div>
    </div>
    <div class="column2">
      <img class="icon" src="Datový zdroj 1.svg" alt="My Happy SVG" />
      <a href="https://www.resinex.co.uk/contacts.html"><img src="Packaging.jpg" alt=""></a>
      <div class="cover light">
        <h2>Packaging</h2>
      </div>
    </div>
    <div class="column1">
      <img class="icon" src="Datový zdroj 3.svg" alt="My Happy SVG" />
      <a href="https://www.resinex.co.uk/contacts.html"><img src="HomeCare.jpg" alt=""></a>
      <div class="cover light">
        <h2>Home care</h2>
      </div>
    </div>

  </div>

  <div class="row3 row">
    <div class="column2">
      <img class="icon" src="Datový zdroj 4.svg" alt="My Happy SVG" />
      <a href="https://www.resinex.co.uk/contacts.html"><img src="Labware.jpg" alt=""></a>
      <div class="cover dark">
        <h2>Labware</h2>
      </div>
    </div>
  </div>

</div>

Upvotes: 1

Views: 48

Answers (3)

en0ndev
en0ndev

Reputation: 692

You can try this. I changed a few codes.

* {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
}

.row {
    display: flex;
    align-items: center;
}

.general-area {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
}

img[src$="jpg"] {
    width: 350px;
    height: 250px;
    display: block;
}

.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 50px;
    justify-content: center;
}

.column1 {
    margin: 20px;
    position: relative;
    height: 0%;
}

.column2 {
    margin: 20px;
    position: relative;
}

.text {
    margin-right: 60%;
}

.text h1 {
    font-family: muli, sans-serif;
    font-size: 3.5rem;
    font-weight: 100;
}

.column2 .cover,
.column1 .cover {
    bottom: 0px;
    position: absolute;
    opacity: 0.8;
    font-family: muli, sans-serif;
    font-weight: 500;
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
}

.dark {
    background-color: #0E4C92;
}

.light {
    background-color: #00B5FD;
}

.icon {
    position: absolute;
    right: -30px;
    top: 8%;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    box-shadow: 0 0 2px 1px gray;
}

@media (max-width: 960px) {
    .row {
        justify-content: center;
        flex-wrap: wrap;
        flex-basis: 50%;
    }
    .text h1 {
        left: 0px;
    }
    .icon {
        width: 60px;
        height: 60px;
        right: -30px
    }
    img[src$="jpg"] {
        width: 250px;
        height: 250px;
        display: block;
    }
    .text {
        margin-right: 0%;
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://use.typekit.net/dsl6sbt.css">
</head>

<body>
    <div class="container">

        <div class="text">
            <h1>Solutions for<br> medical<br> applications</h1>
        </div>

        <div class="general-area">
            <div class="column2">
                <img class="icon" src="Datový zdroj 2.svg" alt="My Happy SVG" />
                <a href="https://www.resinex.co.uk/contacts.html"><img src="Fluids.jpg" alt=""></img>
                </a>
                <div class="cover dark">
                    <h2>Fluids</h2>
                </div>
            </div>
            <div class="column1">
                <img class="icon" src="Datový zdroj 5.svg" alt="My Happy SVG" />
                <a href="https://www.resinex.co.uk/contacts.html"><img src="medical.jpg" alt=""></a>
                <div class="cover dark">
                    <h2>Medical device</h2>
                </div>
            </div>
            <div class="column2">
                <img class="icon" src="Datový zdroj 6.svg" alt="My Happy SVG" />
                <a href="https://www.resinex.co.uk/contacts.html"><img src="SurgicalTools.jpg" alt=""></a>
                <div class="cover dark">
                    <h2>Surgical tools and instruments</h2>
                </div>
            </div>

            <div class="column1">
                <img class="icon" src="Datový zdroj 1.svg" alt="My Happy SVG" />
                <a href="https://www.resinex.co.uk/contacts.html"><img src="DrugDevice.jpg" alt=""></a>
                <div class="cover light">
                    <h2>Drug delivery device</h2>
                </div>
            </div>
            <div class="column2">
                <img class="icon" src="Datový zdroj 1.svg" alt="My Happy SVG" />
                <a href="https://www.resinex.co.uk/contacts.html"><img src="Packaging.jpg" alt=""></a>
                <div class="cover light">
                    <h2>Packaging</h2>
                </div>
            </div>
            <div class="column1">
                <img class="icon" src="Datový zdroj 3.svg" alt="My Happy SVG" />
                <a href="https://www.resinex.co.uk/contacts.html"><img src="HomeCare.jpg" alt=""></a>
                <div class="cover light">
                    <h2>Home care</h2>
                </div>

            </div>

            <div class="column2">
                <img class="icon" src="Datový zdroj 4.svg" alt="My Happy SVG" />
                <a href="https://www.resinex.co.uk/contacts.html"><img src="Labware.jpg" alt=""></a>
                <div class="cover dark">
                    <h2>Labware</h2>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

Upvotes: 1

Omicron
Omicron

Reputation: 375

Well, your container is flex and You have 3 flex divs so they'd be displayed vertically as 3 groups with .column elements inside. You can't "merge" them just like that.

Better solution would be to use display-grid and grid-template-columns in your .container and group your elements inside using different classes

.container {
  width: 400px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr
}

.class {
  width: 100px;
  height: 100px;
  margin-bottom: 50px;
}

.class1 {
  background: #592941;
}

.class2 {
  background: #498467;
}

.class3 {
  background: #EDE5A6;
}

@media (max-width: 960px) {
  .container {
    width: 250px;
    grid-template-columns: 1fr 1fr
  }
}
<div class="container">
  <div class="class1 class"></div>
  <div class="class1 class"></div>
  <div class="class1 class"></div>
  <div class="class2 class"></div>
  <div class="class2 class"></div>
  <div class="class2 class"></div>
  <div class="class3 class"></div>
</div>

Upvotes: 1

Vince
Vince

Reputation: 869

HTML

<div class="container-row">
  <div class="row1 row"></div>
  <div class="row2 row"></div>
  <div class="row3 row"></div>
</div>

CSS

.container-row {
 display: flex;
 flex-wrap: wrap;
}

.row1, .row2 {
 width: 50%;
}

.row3 {
 width: 100%;
}

Upvotes: 1

Related Questions