How to align text vertically in a div on a table?

I am trying to centralize my "lorem" text inside mine, but I have a div containing the number "1435", What am I doing wrong?

HTML:

<table border="1" style="width:220px;">
  <tr>
    <td valign="middle" class="ostitulo">
        <div class="osnumero">
            <span class="osnumerospan">1435</span>
        </div>
        <p class="par">Lorem</p>          
    </td>          
  </tr>
</table>

      

CSS

.ostitulo{
          font-family: Arial, Helvetica, sans-serif;
          font-size: small;
          font-weight: bold;
 }

.par{
          background-color: #c0c0c0;     
          line-height: 15px;    
 }

.osnumero{
          display:table; 
          width: 40px;
          height: 25px;
          border: 1px #808080 solid;
          border-top: 0px;
          border-left: 0px;  
          margin-right: 3px;
          margin-bottom: 3px;
          float:left;
          position: relative;
          top: 0px;
          left: 0px;      
 }

 .osnumerospan{
          display: table-cell;
          vertical-align: middle;
          text-align:center;
          background-color: red;
   }

      

Source: http://jsfiddle.net/47m5tfLu/

+3


source to share


3 answers


Having your par class as follows:

.par {
    background-color: #c0c0c0; 
    min-height: 25px;
    margin-top: 5px;
    height: 25px;
    width:100px;
    display: table-cell;
    vertical-align: middle;
    line-height: 15px;    
}

      



Centers this for me: http://jsfiddle.net/bzzLuy6g/ Are you trying to fix the colors?

0


source


Apply min and max height:50px

on the div you want to display in the center. Then applyvertical-align: middle;



0


source


It will make things easier if you can update this markup. Then you can easily use the great vertical alignment function of table cells.

<td valign="middle" class="ostitulo">
    <div class="osnumero">
        <span class="osnumerospan">1435</span>
        <span class="par">Lorem</span> 
    </div>         
</td>

      


.osnumero span {
    display: table-cell;
    vertical-align: middle;
}

      

JSFIDDLE

.ostable {
    width: 220px;
    border-collapse: collapse;
}
.ostable td {
    border: 1px solid black;
}
.osnumero {
    display: table;
    height: 25px;
    width: 100%;
}
.osnumero span {
    display: table-cell;
    vertical-align: middle;
}
.osnumerospan {
    width: 40px;
    text-align: center;
    background-color: red;
    border-right: 1px solid black;
}
.par {
    background-color: silver;
}
      

<br>
<table class="ostable">
    <tr>
        <td valign="middle" class="ostitulo">
            <div class="osnumero">
                <span class="osnumerospan">1435</span>
                <span class="par">Lorem</span> 
            </div>         
        </td>          
    </tr>      
    <tr>
        <td valign="top">
            <div style="margin: 3px;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
        </td>
    </tr>
</table>
      

Run codeHide result


0


source







All Articles