首页  >  文章  >  web前端  >  如何同时将样式应用于多个类?

如何同时将样式应用于多个类?

王林
王林转载
2023-08-19 09:33:16966浏览

How to apply styles to multiple classes at once?

For Applying Styles to Muitliple classes at once we are going to use dot(.) selector and comma(,). In this article, we will use the dot (.) selector and select all the elements with their class names using the selector and separated by a comma (,).

“class” 是一个HTML属性,它接受由空格分隔的类列表。这些类可以在CSS中用于为特定元素设置样式,或者在javascript中用于操作这些HTML元素。

Example 1

In this example, we will apply the font color “red” to the HTML elements that have classes “header” and “para”. We will achieve this using the dot (.) selector and concatenating them with a comma (,).

<!DOCTYPE html>
<html lang="en">
<head>
   <title>How to apply styles to multiple classes at once?</title>
   <style>
      .header, .para {
         color: red;
      }
   </style>
</head>
<body>
   <h3 class="header">How to apply styles to multiple classes at once?</h3>
   <p class="para">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</body>
</html>

Example 2

在这个例子中,我们将把字体颜色设置为“green”并将字体样式设置为“italic”应用到具有类名“header”和“para”的HTML元素上。我们将使用点(.)选择器,并将它们与逗号(,)连接起来来实现这一目标。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>How to apply styles to multiple classes at once??</title>
   <style>
      .header, .para {
         color: green;
         font-style: italic;
      }
   </style>
</head>
<body>
   <h3class="header">How to apply styles to multiple classes at once?</h3>
   <p class="para">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</body>
</html>

结论

In this article, we learned how to apply multiple CSS classes at once. We did this by first, selecting multiple classes at once, using the dot (.) selector provided by the CSS, and then assigning them the required styles in the stylesheet.

以上是如何同时将样式应用于多个类?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除