2010-10-12 193 views

回答

7

您需要一个具有定义高度的块容器,line-height和vertical-align: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%; 
} 
+2

我可以在'div'中使用Markdown语法吗? – Chetan 2010-10-12 09:43:31

+3

我认为Markdown不用于定位 – MatTheCat 2010-10-12 12:09:11

+0

哦,我正在寻找水平居中。 – Chetan 2010-10-12 20:24:19

27

我想我只是想用水平对齐任何东西来使用HTML。

所以我的代码是这样的:

Hello there! 

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

Hi! 
+16

自1998年以来,'

'元素在HTML4中被**弃用**。只需使用CSS将它或其包装变成一个具有固定宽度和'margin:0 auto;'的块元素就可以了。 – BalusC 2010-10-17 21:13:24

+17

已弃用并不表示无法使用。切坦提出了一个有效的建议。我认为他不值得他目前的评价为-12。 – 2012-12-14 19:58:51

+1

借调,标签可能会被弃用,但在这种情况下,它是有用的。 – Thriveth 2013-07-02 23:07:28

19

如果您正在使用kramdown,你可以做到这一点

Hello there! 

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

Hi! 

.center { 
    text-align: center; 
} 
18

Mou(也许化身),这是很简单的。

-> This is centered Text <- 

所以考虑到这一点,你可以将它应用于img语法。

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

此语法并不仅仅实现上的是什么Daring Fireball记录降价的实施工作。

4

随着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. 

,或利用@我觉得我有这应该工作给予你可以定义CSS的解决方案的响应(史蒂芬竹)

{:.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

这个。这是我出于某种原因痛苦地花了一个小时寻找的答案。 (第一个)谢谢@raisercostin。如果我能三次让你喜欢你,我会。 – Schwad 2017-08-03 09:02:14

+1

这就是答案,现在我知道珠宝为什么在海底:wink: – 2017-08-05 22:30:47

7

,并且不需要任何扩展。首先您的降价图片代码:

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

请注意添加的网址hash #center。

现在CSS

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

添加此规则,你应该能够使用URL的哈希像这样几乎就像定义一个类名。

+0

它的工作!但你能解释为什么CSS部分工作?它只是设置为阻止和自动保证金。没有中心的风格。它如何居中? – 2017-05-12 22:15:35

+0

在CSS中,当使用margin:auto时,元素将相对于其容器居中。 – tremor 2017-05-14 14:19:28

相关问题