Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
align div horizontally after screen resize using css

I'm trying to align div horizontally as the browser resizes, currently, I have 3 divs. As per the requirement, I can add an additional div. My problem is as soon I increase the window size above 2500, the right side of the screen becomes empty & all the divs are floating to left. As I cannot set the div width to 30-33% as per the requirement. Below is my code. kindly help.

div.box-container {
  mc-grid-row: true;
  margin-left: auto;
  margin-right: auto;
  float: left;
  display: flex;
  width: 100%
}

div.box {
  float: left;
  background-color: #ffffff;
  position: relative;
  padding: 10px;
  box-sizing: border-box;
  height: 326px;
  margin-left: 20px;
  margin-bottom: 0;
  top: 55px;
  border-top-right-radius: 0px;
  width: 30%;
  border: 1px solid #cccccc;
}
<div class="box-container">

  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
</div>

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

As @Arman Ebrahimi had already mentioned correctly. Use flex box only. The issue of responsibility can be handled well with media queries.

Working example

* {
  box-sizing: border-box;
}

div.box-container {
  display: flex;
  flex-wrap: wrap;
  font-size: 30px;
  text-align: center;
  gap: 10px;
  width: 80%;
  margin: 0 auto;
  /* or use justify-content: center; */
}

.box {
  background-color: #f1f1f1;
  padding: 10px;
  flex: 30%;
  border: 1px solid #cccccc;
  word-break: break-word;
  height: 326px;
}

@media (max-width: 800px) {
  .box {
    flex: 100%;
  }
}
<div class="box-container">
  <div class="box">1</div>
  <div class="box">2</div>
  <div class="box">3</div>

  <div class="box">4</div>
  <div class="box">5</div>
  <div class="box">6</div>
</div>

over 4 years ago · Santiago Trujillo Relatório

0

Use justify-content: center; when you are using flex. This means the flexed contents will always be centered on all screen types.

div.box-container {
  mc-grid-row: true;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  gap: 10px;
  justify-content: center;
  width: 100%
}

div.box {
  background-color: #ffffff;
  position: relative;
  padding: 10px;
  box-sizing: border-box;
  height: 326px;
  margin-bottom: 0;
  top: 55px;
  border-top-right-radius: 0px;
  width: 33.33%;
  border: 1px solid #cccccc;
}

body {
  margin: 0;
}
<div class="box-container">

  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
</div>

Edit ~ add another div, reduce the % the div covers. Demonstrate min-width responsiveness.

div.box-container {
  mc-grid-row: true;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  width: 100%
}

div.box {
  background-color: #ffffff;
  position: relative;
  padding: 10px;
  box-sizing: border-box;
  height: 326px;
  margin-bottom: 0;
  top: 55px;
  border-top-right-radius: 0px;
  width: 24%;
  min-width: 300px;
  border: 1px solid #cccccc;
}

body {
  margin: 0;
}
<div class="box-container">

  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
</div>

over 4 years ago · Santiago Trujillo Relatório

0

Remove float and only use flex:

* {
  box-sizing: border-box;
}

body,
html {
  margin: auto;
}

div.box-container {
  mc-grid-row: true;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  width: 100%;
}

div.box {
  background-color: #ffffff;
  padding: 10px;
  height: 326px;
  margin-left: 20px;
  margin-bottom: 0;
  top: 55px;
  border-top-right-radius: 0px;
  width: calc(100vw / 3);
  /*calc(100vw / number of div)*/
  border: 1px solid #cccccc;
  word-break: break-word;
}
<div class="box-container">
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
  <div class="box">
    <p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
  </div>
</div>

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda