我們使用class屬性來為HTML元素指定一個類別。
多個 HTML 元素可以共用同一個類別。使用類別的各種屬性,例如更改顏色、字體等,我們可以為這些 HTML 元素定義樣式規則。具有該類別的元素將根據定義的規則進行格式化。這稱為類別選擇器。
要選擇具有特定類別的元素,您需要編寫一個句點(.)字符,後面跟上類別的名稱,例如,讓我們看一下“.black”類,
.black { color: #000000; }
對於我們文件中class屬性設定為black的每個元素,以黑色渲染內容。例如,僅對class屬性設定為black的
元素以黑色渲染內容。
h3.black {
color: #000000;
}
我們使用類別屬性來指向樣式表中的類別名稱。 JavaScript 也可以使用它來存取具有特定類別名稱的元素。
範例 1
下面給出了一個範例,其中我們有兩個 元素,它們的 class 屬性具有相同的值。所有 元素將根據 head 標記中的樣式定義進行相同的樣式設定。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="description" content="meta tag in the web document"> <meta name="keywords" content="HTML,CSS"> <meta name="author" content="lokesh"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .information,ol { border: 2px solid black; margin: 20px; padding: 20px; } ol{ background-color: darkgoldenrod; } </style> </head> <body> <div class="information"> <h2 id="Jason">Jason</h2> <ol> <li>Bachelor's of Engineering</li> <li>IT stream</li> <li>section -A</li> </ol> </div> <div class="information"> <h2 id="Abdul">Abdul</h2> <ol> <li>Bachelor's of Engineering</li> <li>IT stream</li> <li>section -B</li> </ol> </div> </body> </html>
以下是上述範例程式的輸出。
Example 2
的中文翻譯為:範例2
下面給出了一個範例,其中我們有兩個 元素,它們的 class 屬性具有不同的值。兩個 元素將根據 head 標記中的樣式定義設定樣式。
要定義多個類,請用空格分隔類名。元素將根據指定的類別進行樣式設定。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="description" content="meta tag in the web document"> <meta name="keywords" content="HTML,CSS"> <meta name="author" content="lokesh"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .room { font-family: monospace; font-size: 200%; color: tomato; text-align: center; } .two{ font-family: cursive; color: lawngreen; text-align: center; } </style> </head> <body> <p class="room">Meta tag contents are not visible on your browser.</p> <p class="room two"> The mata tag is added inside the head tag.</p> </body> </html>
要定義多個類,請用空格分隔類名或指定不同的值。元素將根據指定的類別進行樣式設定。
Example 3
的中文翻譯為:範例 3
給出以下範例,其中我們有三個具有不同值的class屬性的元素。根據head標籤中的樣式定義,兩個元素將被等同地進行樣式設置,而另一個元素將根據head標籤中的樣式定義進行樣式設置<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="description" content="meta tag in the web document"> <meta name="keywords" content="HTML,CSS"> <meta name="author" content="lokesh"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .information,ol { border: 2px solid black; margin: 20px; padding: 20px; } .computerscience,ul { border: 2px solid black; margin: 20px; padding: 20px; } ol{ background-color: brown; } ul{ background-color: tomato; } </style> </head> <body> <div class="information"> <h2 id="Jason">Jason</h2> <ol> <li>Bachelor's of Engineering</li> <li>IT stream</li> <li>section -A</li> </ol> </div> <div class="information"> <h2 id="Abdul">Abdul</h2> <ol> <li>Bachelor's of Engineering</li> <li>IT stream</li> <li>section -B</li> </ol> </div> <div class="computerscience"> <h2 id="Satya">Satya</h2> <ul> <li>Bachelor's of Engineering</li> <li>Cse stream</li> <li>section -A</li> </ul> </div> </body> </html>
以下是上述範例程式的輸出。
Example 4
的中文翻譯為:範例 4
另一個例子可以包含對
標籤進行樣式設定。透過以下方式,將所有具有class="device"的
元素進行樣式設定
<!DOCTYPE html> <html> <head> <style> p.device { background: #000000; color: #fffffF; } </style> </head> <body> <p>This is demo text</p> <p class="device">Information about devices.</p> <p>This is demo text</p> </body> </html>
範例 5
標籤的樣式可以透過多個類別來完成,即此處的裝置和配件 -
<!DOCTYPE html> <html> <head> <style> p.device { background: #000000; color: #fffffF; } p.accessories { text-align: center; } </style> </head> <body> <p class="device accessories">DEVICE DETAILS</p> <p class="device">Information about devices.</p> </body> </html>
以上是我們如何在HTML中使用不同的CSS類別?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

htmlattributesarecrucialinwebdevelopment forcontrollingBehavior,外觀和功能

alt屬性是HTML中標籤的重要部分,用於提供圖片的替代文本。 1.當圖片無法加載時,alt屬性中的文本會顯示,提升用戶體驗。 2.屏幕閱讀器使用alt屬性幫助視障用戶理解圖片內容。 3.搜索引擎索引alt屬性中的文本,提高網頁的SEO排名。

HTML、CSS和JavaScript在網頁開發中的作用分別是:1.HTML用於構建網頁結構;2.CSS用於美化網頁外觀;3.JavaScript用於實現動態交互。通過標籤、樣式和腳本,這三者共同構築了現代網頁的核心功能。

設置標籤的lang屬性是優化網頁可訪問性和SEO的關鍵步驟。 1)在標籤中設置lang屬性,如。 2)在多語言內容中,為不同語言部分設置lang屬性,如。 3)使用符合ISO639-1標準的語言代碼,如"en"、"fr"、"zh"等。正確設置lang屬性可以提高網頁的可訪問性和搜索引擎排名。

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外觀和互動,使網站互動,響應式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,類,類型,類型,和dissabledtransfransformformformformformformformformformformformformformformforment

toCreateAlistInHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulletedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,casundfornthetthetthetthetthetthetthetttributefordforderfordforderforderentnumberingsnumberingsnumberingStys。

HTML用於構建結構清晰的網站。 1)使用標籤如、、定義網站結構。 2)示例展示了博客和電商網站的結構。 3)避免常見錯誤如標籤嵌套不正確。 4)優化性能通過減少HTTP請求和使用語義化標籤。

toinsertanimageIntoanhtmlpage,usethetagwithsrcandaltattributes.1)usealttextforAcccessibilityandseo.2)instementRcsetForresponSiveImages.3)applylazyloadingWithLoadingWithLoading =“ lazy” tooptimizeperformance.4)tooptimizeperformance.4)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境