{"id":196202,"date":"2015-02-18T09:43:03","date_gmt":"2015-02-18T16:43:03","guid":{"rendered":"http:\/\/css-tricks.com\/?p=196202"},"modified":"2015-02-18T09:43:03","modified_gmt":"2015-02-18T16:43:03","slug":"accidental-css-resets","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/accidental-css-resets\/","title":{"rendered":"Accidental CSS Resets"},"content":{"rendered":"
Any time you use a shorthand property in CSS, you set all<\/em> the values for all the properties it deals with. It’s not a bug, it’s just how CSS works. I’ve seen it confuse people plenty of times. Let’s shine a light on the issue to hopefully make it more understood.<\/p>\n <\/p>\n Here’s an example:<\/p>\n The shorthand CSS background<\/a> property overrides all the sup properties. The default value for background-repeat is repeat, so by not declaring it in the shorthand, it gets set to that default value.<\/p>\n It works that way for every single one of the background sub properties:<\/p>\n This is the case with box model (and related) stuff, like:<\/p>\n Fonts is another situation where you can accidentally reset yourself:<\/p>\n Note that the shorthand requires at least the font-family and font-size to work.<\/p>\n Lists are yet another:<\/p>\n The flex property as part of flexbox layout is also shorthand:<\/p>\n This is an unusual one though, as rather than the shorthand resetting things you might not want reset, it resets them in ways you probably do want reset and might not even know it. Fantasai<\/a>:<\/p>\n We (the Flexbox spec editors) strongly recommend not using the longhands of ‘flex’ unless you really, really want to cascade in flex settings from some other style rule, so I’d suggest somehow discouraging the use of ‘flex-grow\/shrink\/basis’ here (or, preferably, leaving it out\/in an advanced section). The shorthand resets things in appropriate ways, and will therefore result in fewer cascading errors. Please use the shorthand!<\/p><\/blockquote>\n.module {\r\n background-repeat: no-repeat;\r\n background: url(lion.jpg);\r\n \/* Oops! This will repeat. *\/\r\n}<\/code><\/pre>\n
.module {\r\n \/* This will get set to `repeat` *\/\r\n background-repeat: no-repeat;\r\n\r\n \/* This will get set to `0 0` *\/\r\n background-position: top right;\r\n\r\n \/* This will get set to `auto auto` *\/\r\n background-size: 100px;\r\n\r\n \/* This will get set to `scroll` *\/\r\n background-attachment: fixed;\r\n\r\n \/* This will get set to `border-box` *\/\r\n background-origin: content-box;\r\n\r\n \/* This will get set to `border-box` *\/\r\n background-clip: padding-box;\r\n\r\n \/* This will get set to `transparent` *\/\r\n background-color: red;\r\n\r\n \/* This will get overridden *\/\r\n background-image: url(cool.png);\r\n\r\n \/* OVERRIDE *\/\r\n background: url(lion.jpg);\r\n}<\/code><\/pre>\n
.module {\r\n margin-right: 20px;\r\n margin: 10px;\r\n \/* margin-right will be 10px now *\/\r\n\r\n padding-top: 30px;\r\n padding: 10px;\r\n \/* padding-top will be 10px now *\/\r\n\r\n border-left: 1px;\r\n border: 0;\r\n \/* border-left will be removed *\/\r\n}<\/code><\/pre>\n
p {\r\n \/* Will get reset to what is set in shorthand (required) *\/\r\n font-family: Sans-Serif;\r\n\r\n \/* Will get reset to what is set in shorthand (required) *\/\r\n font-size: 24px;\r\n \r\n \/* Will get reset to `normal` *\/\r\n line-height: 2;\r\n\r\n \/* Will get reset to `normal` *\/\r\n font-style: italic;\r\n\r\n \/* will get reset to `normal` *\/\r\n font-weight: bold;\r\n\r\n \/* will get reset to `normal` *\/\r\n font-variant: small-caps;\r\n\r\n \/* OVERRIDE *\/\r\n font: 16px Serif;\r\n}<\/code><\/pre>\n
ul {\r\n \/* Will get reset to what is set in shorthand *\/\r\n list-style-type: square;\r\n \r\n \/* Will get reset to `outside` *\/\r\n list-style-position: inside;\r\n\r\n \/* Will get reset to `none` *\/\r\n list-style-image: url(cooldot.png);\r\n\r\n \/* OVERRIDE *\/\r\n list-style: disc;\r\n}<\/code><\/pre>\n
.flex > span {\r\n \/* Will be reset to `auto` (or `main-size` if supported) *\/\r\n flex-basis: 150px;\r\n\r\n \/* Will be reset to `1` *\/\r\n flex-grow: 0;\r\n\r\n \/* Will be reset to `1` *\/\r\n flex-shrink: 0;\r\n\r\n \/* OVERRIDE *\/\r\n flex: auto;\r\n}<\/code><\/pre>\n
\n