0

I am trying to achieve a vertically aligned child elements which have different heights.

Following is what I want to implement. Currently I set a different negative top margin on both buttons. I feel this can be done in a more cleaner way with a single generic ruling. Please share your thoughts.

enter image description here


Following is the code to illustrate the problem:

.container {
  padding: 35px 2px;
  border: 1px solid gray;
}

.title {
  font-size: 18px;
}

.action-button {
  float: right;
  height: 50px;
  width: 100px;

}

.small-screen {
  width: 160px;
  display: inline-block;
}
HTML:

<div class="container">

  <span class="title">
    Card header
  </span>

  <button class="action-button">
    Action
  </button>

</div>

<br/>

<div class="container">

  <span class="title small-screen">
    Card header title here is longer
  </span>

  <button class="action-button">
    Action
  </button>

</div>

Link to JSFiddle

Raul
  • 800
  • 1
  • 7
  • 22

4 Answers4

3

Replace your .container style with.

height: 120px;
display: flex;
border: 1px solid gray;
align-items: center;
justify-content: space-between;

Check the snippet

.container {
  height: 120px;
  display: flex;
  border: 1px solid gray;
  align-items: center;
  justify-content: space-between;
}

.title {
  font-size: 18px;
}

.action-button {
  float: right;
  height: 50px;
  width: 100px;
}

.small-screen {
  width: 160px;
  display: inline-block;
}
<div class="container">

  <span class="title">
        Card header
      </span>

  <button class="action-button">
        Action
      </button>

</div>

<br />

<div class="container">

  <span class="title small-screen">
        Card header title here is longer
      </span>

  <button class="action-button">
        Action
      </button>

</div>
johannchopin
  • 13,720
  • 10
  • 55
  • 101
Akhil Aravind
  • 5,741
  • 16
  • 35
1

Please replace your .container class code to below code

   .container {
    border: 1px solid gray;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px;
    border: 1px solid gray;
    }
Pratiksha Kale
  • 304
  • 5
  • 13
1

You could use absolute positioning instead of floating.

Add the line position: relative to your .container class to make sure that the buttons are positioned within this element.

Then remove the float from the .action-button class and instead add:

margin: 0;
position: absolute;
right: 20px;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);

Here is the full working code snippet:

.container {
  padding: 35px 2px;
  border: 1px solid gray;
  
  position: relative;
}

.title {
  font-size: 18px;
}

.action-button {
  height: 50px;
  width: 100px;
  
  margin: 0;
  position: absolute;
  right: 20px;
  top: 50%;
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);

}

.small-screen {
  width: 160px;
  display: inline-block;
}
<div class="container">

  <span class="title">
    Card header
  </span>

  <button class="action-button">
    Action
  </button>

</div>

<br/>

<div class="container">

  <span class="title small-screen">
    Card header title here is longer
  </span>

  <button class="action-button">
    Action
  </button>

</div>
Run_Script
  • 2,487
  • 2
  • 15
  • 30
0

Use display:table along with table-cell with vertical align middle property

.container {
    padding: 35px 2px;
    border: 1px solid gray;
    display: table;
    width: 100%;
    vertical-align: middle;
}

.title {
    font-size: 18px;
    display: table-cell;
    vertical-align: middle;
}

.action-button {
    float: right;
    height: 50px;
    width: 100px;
    display: table-cell;
    vertical-align: middle;
}

.small-screen {
  width: 160px;
  display: inline-block;
}
HTML:

<div class="container">

  <span class="title">
    Card header
  </span>

  <button class="action-button">
    Action
  </button>

</div>

<br/>

<div class="container">

  <span class="title small-screen">
    Card header title here is longer
  </span>

  <button class="action-button">
    Action
  </button>

</div>
Awais
  • 4,752
  • 4
  • 17
  • 40