Using Markdown, how do I center an image and its caption?

前端 未结 7 791
小鲜肉
小鲜肉 2021-01-31 08:08

I want to end up with:

Hello there!

      
      This is an image

Hi!

Where the image and the text This is an image

相关标签:
7条回答
  • 2021-01-31 08:18

    If you are using kramdown, you can do this

    Hello there!
    
    {:.center}
    ![cardinal](/img/2012/cardinal.jpg)  
    This is an image
    
    Hi!
    
    .center {
      text-align: center;
    }
    
    0 讨论(0)
  • 2021-01-31 08:24

    In Mou (and perhaps Jekyll) this is quite simple.

    -> This is centered Text <-
    

    So taking that in mind you can apply this to the img syntax.

    ->![alt text](/link/to/img)<-
    

    This syntax doesn't work for Markdown implementations that implement only what is documented on Daring Fireball.

    0 讨论(0)
  • 2021-01-31 08:29

    You need a block container with a defined height, same value for line-height and image with vertical-align:middle; It should work.

    Hello there !
    
    <div id="container">
        <img />
        This is an image
    </div>
    
    Hi !
    
    #container {
        height:100px;
        line-height:100px;
    }
    
    #container img {
        vertical-align:middle;
        max-height:100%;
    }
    
    0 讨论(0)
  • 2021-01-31 08:31

    I think I have a simple solution that will work given that you can define CSS. It also does not require any extensions or HTML! First your markdown image code:

    ![my image](/img/myImage.jpg#center)  
    

    Note the added url hash #center.

    Now add this rule in CSS:

    img[src*='#center'] { 
        display: block;
        margin: auto;
    }
    

    You should be able to use a url hash like this, almost like defining a class name.

    To see this in action, check out my JSFiddle using SnarkDown to parse MarkDown in a textarea - https://jsfiddle.net/tremor/6s30e8vr/

    0 讨论(0)
  • 2021-01-31 08:31

    With kramdown (used by githubpages)

    {: style="text-align:center"}
    That is, while there is value in the items on
    the right, we value the items on the left more.
    

    Or using the response from @(Steven Penny)

    {:.mycenter}
    That is, while there is value in the items on
    the right, we value the items on the left more.
    
    <style>
    .mycenter {
        text-align:center;
    }
    </style>
    
    0 讨论(0)
  • 2021-01-31 08:36

    I figured that I'd just have to use HTML where I want to horizontally align anything.

    So my code would look like this:

    Hello there!
    
          <center><img src="" ...></center>
          <center>This is an image</center>
    
    Hi!
    
    0 讨论(0)
提交回复
热议问题