1

I have three column's and the middle column has a button which i want to vertially align in the middle.

<div class="row">
    <div class="col-md-1">
        <div class="mypanel">
        </div>
    </div>
    <div class="col-md-1">
        <div class="mypanel">
            <div style="vertical-align:middle">
                <button type="button">Click</button>
            </div>
        </div>
    </div>
    <div class="col-md-1">
        <div class="mypanel">

        </div>
    </div>
</div>

Style

  .mypanel {
        height: 200px;
        border: 1px solid black;
    }

However vertical-align:middle does not make any affect on the content of the div

DEMO

LP13
  • 20,711
  • 38
  • 136
  • 286

3 Answers3

1
.mypanel {
    height: 200px;
    border: 1px solid black;
    display:table;
}

 .mypanel div{
    display:table-cell;
    vertical-align:middle
}

this will solve the problem https://jsfiddle.net/xyx3enz1/

Pushpender
  • 193
  • 1
  • 11
0

  .mypanel {
        height: 100px;
        width: 100%;
        display: table;
        border: 1px solid black;
    }
    .text-centered {
                text-align: center;
                display: table-cell;
                vertical-align: middle;
     }
<div class="row">
    <div class="col-md-1">
        <div class="mypanel">
        </div>
    </div>
    <div class="col-md-1">
        <div class="mypanel">
            <div class="text-centered">
                <button type="button">Click</button>
            </div>
        </div>
    </div>
    <div class="col-md-1">
        <div class="mypanel">

        </div>
    </div>
</div>
Aashish
  • 2,355
  • 2
  • 22
  • 35
0

My favorite approach is to use pseudo elements like :after to create an element, set its height, and then vertical align based on that. It doesn't require much CSS either:

// I changed the col-md-1 to col-md-4 for this example
.col-md-4 {
  text-align: center;
  height: 200px;
}

.col-md-4 .mypanel {
   height: 100%; // Make sure .mypanel is set to 100% height otherwise our pseudo element won't know what 100% height is
}

// Here is the pseudo element, set the vertical align here and on your button
.col-md-4:nth-child(2) .mypanel:after {
   content: '';
   height: 100%;
   display: inline-block;
   vertical-align: middle;
}

.col-md-4:nth-child(2) .mypanel button {
   vertical-align: middle;
   display: inline-block;
}

Check out this working jsFiddle.

Jeramiah Harland
  • 844
  • 7
  • 14