1

I need tree cells in tree columns(col-4), with an image, that determines the height of the cell, and vertically middle-aligned text, like this: enter image description here

This is what I get (CodePen Here):

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></script>
<style>   a{display:block;}   </style>
<div class="row">
   <div class="col-xs-4 text-center" style="background:red">
      <img src="http://placehold.it/75">
      <a style="background: yellow">Some text red-yellow</a>        
   </div>
   <div class="col-xs-4 text-center" style="background:lightgreen">
      <img src="http://placehold.it/75">
      <a style="background: gray">some other text long text some other long text lightgreen-gray</a>
   </div>
   <div class="col-xs-4 text-center" style="background:skyblue">
      <img src="http://placehold.it/75">
      <a style="background: violet">this is another long long long text that should be here skyblue-violet</a>
   </div>
</div>

Where is the problem ?

serge
  • 13,940
  • 35
  • 121
  • 205

3 Answers3

2

Try this. Use display: table; for parent class and display: table-cell; vertical-align: middle; for child elements. This will give you vertical align correctly.

<html>

<head>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style>
        a {
            display: block;
        }
        .customclass {
            display: table;
        }
        .customclass img, .customclass a{
            display: table-cell;
            vertical-align: middle;
        }
    </style>
</head>

<body>
    <div class="row">
        <div class="col-xs-4 text-center customclass" style="background:red">
            <img src="http://placehold.it/75">
            <a style="background: yellow">Some text red-yellow</a>
        </div>

        <div class="col-xs-4 text-center customclass" style="background:lightgreen">
            <img src="http://placehold.it/75">
            <a style="background: gray">some other text long text some other long text lightgreen-gray</a>
        </div>
        <div class="col-xs-4 text-center customclass" style="background:skyblue">
            <img src="http://placehold.it/75">
            <a style="background: violet">this is another long long long text that should be here skyblue-violet</a>
        </div>
    </div>
</body>

</html>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • thanks, is there a way to set the max-height after the image (unknown) height and cut the exceeding of the text? – serge Mar 31 '17 at 10:31
1

The problem is that you are closing the first <a> with a </div> tag mate, so you close the div with the row class too early. =)

Ricardo Zorzo
  • 289
  • 1
  • 9
0

You can just add following css code :

img {vertical-align: middle;}

It align your a tag in the middle of image.