使用Markdown,如何将图像及其标题居中?

Che*_*tan 41 html css markdown center

我想最终得到:

Hello there!

      <image>
      This is an image

Hi!
Run Code Online (Sandbox Code Playgroud)

图像和文本This is an image在页面上居中的位置.如何使用Markdown实现这一目标?

编辑:请注意,我希望在页面上水平居中图像和文本.

Che*_*tan 36

我想我只需要在我想要水平对齐任何东西的地方使用HTML.

所以我的代码看起来像这样:

Hello there!

      <center><img src="" ...></center>
      <center>This is an image</center>

Hi!
Run Code Online (Sandbox Code Playgroud)

  • 弃用并不意味着它不能被使用.Chetan给出了有效的建议.我认为这个评论不值得他目前的-12评级. (18认同)
  • 自1998年以来,HTML4中的`<center>`元素被**弃用**.只需使用CSS使它或它的包装器成为具有固定宽度和`margin:0 auto;`的块元素. (17认同)
  • 是的,<center>已被弃用多年.但是,您可以确定浏览器仍然会在2525年完全支持它. (10认同)
  • 当然不应该使用它,这就是"弃用"存在的原因.单行CSS是正确的方法,*内联*如果你不想要样式表.没有理由继续使用`<center>`. (8认同)
  • 不赞成使用复杂解决方案的简单解决方案不应该是imo.两者都应该共存.叹. (8认同)
  • 其次,标签可能已弃用,但在这种情况下很有用。 (2认同)
  • 这件事情让我感到很快乐。在这种情况下,如果弃用意味着只是不好的做法或错误,则更重要。`&lt;center&gt;` 更适合用 Markdown 语言而不是使用 css,因为 Markdown 语言的主要目的是尽量减少写作时的工作,因为你不想一直专注于格式化。否则你甚至可以在 html 中做到这一点。Markdown 存在是因为 html 太复杂了! (2认同)
  • 我对这里的每一条评论都投了赞成票,大意是“已弃用?那又怎样——它很有用” (2认同)

小智 20

如果您使用kramdown,则可以执行此操作

Hello there!

{:.center}
![cardinal](/img/2012/cardinal.jpg)  
This is an image

Hi!

.center {
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)


tre*_*mor 20

我认为我有一个简单的解决方案,因为你可以定义CSS.它也不需要任何扩展或HTML!首先是你的降价图像代码:

![my image](/img/myImage.jpg#center)  
Run Code Online (Sandbox Code Playgroud)

请注意添加的url哈希#center.

现在在CSS中添加此规则:

img[src*='#center'] { 
    display: block;
    margin: auto;
}
Run Code Online (Sandbox Code Playgroud)

您应该能够像这样使用url哈希,就像定义类名一样.

要查看这个,请查看我的JSFiddle,使用SnarkDown在textarea中解析MarkDown - https://jsfiddle.net/8q41zbfj/


vdc*_*uis 18

Mou(也许是Jekyll),这很简单.

-> This is centered Text <-
Run Code Online (Sandbox Code Playgroud)

因此,考虑到这一点,您可以将其应用于img语法.

->![alt text](/link/to/img)<-
Run Code Online (Sandbox Code Playgroud)

此语法不适用于仅实现Daring Fireball上记录的内容的Markdown实现.


Mat*_*Cat 8

你需要一个具有定义高度的块容器,行高相同的值和垂直对齐的图像:middle; 它应该工作.

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%;
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为Markdown不用于定位 (4认同)
  • 我可以在`div`中使用Markdown语法吗? (2认同)

Ali*_*ich 8

我很惊讶没有人以这种方式提及:

Hello there!

<p align="center">

  <img src="">
  This is an image

</p>

Hi!
Run Code Online (Sandbox Code Playgroud)

  • 可以确认,这适用于 GitHub README。感谢您的回答 (3认同)

rai*_*tin 7

与kramdown(由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.
Run Code Online (Sandbox Code Playgroud)

或使用@(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>
Run Code Online (Sandbox Code Playgroud)

  • 这就是答案,现在我知道为什么珠宝在海底了:wink: (2认同)