css – use universal ‘*’ selector vs. html or body selector?

Applying styles to the body tag will be applied to the whole page, so

body { font-family: Verdana }

will be applied to the whole page. This could also be done with

* {font-family: Verdana} 

which would apply to all elements and so would seem to have the same effect.

I understand the principle that in the first instance the style is being applied to one tag, body for the whole page whereas in the second example the font is being applied against each individual html elements. What I am asking is what is the practical difference in doing that, what are the implications and what is a reason, situation or best practice that leads to using one over another.

One side-effect is certainly speed (+1 Rob). I am most interested in the actual reason to choose one over the other in terms of functionality.

1

Functional differences between these two choices of CSS selector…(my take)

body

  • Applies style properties to body element.
  • Elements within body may inherit the property values. Some properties default to ‘inherit’.
  • Style declarations that match an element within body can override the inherited style.

The Universal Selector * (all elements)

  • Applies style properties to all individual elements.
  • Replaces inherited style properties, and default ‘initial values’. Blocks inheritance.
  • Other, more specific css selectors that match an element will replace the style properties applied by *.

Suggestions

  1. Use body for style properties that default to inherit, such as font, color, in order to provide a sensible default value for elements, reducing the need to explicitly code for every case, and preserving the ability for elements contained at lower levels below body to inherit from their parents.
  2. Probably better not to use the Universal Selector * in this case. It interrupts inheritance between other elements within body, and may force you to write more css rules to compensate. It can be a factor in slowing rendering of pages. This depends on the size and content of the page and how many css rules there are.

0

Try something like this

body { font-family: Verdana }
table { font-family: Arial }

against

* { font-family: Verdana }
table { font-family: Arial }

And see which styles are applied to the cells of the table.

There is a difference between “applied to the whole document” and “applied to every element of the document” when you’re dealing with cascading stylesheets.

Applying a cascading style to the body applies it to all tags within body until a tag overwrites it. Then the overwritten style is applied to all tags within that one.

However, there are some styles that don’t cascade, such as margin and padding (usually where it makes no sense to). Those can only be applied to specific tags and that’s where a wildcard can come in useful (albeit rarely).

Most non-cascading styles also have a value of inherit (eg. margin: inherit), which means “take the parent tag’s values”.

2

I’ve forgotten the complete details but, with the * selector, performance is slowed down as each and every element gets evaluated as the browser parses the CSS and applies the style. iirc, setting the font, in this case, to just the parent makes one reference for each element and additional work is not needed.

There are some other issues, too, but, again, I don’t recall them all and have not used * in years.

As others have mentioned, body { font-family: Verdana } will select the font Verdena to only those elements which inherit the font-style property from its parents such that all of its parents too inherit the property eventualy form the body element, and, * {font-family: Verdana} will select the font Verdena to all the elements. I’d like to illustrate the difference with an example:

Using body selector:

body 
{ 
font-family: courier;
}

<p> This is paragraph with courier font </p>
<form>
<label for="X">Please type inside me: </label><input type="text" naem="X" value="Not Courier :("> 
</form>

Using universal selelctor *:

* 
{ 
font-family: courier;
}

<p> This is paragraph with courier font </p>
<form>
<label for="X">Please type inside me: </label><input type="text" naem="X" value="Courier :)"> 
</form>

Use the css and html codes of the examples you’ll recognize that <input> element doesn’t inherit the font-style at all and hence whatever you type into the form gets the default font-style of user-agent style sheet. In the second example the universal selector* explicitly sets font-style for every element including the input element.

The general guideline for good CSS design is to be as specific as possible, but not more.

So, in your example, applying the style to the body element would be as specific as possible and certainly more specific than the ‘*’ selector.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật