Say you have a blog site with Jekyll and the blog posts are centered on the page. I want content (i.e. paragraphs, words, code, etc.) to be aligned to the left, but images to be centered.
I have the text, etc. left-aligned, but now I'm struggling to get the image (who has a CSS style of max-height: 80%
) to be centered as well.
It appears that Jekyll's blog generator (from Markdown) wraps images in <p></p>
tags so margin: 0 auto
doesn't center the image. I've attached code snippets below:
<li> <h2 class="post-title">Hello World</h2> <div class="post-date"><span>May 21, 2014</span></div> <div class="post-content"> <p><img src="/images/photos/blog/hello-world.jpg" alt="blog-image"></p> </div> </li> ul#posts { list-style-type: none; margin: 0 auto; width: 60%; } ul#posts > li { margin-bottom: 35px; text-align: center; } ul#posts > li div.post-content { text-align: left; } ul#posts > li > div.post-date { color: #A0A0A0; font-style: italic; margin-bottom: 15px; } ul#posts > li > div.post-content > p > img { margin: 0 auto; max-height: 80%; max-width: 100%; }
How can I center the image in the blog post?
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.
To center an image, we have to set the value of margin-left and margin-right to auto and make it a block element by using the display: block; property. If the image is in the div element, then we can use the text-align: center; property for aligning the image to center in the div.
Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.
Here is a way to do it via kramdown:
{:refdef: style="text-align: center;"} ![My Image]({{ site.baseimg }}/images/example1.png) {: refdef}
This will create another paragraph around the paragraph added by kramdown. Source
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