如何在 HTML 中使用不同的 CSS 类?
我们使用 class 属性为 HTML 元素指定一个类。
多个 HTML 元素可以共享同一个类。使用类的各种属性,例如更改颜色、字体等,我们可以为这些 HTML 元素定义样式规则。具有该类的元素将根据定义的规则进行格式化。这称为类选择器。
要选择具有特定类的元素,您需要编写一个句点 (.) 字符,后跟类的名称,例如,让我们看一下".black"类。
.black {
color: #000000;
}
将文档中每个类属性设置为 black 的元素的内容渲染为黑色。例如,仅将 <h3> 的内容渲染为黑色。元素的 class 属性设置为 black。
h3.black {
color: #000000;
}
我们使用 class 属性指向样式表中的类名。JavaScript 也可以使用它来访问具有特定类名的元素。
示例 1
以下示例有两个 <div> 元素,它们的 class 属性值相同。所有 <div> 元素将根据 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>Jason</h2> <ol> <li>Bachelor's of Engineering</li> <li>IT stream</li> <li>section -A</li> </ol> </div> <div class="information"> <h2>Abdul</h2> <ol> <li>Bachelor's of Engineering</li> <li>IT stream</li> <li>section -B</li> </ol> </div> </body> </html>
以下是上述示例程序的输出。
示例 2
以下示例有两个 <div> 元素,它们的 class 属性值不同。这两个 <div> 元素将根据 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>
要定义多个类,请用空格分隔类名或指定不同的值。元素将根据指定的类设置样式。
示例 3
以下示例有三个 <div> 元素,它们的 class 属性值不同。两个 <div> 元素将根据 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>Jason</h2> <ol> <li>Bachelor's of Engineering</li> <li>IT stream</li> <li>section -A</li> </ol> </div> <div class="information"> <h2>Abdul</h2> <ol> <li>Bachelor's of Engineering</li> <li>IT stream</li> <li>section -B</li> </ol> </div> <div class="computerscience"> <h2>Satya</h2> <ul> <li>Bachelor's of Engineering</li> <li>Cse stream</li> <li>section -A</li> </ul> </div> </body> </html>
以下是上述示例程序的输出。
示例 4
另一个示例可以包含对 <p> 标签进行样式设置。通过以下代码,为所有 <p> 元素添加 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
<p> 标签的样式可以通过多个类(例如此处的设备和配件)来实现 -
<!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 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

