Sass – Nesting

Stefanescu Mihai 2 years ago Sass si Scss
Acest articol face parte din cursul Toate lectiile SASS & SCSS. Te poti inscrie la acest curs aici!

In acest articol din serie am sa vorbesc despre nesting.

Ce inseamna nesting in sass si scss? Inseamna ca putem scrie proprietati css in interiorul altor proprietati css. Chiar daca acum nu intelegeti ce vreau sa spun si probabil va ganditi ca este foarte complicat, nu este, de fapt este foarte intuitiv.

Pentru a va explica nesting-ul cat mai simplu mi-am modificat putin codul HTML, astfel incat sa am un articol cu care sa lucrez, iar acum arata astfel:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Invata-Programare.ro - Tutorial SASS & SCSS</title>
    <link href="style/style.css" rel="stylesheet">
</head>
<body>

	<article>
		<header>
			<h1>Tutorial SASS si SCSS</h1>
			<span>Articol scris de <a href="#">Mihai</a> la data de <time>23 August 2015</time>  </span>
		</header>
		<p>
			Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque mollis, erat at consequat efficitur, lacus massa tempor enim, at semper lectus diam vitae enim. Maecenas varius, mi sed dignissim tincidunt, metus erat aliquam leo, sed molestie tellus turpis ut lorem. Aliquam nec nisl at sem placerat molestie. Sed sapien eros, iaculis eget congue quis, laoreet ac sapien. Nulla sed sodales libero. Vivamus rhoncus sem velit, mattis interdum justo imperdiet vitae. Morbi eget eros sodales, volutpat enim a, lobortis enim. Quisque et nulla eu tortor egestas pharetra nec vel ex. Duis consectetur dui vel dolor aliquam, nec facilisis ligula efficitur. Proin accumsan, tellus non posuere tempus, ipsum nulla rutrum libero, lobortis commodo elit justo eu nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eget tristique mauris. Donec eu sagittis nunc, a egestas massa. Donec malesuada condimentum dui sit amet commodo. Quisque dictum vehicula ex, eu hendrerit erat. Sed vehicula, nisl sed fringilla finibus, ligula elit congue orci, eget finibus nisi augue sit amet nibh. 
		</p>
	</article>

  </body>
</html>

Iata si cum arata in clipa aceasta fisierul html:

Nu arata prea bine. nu?

Hai sa incepem lucrurile cunoscute deja, hai sa scriem cateva variabile care sa contina culorile dorite.

Fisierul meu SCSS arata astfel in acest moment:

$heading: #3877df;
$paragraf: #515151;
$autor: #41b7d8;
$bg: #ececec;
$border: #b2b2b2;

N-am facut mare lucru, ci doar mi-am setat cateva variabile. Acum am sa incep sa adaug elemente de stil elementelor din interiorul articol-ului.

Inainte, in CSS luam fiecare element si ii adaugam propriul stil, dar, desi putem face asta si in sass/scss dorim sa-l facem mai intuitiv. Acum, am sa incep cu elementele de stil pentru elementul <article>, dupa care am sa adaug elemente de stil pentru fiecare element din interiorul acestuia:

In fisierul SCSS am scris urmatorul cod:

article{
	background-color: $bg;
	border: 1px solid $border;
	border-radius: 5px;
	padding: 10px 15px;
}

Si iata si rezultatul:

Acum, daca dorim sa schimbam si stilul heading-ului ( tag-ului <h1> ) putem face acest lucru in interiorul lui article, nu trebuie sa mai scriem ca in css:

article{
	background-color: $bg;
	border: 1px solid $border;
	border-radius: 5px;
	padding: 10px 15px;
	h1{
		color: $heading;
		font-family: "Times New Roman";
	}
}

Iar rezultatul va fi acesta:

! Atentie, atunci cand avem un cod precum cel de mai sus, nu sunt afectate toate tag-urile h1 din pagina, ci doar cele ce se afla intr-un tag article.

Acum, pentru ca nu vreau sa va plictisesc aratandu-va si explicandu-va rand cu rand fiecare lucru scris de mine am sa va scriu mai jos tot codul:

$heading: #3877df;
$paragraf: #515151;
$autor: #41b7d8;
$autor-hover: #3877df;
$bg: #ececec;
$border: #b2b2b2;

article{
    background-color: $bg;
    border: 1px solid $border;
    border-radius: 5px;
    padding: 10px 15px;
    h1{
        color: $heading;
        font-family: "Times New Roman";
    }
    span{
        a{
            color: $autor;        
            text-decoration: none;
            &:hover{
                color: $autor-hover;
            }
        }
        time{
            font-style: italic;
        }
    }
    p{
        color: $paragraf;
    }
}

Iata si rezultatul:

Inainte de a incheia acest articol vreau sa va mai explic un lucru care probabil vi se pare ciudat in codul de mai sus. Ma refer la codul &:hover, pentru ca probabil este ceva nou pentru voi.

Acest cod functioneaza precum si a:hover din CSS normal, putem sa pun dupa tag-ul a{} a:hover{} si sa scriu acolo ce stil sa aiba la hover, dar am ales sa il scriu in interior cu un & in fata.

Puteti face acest lucru si pentru :visited, :active, :nth-child, s.a.m.d.

Programator de ~8 ani, am lucrat la proiecte din mai multe industrstrii, de la eCommerce la telecomunicatii la automatizari. In acest timp am folosi diferite tehnologii, de la PHP, MySQL, PostgreSql, RabbitMq, Redis, Memcached si altele.


Get in touch
Pentru nelamuriri, dubii, comentarii si chestii de pe suflet ne putem auzi pe Discord, Reddit sau poti deschide o discutie noua pe forum

????? ??? ??????????? ????????????. # 5 months ago These aare genuinely wonderful ideas in about blogging. Yoou have touched some pleasant points here. Any wayy kep uup wrinting.
331 # 4 months ago Heya i'm for tthe first time here. I came acrkss this board and I fin It truly useful & it heped mee out much. I hope to give smething back and help others llike yyou helped me.
tinyurl.com # 4 months ago It iis the best ttime tto make some plasns for the long ruun and it's time to bbe happy. I've read thks put upp and iif I could I want to counsel youu few interesting things or suggestions. Maybe yyou can wrige subsequent articles referringg to tbis article. I want tto read morfe thinggs approximately it!
025 # 4 months ago I absolutely love your site.. Great collrs & theme. Did you creste thus site yourself? Please reply back as I'm attemnpting to create my own personal blog aand would lke to learn where youu ggot thhis frlm or exactly what the theme iis called. Thank you!
Gennie # 4 months ago Hey are usibg Wordpress foor your blkog platform? I'm new tto tthe bloog world buut I'm trying too gget started and create myy own. Do you need any html coding knowledge too make your own blog? Any help would bee greatly appreciated!
bit.ly # 4 months ago Excelldnt blog! Do yoou have any hinnts for aspiring writers? I'm plannbing tto start my own sitre sopn but I'm a ljttle lost oon everything. Wold you advise startfing wirh a free platvorm luke Wordpress orr goo for a paid option? Theere aare so many options out there that I'm completeloy confused .. Any ideas? Apprecikate it!
152 # 4 months ago Thanks iin faavor of sharing succh a goo opinion, piece of writing iss nice, thts why i havve rewd iit entirely
Sidney # 3 weeks ago Highly descriptive blog, I loved that bit. Will there be a part 2?
i5.chinabest.xyz # 3 weeks ago Hi this is kind oof off offf toopic butt I was wabting to now if bogs usee WYSIWYG editrors oor iif yoou have too manually code wijth HTML. I'm staqrting a blog soln buut have no coding knowoedge sso I wantfed too get advice from somone witgh experience. Any hhelp would bee greatly appreciated!
180 # 3 weeks ago Exceptgional post hopwever I was wondering if yyou coul wrie a litte more onn this topic? I'd be ver thankful iff yyou could elaborate a little bit more. Kudos!
Dale # 3 weeks ago Hey! Someone in my Myspace group shared this website with us so I came to take a look. I'm definitely loving the information. I'm bookmarking and will be tweeting this to my followers! Wonderful blog and wonderful style and design.
Arnulfo # 3 weeks ago Heyy there would you mind letting mme know wuich webbhost you're utilizing? I've loadded ylur blog iin 3 completely different browsers and I must say this blokg loads a lot faster then most. Caan you suggest a goid webb hosting provider at a reasonable price? Thanmk you, I appreciate it!
Dann # 3 weeks ago Asking questions are genuinely good thing if you are not understanding something entirely, but this post gives good understanding even.
Susie # 3 weeks ago Hello! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a wonderful job!
Rafael # 3 weeks ago Wow, fantastic blog format! How lengthy have you been blogging for? you made running a blog glance easy. The entire glance of your site is magnificent, as smartly as the content!
flagyl met of zonder recept in Nederland # 2 weeks ago It's amazing in support of me to have a web site, which is beneficial designed for my knowledge. thanks admin
Lenore # 2 weeks ago Aw, this was a really nice post. Taking a few minutes and actual effort to generate a superb article… but what can I say… I put things off a whole lot and don't manage to get nearly anything done.
Fanny # 2 weeks ago Hello everybody, hre evwry person is sharing these kinds off knowledge, therefore it's nice too readd this website, aand I used tto goo to seee thhis webszite every day.
precio del trihexyphenidyl # 2 weeks ago Hello! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no data backup. Do you have any methods to prevent hackers?
compra amaryl de Mylan # 2 weeks ago Thanks for every other excellent article. Where else could anyone get that type of information in such a perfect manner of writing? I have a presentation subsequent week, and I am at the look for such info.
Adquiere montelukast en Europa # 1 week ago That is a really good tip particularly to those fresh to the blogosphere. Simple but very accurate info… Appreciate your sharing this one. A must read post!
madepics.org # 1 week ago Thanks for a marvelous posting! I actually enjoyed reading it, you may be a great author. I will make sure to bookmark your blog and will eventually come back later in life. I want to encourage one to continue your great writing, have a nice day!
obtenir du montelukast sans ordonnance en Suisse # 1 week ago Oh my goodness! Impressive article dude! Thank you, However I am experiencing troubles with your RSS. I don't know the reason why I cannot join it. Is there anyone else getting similar RSS issues? Anybody who knows the answer will you kindly respond? Thanks!!
obtenir du benzhexol sans ordonnance en Suisse # 6 days ago Thank you for the auspicious writeup. It in truth was a entertainment account it. Look complicated to far added agreeable from you! By the way, how could we keep in touch?
Bestellung von Medikamenten in Belgien # 5 days ago I like the helpful information you supply for your articles. I will bookmark your blog and test again right here regularly. I am moderately sure I'll be told a lot of new stuff right right here! Best of luck for the next!
madepics.net # 4 days ago I was suggested this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my trouble. You're amazing! Thanks!
ribavirin vrij verkrijgbaar # 4 days ago Can I simply just say what a relief to discover a person that actually understands what they are talking about on the net. You definitely realize how to bring a problem to light and make it important. A lot more people need to read this and understand this side of the story. I was surprised you are not more popular given that you surely have the gift.
zakup jalanimba bez recepty medycznej w Katowicach # 3 days ago I like what you guys are up too. Such clever work and reporting! Keep up the excellent works guys I've incorporated you guys to blogroll.
progesterone recomendado por médicos # 1 day ago Hello Dear, are you actually visiting this site regularly, if so after that you will without doubt get pleasant experience.
armidex en Argentina # 58 minutes ago Hi there friends, good post and good arguments commented at this place, I am truly enjoying by these.
Club-ul este dedicat membrilor si ofera access la mai multe zone ale website-ului.
Login Register

🔖 Bookmarks
✨ Pentru a sustine aceasta comunitate am sa te rog sa te autentifici sau sa te inregistrezi!

🌪️ Discord
Back to top
Folosim cookie-uri pentru a oferi functionalitatile critice ale aplicatiei Invata-Programare. Folosim cookie-uri si pentru a analiza traficul, pentru care e nevoie de consimtamantul dvs. explicit.