SlideShare a Scribd company logo
Specyficzne
kombinacje w CSS
Wojciech Urbański
CSS jest prosty
2
Źródło: http://www.tomaszbobrus.info/?p=1385
–Nie znam nikogo takiego, a Wy?
“Moja aplikacja ma wiele komponentów i pracują nad
nią ponad 3 osoby. Nie mamy żadnych problemów z
CSS.”
3
4
#navigation ul.menu *:first-child > a[rel=nofollow]::before
Z czego składa się selektor
ID klasa
kombinator
pseudo-klasa pseudo-element
dowolny element
element
atrybut
Kombinatory opisują relacje
pomiędzy elementami.
5
Kombinator potomka A B
6
Wszystkie B, które są potomkami A.
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About us</a>
<ul>
<li><a href="#">History</a></li>
<li><a href="#">Our team</a></li>
<li><a href="#">Services</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
.menu li:first-child {
background: yellow;
}
Kombinator dziecka A > B
7
Wszystkie B, które są bezpośrednim dzieckiem A.
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About us</a>
<ul>
<li><a href="#">History</a></li>
<li><a href="#">Our team</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
.menu > li:first-child {
background: yellow;
}
.menu ul li:first-child {
background: lightgreen;
}
Kombinator następnika A + B
8
Wszystkie B, które następują bezpośrednio po A.
<div class="question">
<input type="checkbox" id="bool">
<label for="bool">Check me</label>
</div>
<div class="question">
<input type="checkbox" id="bool2">
<label for="bool2">Check me</label>
</div>
.question + .question {
margin-top: 20px;
}
input:checked + label {
background: green;
}
Kombinator ogólnego następnika A ~ B
9
Wszystkie B, przed którymi jest A.
<h1>Table of contents</h1>
<ol class="toc">
<li><a href="#">Chapter 1</a></li>
<li><a href="#">Chapter 2</a></li>
<li class="current">
<a href="#">Chapter 3</a>
</li>
<li><a href="#">Chapter 4</a></li>
<li><a href="#">Chapter 5</a></li>
<li><a href="#">Chapter 6</a></li>
</ol>
.toc .current a { font-weight: bold; }
.toc .current ~ li a { color: grey; }
Kaskada
Kaskada to algorytm, który definiuje jaka wartość
właściwości zostanie zastosowana jeśli próbuje ją
ustawić kilka reguł z różnych źródeł. Jest to rdzeń
języka CSS, czyli Cascading Style Sheets.
1. Ważność (!important)
2. Specyficzność
3. Źródło i kolejność
10
Specyficzność
Specyficzność to miara tego, jak dokładnie dany selektor
wskazuje na element. Na jej podstawie przeglądarka decyduje,
które wartości właściwości zastosować w przypadku konfliktów.
Wpływają na nią:
1. Style inline dodane za pomocą atrybutu style
2. Liczba identyfikatorów (ID) elementów
3. Liczba klas, pseudo-klas oraz atrybutów
4. Liczba elementów i pseudo-elementów
11
Obliczanie specyficzności
12
3,, ,
Atrybut
style
ID
Klasa
Pseudo-klasa
Atrybut
Element
Pseudo-element
Wysoka specyficzność Niska specyficzność
30 1
#navigation ul.menu *:first-child > a[rel=nofollow]::before
Obliczanie specyficzności
13
.menu #first { }
<ul class="menu" id="navigation">
<li id="first"><a href="#">About</a></li>
<li><a href="#">About us</a>
<li><a href="#">Contact</a></li>
</ul>
,, ,
Atrybut
style ID
Klasa
Pseudo-klasa
Atrybut
Element
Pseudo-element
,, ,
Atrybut
style ID
Klasa
Pseudo-klasa
Atrybut
Element
Pseudo-element
#navigation li:first-child { }
1 10 1 1 00 1
Obliczanie specyficzności
14
.error { }
<div class="error">
Something went wrong :(
</div>
,, ,
Atrybut
style ID
Klasa
Pseudo-klasa
Atrybut
Element
Pseudo-element
,, ,
Atrybut
style ID
Klasa
Pseudo-klasa
Atrybut
Element
Pseudo-element
div:not(.container) { }
1 10 0 1 00 0
:not() nie wpływa na specyficzność
selektora, ale jego zawartość już tak.
15
Obliczanie specyficzności
16
.question [for=bool] { }
<div class="question">
<input type="checkbox" id="bool">
<label for="bool">Check me</label>
</div>
,, ,
Atrybut
style ID
Klasa
Pseudo-klasa
Atrybut
Element
Pseudo-element
,, ,
Atrybut
style ID
Klasa
Pseudo-klasa
Atrybut
Element
Pseudo-element
[id=bool] + label { }
1 10 0 2 00 0
Forma ma znaczenie
17
#menu !== [id=menu]
Co NIE wpływa na specyficzność
• Kolejność elementów w selektorze
• Selektor globalny (*)
• Kombinatory ( ~, >, oraz +)
• :not nie wpływa na specyficzność, ale jego
zawartość już tak
18
.content img:not(.small) /* 0-0-2-1 */
Pimp my specificity
19
.content.content.content
Specificity Calculator
https://specificity.keegan.st/
20
ID jest jak !important
21
–Bill Gates, tak jakby
“Three parts in a selector should
be enough for everyone.”
22
3
Selektory powinny być dokładne
tylko na tyle, na ile to potrzebne.
23
.menu .active
zamiast
nav.menu ul li a.active
Przeglądarka czyta selektory
od prawej do lewej.
24
#menu ul li
BEM to konwencja nazewnicza, która sprawia,
że kod jest bardziej zrozumiały, lepiej
skalowalny oraz przyjemniej się z nim pracuje.
25
.thread-tile__header--highlighted
block element modifier
Preprocesory!
26
.thread-tile {
&__header {
&--highlighted {
}
}
}
Dziękuję za uwagę
Wojtek Urbański <wojtiku@gmail.com>
27

More Related Content

What's hot

Robot Framework Dos And Don'ts
Robot Framework Dos And Don'tsRobot Framework Dos And Don'ts
Robot Framework Dos And Don'ts
Pekka Klärck
 
Desenvolvimento ágil com scrum
Desenvolvimento ágil com scrumDesenvolvimento ágil com scrum
Desenvolvimento ágil com scrum
Carlos Lucas Brandão
 
Python 03 - Sintaxe
Python 03 - SintaxePython 03 - Sintaxe
Python 03 - Sintaxe
PROF COMPUTAÇÃO Bordoni
 
Apresentação | Gestão de QA | Modelo Human driven | Qualidade de software | ...
Apresentação | Gestão de QA |  Modelo Human driven | Qualidade de software | ...Apresentação | Gestão de QA |  Modelo Human driven | Qualidade de software | ...
Apresentação | Gestão de QA | Modelo Human driven | Qualidade de software | ...
Rosa Sampaio
 
Robot Framework Introduction
Robot Framework IntroductionRobot Framework Introduction
Robot Framework Introduction
Pekka Klärck
 
Análise Estática de Código
Análise Estática de CódigoAnálise Estática de Código
Análise Estática de Código
Ricardo Terra
 
Ciclo de Vida Clássico da Engenharia de Software
Ciclo de Vida Clássico da Engenharia de SoftwareCiclo de Vida Clássico da Engenharia de Software
Ciclo de Vida Clássico da Engenharia de Software
Eduardo Santos
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
Maveryx
 
Implementando Testes Unitários em Java - Manoel Pimentel
Implementando Testes Unitários em Java - Manoel PimentelImplementando Testes Unitários em Java - Manoel Pimentel
Implementando Testes Unitários em Java - Manoel Pimentel
Manoel Pimentel Medeiros
 
"Experiences Of Test Automation At Spotify" with Kristian Karl
"Experiences Of Test Automation At Spotify" with Kristian Karl"Experiences Of Test Automation At Spotify" with Kristian Karl
"Experiences Of Test Automation At Spotify" with Kristian Karl
TEST Huddle
 
Software testing
Software testingSoftware testing
Software testing
KarnatiChandramoules
 
Types of Software Testing
Types of Software TestingTypes of Software Testing
Types of Software Testing
Nishant Worah
 
Testes de contrato
Testes de contratoTestes de contrato
Testes de contrato
Paula Santana
 
Aula 2 - Modelos de processos
Aula 2 -  Modelos de processosAula 2 -  Modelos de processos
Aula 2 - Modelos de processos
Leinylson Fontinele
 
ISTQB - CTFL Summary v1.0
ISTQB - CTFL Summary v1.0ISTQB - CTFL Summary v1.0
ISTQB - CTFL Summary v1.0
Samer Desouky
 
Automated Testing with Agile
Automated Testing with AgileAutomated Testing with Agile
Automated Testing with Agile
Ken McCorkell
 
How Spotify Does Test Automation - Kristian Karl
How Spotify Does Test Automation - Kristian KarlHow Spotify Does Test Automation - Kristian Karl
How Spotify Does Test Automation - Kristian Karl
SmartBear
 
Teste de software
Teste de softwareTeste de software
Teste de software
Rafael Sanches
 
Robot Framework no DevTests #34
Robot Framework no DevTests #34Robot Framework no DevTests #34
Robot Framework no DevTests #34
Mayara Fernandes
 
Introdução a Métodos Ágeis de Desenvolvimento de Software
Introdução a Métodos Ágeis de Desenvolvimento de SoftwareIntrodução a Métodos Ágeis de Desenvolvimento de Software
Introdução a Métodos Ágeis de Desenvolvimento de Software
Daniel Cukier
 

What's hot (20)

Robot Framework Dos And Don'ts
Robot Framework Dos And Don'tsRobot Framework Dos And Don'ts
Robot Framework Dos And Don'ts
 
Desenvolvimento ágil com scrum
Desenvolvimento ágil com scrumDesenvolvimento ágil com scrum
Desenvolvimento ágil com scrum
 
Python 03 - Sintaxe
Python 03 - SintaxePython 03 - Sintaxe
Python 03 - Sintaxe
 
Apresentação | Gestão de QA | Modelo Human driven | Qualidade de software | ...
Apresentação | Gestão de QA |  Modelo Human driven | Qualidade de software | ...Apresentação | Gestão de QA |  Modelo Human driven | Qualidade de software | ...
Apresentação | Gestão de QA | Modelo Human driven | Qualidade de software | ...
 
Robot Framework Introduction
Robot Framework IntroductionRobot Framework Introduction
Robot Framework Introduction
 
Análise Estática de Código
Análise Estática de CódigoAnálise Estática de Código
Análise Estática de Código
 
Ciclo de Vida Clássico da Engenharia de Software
Ciclo de Vida Clássico da Engenharia de SoftwareCiclo de Vida Clássico da Engenharia de Software
Ciclo de Vida Clássico da Engenharia de Software
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
 
Implementando Testes Unitários em Java - Manoel Pimentel
Implementando Testes Unitários em Java - Manoel PimentelImplementando Testes Unitários em Java - Manoel Pimentel
Implementando Testes Unitários em Java - Manoel Pimentel
 
"Experiences Of Test Automation At Spotify" with Kristian Karl
"Experiences Of Test Automation At Spotify" with Kristian Karl"Experiences Of Test Automation At Spotify" with Kristian Karl
"Experiences Of Test Automation At Spotify" with Kristian Karl
 
Software testing
Software testingSoftware testing
Software testing
 
Types of Software Testing
Types of Software TestingTypes of Software Testing
Types of Software Testing
 
Testes de contrato
Testes de contratoTestes de contrato
Testes de contrato
 
Aula 2 - Modelos de processos
Aula 2 -  Modelos de processosAula 2 -  Modelos de processos
Aula 2 - Modelos de processos
 
ISTQB - CTFL Summary v1.0
ISTQB - CTFL Summary v1.0ISTQB - CTFL Summary v1.0
ISTQB - CTFL Summary v1.0
 
Automated Testing with Agile
Automated Testing with AgileAutomated Testing with Agile
Automated Testing with Agile
 
How Spotify Does Test Automation - Kristian Karl
How Spotify Does Test Automation - Kristian KarlHow Spotify Does Test Automation - Kristian Karl
How Spotify Does Test Automation - Kristian Karl
 
Teste de software
Teste de softwareTeste de software
Teste de software
 
Robot Framework no DevTests #34
Robot Framework no DevTests #34Robot Framework no DevTests #34
Robot Framework no DevTests #34
 
Introdução a Métodos Ágeis de Desenvolvimento de Software
Introdução a Métodos Ágeis de Desenvolvimento de SoftwareIntrodução a Métodos Ágeis de Desenvolvimento de Software
Introdução a Métodos Ágeis de Desenvolvimento de Software
 

Specyficzność css

  • 2. CSS jest prosty 2 Źródło: http://www.tomaszbobrus.info/?p=1385
  • 3. –Nie znam nikogo takiego, a Wy? “Moja aplikacja ma wiele komponentów i pracują nad nią ponad 3 osoby. Nie mamy żadnych problemów z CSS.” 3
  • 4. 4 #navigation ul.menu *:first-child > a[rel=nofollow]::before Z czego składa się selektor ID klasa kombinator pseudo-klasa pseudo-element dowolny element element atrybut
  • 6. Kombinator potomka A B 6 Wszystkie B, które są potomkami A. <ul class="menu"> <li><a href="#">Home</a></li> <li><a href="#">About us</a> <ul> <li><a href="#">History</a></li> <li><a href="#">Our team</a></li> <li><a href="#">Services</a></li> </ul> </li> <li><a href="#">Contact</a></li> </ul> .menu li:first-child { background: yellow; }
  • 7. Kombinator dziecka A > B 7 Wszystkie B, które są bezpośrednim dzieckiem A. <ul class="menu"> <li><a href="#">Home</a></li> <li><a href="#">About us</a> <ul> <li><a href="#">History</a></li> <li><a href="#">Our team</a></li> </ul> </li> <li><a href="#">Contact</a></li> </ul> .menu > li:first-child { background: yellow; } .menu ul li:first-child { background: lightgreen; }
  • 8. Kombinator następnika A + B 8 Wszystkie B, które następują bezpośrednio po A. <div class="question"> <input type="checkbox" id="bool"> <label for="bool">Check me</label> </div> <div class="question"> <input type="checkbox" id="bool2"> <label for="bool2">Check me</label> </div> .question + .question { margin-top: 20px; } input:checked + label { background: green; }
  • 9. Kombinator ogólnego następnika A ~ B 9 Wszystkie B, przed którymi jest A. <h1>Table of contents</h1> <ol class="toc"> <li><a href="#">Chapter 1</a></li> <li><a href="#">Chapter 2</a></li> <li class="current"> <a href="#">Chapter 3</a> </li> <li><a href="#">Chapter 4</a></li> <li><a href="#">Chapter 5</a></li> <li><a href="#">Chapter 6</a></li> </ol> .toc .current a { font-weight: bold; } .toc .current ~ li a { color: grey; }
  • 10. Kaskada Kaskada to algorytm, który definiuje jaka wartość właściwości zostanie zastosowana jeśli próbuje ją ustawić kilka reguł z różnych źródeł. Jest to rdzeń języka CSS, czyli Cascading Style Sheets. 1. Ważność (!important) 2. Specyficzność 3. Źródło i kolejność 10
  • 11. Specyficzność Specyficzność to miara tego, jak dokładnie dany selektor wskazuje na element. Na jej podstawie przeglądarka decyduje, które wartości właściwości zastosować w przypadku konfliktów. Wpływają na nią: 1. Style inline dodane za pomocą atrybutu style 2. Liczba identyfikatorów (ID) elementów 3. Liczba klas, pseudo-klas oraz atrybutów 4. Liczba elementów i pseudo-elementów 11
  • 12. Obliczanie specyficzności 12 3,, , Atrybut style ID Klasa Pseudo-klasa Atrybut Element Pseudo-element Wysoka specyficzność Niska specyficzność 30 1 #navigation ul.menu *:first-child > a[rel=nofollow]::before
  • 13. Obliczanie specyficzności 13 .menu #first { } <ul class="menu" id="navigation"> <li id="first"><a href="#">About</a></li> <li><a href="#">About us</a> <li><a href="#">Contact</a></li> </ul> ,, , Atrybut style ID Klasa Pseudo-klasa Atrybut Element Pseudo-element ,, , Atrybut style ID Klasa Pseudo-klasa Atrybut Element Pseudo-element #navigation li:first-child { } 1 10 1 1 00 1
  • 14. Obliczanie specyficzności 14 .error { } <div class="error"> Something went wrong :( </div> ,, , Atrybut style ID Klasa Pseudo-klasa Atrybut Element Pseudo-element ,, , Atrybut style ID Klasa Pseudo-klasa Atrybut Element Pseudo-element div:not(.container) { } 1 10 0 1 00 0
  • 15. :not() nie wpływa na specyficzność selektora, ale jego zawartość już tak. 15
  • 16. Obliczanie specyficzności 16 .question [for=bool] { } <div class="question"> <input type="checkbox" id="bool"> <label for="bool">Check me</label> </div> ,, , Atrybut style ID Klasa Pseudo-klasa Atrybut Element Pseudo-element ,, , Atrybut style ID Klasa Pseudo-klasa Atrybut Element Pseudo-element [id=bool] + label { } 1 10 0 2 00 0
  • 18. Co NIE wpływa na specyficzność • Kolejność elementów w selektorze • Selektor globalny (*) • Kombinatory ( ~, >, oraz +) • :not nie wpływa na specyficzność, ale jego zawartość już tak 18 .content img:not(.small) /* 0-0-2-1 */
  • 21. ID jest jak !important 21
  • 22. –Bill Gates, tak jakby “Three parts in a selector should be enough for everyone.” 22 3
  • 23. Selektory powinny być dokładne tylko na tyle, na ile to potrzebne. 23 .menu .active zamiast nav.menu ul li a.active
  • 24. Przeglądarka czyta selektory od prawej do lewej. 24 #menu ul li
  • 25. BEM to konwencja nazewnicza, która sprawia, że kod jest bardziej zrozumiały, lepiej skalowalny oraz przyjemniej się z nim pracuje. 25 .thread-tile__header--highlighted block element modifier
  • 27. Dziękuję za uwagę Wojtek Urbański <wojtiku@gmail.com> 27