views:

59

answers:

3

is body properties and * properties have different?

i always use body and html properties same. can i use * properties with body and html? and what should be different in * and body property? i unable to understand that is this necessary to use these both property?

if i use one of them than its create any problem?

i use this

*{
    margin:0;
    padding:0;
    background:#FFF;
}

and in body

body, html{
    margin:0;
    padding:0;
    background:#FFF;
    font:normal 12px verdana;
    color:#0086ca;
}

when i link a text its take background. and when i remove background from * this didn't take bg color.

+2  A: 

* (Asterisk) - It is a wildcard, this means it will select all elements within that portion of the DOM. It's a universal rule which affect on every element. for example:

Margin every element on the page

* {
    margin: 10px;
}

All HTML components will have those attributes.

Body affects only on the body tag...The elements within the tag aren't affected - (they are not getting the same attributes.)

cthulhu
Priority> and if two instructions with the same property are defined for both `*` and `body` (or any other element), than the one defined for the element will have a higher priority (more weight) and its value will appear.
Felipe Alsacreations
+1  A: 

body applies to the <body> tag, while * applies to every tag. An example of the difference can be seen in the following:

body { margin: 2cm; }

versus

* { margin: 2cm; }

The first gives the body a margin – the second gives every element a margin.

On the other hand, the following code:

body { font-family: Courier; }

will change the font family in the whole document since CSS uses cascading styles, i.e. nested tags inherit certain style properties from their parents – in this case, the font.

Konrad Rudolph
+1  A: 

Using * in CSS matches any element. Using it alone is rarely useful, because you will target every element in the page.

If you for have html code like this:

<html>
<head>
  <title></title>
  <style>
  body { font-size: 50px; }
  </style>
</head>
<body>
  <div>
    <form>
      Name: <input type="text"/>
    </form>
  </div>
</body>
</html>

The font size set for the body will affect the text "Name:", but it will not affect the font size of the input element, as it has a specific size set by default.

If you now add the style * { border: 10px solid red; font-size: 100px; } this will put a border on the body, div, form and input elements, and both the text and the input element will get the font size.

The * selector is more useful in combination with other selectors, like selecting any child element to a specific element:

#Menu > * { float: left; }

Regarding what to use for the html and body element, you only need to set the margin, padding and background for the body element.

Guffa
@guffa is this necessary to use * properties or not in my condition?
kc rajput
@kc: No, it's certainly not neccesary. It's rarely used, and when it's used it's mostly for css hacks (the * html hack). I have never used it for anything myself.
Guffa