0

inspired by:

Flexbox - Vertically Center and Match Size

fiddle with the problem: http://jsfiddle.net/Nbknc/22/

what i try to achieve:

I want to get the text of the second button to start at the same height as the text on the first button.

HTML

<section class="buttonsSection">
    <a class="button" href="#">Very Long Word aaaa xx ccc ddd ee</a>
    <a class="button" href="#">Short Phrase</a>
</section>

CSS

.button {
    padding: 10px 15px;
    width: 150px;
    background-color: deepskyblue;
    color: white;
    margin: 3px;
    text-align: top;

    display: flex;
    flex-direction: column;
    justify-content: center;
}

.buttonsSection {
    margin: 30px 0;
    display: flex;
    justify-content: center;
    height: 500px;
}

body
{
    width: 20%; /*Simulate page being reduced in size (i.e. on mobile)*/
    margin: 0 auto;
}

a photo of how i want it to look

enter image description here

EDIT the reason I use flexbox and justify-content is to make it work with different screen sizes. Space is perfectly distributed with flexbox. Adding a padding is suboptimal as it will stay the same, even if the screen has a height of say 200px.

Community
  • 1
  • 1
Toskan
  • 13,911
  • 14
  • 95
  • 185

4 Answers4

1

Here is one way, one where I added an extra wrapper that centers

.buttonsSection {
    display: flex;
    flex-direction:column;
    justify-content: center;
    height: 400px;
    border: 1px solid;
    width: 200px;
    margin: 0 auto;
}
.buttonsWrap {
    margin: 30px 0;
    display: flex;
}
.button {
    padding: 50px 15px;
    width: 150px;
    background-color: deepskyblue;
    color: white;
    margin: 3px;
    text-align: top;
}
<section class="buttonsSection">
  <div class="buttonsWrap">
    <a class="button" href="#">Very Long Word aaaa xx ccc ddd ee</a>
    <a class="button" href="#">Short Phrase</a>
  </div>
</section>
Asons
  • 84,923
  • 12
  • 110
  • 165
1

You can accomplish this by removing the flexbox properties from the button and adding a span around your button text with the following CSS:

position: relative;
top: 50%;
transform: translateY(-50%);

You may need to play with those percentages to get things to line up ideally, but this gets you in the ballpark.

http://codepen.io/angeliquejw/pen/QNdrOZ?editors=0100

Angelique
  • 906
  • 7
  • 15
  • you mean around both buttons. Sadly, that is not possible for me, in my specific applicaiton. But: it would work if I set the second button to the same height as the first (bigger) one and then apply it for each button. – Toskan Mar 18 '16 at 07:10
  • that is probably the most stable solution cross browser wise. – Toskan Mar 18 '16 at 07:13
0

Edit: http://jsfiddle.net/Dneilsen22/36yL3y5m/5/

Removing the justify-content for .button and increasing the top padding would accomplish this.

.button {
  padding: 100px 15px;
  width: 150px;
  background-color: deepskyblue;
  color: white;
  margin: 3px;
  text-align: top;

  display: flex;
  flex-direction: column;
}
Danielson
  • 88
  • 1
  • 8
  • See c00ki3s answer with "padding-top: 50%;", it will adjust with your screen size. – Danielson Mar 17 '16 at 20:04
  • same story. The value of padding top 50%, the 50% are related to the size of the element. E.g. if the button text has a height of 100, 50% will be 50px. Agree? this is the same as setting padding-top: 50px;. If the screen has a height of 100px, the button with text 100px, and you add padding-top 50px it wont fit any longer on the screen. That's why flexbox is cool, look it up – Toskan Mar 17 '16 at 20:18
  • I did look up flexbox, very cool and I'm going to work with it a bit more. I've created a jsFiddle without padding, let me know if that is what you were suggesting in your previous comment. http://jsfiddle.net/Dneilsen22/36yL3y5m/5/ – Danielson Mar 18 '16 at 18:31
0

I updated the fiddle Suggest if its not that you require.

http://jsfiddle.net/Nbknc/30/

.button {
padding: 50% 15px 0 15px;
width: 150px;
background-color: deepskyblue;
color: white;
margin: 3px;  

}

.buttonsSection {
    margin: 30px 0;
    display: flex;
   flex-direction:row;
    height: 500px;
}

Now its much simpler , now you can add the required padding to your button so that the text in both button will align with equal top padding .


UPDATE
included some changes to your html and css

http://jsfiddle.net/Nbknc/32/

Sachin
  • 2,627
  • 1
  • 19
  • 35