This slide is designed to give some basic idea & working knowledge of CSS to web developers who already have some working experience & want to enlarge their CSS knowledge.
2. CSS Box Model
All HTML elements can be considered as boxes. It includes:
Content (text, images)
Padding
Border
Margin
div {
border: 25px solid orange;
padding: 25px;
margin: 25px;
3. CSS3 Box Sizing
By default, the width and height of an element is calculated like this:
width + padding + border = actual width of an element
height + padding + border = actual height of an element
.div1 {
width: 300px;
height: 100px;
border: 1px solid blue;
padding: 20px;
}
Calculated width: 300(original width) + 40(padding-left: 20 + padding-right: 20) + 2(border-left: 1 + border-
right: 1) = 342px
Calculated height: 100(original height) + 40(padding-top: 20 + padding-bottom: 20) + 2(border-top: 1 +
border-bottom: 1) = 142px
8. CSS Pseudo-elements
A CSS pseudo-element is used to style
specified parts of an element.
Some pseudo-elements are:
:first-letter
:first-line
:selection
:before
:after
<h1>This is a heading</h1>
h1:before {
content: url('smiley.gif');
}
<ul>
<li><a href=”#”>Home</a></li>
<li><a href=”#”>About Us</a></li>
<li><a href=”#”>Contact</a></li>
</ul>
li:after {
content: '/';
}
9. CSS Pseudo-classes
A pseudo-class is used to define a special state of an element.
Some pseudo-classes are:
:hover
:focus
:first-child
:last-of-type
:not(selector)
:nth-child(n)
:nth-of-type(n)
10. The Difference Between :nth-child and :nth-of-type
<section>
<h1>Words</h1>
<p>One</p>
<p>Two</p> <!-- Want this one -->
<p>Three</p>
<p>Four</p>
</section>
:nth-child(n) - p:nth-child(2)
Selects every <p> element that is the second child of its
parent
:nth-of-type(n) - p:nth-of-type(2)
Selects every <p> element that is the second <p>
element of its parent
p:nth-child(2) { color: red; }
p:nth-of-type(2) { color: red; }
11. Some CSS Selectors
<div id="container">
<ul>
<li> List Item
<ul>
<li> Child </li>
</ul>
</li>
<li> List Item </li>
<li> List Item </li>
<li> List Item </li>
</ul>
</div>
#container ul {
border: 1px solid white;
}
#container > ul {
border: 1px solid white;
}
The difference between the standard X Y and X > Y is
that the latter will only select direct children.
12. Some CSS Selectors (continued..)
ul + p {
color: red;
}
X + Y will select only the element that is placed
immediately after the former element.
ul ~ p {
color: red;
}
X ~ Y will select, referring to our example above,
any p elements, as long as they follow a ul
13. Position
Mainly, there are 4 values:
Static: Default value. Elements render in order, as they appear in the document
flow
Relative: The element is positioned relative to its normal position
Absolute: The element is positioned directly in relation to their containing
parent whom is relatively or absolutely positioned.
Fixed: The element is positioned relative to the browser window
27. PX, EM or REM?
px: The pixels are an absolute unit of measurement. In
practice, they aren’t the same length everywhere because
different devices treat them differently, but on each device a
pixel is always the same.
em: Relative to the font-size of the element (2em means 2
times the size of the current font).
rem: Relative to font-size of the root element.
28. PX, EM or REM? (continued..)
Rems are better.
Px are still safe.
Reference:
https://benfrain.com/just-use-pixels
http://engageinteractive.co.uk/blog/em-vs-rem-vs-px
https://zellwk.com/blog/media-query-units
https://alastairc.ac/2017/04/px-em-or-rem-media-queries-different-conclusion
29. Legible Font Sizes
body {
font-size: 16px;
}
h2 {
font-size: 32px; /* 200% of the baseline */
}
.large {
font-size: 20px; /* 125% of the baseline */
}
.medium-small {
font-size: 13.6px; /* 85% of the baseline */
}
body {
font-size: 1rem; /* 16px */
}
h2 {
font-size: 2rem; /* 32px */
}
.large {
font-size: 1.25rem; /* 20px */
}
.medium-small {
font-size: 0.85rem; /* 13.6px */
}
Reference:
https://developers.google.com/speed/docs/insights/UseLegibleFontSizes
31. Web Fonts (font-face) (continued..)
<p>All of us have been through the dreaded
experience of <strong>sleepless
nights</strong> before the exam day.</p>
body {
font-family: 'Roboto-Regular';
}
strong {
font-family: 'Roboto-Bold';
}
Correct Way:
b, strong {
font-family: 'Roboto-Bold';
font-weight: normal;
}
39. CSS Preprocessors (LESS, SASS etc.) (continued..)
When to use @extend; when to use a mixin
@extend should be used only when the rulesets which are being tried to DRY out are inherently and
thematically related.
Reference:
http://vanseodesign.com/css/sass-mixin-or-extend
https://csswizardry.com/2014/11/when-to-use-extend-when-to-use-a-mixin
44. CSS Reset
Documentation:
https://github.com/shannonmoeller/reset-css
gem: https://github.com/adamstac/meyer-reset
Overwriting reset.css & adding custom css:
body {
font-size: 16px;
line-height: 1.2;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
:after, :before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
N.B. If we use framework like bootstrap, we won’t need that reset.css or custom reset. But, we should set
base font & line-height at boddy according to google developer’s guide.
The CSS3 box-sizing property allows us to include the padding and border in an element's total width and height.
The ::before pseudo-element can be used to insert some content before the content of an element.
The ::after pseudo-element can be used to insert some content after the content of an element.
The ::selection pseudo-element matches the portion of an element that is selected by a user.
:first-child - p:first-child Selects every <p> elements that is the first child of its parent:first-of-type - p:first-of-type Selects every <p> element that is the first <p> element of its parent
:nth-child(n) - p:nth-child(2) Selects every <p> element that is the second child of its parent
:nth-last-child(n) - p:nth-last-child(2) Selects every <p> element that is the second child of its parent, counting from the last child:nth-last-of-type(n) - p:nth-last-of-type(2) Selects every <p> element that is the second <p> element of its parent, counting from the last child:nth-of-type(n) - p:nth-of-type(2) Selects every <p> element that is the second <p> element of its parent
Elements with display:inline-block are like display:inline elements, but they can have a width and a height. That means that we can use an inline-block element as a block while flowing it within text or other elements.
The + and - operators must always be surrounded by whitespace. The operand of calc(50% -8px) for instance will be parsed as a percentage followed by a negative length, an invalid expression, while the operand of calc(50% - 8px) is a percentage followed by a minus sign and a length. Even further, calc(8px + -50%) is treated as a length followed by a plus sign and a negative percentage.The * and / operators do not require whitespace, but adding it for consistency is allowed, and recommended.
@import: CSS @import makes another http request to fetch another stylesheet, while a Preprocessor @import grabs the content from inside our imported file and includes it within the compiled stylesheet. This means only one http request, allowing us to create partials and organize our css just that little bit better without any downsides!