How to align two images side by side using CSS: Centering both images side by

Struggling to center two images side by side? Despite efforts, the images always end up stacked vertically. Any tips on achieving the desired side-by-side alignment?

Thank you!

HTML code

<a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="701f1c091d00191118111113180430181f041d11191c5e131f1d">[email protected]</a>">
<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; 
}

Answer №1

Consider making the following adjustments:

.fblogo {
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
    height: 30px; 
}

#images{
    text-align:center;
}

HTML

<div id="images">
    <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56393a2f3b263f3736353e22163e39223b373f3a7835393b">[email protected]</a>">
    <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>​

View the DEMO here.

Answer №2

You should never assign the same ID to two different elements.

Furthermore, by defining them as block elements, you are essentially forcing them to display on separate lines, for better readability.

Consider using the following structure instead:

<div class="button"><a href="..."><img src="..."... /></a></div>
<div class="button"><a href="..."><img src="..."... /></a></div>

CSS:

.button {
    width: 50%;
    float: left;
    text-align: center;
}

Answer №3

Utilizing Flexbox is a great way to achieve this effect with only two CSS rules applied to a wrapping div.

.social-media{
    display: flex;
    justify-content: center;
}
<div class="social-media">
<a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bad5d6c3d7Cad3dbd2dbdbd9d2cefad2D5ced7dbd3d694d9d5d7">[email protected]</a>">
<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>

Answer №4

While this question may be old, there is a great HTML5 solution available. You can enclose everything in a <figure></figure> tag. Your code would resemble the following:

<div id="wrapper">
<figure>

<a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="442b283d29342d252c30042c2b3029252d286a272b29">[email protected]</a>">
<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>

Additionally, include the following CSS:

#wrapper{
 text-align:center;
}

Answer №5

For a recent project, I implemented a unique solution using the h6 tag, which I hadn't utilized before:

Here is the HTML code snippet:

<h6><img alt="small drawing" src="../Images/image1.jpg" width="50%"/> <img alt="small drawing" src="../Images/image2.jpg" width="50%"/><br/>Optional caption text</h6>

The spacing between the image tags creates a vertical gap between the images. While the width argument in each img tag is optional, it helps in neatly sizing the images to fit the page width. It's important to ensure that each image takes up only 50% of the width (or 33% for 3 images). Additionally, the width argument should follow the alt and src attributes for proper functionality.

Here is the corresponding CSS code snippet:

/* h6: set presentation of images */
h6
  {
  font-family: "Franklin Gothic Demi", serif;
  font-size: 1.0em;
  font-weight: normal;
  line-height: 1.25em;
  text-align: center;
  }

The CSS styling defines the appearance of the caption text, and the text-align property centers both the images and the caption text on the page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The elements are stacked on top of each other, but unfortunately, the scroll

While scrolling the page, I encounter a small issue where the elements stack on top of each other. Additionally, when I perform a search, the elements move with the keyboard. I have attached screenshots to illustrate the problem. My goal is to have the cat ...

Connecting a custom bootstrap stylesheet to WordPress

I'm having trouble linking my WordPress Stylesheet to a child theme in a different directory/subfolder. The code I placed in the functions.php file doesn't seem to be working correctly. Here is the code snippet: function theme_add_bootstrap() { ...

Is it possible to manipulate an image tag's image using only CSS?

Is it possible to hide an image from the src attribute of an <img> tag and instead set a background image on the tag using CSS? One idea could be adjusting the positioning or text-indent of the actual image to move it out of view, and then applying ...

Avoiding Backbone Routing Conflicts when Implementing CSS3 Targeting

I have implemented a CSS target to create some basic animation in my Backbone project. However, I am facing an issue where the method I am currently using adds a #banner to the URL, which Backbone tries to interpret as a route if users refresh the page aft ...

Why isn't my li:hover responding the way it should?

Check out this Fiddle When the mouse hovers over the element, an opaque "cloud" appears and I want to completely hide the underline that is normally displayed. The underline is created by using a border-bottom on the parent div. .element_style{ width ...

What are the steps to create a reliable menu?

Looking to add a fixed menu to my website that stays at the top even when scrolling down the page. For an example, check out iplex.co.in for a demonstration. ...

Conceal specific image(s) on mobile devices

Currently, I am facing an issue on my website where I need to hide the last two out of six user photos when viewed on mobile or smaller screens. I believe using an @media code is the solution, but I'm unsure about the specific code needed to achieve t ...

Personalized Radio Styling and Multi-line Text Wrapping

After customizing our radio buttons for a survey form using CSS, we encountered an issue where the text wraps below the replacement graphic when it is too long. We want to avoid modifying the HTML as it has been generated by our CRM system, and making chan ...

Nested accordions with Bootstrap 4.3 featuring plus and minus icons

I am trying to implement a nested accordion structure that displays plus and minus buttons when opening and collapsing each section. <div style="padding: 10px"> <div class="accordion" id="base-data"> &l ...

CSS: dealing with content that spills outside of a div

Currently, I am struggling to identify the solution to a problem on my website but I am unsure of how to categorize it. If you would like to take a look, here is the link: The issue at hand involves having a webpage divided into 3 standard sections: top, ...

Web Essentials is experiencing a problem with minified CSS

Today, while using Web Essentials 2013 for Update 3 with Visual Studio Express 2013 for Web, I encountered an issue with the minified CSS file. The website I am currently working on is built on a Twitter Bootstrap platform and utilizes two separate CSS fil ...

Is there a way to set a custom background image for a specific Vaadin view?

I am developing a view in Vaadin that looks something like this... public class PosAppPickerView extends VerticalLayout implements View { Everything is functioning smoothly, but I am interested in incorporating an image as the background for the body/mai ...

Setting the maximum width for images across various Bootstrap responsive sizes: a step-by-step guide

I'm struggling with optimizing images of varying sizes in bootstrap to ensure they are responsive without losing quality. Current Image: https://i.sstatic.net/akoNy.jpg Desired Image: https://i.sstatic.net/37VFX.jpg HTML: <div class="row"> ...

Bracketing the Webpage: A Display of Unique Design

Currently, I am utilizing the Brackets code editor to create a webpage in HTML format. My aim is to transfer these files to another computer so that the user on that device can view the webpage created from these HTML files. Strangely, when I open the HTML ...

Are box shadows displaying different hues across various web browsers?

Yesterday, I came across a minor issue with my website. The Box-Shadows appear differently on various browsers. Previously, it was red on Firefox and IE, but now it looks completely different on: Opera: Orange Chrome: Violet Safari: Blue Why is that hap ...

Splitting a CSS hierarchical list into horizontally aligned levels of hierarchy

I am trying to horizontally lay out an ordered list with multiple top level nodes while preserving the vertical layout of child nodes. For example, consider a basic list structure like this: <ol> <li>Top 1 <li>Sub 1</li ...

How to position text in the center of a video using CSS

My attempt to center the name of my blog on top of a video background was not successful, even though I tried positioning it absolutely with 50% from the top. Can someone assist me in vertically and horizontally centering the text over the video? Here is ...

The Bootstrap row snag causing text display issues

There seems to be a glitch when the first column has more text than the one next to it. Check out the screenshot provided for reference. Any suggestions on how to fix this issue in Bootstrap? https://i.sstatic.net/fSq68.png Live Demo: https://jsfiddle. ...

How to Convert Pixel Units to Rem and Em in CSS?

Until recently, I developed my website exclusively using pixels, which has made it less responsive than I'd like. Is there a method to efficiently convert pixel measurements to rem/em units, allowing for quicker and easier modifications? ...

What is the best way to create a design with an image on the left and text on the right side?

I am trying to create a layout that features an image on the left and text on the right in a specific structure: Item 0: value 0 Item 1: value 1 Item 2: value 2 However, the text is appearing all in one row like this: Item 0: value 0 Item 1: value 1 Ite ...