CSS - center two images in css side by side

后端 未结 5 1653
悲哀的现实
悲哀的现实 2020-11-27 16:40

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

相关标签:
5条回答
  • 2020-11-27 17:03

    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:olympiahaacht@hotmail.com">
        <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.

    0 讨论(0)
  • 2020-11-27 17:04

    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:olympiahaacht@hotmail.com">
    <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>

    0 讨论(0)
  • 2020-11-27 17:18

    I've just done this for a project, and achieved it by using the h6 tag which I wasn't using for anything else:

    in html code:

    <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 space between the image tags puts a vertical gap between the images. The width argument in each img tag is optional, but it neatly sizes the images to fill the width of the page. Notice that each image must be set to take up only 50% of the width. (Or 33% if you're using 3 images.) The width argument must come after the alt and src arguments or it won't work.

    in css code:

    /* 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 text items set the look of the caption text, and the text-align property centers both the images and the caption text.

    0 讨论(0)
  • 2020-11-27 17:19

    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;
    }
    
    0 讨论(0)
  • 2020-11-27 17:23

    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:olympiahaacht@hotmail.com">
    <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;
    }
    
    0 讨论(0)
提交回复
热议问题