Aaditya
Aaditya

Reputation: 13

<div> element not getting centered using justify-content:center;

Using justify-content:center; does not generate the required results as shown in the pictures.

.box {
    height: 200px;
    width: 200px;
    background-color: blue;
    margin:10px ;
}

.flex{
    margin:10px;
    border:3px solid black;
    display:flex;
    flex-wrap:wrap;
    justify-content:center;
}
<div class="flex"><p>
    Lets learn about Flexbox.Flexbox needs a parent 
    container in order to work elements inside of that container
    these elements can be centered and adjusted easily.
</p>
<div class="box" id="box1">Box1</div>
<div class="box" id="box2">Box2</div>
<div class="box" id="box3">Box3</div>
<div class="box" id="box4">Box4</div>
<div class="box" id="box5">Box5</div>
<div class="box" id="box6">Box6</div>
</div>

Desired:

Desired

Actual:

Actual

Upvotes: 1

Views: 117

Answers (4)

s.kuznetsov
s.kuznetsov

Reputation: 15223

Wrap your sections with div, and set flex-direction: column to the parent container. This way your flex-wrap will work correctly. Now, as in your picture the desired result.

.box {
    height: 200px;
    width: 200px;
    background-color: blue;
    margin:10px ;
}

.boxes {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    width: calc(600px + 60px);
    max-width: 100%;
}

.flex{
    margin:10px;
    border:3px solid black;
    display:flex;
    flex-direction: column;
    align-items: center;
}
<div class="flex">
  <p>
      Lets learn about Flexbox.Flexbox needs a parent 
      container in order to work elements inside of that container
      these elements can be centered and adjusted easily.
  </p>
  <div class="boxes">
  <div class="box" id="box1">Box1</div>
  <div class="box" id="box2">Box2</div>
  <div class="box" id="box3">Box3</div>
  <div class="box" id="box4">Box4</div>
  <div class="box" id="box5">Box5</div>
  <div class="box" id="box6">Box6</div>
  </div>
</div>

Upvotes: 0

user2560539
user2560539

Reputation:

You could also set width to p and solve this. In the snippet below flex-grow and flex-shrink are used, you can check the ouput also in this JSFiddle

.box {
  height: 200px;
  width: 200px;
  background-color: blue;
  margin: 10px;
  flex-grow: 4;
  flex-shrink: 4;
}

.flex {
  margin: 10px;
  border: 3px solid black;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.flex p {
  width: 100%;
  padding: 0 1rem;
}
<div class="flex">
  <p>
    Lets learn about Flexbox.Flexbox needs a parent container in order to work elements inside of that container these elements can be centered and adjusted easily.
  </p>
  <div class="box" id="box1">Box1</div>
  <div class="box" id="box2">Box2</div>
  <div class="box" id="box3">Box3</div>
  <div class="box" id="box4">Box4</div>
  <div class="box" id="box5">Box5</div>
  <div class="box" id="box6">Box6</div>
</div>

Upvotes: 0

Rayees AC
Rayees AC

Reputation: 4659

.box {
    height: 200px;
    width:  calc((100% / 3) - 10px);
    background-color: blue;
    margin:10px ;
}

.flex{
    margin:10px;
    border:3px solid black;
    display:flex;
    flex-wrap:wrap;
    justify-content:center;
    align-items:center;
}
<div class="flex"><p>
    Lets learn about Flexbox.Flexbox needs a parent 
    container in order to work elements inside of that container
    these elements can be centered and adjusted easily.
</p>
<div class="box" id="box1">Box1</div>
<div class="box" id="box2">Box2</div>
<div class="box" id="box3">Box3</div>
<div class="box" id="box4">Box4</div>
<div class="box" id="box5">Box5</div>
<div class="box" id="box6">Box6</div>
</div>

Upvotes: 0

Kumar Gaurav
Kumar Gaurav

Reputation: 738

You can try this. I hope , It will resolve your issue. Use flex-basis: 100% for p tag.

.box {
  height: 200px;
  width: calc((100% / 3) - 10px);
  background-color: blue;
  margin: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
}
p {
  flex-basis: 100%;
}

.flex {
  margin: 10px;
  padding: 10px;
  border: 3px solid black;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
<!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>
</head>
<body>
  <div class="flex"><p>
    Lets learn about Flexbox.Flexbox needs a parent 
    container in order to work elements inside of that container
    these elements can be centered and adjusted easily.
    </p>
<div class="box" id="box1">Box1</div>
<div class="box" id="box2">Box2</div>
<div class="box" id="box3">Box3</div>
<div class="box" id="box4">Box4</div>
<div class="box" id="box5">Box5</div>
<div class="box" id="box6">Box6</div>
</div>
</body>
</html>

Upvotes: 1

Related Questions