-
Notifications
You must be signed in to change notification settings - Fork 2
/
9-extend-one-set-of-css-styles-to-another-element.html
49 lines (40 loc) · 1.61 KB
/
9-extend-one-set-of-css-styles-to-another-element.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!-- Extend One Set of CSS Styles to Another Element
Sass has a feature called extend that makes it easy to borrow the CSS rules from one element and build upon them in another.
For example, the below block of CSS rules style a .panel class. It has a background-color, height and border.
.panel{
background-color: red;
height: 70px;
border: 2px solid green;
}
Now you want another panel called .big-panel. It has the same base properties as .panel, but also needs a width and font-size. It's possible to copy and paste the initial CSS rules from .panel, but the code becomes repetitive as you add more types of panels. The extend directive is a simple way to reuse the rules written for one element, then add more for another:
.big-panel{
@extend .panel;
width: 150px;
font-size: 2em;
}
The .big-panel will have the same properties as .panel in addition to the new styles.
Make a class .info-important that extends .info and also has a background-color set to magenta.
- Your info-important class should have a background-color set to magenta.
- Your info-important class should use @extend to inherit the styling from the info class.
-->
<style type='text/scss'>
h3 {
text-align: center;
}
.info {
width: 200px;
border: 1px solid black;
margin: 0 auto;
}
.info-important {
@extend .info;
background-color: magenta;
}
</style>
<h3>Posts</h3>
<div class="info-important">
<p>This is an important post. It should extend the class ".info" and have its own CSS styles.</p>
</div>
<div class="info">
<p>This is a simple post. It has basic styling and can be extended for other uses.</p>
</div>