Magearlik Magearlik - 5 months ago 8
CSS Question

How to center a div horizontally in a table using CSS

I just updated this for clarity.

So I have some buttons I have created which consist of a circle and a font-awesome icon. Each of these buttons are to link to social media. I now want to put these buttons inside a table row, where each will be horizontally and vertically aligned to the center of each table cell. How do I horizontally align them?

Here is my 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>

Answer
try below code..

do it in every tag And add below code in your 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 
}
Comments