search
HomeWeb Front-endCSS TutorialWhat is the use of the properties before the asterisk in CSS?

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
}

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 id="Star-Preceded-Property">Star Preceded Property</h1>
   <h3 id="Given-below-is-a-div-element-whose-margin-top-will-be-in-IE-while-it-will-be-px-in-all-other-browsers">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>

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 id="Star-Preceded-Property">Star Preceded Property </h1>
   <h3 id="Given-below-is-a-div-element-whose-background-color-will-be-red-in-IE-and-below-while-it-will-be-blue-in-all-other-browsers">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>

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools