2010-10-12 196 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

相關問題