BACK ⟲
Scris de Marius Chiriac

Combinatorii CSS

In termeni simpli, combinatorii CSS sunt un mod care se pot “combina” selectorii bazat pe locatia unuia fata de celalalt in document.

Acestia sunt:

1. Selectorul descendend, reprezentat printr-un singur spatiu intre selectorii folositi. In exemplul “section p”, se vor selecta toate elementele “p” din interiorul elementelor “section” indiferent de cat de adanc se afla in structura:

section
 paragraph
 div
  paragraph
 /div
 paragraph
/section

2. Selectorul de copil direct, reprezentat prin simbolul “>”. In exemplul “section > p”, se vor selecta toate elementele “p” care sunt copii directi ale elementelor “section”:

section
 paragraph
 div
  paragraph
 /div
 paragraph
/section

3. Selectorul de copil adiacent, reprezentat prin simbolul “+”. In exemplul “section div + p”, se va selecta primul element “p” din “section”, copil direct, ce are inaintea lui un element “div”:

section
 paragraph
 div
  paragraph
 /div
 paragraph
 paragraph
/section

4. Selectorul de copil general, reprezentat prin simbolul “~”. In exemplul “section div ~ p”, se vor selecta toate elementele “p” din “section”, copii directi, ce au inaintea lor un element “div”:

section
 paragraph
 div
  paragraph
 /div
 paragraph
 paragraph
/section

Daca sunt folositi corect, acesti combinatori pot crea design-uri complicate si chiar pot inlocui codul javascript.

In exemplul urmator vom crea un mod interactiv, cu butoane pentru utilizatori, de a schimba background-ul a unul element dintr-un site. Pentru a realiza acest lucru ne vom folosi de combinatorul “~” si de pseudo-elementul “checked” ce indica daca un element de tipul “radio” este selectat sau nu.

Vom incepe prin crearea codului html. Intr-un “div” vom introduce multiple elemente de tipul “input” (in functie de numarul de culori dorite) si un alt element, in acez caz inca un div, ce va fi folosit pentru a afisa culoarea dorita:

<div>
 <input type=”radio”>
 <input type=”radio”>
 <input type=”radio”>
 <div></div>
</div>

Dupa ce avem structura incepem sa construim CSS-ul si la input introducem attributul “name” cu acelasi nume pentru toate (acest lucru va permite deselctarea unui input la selectarea altuia).

<div class=”bkg-wrapper”>
 <input name=”color” class=”bkg1 bkg-select” type=”radio”>
 <input name=”color” class=”bkg2 bkg-select” type=”radio”>
 <input name=”color” class=”bkg3 bkg-select” type=”radio”>
 <div class=”bkg-container”></div>
</div>

Clasele CSS introduse ne vor ajuta sa identificam toate elementele si sa le dam dimensiuni, culori si pozitii.

Clasa “bkg-wrapper” va functiona drept container-ul pentru toate celelalte elemente:

.bkg-wrapper{
 width:300px;
 height:300px;
 position:relative;
}

Clasa “bkg-container” va stiliza elementul caruia dorim sa ii schimbam culoarea la background:

.bkg-container{
 width:100%;
 height:100%;
 position:relative;
 border-radius:4px;
 transition:0.3s ease-out;
 margin-top:10px;
 border:1px solid #000;
}

Clasa “bkg-select” va stiliza elementele de tipul “input”:

.bkg-select{
 position:relative;
 width:32px;
 height:32px;
 margin-right:1px;
 cursor:pointer;
}

Clasele “bkg1”, “bkg2”, “bkg3” sunt folosite pentru a identifica ce input este selectat. Folosindu-le impreuna cu combinatorul “~” vom schimba culoarea la background:

input.bkg1:checked ~ .bkg-container{
 background:#CE1126;
}
input.bkg2:checked ~ .bkg-container{
 background:#FCD116;
}
input.bkg3:checked ~ .bkg-container{
 background:#002B7F;
}

La final am adaugat elemente de tipul “span” ce vor reprezenta culorile si stau peste elementele “input”. Desi nu sunt necesare, ele vor ajuta utilizatorul sa stie ce culori are la dispozitie fara sa fie nevoie sa dea click pe fiecare input sau sa adaugam text. Putem refolosi clasele “bkg1”, “bkg2”, “bkg3” pentru a adauga background-ul la aceste elemente:

.bkg1{background:#CE1126}
.bkg2{background:#FCD116}
.bkg3{background:#002B7F}

Puteti vedea codul final aici:

See the Pen
CSS Simple Color Picker
by Puiu (@Puiu)
on CodePen.

Dupa cum se vede, combinatorii CSS pot crea website-uri dimanice si interactive fara a fi nevoie sa introduceti cod Javascript si nu sunt dificil de invatat.

More posts

What large and small companies ask of branding

Small companies ask branding for a clear identity, while large companies ask for a system that keeps it the same in dozens of places. In the INOVEO archive, companies with revenue above 250 million lei [...]

Read More... from Combinatorii CSS

Read the post

What each industry asks of its brand

Brand accounts for 32% of the purchase decision in technology and 67% in luxury, according to Interbrand. That is why each industry asks something different of its brand, depending on where and how fast its [...]

Read More... from Combinatorii CSS

Read the post

Which branding work you do together, and in what order

Branding work delivers results when it is done as a system, in the order in which each piece supports the next: strategy first, then identity, then applications. The wrong order costs twice: an identity made [...]

Read More... from Combinatorii CSS

Read the post

Branding for companies outside Bucharest

About 73% of Romania’s economy is produced outside the capital, and the companies there have a brand argument that is hard to copy: the place they come from. In the INOVEO archive, 45% of clients [...]

Read More... from Combinatorii CSS

Read the post

INOVEO
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.