David
David

Reputation: 23

Left and right aligning flex items

I have 4 divs all in the same wrapper, I need the first 3 to align left (text) and the 4th to align right (an image).

There desired effect is as follows.

 <one>      <   
 <two>      Four
 <three>       >

The html is basic literally just 4 divs in a wrapper and the css can just be display:flex on the wrapper.

I have partially achieved this by adding flex direction column onto the wrapper so all 4 then stack vertically but I need the 4th to align right as above.

Any help would be hugely appreciated.

Thanks

Upvotes: 0

Views: 339

Answers (2)

fen1x
fen1x

Reputation: 5860

Using flexbox:

* {
  box-sizing: border-box;
}
.container {
  height: 500px;
  border: 1px solid green;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.item {
  flex: 1;
  padding: .5rem;
  border: 1px dashed lightblue;
}

.one, .two, .three {
  flex-basis: 33.333333%;
}
<div class="container">
  <div class="item one">1</div>
  <div class="item two">2</div>
  <div class="item three">3</div>
  <div class="item four">4</div>
</div>

Using css-grid:

* {
  box-sizing: border-box;
}
.container {
  height: 500px;
  border: 1px solid green;
  display: grid;
}
.item {
  padding: .5rem;
  border: 1px dashed lightblue;
}
.four {
  grid-column: 2;
  grid-row: 1 / span 3;
}
<div class="container">
  <div class="item one">1</div>
  <div class="item two">2</div>
  <div class="item three">3</div>
  <div class="item four">4</div>
</div>

Upvotes: 3

Kix__g
Kix__g

Reputation: 25

.wrapper {
  display: flex;
  flex-direction: row;
 }
 
<div class="wrapper">
  <div>
    <div>one</div>
    <div>two</div>
    <div>three</div>
  </div>
  <div>
    <div>four</div>
  </div>
</div>

Upvotes: -2

Related Questions