如何在单个元素上使用多个css类(use multiple css classes on a single element)

级联样式表通过挂接应用于该元素的属性来定义网页元素的外观。这些属性可以是ID或类,与所有属性一样,它们向附加到的元素添加有用的信息。...

级联样式表通过挂接应用于该元素的属性来定义网页元素的外观。这些属性可以是ID或类,与所有属性一样,它们向附加到的元素添加有用的信息。

根据添加到元素的属性,可以编写CSS选择器来应用必要的视觉样式,以实现该元素和整个网站的外观。

虽然ID或类都是为了用CSS规则连接到它们,但现代web设计方法更喜欢类而不是ID,部分原因是它们不太具体,更易于整体使用。

css中的一个或多个类?

在大多数情况下,您为一个元素指定一个类属性,但实际上,您并不局限于一个类,就像使用ID一样。虽然一个元素只能有一个ID属性,但您可以为一个元素提供几个类,在某些情况下,这样做会使页面更容易设置样式,也更灵活。

如果需要为一个元素分配多个类,请添加其他类,并在属性中用空格将它们分隔开。

例如,本段分为三类:

This would be the text of the paragraph

This sets the following three classes on the paragraph tag:

  • Pullquote
  • Featured
  • Left

Notice the spaces between each one of these class values. Those spaces are what sets them up as different, individual classes. This is also why class names cannot have spaces in them because doing so would set them as separate classes.

Once you have your class values in HTML, you can then assign these as classes in your CSS and apply the styles you would want to add. For example.

.pullquote { ... }.featured { ... }p.left { ... }

In these examples, the CSS declarations and values pairs appear inside the curly braces, which is how those styles would be applied to the appropriate selector.

If you set a class to a specific element (for example, p.left), you can still use it as part of a list of classes; however, be aware that it will only affect those elements that are specified in the CSS. In other words, the p.left style will only apply to paragraphs with this class since your selector is actually saying to apply it to "paragraphs with a class value of left," By contrast, the other two selectors in the example do not specify a certain element, so they would apply to any element that uses those class values.

Multiple Classes, Semantics, and JavaScript

Another advantage of using several classes is that it increases interactivity possibilities.

Apply new classes to existing elements using JavaScript without removing any of the initial classes. You can also use classes to define the semantics of an element — add additional classes to define what that element means semantically. This approach is how Microformats works.

Advantages of Multiple Classes

Layering several classes can make it easier to add special effects to elements without having to create a whole new style for that element.

For example, to float elements to the left or right, you might write two classes:

left

and

right

with just

float:left;

and

float:right;

in them. Then, whenever you had an element you need to float left, you would simply add the class "left" to its class list.

There is a fine line to walk here, however. Remember that web standards dictate the separation of style and structure. Structure is handled using HTML while style is in CSS. If your HTML document is filled with elements that all have class names like "red" or "left," which are names that dictate how elements should look rather than what they are, you are crossing that line between structure and style.

Disadvantages of Multiple Classes

The biggest disadvantage of using several simultaneous classes on your elements is that it can make them a bit unwieldy to look at and manage over time. It may become difficult to determine what styles are affecting an element and if any scripts affect it. Many of the frameworks available today, like Bootstrap, make heavy use of elements with multiple classes. That code can get out of hand and hard to work with very quickly if you are not careful.

When you use several classes, you also run the risk of the style for one class overriding the style of another. This collision then can make it difficult to figure out why your styles aren't being applied even when it appears that they should. Remain aware of specificity, even with the attributes applied to that one element.

By using a tool like the Webmaster tools in Google Chrome, you can more easily see how your classes are affecting your styles and avoid this problem of conflicting styles and attributes.

  • 发表于 2021-09-08 05:49
  • 阅读 ( 77 )
  • 分类:数学

你可能感兴趣的文章

如何用selenium制作网络爬虫

...称为q。我们使用该方法在页面上定位HTML元素WebDriver.findElement文件(). ...

  • 发布于 2021-03-11 23:41
  • 阅读 ( 383 )

图标字体对你的网站来说非常棒:原因如下

...么是图标字体?今天我将向您展示什么是图标字体,以及如何使用它们来活跃您的网站。我们开始吧。 ...

  • 发布于 2021-03-14 08:32
  • 阅读 ( 200 )

如何建立一个网站:初学者

...许你读过我们的一些HTML(理解HTML)和CSS教程,但不知道如何在更大的项目中使用这些语言。 ...

  • 发布于 2021-03-15 13:25
  • 阅读 ( 262 )

10分钟内可以学习10个简单的css代码示例

... 下面是几个简单的CSS示例,向您展示如何在web页面上进行一些基本的样式更改。 ...

  • 发布于 2021-03-18 17:25
  • 阅读 ( 423 )

css3基本属性备忘单

...xt.List Properties list-styleDefines the display style for a list and list elements.list-style-imageSpecifies the image to be used as a list-item marker.list-style-positionSpecifies the position of the list-item marker.list-style-typeSpecifies the marker style for a list-item.Margin Propertiesmargin...

  • 发布于 2021-03-19 04:34
  • 阅读 ( 298 )

如何使用css选择器定位网页的一部分

...的格式。每一条规则都由两部分组成:设置什么样式以及如何设置样式。第一部分使用一系列称为“选择器”的术语进行控制 ...

  • 发布于 2021-03-29 22:09
  • 阅读 ( 304 )

网站的英雄:理解dom

...码之间的接口层。当您访问一个网站时,您将看到浏览器如何呈现该网站的DOM。编写HTML时,实际上是在使用DOM的API(编程接口)编程。 ...

  • 发布于 2021-03-30 05:25
  • 阅读 ( 266 )

如何阻止谷歌隐私提醒信息不断出现?

...may not work if you are actually in the UK). Use AdBlock AdBlock can block elements by CSS selectors. The privacy reminder has the CSS class _vGg, so adding this rule to AdBlock should hide it: ##._vGg They may change the class at any point and the rule will stop working. Use Stylish or User Scrip...

  • 发布于 2021-04-10 08:12
  • 阅读 ( 231 )

部门(div)和跨度(span)的区别

...Featured”></div> </body> </html> The <div>  elements can have different attributes, specifically different sizes for resp***ive interaction depending on the screen size of the device being used. Here is an example of how each <div> element can be styled in the HTM...

  • 发布于 2021-06-24 23:59
  • 阅读 ( 260 )

身份证件(id)和css中的类(class in css)的区别

...中id和class的主要区别在于id用于将样式应用于一个唯一的元素,而class用于将样式应用于多个元素。 web开发中有多种技术。开发网站的主要语言是HTML、CSS和JavaScript。HTML代表超文本标记语言。它有助于开发网页的结构。JavaScript...

  • 发布于 2021-06-30 21:53
  • 阅读 ( 324 )
bbd304565
bbd304565

0 篇文章

相关推荐