2016-03-02 29 views
4

我嘗試使用IMG如何在css上調用圖像?

調用圖像,這是我的代碼:

<img src="/pc/images/leader.png"> 

這是可能把這個在CSS?像這樣的代碼: 而不使用背景圖像。

CSS

.leader{ 
    src: /pc/images/leader.png; 
} 

HTML

<img class="leader"> 

謝謝:)

回答

6

號你不能用CSS做。

CSS沒有使用前景圖像。您只能使用背景圖片。

不過,如果我理解你的問題,你可以做的地方使用pseudo element圖像:

.leader::after{ 
    content: ""; 
    background: url(path.jpg) no-repeat; 
    position:absolute; 
    top: 0; 
    left: 0; 
    width: 50%; 
    height: 300px; 
} 
+0

爲什麼50%的寬度? – symlink

+0

這只是一個例子。你可以定義你想要的。 –

+0

我想我的意思是,如果您知道圖像的尺寸,是否有沒有設置固定寬度的好處? – symlink

0
.leader::after{ 
    background: url(path.jpg) no-repeat 50% 50%; 
} 

if you use 50% 50% then the image will be at centre possition.