How do I place images side by side in div? (wordpress)

I have been using the code from here but ever since I needed to wrap my images in a div, it doesn’t work even though I changed the selector to images. Had to wrap images in a div to style it to have different width from the text.

This is what I have in my functions to wrap my images in a div:

Read More
<?php

function breezer_addDivToImage( $content ) {

   // A regular expression of what to look for.
   $pattern = '/(<img([^>]*)>)/i';
   // What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
   $replacement = '<div class="image">$1</div>';

   // run preg_replace() on the $content
   $content = preg_replace( $pattern, $replacement, $content );

   // return the processed content
   return $content;
}

add_filter( 'the_content', 'breezer_addDivToImage' );

?>

And this is the CSS regarding it:

#image {
        margin: 0px auto 24px !important;
        float: left !important;
    display: block;
}
#image:after { clear: both; }
#image:before, .halfeach:after { content: ""; display: table; }
p #image img { width: 49%; margin: 0; }
p #image a:first-child img { float: left; }
p #image a:last-child img { float: right; }

I have an example post here.

THE EDITED CSS:

    .alignright {
    float: right;
    margin: 0 0 50px 0;
    display: inline-block;
}
.alignleft {
    float: left;
    margin: 0 0 50px 0;
    display: inline-block;
}
a img.alignright {
    float: right;
    margin: 0 0 50px 0;
    display: inline-block;
}
a img.alignleft {
    float: left;
    margin: 0 0 50px 0;
    display: inline-block;
}
#page-container .image {
    margin: 7px 0px !important;
}

Related posts

Leave a Reply

2 comments

  1. You’re container is too small for the images to fit next to eachother, that’s why they are pushed down.

    Secondly your using the class .image but you’re using the ID #image in your CSS, so that won’t work either. So change that.

    Two options:

    • Make sure your images are 50% of the width (if 2 images)
    • Make your content container bigger so both images would fit next to eachother.