I am trying to center two images side by side, but for some reason it always displays the images beneath each other. Does anyone know how I could get them centered and next to each other?
Thanks!
HTML code
<a href="mailto:[email protected]">
<img id="fblogo" border="0" alt="Mail" src="http://olympiahaacht.be/wp-content/uploads/2012/07/email-icon-e1343123697991.jpg"/></a>
<a href="https://www.facebook.com/OlympiaHaacht" target="_blank">
<img id="fblogo" border="0" alt="Facebook" src="http://olympiahaacht.be/wp-content/uploads/2012/04/FacebookButtonRevised-e1334605872360.jpg"/></a>`
CSS code
#fblogo {
display: block;
margin-left: auto;
margin-right: auto;
height: 30px;
}
To center an image with CSS Grid, wrap the image in a container div element and give it a display of grid . Then set the place-items property to center. P.S.: place-items with a value of center centers anything horizontally and vertically.
Simply put, if you have a div, and the elements inside are set to “display:inline-block” then you can just use “text-align:center” to center them inside.
Select the img tag in CSS and apply the float property. Set the option right to the float property. Next, select the p tag and set the clear property to right . Here, the image will be aligned to the right of the webpage.
Try changing
#fblogo {
display: block;
margin-left: auto;
margin-right: auto;
height: 30px;
}
to
.fblogo {
display: inline-block;
margin-left: auto;
margin-right: auto;
height: 30px;
}
#images{
text-align:center;
}
HTML
<div id="images">
<a href="mailto:[email protected]">
<img class="fblogo" border="0" alt="Mail" src="http://olympiahaacht.be/wp-content/uploads/2012/07/email-icon-e1343123697991.jpg"/></a>
<a href="https://www.facebook.com/OlympiaHaacht" target="_blank">
<img class="fblogo" border="0" alt="Facebook" src="http://olympiahaacht.be/wp-content/uploads/2012/04/FacebookButtonRevised-e1334605872360.jpg"/></a>
</div>
DEMO.
You can't have two elements with the same ID.
Aside from that, you are defining them as block elemnts, meaning (in layman's terms) that they are being forced to appear on their own line.
Instead, try something like this:
<div class="link"><a href="..."><img src="..."... /></a></div>
<div class="link"><a href="..."><img src="..."... /></a></div>
CSS:
.link {
width: 50%;
float: left;
text-align: center;
}
Flexbox can do this with just two css rules on a surrounding div.
.social-media{
display: flex;
justify-content: center;
}
<div class="social-media">
<a href="mailto:[email protected]">
<img class="fblogo" border="0" alt="Mail" src="http://olympiahaacht.be/wp-content/uploads/2012/04/FacebookButtonRevised-e1334605872360.jpg"/></a>
<a href="https://www.facebook.com/OlympiaHaacht" target="_blank">
<img class="fblogo" border="0" alt="Facebook" src="http://olympiahaacht.be/wp-content/uploads/2012/04/FacebookButtonRevised-e1334605872360.jpg"/></a>
</div>
I understand that this question is old, but there is a good solution for it in HTML5.
You can wrap it all in a <figure></figure>
tag. The code would look something like this:
<div id="wrapper">
<figure>
<a href="mailto:[email protected]">
<img id="fblogo" border="0" alt="Mail" src="http://olympiahaacht.be/wp-
content/uploads/2012/07/email-icon-e1343123697991.jpg"/>
</a>
<a href="https://www.facebook.com/OlympiaHaacht" target="_blank">
<img id="fblogo" border="0" alt="Facebook" src="http://olympiahaacht.be/wp-
content/uploads/2012/04/FacebookButtonRevised-e1334605872360.jpg"/>
</a>
</figure>
</div>
and the CSS:
#wrapper{
text-align:center;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With