UNPKG

7.9 kBMarkdownView Raw
1# destyle.css
2
3[![npm][npm-image]][npm-url] [![license][license-image]][license-url]
4
5Opinionated [reset stylesheet](https://cssreset.com/what-is-a-css-reset/) that provides a clean slate for styling your html.
6
7## Benefits
8
9- Ensures consistency across browsers as much as possible
10- Prevents the necessity of reseting user agent styles
11- Prevents style inspector bloat by only targeting what is necessary
12- Removes margins & paddings
13- Removes default font styles and ensures proper inheritance
14- Contributes to the separation of presentation and semantics
15- Sets sensible default styles (see [rules](#rules))
16- Well suited for utility class libraries and large codebases
17- Made for modern browsers only, therefor small in size (~0.95kb)
18
19## Installation
20
21```shell
22$ npm install --save destyle.css
23```
24
25- Download: https://raw.githubusercontent.com/nicolas-cusan/destyle.css/master/destyle.css
26- CDN: https://classic.yarnpkg.com/en/package/destyle.css
27
28## Usage
29
30Include `destyle.css` in the `head` of your HTML file before your main stylesheet.
31
32### Recommended
33
34Add your base font and color styles to the `html` or `body` element in your stylesheet, all other elements will inherit the style from the body.
35
36```css
37/* app.css */
38
39html {
40 color: #333;
41 font: 16px/1.4 "Helvetica Neue", sans-serif;
42}
43```
44
45### Styling generated content
46
47It is discouraged to define styles for raw html tags apart from `body` and `html`, use classes (or any other selectors / system) for styling.
48
49If you need to create styles for tags generated by a CMS or markdown wrap them in a class (e.g. `.type`).
50
51```css
52.type h1 {
53 /* styles */
54}
55
56.type h2 {
57 /* styles */
58}
59```
60
61```html
62<div class="type">{{ generated_markup_goes_here }}</div>
63```
64
65## Rules
66
67- The box model is set to `border-box` for `*`, `::before` and `::after`.
68- The `border-style` is set to `solid` for `*`, `::before` and `::after` and the `border-width` is set to 0 (to hide the borders).
69- `code`, `pre`, `kbd`, `samp` maintain a monospaced font-family.
70- `hr` is set to be a solid 1px line using `border-top` that inherits its color from its parent's `color` property.
71- Inline elements that carry style (`b`, `i`, `strong`, etc.) are not reset.
72- `canvas` and `iframe` maintain their default width and height (varies depending on the browser).
73- `button`, `select`, `textarea` and `input` (except `[type='checkbox']` and `[type='radio']`), are reset using `appearance: none`.
74- `textarea` maintains its default height.
75- `meter` and `progress` elements are not reset.
76- Replaced content like `img`, `iframe` and `svg` use `vertical-align: bottom` to prevent alignment issues.
77- Focusable elements retain a focus outline, style depends on browser.
78
79## Caveats
80
81- `range` & `color` inputs are affected by `appearance: none` but are not completely destyled (varies depending on the browser).
82- `button` elements that have a fixed `height` will center its content vertically (can not be reset).
83
84## Examples
85
86### Headings
87
88An `h1` might need to be bold & large in some context (e.g. at the top of a text page) but might be small and inconspicuous in others (e.g. on a settings page in an app).
89
90Creating two different styles for `h1` is made easy, only the properties for the respective desired visual results have to be applied, there is no need to overwrite default styles, all while maintaining semantics.
91
92```css
93/* No reseting of the user agent styles necessary,
94 * just take care of making things look how you want to. */
95
96/* Bold, large title */
97.main-title {
98 font-size: 3em;
99 margin-bottom: 20px;
100 font-weight: bold;
101}
102
103/* Just some padding and gray color, otheriwse looks like normal text */
104.secondary-title {
105 color: gray;
106 padding: 10px;
107}
108```
109
110```html
111<!-- article.html -->
112<h1 class="main-title">Large title</h1>
113
114<!-- profile.html -->
115<h1 class="secondary-title">Small title</h1>
116
117<!-- Looks the same as `h1.secondary-title` -->
118<p class="secondary-title">Other small title</p>
119```
120
121### Buttons
122
123`button` tags have a lot of default styles that can make them cumbersome to use from a styling perspective, especially if they should look like plain links or need to wrap some other content, but `button` tags are the recommended elements to use as click targets for user interactions. Falling back to using `<a href="#">` even with `role="button"` is [not recomended](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role) from an accessibility standpoint as screen readers will recognize `button`s as interactive elements by default and treat them accordingly. `a` should be used when there is the need to link to a page via `href`.
124
125<!-- prettier-ignore-start -->
126destyle.css resets buttons completely to make them usable as any other element <small>* see note [below](#caveats)</small>.
127<!-- prettier-ignore-end -->
128
129```css
130/* Make anything look like a link, even a <button> */
131.link {
132 color: lightblue;
133 text-decoration: underline;
134}
135
136/* Make anything look like a button
137 * font styles will be inheritet from the parent */
138.btn {
139 padding: 0.2em 0.5em;
140 border-radius: 0.2em;
141 background-color: blue;
142 color: white;
143 text-align: center;
144}
145
146.block {
147 display: block;
148 width: 100%;
149}
150```
151
152```html
153<!-- Make it look like a link -->
154<button class="link">Interactive link</button>
155
156<!-- Make anchor look like a button -->
157<a href="page.html" class="btn">Link that looks like a button</a>
158
159<!-- Use as block level element -->
160<button class="block">
161 <img src="..." alt="..." />
162</button>
163```
164
165## Changelog
166
167### **v3.0.2.** 2021-12-07
168
169- Add `border-collapse: collapse` to tables.
170
171### **v3.0.0.** 2021-09-03
172
173- Remove IE support 🎉
174- Bring back `outline` for focusable elements
175- Remove redundant `line-height: inherit` rule from headings reset
176- Remove redundant `text-decoration` rule from `abbr`
177- Added `svg` selector to replaced content rule
178- Added `text-transform: inherit` rule to form elements
179- Replaced `[disabled]` selector with `:disabled`
180- Removed `::-moz-focus-inner` rules for old Firefox versions
181- Improved `:-moz-focusring` style, no more dotted outline
182- Destyled `select:disabled` in Chrome
183- Add outline to focused `[contenteditable]` elements
184- Fixed border color inheritance for `table` borders in Chrome
185
186### **v2.0.0.** 2020-10-15
187
188- Add `border-style: solid` and `border-width: 0` to `*, ::before, ::after` selector. This change might affect how borders are used and therefor is considered a breaking change. The benefit is that simply adding a border-width to an element will display a border without the need to set the border-style explicitly.
189
190## Why?
191
192[Eric Meyer's reset](https://meyerweb.com/eric/tools/css/reset/) resets properties on elements that do not need it, are unused or even deprecated, this creates bloat in the browser's style inspector which makes developing and debugging less efficient. [Normalize.css](https://github.com/necolas/normalize.css) makes elements look consistent across browsers and it does it well, but it does not remove the user agent's assumptions about how things look. Destyle.css targets both reseting & normalization.
193
194Compare the results [here](https://nicolas-cusan.github.io/destyle.css/compare.html).
195
196## Credits
197
198This project is heavily inspired by [normalize.css](https://github.com/necolas/normalize.css) and the original [reset](https://meyerweb.com/eric/tools/css/reset/) by Eric Meyer. The source of the test page is from [html5-test-page](https://github.com/cbracco/html5-test-page) with some additions.
199
200Tested with:
201
202<a href="https://www.browserstack.com">
203 <img src="assets/Browserstack-logo.svg?sanitize=false" alt="browserstack" width="160">
204</a>
205
206[license-image]: https://img.shields.io/npm/l/destyle.css.svg?style=flat-square
207[license-url]: LICENSE
208[npm-image]: https://img.shields.io/npm/v/destyle.css.svg?style=flat-square
209[npm-url]: https://www.npmjs.com/package/destyle.css
210
211<!-- Outline -->