1

Here is my html and css below

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

    .my-btn {
        width: 150px;
        height: 50px;
        background-color: #3f729b;
        line-height: 50px;
        text-align: center;
        color: #FFFFFF;
        cursor: pointer;
        display: inline-block;
    }

    <div class="flex">
        <span class="my-btn btn-back">back</span>

        <span class="my-btn btn-download">download</span>

        <span class="my-btn btn-next">next</span>
    </div>

if all the .my-btn are visible, everything is looking fine, but if I want to have only .btn-next visible it is left aligned. How can I have it right aligned. I've tried to use

.btn-next {
    align-self: flex-end;
}

but it's not working.

And ideas?

http://jsfiddle.net/ahzc1f41/1/

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Hayk Aghabekyan
  • 1,087
  • 10
  • 19

2 Answers2

3

Add margin-left: auto; to your .btn-back. That's how it's meant to be done

EDIT: Add margin-right: auto; to btn-back and margin: 0 auto; to btn-download because when the one of the other buttons are shown, it fails without.

JSFIDDLE

HTML

<div class="flex">
    <span class="my-btn btn-back">back</span>

    <span class="my-btn btn-download">download</span>

    <span class="my-btn btn-next">next</span>
</div>

CSS

.flex {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  justify-content: space-between;
}

.my-btn {
  width: 150px;
  height: 50px;
  background-color: #3f729b;
  line-height: 50px;
  text-align: center;
  color: #FFFFFF;
  cursor: pointer;
  display: inline-block;
}

.btn-next {
  align-self: flex-end;
  margin-left: auto;
}

.btn-download{
  margin: 0 auto;
}

.btn-back{
  margin-right: auto;
}

.btn-download, .btn-back {
  //display: none;      
}

SOURCE: http://www.sketchingwithcss.com/samplechapter/cheatsheet.html

Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
1

You can use margin-left: auto; on .btn-next and margin-right: auto; on .btn-back

DEMO

.flex {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  justify-content: space-between;
}

.my-btn {
  width: 150px;
  height: 50px;
  background-color: #3f729b;
  line-height: 50px;
  text-align: center;
  color: #FFFFFF;
  cursor: pointer;
  display: inline-block;
}

.btn-next {
  margin-left: auto;
}

.btn-back {
  margin-right: auto;
}

.btn-download, .btn-back {
  display: none;      
}
    <div class="flex">
        <span class="my-btn btn-back">back</span>

        <span class="my-btn btn-download">download</span>

        <span class="my-btn btn-next">next</span>
    </div>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176