0

Here is my code:

.container{
  width: 50%;
  border: 1px solid;
  text-align: center;
}

.box{
  display: inline-block;
  border: 1px solid red;
  width: 100px;
  height: 20px;
  margin: 10px;
}
<div class="container">
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
</div>

As you see, the last element is also center. While I need to keep in the same line (vertically) on left column. So this is the expected result:

enter image description here

How can I do that?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111

5 Answers5

2

Use float:left on your block.

You need to add nested div and set a width on it.

Then your box elements will all be centered to the nested div and have your last box in the left column.

.center {
    text-align: center;
    margin-left:auto%;
    margin-right:auto%;
    width: 100%;  
}
.container{
    margin-left:25%;
    margin-right:25%; 
    width:40%;
    border: 1px solid;  

}

.box{
  display: inline-block;
  border: 1px solid red;
  float: left;
  width: 100px;
  height: 20px;
  margin: 10px;

}
<div class="center">
<div class="container">
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
</div>
</div>
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
1

Try with floating property .And fix the container with min heigh

.container{
  width: 50%;
  border: 1px solid;
  text-align: center;
  min-height:150px;
}

.box{
  float:left;
  border: 1px solid red;
  width: 100px;
  height: 20px;
  margin: 10px;
}
<div class="container">
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
</div>
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • Seems my question isn't clear enough. Because in your code, all boxes are in the left side of container. while I need to keep in the container center. – Martin AJ Jul 11 '17 at 06:54
1

I added some background colours for you to see what the containers are doing.

.container{
  width: 50%;
  border: 1px solid;
  text-align: center;
  background-color: silver;
  
}

.box{
  display: inline-block;
  border: 1px solid red;
  width: 100px;
  height: 20px;
  margin: 10px;
  text-align: center;
}

.innerContainer {
  width: 250px;
  background-color: lightgray;
  margin: auto;
}

.row {
  text-align: left;
}
<div class="container" align="center">
  <div class="innerContainer">
    <div class="box">sth</div>
    <div class="box">sth</div>
    <div class="box">sth</div>
    <div class="box">sth</div>
    <div class="row">
      <div class="box">sth</div>
    </div>
  </div>
</div>
Ricky Dam
  • 1,833
  • 4
  • 23
  • 42
1

Try this I think it will work fine if you want it to be responsive I will help you with mediaQuery.

.container {
  width: 50%;
  border: 1px solid;
  text-align: center;
  display: flex;
  flex-direction: colomn;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: center;
  align-content: stretch;
}

.box {
  display: inline-block;
  border: 1px solid red;
  width: 100px;
  height: 20px;
  margin: 10px;
}
<div class="container">
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
  <div class="box">sth</div>
</div>
Jithin Raj P R
  • 6,667
  • 8
  • 38
  • 69
1

Use float:left; inside box class, like:

.box{
   display: inline-block;
   border: 1px solid red;
   width: 100px;
   height: 20px;
   margin: 10px;
   float: left;
}
flob
  • 3,760
  • 2
  • 34
  • 57
Prk
  • 36
  • 6