css - 如何使用 CSS 使表格中的 div 水平居中

标签 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>

最佳答案

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 
}

关于css - 如何使用 CSS 使表格中的 div 水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37762070/

相关文章:

html - 如何将 &lt;input&gt; 放置在 <div> 的中心

css - flex-basis和width有什么区别?

html - 如何使用 Bootstrap 以响应方式显示所有名称

highcharts - 如何在 Highcharts.js 中对齐 xAxes 上的类别标签

java - BoxLayout 忽略子面板对齐

html - 垂直对齐并排 div 中的内容

text - 图像魔术 : right align text in area

javascript - JavaScript 的滑动拼图

html - 选择和按钮元素弄乱了表格

android - Android 浏览器上的 CSS3 动画问题