Table of Contents
Celebrity First Property
grammar
Example
Uses and Limitations of Star Property Hack
use
limit
in conclusion
Home Web Front-end CSS Tutorial What is the use of the properties before the asterisk in CSS?

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

Sep 16, 2023 pm 02:53 PM

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!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles