Explain Codes LogoExplain Codes Logo

Markdown and image alignment

html
responsive-design
css
markdown
Nikita BarsukovbyNikita Barsukov·Nov 5, 2024
TLDR

Centering an image in Markdown involves HTML:

<img src="image.jpg" alt="alt text" style="display: block; margin-left: auto; margin-right: auto;" />

For left or right alignment, the style float is used:

Left:

<img src="image.jpg" alt="alt text" style="float: left; margin-right: 10px;" />

Right:

<img src="image.jpg" alt="alt text" style="float: right; margin-left: 10px;" />

Harnessing extended Markdown processors allows the enhancement of alignment without the use of HTML.

Markdown paired with CSS

Using a CSS class gives a more organized alignment, especially when working with platforms that support integrated HTML with Markdown, like Jekyll or GitHub Pages.

<!-- Laughs in Markdown --> ![alt text](image.jpg){: .right-align } <!-- Here comes the fashion police! --> <style> .right-align { float: right; margin-left: 10px; } </style>

Using an external stylesheet helps bypass the use of inline styles, leading to more clean and maintainable Markdown.

Managing Markdown's shortcomings

The limitations of Markdown can be bypassed with Markdown processors like Maruku, Kramdown, PHP Markdown Extra or Python Markdown that support extended features:

<!-- Markdown on steroids --> ![alt text](image.jpg){: .align-right}

These processors grant you the ability to add CSS classes and attributes directly to the Markdown.

Picking your Markdown flavor

Markdown comes in various flavors. Choose the one that supports extended syntax to leverage image alignment options.

If supported, HTML tags provide complete control over image alignment.

Modern alignment methods

For more contemporary design standards, Flexbox & Grid are ideal:

<!-- Center of attention --> <div style="display: flex; justify-content: center;"> <img src="image.jpg" alt="alt text" /> </div>

Flexbox is great for centered images. Advanced layout options can be achieved using CSS Grid.

Markdown myths & mishaps

Watch out for common issues when aligning images:

  • Overlapping Text: Ensure floating images don't step on text.
  • Broken Responsiveness: Using static values might compromise responsiveness. Use percentages or viewport units.
  • Markdown Limitations: All Markdown renderers don't support advanced features or embedded HTML.

Enhancements for different platforms

Platforms like GitHub and Jekyll have unique Markdown quirks. Follow the inline-specific syntax to optimize image alignment.

Never forget to test your Markdown to confirm the correct rendering.