如何使用CSS在表格中水平居中div

时间:2016-06-11 09:23:17

标签: css alignment

为了清晰起见,我刚刚对此进行了更新。

所以我创建了一些按钮,包括一个圆圈和一个字体很棒的图标。这些按钮中的每一个都链接到社交媒体。我现在想把这些按钮放在一个表格行中,其中每个按钮将水平和垂直对齐到每个表格单元格的中心。如何水平对齐它们?

这是我的HTML:

<!DOCTYPE html>
    <html lang="en">
    <head>
    <script src="https://use.fontawesome.com/604215ea7e.js"></script>
        <style>
         .social_icon
        {  
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: #ccc;
            text-align: center;
            display: table-cell;
            vertical-align: middle;
        }
        .social_icon:hover
        {   
            background-color: #999;
            cursor: pointer;
        }
        #footer_social_icons
        {       
            width: 20%;
            height: 80px;
            background-color: #636363;
            text-align: center;
        }
        #footer_social_icons table
        {   
            width: 100%;
            height: 80px;
        }
        #footer_social_icons td
        {
            vertical-align: middle;
            border: 1px solid #fff; /* to show not centred */
        }
        </style>
    </head>
    <body>
    <div id="footer_social_icons">
        <table>
            <tr>
                <td>
                    <div class="social_icon">
                        <i class="fa fa-google-plus" aria-hidden="true"></i>
                    </div>
                </td>
                <td>
                    <div class="social_icon">
                        <i class="fa fa-facebook" aria-hidden="true"></i>
                    </div>
                </td>
                <td>
                    <div class="social_icon">
                        <i class="fa fa-twitter" aria-hidden="true"></i>
                    </div>
                </td>
                <td>
                    <div class="social_icon">
                        <i class="fa fa-envelope" aria-hidden="true"></i>
                    </div>
                </td>
            </tr>
        </table>
    </div>

    </body>
    </html>

3 个答案:

答案 0 :(得分:0)

为footer_social_icons id提供margin auto。

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
  <style>
 .social_icon
{
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: #ccc;
    text-align: center;
    display: table-cell;
    vertical-align: middle;
}
.social_icon:hover
{
    background-color: #999;
    cursor: pointer;
}
#footer_social_icons
{    
    width: 20%;
    height: 80px;
	margin:0 auto;
    background-color: #636363;
    text-align: center;
}
#footer_social_icons table
{
    width: 100%;
    height: 80px;
}
#footer_social_icons td
{
    vertical-align: middle;
    border: 1px solid #fff;
}
  </style>
</head>
<body>
<div id="footer_social_icons">
    <table>
        <tr>
            <td>
                <div class="social_icon">
                    <i class="fa fa-google-plus" aria-hidden="true"></i>
                </div>
            </td>
            <td>
                <div class="social_icon">
                    <i class="fa fa-facebook" aria-hidden="true"></i>
                </div>
            </td>
            <td>
                <div class="social_icon">
                    <i class="fa fa-twitter" aria-hidden="true"></i>
                </div>
            </td>
            <td>
                <div class="social_icon">
                    <i class="fa fa-envelope" aria-hidden="true"></i>
                </div>
            </td>
        </tr>
    </table>
</div>
	  
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

try below code..

在每个标签中执行此操作 并在您的CSS中添加以下代码

<td>
   <div class="socicon">
       <div class="social_icon">
            <i class="fa fa-google-plus" aria-hidden="true"></i>
        </div>
    </div>
</td> 
.socicon {
    margin: 0 auto;
    text-align: center;
    width: 57%; // you have to adjust width as your requirement 
}

答案 2 :(得分:0)

Do you need like this


<!DOCTYPE html>
<html lang="en">
<head>
  <style>
 .social_icon
{  
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: #ccc;
    text-align: center;
    display: table-cell;
    vertical-align:center;
}
.social_icon:hover
{   
    background-color: #999;
    cursor: pointer;
}
#footer_social_icons
{    
    width:20%;
    height:80px;
    margin:auto ;
    background-color: #636363;
    text-align: center;
}
#footer_social_icons table
{   
    width: 100%;
    height: 80px;
}
#footer_social_icons td
{
    vertical-align: middle;
    border: 1px solid #fff;
}
  </style>
</head>
<body>
<div id="footer_social_icons">
    <table>
        <tr>
            <td>
                <div class="social_icon">
                    <i class="fa fa-google-plus" aria-hidden="true"></i>
                </div>
            </td>
            <td>
                <div class="social_icon">
                    <i class="fa fa-facebook" aria-hidden="true"></i>
                </div>
            </td>
            <td>
                <div class="social_icon">
                    <i class="fa fa-twitter" aria-hidden="true"></i>
                </div>
            </td>
            <td>
                <div class="social_icon">
                    <i class="fa fa-envelope" aria-hidden="true"></i>
                </div>
            </td>
        </tr>
    </table>
</div>

</body>
</html>