Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The confusion about * {margin:0; padding:0;}

In some articles that I've read, the use of * {margin:0; padding:0;} is discouraged as it would affect the web site's performance. So I turned to a reset.css stylesheet.

But I'm wondering, how does it affect the performance?

like image 246
hh54188 Avatar asked Jun 06 '11 13:06

hh54188


People also ask

What does margin 0 padding 0 mean?

margin: 0; padding: 0; } What this code does is to run over, like an elephant, every default style on the page. This can be useful as sometimes different browsers have different default stylings for different elements.

Why do we use margin 0 and padding 0 in CSS?

There is no space between the inner (content) box and the outer (border) box so there is zero padding. Margin is just the space outside the border. This is important for how you want your element to appear when other elements are nearby.

What is difference padding and margin?

In CSS, a margin is the space around an element's border, while padding is the space between an element's border and the element's content. Put another way, the margin property controls the space outside an element, and the padding property controls the space inside an element.

Why padding is better than margin?

Padding can be set to zero pixels and above. It can change its own element's size and impact how much of a fill color or background image is visible within a box. Margins, on the other hand, are essentially invisible barriers around a box.


2 Answers

The reasoning behind this was discussed in this Eric Meyer post.

This is why so many people zero out their padding and margins on everything by way of the universal selector. That’s a good start, but it does unfortunately mean that all elements will have their padding and margin zeroed, including form elements like textareas and text inputs. In some browsers, these styles will be ignored. In others, there will be no apparent effect. Still others might have the look of their inputs altered. Unfortunately, there’s just no way to know, and it’s an area where things are likely to change quite a bit over the next few years.

So that’s why I don’t want to use the universal selector, but instead explicitly list out elements to be reset. In this way, I don’t have to worry about munging form elements. (I really should write about the weirdnesses inherent in form elements, but that’s for another day.)

That said, the following chart from this Steve Souders post shows the difference in load times for a test page using universal selectors compared with a page using descendant selectors.

enter image description here

like image 128
melhosseiny Avatar answered Sep 17 '22 13:09

melhosseiny


it is effect the performance because the browsers engine have to apply this style to every element on the page this will lead to heavy rendering specially in the big pages with a lot of elements and this method is a bad practice too because it may remove a good default styles for some elements

you may optimize this code by reduce the scope of it like using it on just some elements that make the problems like this

h1,ul {
margin:0;

padding:0;
}

like image 39
Reda Avatar answered Sep 20 '22 13:09

Reda