您使用的是什麼樣的<img1 />
是無效的HTML標籤,序選擇唯一的元素,我們定義class
或id
的元素,即使由於某種原因,你不想定義一個class
或id
,您可以定義Custom Attributes的元素,具有data-
前綴,但你不能定義自定義HTML元素 ..
定義class
每個img
標籤.. (聲明id
,確保他們是獨特的,你不能對多個元素使用相同的id
..)
<img src="" class="img1" />
<img src="" class="img2" />
而且比我們寫出這樣相應的CSS
.img1 {
/* Selects element having a class of .img1 */
}
.img2 {
/* Selects element having a class of .img2 */
}
假如你有共同的屬性被宣佈爲兩個,用一個逗號,不是他們組重新聲明爲其他class
獨特的性能,如
.img1, .img2 {
padding-left: 15pt;
/* Other common properties goes here */
}
.img2 {
/* Unique properties for .img2 */
}
如果你是不是想找一個class
分配給每個img
標籤,比你可以使用:nth-of-type
或:nth-child
僞選擇每個img
,但要確保你使用它們包裝一個div
或section
任何你認爲合適的...
假設我們總結他們div
標籤裏面,我們分配一個class
說.container
<div class="container">
<img src="" />
<img src="" />
</div>
所以在這裏,你可以選擇第一個img
作爲
.container img:nth-of-type(1) {
/* Properties here will apply to first img tag nested inside element having
class of .container */
}
.container img:nth-of-type(2) {
/* Properties here will apply to second img tag nested inside element having
class of .container */
}
注:這些CSS3僞,但這些被廣泛支持,但像往常一樣,舊的IE版本,就會破壞遊戲,雖然polyfills可用,但最後你必須決定你要選擇什麼。
此外,在每個我上面定義的,如果你不知道特異性的選擇的特殊性問題,不是給出一個快速閱讀here ...
只是定義樣式'img1'和HTML代碼使用它作爲'
'這應該工作! –
Nullpointer