Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

align div horizontally after screen resize using css

Tags:

html

css

flexbox

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>
like image 910
user3448925 Avatar asked Nov 24 '25 23:11

user3448925


2 Answers

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>
like image 161
Maik Lowrey Avatar answered Nov 26 '25 13:11

Maik Lowrey


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>
like image 31
Arman Ebrahimi Avatar answered Nov 26 '25 13:11

Arman Ebrahimi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!