How to add class in image markdown in Ghost?
Asked Answered
L

5

23

In Ghost, the markdown for image is

![alt](src)

Is there a hidden format for adding a class in the img tag? Or is this feature not in Ghost yet?

I wanted to have a result like this:

<img src="src" alt="alt" class="img-thumbnail">

I don't want to use the html markup. I really need to achieve this using markdown. Please help..

Lasalle answered 25/3, 2014 at 7:0 Comment(0)
G
69

In plain markdown classes for images are not possible. In some implementations it is possible, e.g. markdown extra uses ![alt](src) {.class}. I tried it, but in Ghost it is not possible, neither ![alt|img-thumbnail](src). Also no hints in the doku.

But you can use a workaround:

If you use the src as css-attribute!

Just add an 'useless' hash to the URL:

![alt](src#img-thumbnail)

Then you can address this in CSS or JavaScript:

img[src$='#img-thumbnail'] { float:left;}

Geomorphic answered 27/3, 2014 at 9:43 Comment(6)
This awesome! However it does not seem to work with pandoc used with option --self-contained, that is when image gets embedded as a base64.Lucila
@Michał the useless hash works for me also with base64 jsfiddle.net/g6w2o91k , can you give me the code of your base64 image.Geomorphic
this is not even a hack, but a very elegant method. Wouldn´t have find out on my own, thanks !Shrewish
Anyone looking to do this in Kramdown (Jekyll) will need to knock out the space between the src and class: ![alt](src){.class}Thamos
FYI, this won't work in github's markdown. See #40261961Spindling
THIS IS SOME KING ###! I was looking for a solution for this unsing vuepress.. but the plugins doesn't seems to work very wellRamos
J
3

this is perhaps too obvious but you can put it in any element you wish i.e.

 <div class="myDiv">
    ![](...)
 </div>

and then use css inheritance

.myDiv img { width: 50px; }

since markup in ghost supports html (probably most do) you can also do regular <img...> tags as well

Jonathonjonati answered 13/6, 2016 at 23:37 Comment(0)
M
0

in ghost the file post.hbs rendering de {{content}} whit class .kg-card-markdown you can create this function for js add class in all image in post content

function imageResponsive() {
  $('.kg-card-markdown').each(function(){
    $('img').addClass('img-responsive');     
  });
}
Modernistic answered 11/2, 2018 at 20:45 Comment(0)
M
0

in ghost the file post.hbs rendering de {{content}} whit class .kg-card-markdown u can use css to whit sass

.kg-card-markdown { 
  img{
    @extend .img-responsive;
  } 
} 
Modernistic answered 12/2, 2018 at 0:51 Comment(1)
While this code-only post might answer the question, please add an explanation of why it does so. This will help readers evaluate the answer for their situation.Haustorium
N
-6

You can add them through jQuery with the following code before the body end tag on the default.hbs file

$('img').addClass('img-thumbnails');
Noncooperation answered 6/5, 2015 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.