What is the use of the properties before the asterisk in CSS?

王林
Release: 2023-09-16 14:53:04
forward
961 people have browsed it

CSS 中星号前面的属性有什么用?

In web development, CSS (Cascading Style Sheets) enables developers to determine the visual appearance and layout of a website. However, since different browsers have different support mechanisms for CSS, there may be inconsistencies when the compiler renders web pages.

To overcome this compatibility issue, developers often choose to use CSS hacks to ensure that their web pages appear consistently across different browsers and devices. One such hack is the asterisk attribute (also known as the asterisk attribute hack), which is used against certain versions of Internet Explorer (IE) that have limited support for CSS.

In this article, we will explore the star attribute hack in CSS and discuss its uses and limitations. We'll also provide examples of how to use this technique effectively and best practices for implementing it in CSS code.

Celebrity First Property

This is a CSS hack for declaring different properties of HTML elements. An attribute preceded by an asterisk (*) or an underscore (_) is only rendered in IE 7 and below, while for IE 8 and above it is treated as garbage by the compiler.

grammar

element{
   background-color: red;  // for other browsers
   _background-color: red;   // for IE 6 and below
   *background-color: red;   // for IE 7 and below
}
Copy after login

Now, let us understand this better with an example. We will use this hack to render properties in IE 6, IE 7 and other browsers.

NOTE - This hack works with different browsers, so run the program in the specified browser to observe the correct output.

Example

Here's how to tell the compiler to render CSS properties to elements in Internet Explorer 7 and earlier.

<!DOCTYPE html>
<html>
<head>
   <title>Internet Explorer 7 and Below Versions</title>
   <style>
      .my-div {
         background-color: red;
         width: 30%;
         height: 80%;
         padding: 3px;
         letter-spacing: 1px;
         margin-top: 40px;
         /* default margin applied in all other browsers */
         *margin-top: 0;
         /* IE6 margin */
      }
   </style>
</head>
<body>
   <h1>Star Preceded Property</h1>
   <h3>Given below is a div element whose margin-top will be 0 in IE 6 while it will be 20px in all other browsers.</h3>
   <div class="my-div"> This is my div element. </div>
</body>
</html>
Copy after login

For IE7 and below, the margin-top of div elements is zero.

If you run the code in any other browser, the margin-top of the div element is 40px.

In the example above, the CSS selector .my-div applies a top margin of 40 pixels. However, the *margin-top: 0; rule only works in Internet Explorer 6, setting the margins to 0 pixels. The asterisk (*) before the property name (margin-top) is the "asterisk" in the "star property hack". This is a syntax error in other browsers, so they ignore this line.

Example

Another way to make the compiler render CSS properties to elements in Internet Explorer 6 and earlier is explained below. It doesn't work with IE 7.

<!DOCTYPE html>
<html>
<head>
   <style>
      .my-div {
         background-color: blue;
         /* default background color */
         width: 30%;
         height: 80%;
         padding: 3px;
         letter-spacing: 1px;
         _background-color: red;
         /* background color in IE 6 and below versions */
      }
   </style>
</head>
<body>
   <h1>Star Preceded Property </h1>
   <h3>Given below is a div element whose background color will be red in IE 6 and below while it will be blue in all other browsers.</h3>
   <div class="my-div"> This is my div element. </div>
</body>
</html>
Copy after login

For IE6 and below, the background color of the div element will be blue.

If you run the code in any other browser, the background color will be red.

In the example above, the CSS selector .my-div applies the red background color. However, the _background-color: blue; rule only works in Internet Explorer 6, setting the background color to blue.

Uses and Limitations of Star Property Hack

The "star attribute" is a technique used in the past to target a specific version of Internet Explorer using CSS styles. While it achieves this goal effectively, it also has some advantages and disadvantages.

use

  • It enables web developers to apply various specific CSS styles to older versions of Internet Explorer without affecting the results in all other browsers. This helps create a consistent and unified experience for users across multiple browsers.

  • It is easy to use and reduces the amount of code, making it an attractive alternative for web developers. It prevents them from writing conditional comments or specific stylesheets for specific browsers.

  • It is widely used and popular in the web development community, which makes it easy to find examples and support. Moreover, it is also very user-friendly.

limit

  • "Asterix before property" is a hack. This is not a standard and compatible way of writing CSS code. It relies on a bug in Internet Explorer to work. Additionally, there is no guarantee that it will work in future modified versions of the browser or in other browsers.

  • This makes the code more difficult to read and maintain. Since it involves writing non-standard code, it is difficult to understand what the code does without additional comments or documentation.

  • This may cause unintended consequences, such as affecting other elements on the page or causing the browser to behave unexpectedly.

in conclusion

This technique is a method that targets specific browsers with different styles, providing a fallback for older browsers such as Internet Explorer 6. Overall, while "star attribute hacking" was useful at the time, it is no longer recommended as a best-choice web development practice. Modern web development techniques focus on using standard and compatible code that runs on multiple browsers rather than relying on browser-specific hacks.

The above is the detailed content of What is the use of the properties before the asterisk in CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template