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

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

王林
发布: 2023-08-19 09:33:16
转载
1077 人浏览过

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板