CSS is awesome!
$ whoami
Igor Laborie
@ilaborie igor@monkeypatch.io ⚠️ Je ne suis pas un designerThe Rule of Least Power
When designing computer systems, one is often faced with a choice between using a more or less powerful language for publishing information, for expressing constraints, or for solving some problem. This finding explores tradeoffs relating the choice of language to reusability of information. The "Rule of Least Power" suggests choosing the least powerful language suitable for a given purpose.
Règles du jeu
- Texte
- HTML (sémantique)
- CSS (layout, style, animations simples)
- SVG (formes et animations complexes)
- JavaScript, WebAssembly (gestion d'états, appel backend, calculs)
Le CSS c'est vaste
- Selectors
- Box model
- Float
- Media Query
- Animations
- Gradients
- Responsive Design
- Media
- Variables
- Colors
- Shapes
- ...
Plan
- Utiliser un pré‑processeur ?
- Unités
- Flexbox et Grid
- Pseudo éléments
- Animations
- Pseudo classes d'état
- HTML
- Conclusion
Utiliser un pré‑processeur ?
Bordure des boutons
button {
background: lightblue;
color: purple;
/*border: medium solid currentColor;*/
border: medium solid rgba(0,0,0,.42);
}
button.danger {
background: salmon;
color: rebeccapurple;
}
Alors utilise-t-on un pré‑processeurs ?
Oui, mais privilégiez:
- le CSS
- les post‑processeurs
Compatibilité
Partial support is due to bugs present (see known issues)
Unités
Une histoire d’unités CSS
Les unités de longueur
- px, cm, pt, ...
- longueurs absolues (mesure physique)
- em, rem
- fonction de la
font-size
- ex, ch
- hauteur d'un
x
, largeur d'un0
- vh, vw
- (100vh, 100vw) = (hauteur, largeur) du viewport
- vmin, vmax
- min(1vh, 1vw), max(1vh, 1vw)
Holy Grail avec calc
<body>
<header>Header</header>
<div>
<nav>Menu</nav>
<main>Content</main>
<aside>Side</aside>
</div>
<footer>Footer</footer>
</body>
Bilan unités
Compatibilité
Partial support in Android Browser 4.4 refers to the browser lacking the ability to multiply and divide values.
Flexbox et Grid
Holy Grail avec flexbox
<body>
<header>Header</header>
<div>
<nav>Menu</nav>
<main>Content</main>
<aside>Side</aside>
</div>
<footer>Footer</footer>
</body>
Holy Grail avec grid
<body>
<header>Header</header>
<div>
<nav>Menu</nav>
<main>Content</main>
<aside>Side</aside>
</div>
<footer>Footer</footer>
</body>
Bilan Flexbox & Grid
Compatibilité
Partial support is due to large amount of bugs present (see known issues)
Pseudo éléments
Le dîner d'un philosophe
<div class="table">
<div class="plate"></div>
</div>
.table::before, .table::after {
color: gray;
font-size: 2rem;
content: '⋔';
transform: rotate(180deg);
}
Triangle avec des bordures
div.top, div.right, div.bottom, div.left {
border: 2em solid transparent;
display: inline-block;
box-shadow: 0 0 0 .1em red;
}
div.top { border-top-color: cyan; }
div.right { border-right-color: purple; }
div.bottom { border-bottom-color: green; }
div.left { border-left-color: gold; }
Info-bulle
.popover {
position: relative;
background: teal;
}
.popover::before {
position: absolute;
z-index: -1;
content: '';
top: 1.25em; left: 1em;
border: .8em solid transparent;
border-top-color: teal;
transform: skew(-30deg);
}
Bilan pseudo éléments
- The :before and :after pseudo-elements
- mais aussi
::first-letter
,::first-line
,::selection
,::backdrop
- An Ultimate Guide To CSS Pseudo-Classes And Pseudo-Elements
⚠️ ::before
et ::after
ne marchent pas sur input
, img
, iframe
(pas encore spécifié)
- Table et assiette de CSS Diner
- Dîner des philosophes
Compatibilité
Does not support CSS transforms on SVG elements (transform attribute can be used instead)
Does not support CSS transforms on SVG elements (transform attribute can be used instead)
Does not support CSS transforms on SVG elements (transform attribute can be used instead)
Animations
Texte de chargement
.loader {
display: inline-block;
white-space: pre;
height: 1.3em; margin-top: -.3rem;
line-height: 1.5;
overflow: hidden;
}
.loader::before {
display: inline-table;
content: '⠋\a ⠙\a ⠹\a ⠸\a ⠼\a ⠴\a ⠦\a ⠧\a ⠇\a ⠏';
animation: spin 1s steps(10, end) infinite;
}
@keyframes spin {
to { transform: translateY(-15em); }
}
Dessiner
.editable svg path {
stroke: purple;
stroke-width: 1em;
fill: none;
stroke-dasharray: 4700;
stroke-dashoffset: 4700;
animation: draw 2s linear infinite;
}
@keyframes draw {
to { stroke-dashoffset: 0; }
}
Bilan animations
Compatibilité
IE9-11 desktop & mobile don't properly scale SVG files. Adding height, width, viewBox, and CSS rules seem to be the best workaround.
IE9-11 desktop & mobile don't properly scale SVG files. Adding height, width, viewBox, and CSS rules seem to be the best workaround.
IE9-11 desktop & mobile don't properly scale SVG files. Adding height, width, viewBox, and CSS rules seem to be the best workaround.
Pseudo classes d'état
Usage des info-bulles
Pseudo états
:hover
:focus
:visited
:checked
:valid
:invalid
:empty
:active
:target
- ...
Checkbox Hack
.editable input[type=checkbox] + label::before {
content: 'Click if you like it';
}
.editable input[type=checkbox]:checked + label::before {
content: '💗💖💕';
}
fieldset input[type=checkbox] { opacity: 0; }
The science of operations, as derived from mathematics more especially, is a science of itself, and has its own abstract truth and value.
Switch
.switch + label {
display: block;
position: relative;
padding: .1em;
width: 2em;
height: 1em;
background-color: #ccc;
border-radius: 1em;
border: medium solid #444;
transition: 0.4s;
}
.switch:checked + label {
background-color: green;
}
.switch + label::before {
display: block;
position: absolute;
content: '';
top: 0.1em;
left: 0.1em;
height: 1em;
width: 1em;
background-color: #fff;
border-radius: 50%;
transition: all 0.25s;
}
.switch:checked + label::before {
transform: translateX(1em);
}
Panel
.panel input[type=checkbox] {
position: fixed;
left: -100vmax;
}
The computer (or rather the software in it) was smart enough to recognize that it was being asked to perform more tasks than it should be performing. It then sent out an alarm, which meant to the astronaut, I'm overloaded with more tasks than I should be doing at this time and I'm going to keep only the more important tasks; i.e., the ones needed for landing ... Actually, the computer was programmed to do more than recognize error conditions. A complete set of recovery programs was incorporated into the software. The software's action, in this case, was to eliminate lower priority tasks and re-establish the more important ones ... If the computer hadn't recognized this problem and taken recovery action, I doubt if Apollo 11 would have been the successful moon landing it was.[26]
Principe pour les onglets
<div class="tabs">
<input type="radio" name="tab" id="home" checked>
<input type="radio" name="tab" id="projects">
<input type="radio" name="tab" id="about">
<nav>
<label for="home">Home</label>
<label for="projects">Projects</label>
<label for="about">About</label>
</nav>
<div data-for="home">Home page</div>
<div data-for="projects">Projects page</div>
<div data-for="about">About page</div>
</div>
Démo des onglets
Compatibilité
Partial support refers to lack of notice when form with required fields is attempted to be submitted. See WebKit bug.
Partial support refers to lack of notice when form with required fields is attempted to be submitted. See WebKit bug.
Partial support refers to lack of notice when form with required fields is attempted to be submitted. See WebKit bug.
Partial support refers to lack of notice when form with required fields is attempted to be submitted. See WebKit bug.
Partial support refers to lack of notice when form with required fields is attempted to be submitted. See WebKit bug.
Safari 9, 10 & 11 are reported to still require a prefix for the related backface-visibility
property.
Safari 9, 10 & 11 are reported to still require a prefix for the related backface-visibility
property.
Safari 9, 10 & 11 are reported to still require a prefix for the related backface-visibility
property.
Safari 9, 10 & 11 are reported to still require a prefix for the related backface-visibility
property.
Partial support in IE refers to not supporting the transform-style: preserve-3d property. This prevents nesting 3D transformed elements.
Safari 9, 10 & 11 are reported to still require a prefix for the related backface-visibility
property.
Safari 9, 10 & 11 are reported to still require a prefix for the related backface-visibility
property.
Safari 9, 10 & 11 are reported to still require a prefix for the related backface-visibility
property.
HTML
Progress
<progress value="42" max="100">42 %</progress>
<progress></progress>
Panel
<details>
<summary>Des détails</summary>
<p>Plus d'infos
à propos des détails.</p>
</details>
details {
border: medium solid currentcolor;
border-radius: .25em;
}
details summary {
background: #888; color: #eee;
}
Des détails
Plus d'infos à propos des détails.
Dialog
.editable dialog {
box-shadow : .25em .25em .125em rgba(0, 0, 0, 0.42);
}
.editable dialog::backdrop {
position : fixed;
top : 0; right : 0; bottom : 0; left : 0;
background-color : rgba(0, 0, 0, 0.8);
}
Polyfill
Compatibilité
iOS Safari does not support "indeterminate" <progress> elements.
iOS Safari does not support "indeterminate" <progress> elements.
iOS Safari does not support "indeterminate" <progress> elements.
iOS Safari does not support "indeterminate" <progress> elements.
'toggle' event is not supported
'toggle' event is not supported
Conclusion
Bilan
- Utilisez du CSS pour simpifier le code
- Utilisez intelligemment les pre/post‑processeurs
- HTML, SVG are Awesome !
- JavaScript, TypeScript can be Awesome !
Liens
Pour apprendre
(Ctrl|⌘) + Shift + i
- CSS Secrets by Lea Verou
- CSS sur MDN , The A11Y Project
- CodePen , JSFiddle , Dabblet ,...
- CSS Tricks , Smashing Magazine
- CSS Flags , A Single Div