"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Does Setting a \'title\' Attribute on a `` Tag Prevent CSS Styles from Rendering?

Why Does Setting a \'title\' Attribute on a `` Tag Prevent CSS Styles from Rendering?

Published on 2024-11-14
Browse:684

Why Does Setting a \'title\' Attribute on a `` Tag Prevent CSS Styles from Rendering?

Why Does the 'title' Attribute Interfere with CSS Rendering?

The HTML document provided attempts to apply both a.css and b.css to elements on the page. However, the styles from b.css are not rendered, as demonstrated in the code snippet.

By setting the title attribute on the element linking to b.css, the stylesheet is categorized as a "preferred stylesheet." In contrast, stylesheets without the title attribute are considered "persistent stylesheets." According to the HTML specification:

Stylesheet Types

  • Persistent stylesheets: Linked with rel="stylesheet" and no title attribute. Always applied during rendering.
  • Preferred stylesheets: Linked with rel="stylesheet" and a title attribute. Grouped by title, but browsers only render one preferred stylesheet per group.
  • Alternate stylesheets: Linked with rel="alternate stylesheet" and a title. Grouped by title, allowing users to select stylesheets from a browser-provided interface.

In this case, by adding the title attribute, you are inadvertently classifying b.css as a preferred stylesheet. As there should only be one preferred stylesheet per group, the browser is choosing one of the stylesheets to render, resulting in the exclusion of b.css.

Therefore, to ensure that both a.css and b.css are applied, you should remove the title attribute from the element linking to b.css. This will classify it as a persistent stylesheet, ensuring that it is used alongside a.css during rendering.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3