-- Leo's gemini proxy

-- Connecting to gemlog.blue:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

How to center images horizontally using Grav


Note: This post is a Gemini space version of my post originally published on May 21, 2020

https version of this post


I've been playing around a bit with Grav. I was posing the question to myself: for the relatively simple use-cases I'm dealing with, could it possibly work for my purposes as an alternative to ProcessWire?


The problem

I was initially dismayed to find that Grav uses Markdown as its editor, which does not offer native support for horizontal centering of anything (text or images). However, Grav offers some tweaks that help make it easier to do specific things you might commonly want to do. I tried writing a sample article, and I found that one of the hardest things to do was to center an image horizontally. And horizontal centering of images is something I would typically do in most of the articles I would write.


So the lack of easy horizontal centering is a highly significant drawback IMHO (most people do want to center images in an article!) However, this issue is made up for by other things in Grav: the relative speed, ease and flexibility of custom theming and built-in support for metadata. It's rare to get all those in one package natively.


After playing around a bit, I found a solution to the horizontal centering of images. I'm not saying it's the only solution - I've been playing with Grav for only a few hours at this point and still have lots to learn. I'll update this if I find a more elegant solution later.


Solution A - easiest setup but annoying for ongoing use

1. In admin panel, use Markdown Extra instead of the default Markdown. This allows you to add extra html beyond what is normally allowed in Markdown. You'll need it.


When reading the PHP Markdown Extra documentation by Michael Fortin, you can see what you need to do, which I'll describe below in the next step.

PHP Markdown Extra documentation by Michael Fortin


2. Wrap the image markdown code in a div with markdown="1" and add whatever style you like to the div, as follows:

Configuration -> System -> Markdown -> Markdown Extra


<div markdown="1" style="text-align:center">
![My Image](/images/apples_500.jpg)
</div>

The above code assumed you have put all your images into an "images" folder. I'm still debating which way is best; images folder or just use the Grav default of per-page images. I'm leaning toward per-page images, since I don't expect to use most images in more than one article.


If you're using per-page images, only the image code would change, so it would look like this:


<div markdown="1" style="text-align:center">
![My Image](beach_300.jpg)
</div>

If you only want to center text and not images, you don't even have to worry about enclosing it in a div with markdown set to 1. Simply insert the HTML, e.g.

<p style="text-align:center">Let's center this text.</p>

Solution B - harder initial setup but easiest for ongoing use

1. Although you won't need Markdown Extra, you'll need to add a code snippet to your CSS file, i.e. to whichever CSS file is in use. Most Grav themes support and automatically load the file custom.css (in your theme's CSS folder) so you can add it in there. Here's what to add:


.align_center {
    /* for images  */
    display: block;
    margin: 1em auto;
    position: relative;
    top: 0.5em;
}

2. Insert your image into your content area as normal by dragging it in, then add "?classes=align_center" (without the quotes) to the filename of your image.


Here's an example:


![My new image](sunset.jpg?classes=align_center)

This is probably the easiest way to do it so long as you're planning on sticking with the same theme. However, if you're planning on changing themes then either you'll have to add the CSS code snippet whenever you switch to a new theme, or simply use Solution A, which will still work even if you switch themes.


UPDATE September 3, 2020:


I decided that I will not be using Grav and instead will be staying with ProcessWire. This is because the documentation for custom fields (both the input and the usage) is much better for ProcessWire than for Grav. For example, when setting up a new field, limiting the range of valid data input into a custom field was intuitive in ProcessWire but poorly documented in Grav. This is not a big criticism of Grav; after all I've been using ProcessWire for years but only just started playing briefly with Grav, so I'm already more familiar with ProcessWire. Nonetheless, I see no compelling reason for me to use Grav as it does not appear to offer any advantage over ProcessWire for my particular use case, other than not requiring a database, which is not a dealbreaker for me.


It's certainly possible to center images horizontally in the body of the main text in Grav. However, it's rather clunky. This is something that ought to be achievable in one click - but is not. This is definitely a negative UX for the content creator. I'm not sure why they decided on the Markdown editor instead of something more fully featured (and customizable for those who want it) like CK Editor. That being said, Markdown Extra can handle a lot of stuff, and I'm finding it's actually much faster to put in code, links, etc all in one spot. As opposed to constantly toggling between HTML and WYSIWYG like I have been doing in CK Editor and other html editors each time I want to add specialty code.


I think when I get used to it, Markdown Extra will work out really well for me. But I still miss the ability to center images or text with just one click.

-- Response ended

-- Page fetched on Sun Jun 2 11:22:16 2024