sg552
sg552

Reputation: 1543

Centering flexbox wrap on mobile viewport

How can I center the stacked item on mobile viewport?

Here is my snippet:

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
}

.space-between {
  justify-content: space-between;
}

.space-between div {
  background: pink;
}

.flex-item {
  line-height: 50px;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}
<div class="flex-container space-between ">
  <div class="flex-item">very long text 1</div>
  <div class="flex-item">very long text 2</div>
  <div class="flex-item">very long text 3</div>
</div>

Upvotes: 1

Views: 151

Answers (1)

Yashwardhan Pauranik
Yashwardhan Pauranik

Reputation: 5566

You should use @media-query for the mobile view. After refactoring your HTML and CSS, here is your updated code

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between; 

}

@media only screen and (max-width: 768px) {
  .flex-container {
    justify-content: center; 
 }
}

.flex-container div {
  background: pink;
}

.flex-item {
  line-height: 50px;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}

HTML

<div class="flex-container space-between ">
  <div class="flex-item">very long text 1</div>
  <div class="flex-item">very long text 2</div>
  <div class="flex-item">very long text 3</div>
</div>

Upvotes: 1

Related Questions