CSS Basics Notes
- Staff Curator
- Web
- 15 Feb, 2020
- 20 Feb, 2020
- 6 min read
CSS basic rules and principles.
em and rem
Both are unit of current font-size used for removing the hardcoded font sizes. em is relative to font-size of its direct or nearest parent. em is the inherited (computed) value from its parent element. The root element with no parent has 1em font size.
rem (Root em) is only relative to font size of html (root) tag. Root tag primarily referred as unit of 1 rem.
You want to use em or rem based on current element relevance whether it is relative to Root or something else.
History:
Old typography unit, an em square was a square blank type, where the length of each side was the same as the width of an uppercase ‘M’ – normally the widest glyph in a given font.
CSS Units of Measurement
Relative units
px: pixel, considered relative in CSS2.1 because it varies with display resolutionem: a unit of measurement equal to the current font sizeex: x-height, approximately the height of a lowercase “x” in the font
The following units are new in CSS3. Browser support may take a while to ramp up.rem: root em, equal to the em size of the root element (html)ch: zero width, equal to the width of a zero (0) in the current font and sizevw: viewport width unit, equal to 1/100 of the current viewport (browser window) widthvh: viewport height unit, equal to 1/100 of the current viewport heightvm: viewport minimum unit, equal to the value of vw or vh, whichever is smaller
Absolute units
px: pixel, defined as an absolute measurement equal to 1/96 of an inch in CSS3pt: points (1/72 inch in CSS2.1)pc: picas (1 pica = 12 points)mm: millimeterscm: centimetersin: inches
Colors in CSS
Colors can be specified in properties like text-color, bg-color etc. using three units Actual Color Names RGB - Red Green Blue with Decimal and Hex Values
White
rgb (255, 255, 255)
rgb (100%, 100%, 100%)
#FFFFFF
#FFF
RGBa - Red Green Blue with alpha property for making it transparent / opaque. Last parameters contains values between 0 to 1. Where 0 is fully transparent and 1 is fully opaque.
HSL - Hue (Color), Saturation and Lightness HSLa - HSL with alpha for making it transparent / opaque
color: hsla(0, 0%, 0%, .5);
Vendor Prefixes
There are many css styles, which are vendor specific. Below list contains vendor specific prefixes:
| Prefix | Organization | Most popular browsers |
|---|---|---|
| -ms- | Microsoft | Internet Explorer |
| -moz- | Mozilla Foundation | Firefox, Camino, Seamonkey |
| -o- | Opera Software | Opera, Opera Mini, Opera Mobile |
| -webkit- | Originally Apple; now open source | Safari, Chrome, Android, Silk, Blackberry, WebOS, many others |
| -khtml- | Konqueror | Konqueror |
Box Model
All elements follow the box model while rendering. You can specify the width, height, padding, border and margin. Prior to css level 3, total box width and height may vary based on width, height, padding and border size (margin not included, as it is the area outside border). As all will be sum up to find the total width and height.
However, CSS level 3 allows you to use box-sizing property (box-sizing: border-box) that would render the element with width and height specified in CSS.
You can replicate the older style by using box-sizing: content-box; to have the old style box model height and width.
Tip
You can use mnemonic TRouBLe (Top, Right, Bottom, Left) when you apply the shorthand style values. For e.g.
padding: 20px 10px 20px 10pxorpadding: 20px 10pxwould add the20pxpadding at top and bottom. Also,10pxpadding at left and right.
Example:
p {
background: #c2f670;
width: 500px;
height: 150px;
padding: 20px;
border: 2px solid gray;
margin: 20px;
}
.p {
background: #c2f670;
box-sizing: border-box;
width: 500px;
height: 150px;
margin: 20px;
padding: 20px;
border: 2px solid gray;
}
<p>This is the paragraph</p>this will be outside
<div class="p">This is the paragraph with class p</div>this will be outside 2
You may enter content more than width/height available to p and div in above example. To control that effect you can use overflow property. Based on desired result it can be set to visible (text will be display outside box), hidden, scroll, and auto (could short the text or add scroll bars)
Margins
Collapsing margins: Let’s say two elements align vertically. Top element is having
40pxbottom margin. Element at bottom is having30pxtop margin. In that case, max margin (40px) between them will be calculated and put on display instead of70px. This is called collapsing margins. Caution: margins on floated elements do not collapse.Margins on inline elements: You can apply margins on text elements (non-replaced inline elements). But, only left and right margin would be applicable. Top and bottom margins won’t have any affect. On the other hand, margins on inline images (replaced element) would apply in all directions.
Clear property comes to your rescue when you want to add a new element below the floated element. You can use
clear: both | left | rightas per your use case.
Tip
You can capitalize the first letter of a first paragraph of your page using following style:
p:first-child::first-letter{ text-transform: uppercase; text-size: 2em }. You can only have one pseudo-element per selector and it must come last. Therefore,first-letterwas added at end after pseudo classfirst-child.
Shorthand
- Border -
border: width style colore.g._border: 2px solid blue;orborder-bottom: 2px solid blue; - Border radius -
border-radius: top-left top-right bottom-right bottom-lefte.g.border-radius: 30px 35px 25px 40px - Text shadow -
text-shadow: horizontal-offset vertical-offset blur-radius colore.g.text-shadow: 0.2em 0.2em 0.3em pink; - Box shadow -
box-shadow: horizontal-offset vertical-offset blur-distance spread-distance colore.g.box-shadow: 0px 1px 1px 0px #0000001a;can be used to show the little shadow at page navigation bar. - Transition -
transition: property duration timing-function delaye.g.transition: background-color 2s ease-in-out .2s;You can add multiple transitions separated by commas here. Or you can use all to perform all transition astransition: all 0.2s ease-in-out .2s; - Animation -
animation: name duration timing-function iteration-count direction;e.g.animation: colors 5s linear infinite alternate;
Display
- Use of inline display role convert a vertical list to horizontal list.
li { display: inline; }Popular use case is navigation.