Home > Mobile >  Moving div to the right inside flexbox while keeping space-around width
Moving div to the right inside flexbox while keeping space-around width

Time:11-02

So I am trying to move a div to the right inside flexbox.

I dont want to change justify-content to anything else, but I am trying to move the middle div to the right. However if I use margin and move it then the space around the whole parent div is also affected. Is it possible to move the middle div to the right without affecting the space of the whole div?

So far this solution kind of works- but the items will overlay on each other on smaller screen so this is not responsive at all:

.item2 {
   margin-left: 100px;
   margin-right: -100px;
   border: 1px solid red;
}

But it doesnt seem very elegant. Is there a way to do it with flexbox only?

.main {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
}

.item1 {
   border: 1px solid red;
}

.item2 {
   margin-left: 100px;
   margin-right: -100px;
   border: 1px solid red;
}
<div >
 <div >
  Item 1
 </div>
  <div >
  Item 2
 </div>
  <div >
  Item 3
 </div>
</div>

CodePudding user response:

CSS transform can alter the positioning of an element but it doesn't affect surrounding elements.

This snippet removes the margin settings and instead uses transform: translateX(100px).

.main {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
}

.item1 {
  border: 1px solid red;
}

.item2 {
  /*margin-left: 100px;
   margin-right: -100px;
   */
  transform: translateX(100px);
  border: 1px solid red;
}
<div >
  <div >
    Item 1
  </div>
  <div >
    Item 2
  </div>
  <div >
    Item 3
  </div>
</div>

CodePudding user response:

I think you could utilize the order property here

.main {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
}

.item1 {
   border: 1px solid red;
}

.item2 {
   order: 3;
   border: 1px solid red;
}
<div >
 <div >
  Item 1
 </div>
  <div >
  Item 2
 </div>
  <div >
  Item 3
 </div>
</div>

CodePudding user response:

You can do it in several ways:

1)

transform: translateX(100px)

2)

position: relative;
left: 100px;

3) if you want to change order of flex-items, use this:

.flex-item-2{
   order: 1
}

It moves second item to third. Because default order value: 0.

Maybe there are other useful ways you can use!

CodePudding user response:

.main {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
}
.item1 {
   border: 1px solid red;
}
.rightSide{
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
}
.item2 {
   border: 1px solid red;
}
<div >
  <div >
    Item 1
  </div>
   <div >
  
    <div >
      Item 2
    </div>
    <div >
      Item 3
    </div>
  </div>
</div>

CodePudding user response:

.main {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
}
.parent {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
}
.p-1{
  flex-grow: 2;
}
.p-2{
  flex-grow: 1;
}
.item1 {
    border: 1px solid red;    
}
.item2 { 
    border: 1px solid red;
}
.item3 { 
    border: 1px solid red;
}
<div >
 <div >
   <div >
    Item 1
   </div>
 </div>
 <div >
      <div >
      Item 2
     </div>
      <div >
      Item 3
     </div>
 </div>
</div>

  • Related