Using flex to make the horizontal line vertical

Viewed 209

I have read that inside a flex container if we give an hr tag it will become vertical. I have tried the same, but not able to achieve. Am I missing something here ?

.outer-div {
  background-color: aqua;
}

.inner-div {
  display: flex;
  justify-content: space-between;
}
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
<div class="outer-div">
  <div class="inner-div">
    <div class="details-div">
      <i class="fas fa-band-aid">I</i>
      <span>Get Details</span>
    </div>
    <hr />
    <div class="call-div">
      <i class="fas fa-band-aid">I</i>
      <span>Call Customer Care</span>
    </div>
  </div>
</div>

4 Answers

It is working. Your element is just so short that it appears as a dot.

.outer-div {
  background-color: pink;
}

.inner-div {
  display: flex;
  justify-content: space-between;
  min-height: 100px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2D7kSs0v4LLanw2qksYuRlEzO+tcaEPQogQ0KaoGN26/zrn20ImR1DfuLWnOo7aBA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<div class="outer-div">
  <div class="inner-div">
    <div class="details-div">
      <i class="fas fa-band-aid"></i>
      <span>Get Details</span>
    </div>
    
    <hr />
    
    <div class="call-div">
      <i class="fas fa-band-aid"></i>
      <span>Call Customer Care</span>
    </div>
  </div>
</div>

It does, the problem is, that the margin on the <hr/> makes it almost a dot. You could set your margin: 0 on the <hr/> to achieve expected behavior

The reason for this is that default value of align-items is stretch so <hr> will get the same height as largest element in flex-container [...].

Quoted from this SO Post

.outer-div {
  background-color: aqua;
}

.inner-div {
  display: flex;
  justify-content: space-between;
}

hr {
 margin: 0;
 }
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
<div class="outer-div">
  <div class="inner-div">
    <div class="details-div">
      <i class="fas fa-band-aid">I</i>
      <span>Get Details</span>
    </div>
    <hr />
    <div class="call-div">
      <i class="fas fa-band-aid">I</i>
      <span>Call Customer Care</span>
    </div>
  </div>
</div>

Approach #1 : transform: rotate(90deg)

One approach to achieving a vertical <hr> is simply to apply:

transform: rotate(90deg);

Working Example:

.inner-div {
  display: flex;
  justify-content: space-around;
}

.inner-div hr {
  flex: 0 0 40%;
  transform: rotate(90deg);
}
<div class="inner-div">
  <div class="details-div">Get Details</div>
  <hr />
  <div class="call-div">Call Customer Care</div>
</div>


Approach #2 : Explicit height and width

Another approach is to declare exlicit dimensions for your <hr>:

  • width: 0;
  • height: 120px

Working Example:

.inner-div {
  display: flex;
  justify-content: space-around;
}

.inner-div hr {
  width: 0px;
  height: 120px;
}
<div class="inner-div">
  <div class="details-div">Get Details</div>
  <hr />
  <div class="call-div">Call Customer Care</div>
</div>

you can use this code for do it

.className{
   display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
}
Related