-
Responsive Design
- Fluid Layouts
- Media queries
- Responsive Images
- Correct units
- Desktop-first vs. mobile-first
-
Maintainable and scalable code
- Clean
- Easy-to-understand
- Growth
- Reusable
- How to organize files
- How to name classes
- How to structure HTML
-
Web Performance
- Less HTTP requests
- Less code
- Compress code
- Use a CSS preprocessor
- Less images
- Compress images
A CSS Rule
.my-class /* selector */ {
/* declaration block */
color: red;
text-align: center;
font-size: 2rem; /* declaration */
}
/* property: declared value; */
Cascade is a process of combining different stylesheets and resolving conflicts between different CSS rules and declarations, when more than one rule applies to a certain element.
Importance (weight) > Specificity > Source Order
- CSS declarations marked with
!important
have the highest priority and will always win over other declarations. - But only use
!important
as a last resort. It's better to use correct specificities - more maintainable code - Inline styles will always have priority over styles in external stylesheets
- A selector that contains 1 ID is more specific than one with 1000 classes
- A selector that contains 1 class is more specific than one with 1000 elements
- The universal selector
*
has no specificity value (0,0,0,0) - Rely more on specificity than on the order of selectors;
- But, rely on order when using 3rd-party stylesheets (e.g. normalize.css) - always put your author stylesheets last
- Each property has an initial value, used if nothing is declared (and if there is no inheritance — see next lecture);
- Browsers specify a root
font-size
for each page (usually 16px); - Percentages and relative values are always converted to pixels;
- Percentages are measured relative to their parent’s
font-size
, if used to specifyfont-size
; - Percentages are measured relative to their parent’s
width
, if used to specify lengths; em
are measured relative to their parentfont-size
, if used to specifyfont-size
;em
are measured relative to the currentfont-size
, if used to specify lengths;rem
are always measured relative to the document’s rootfont-size
;vh
andvw
are simply percentage measurements of the viewport’sheight
andwidth
.
- Inheritance passes the values for some specific properties from parents to children — more maintainable code;
- Properties related to text are inherited:
font-family
,font-size
,color
, etc; - The computed value of a property is what gets inherited, not the declared value.
- Inheritance of a property only works if no one declares a value for that property;
- The
inherit
keyword forces inheritance on a certain property; - The
initial
keyword resets a property to its initial value.
The Visual Formatting Model is an algorithm that calculates boxes and determines the layout of these boxes, for each element in the render tree, in order to determine the final layout of the page.
- Dimensions of boxes: the box model
- Box type: inline, block and inline-block
- Positioning scheme: floats and positioning
- Stacking context
- Other elements in the render tree
- Viewport size, dimensions of images, etc.
- total width = right border + right padding + specified width + left padding + left border
- total height = top border + top padding + specified height + bottom padding + bottom border
Example: height = 0 + 20px + 100px + 20px + 0 = 140px
Block-level boxes | Inline-block boxes | Inline boxes |
---|---|---|
Elements formatted visually as block | A mix of block and inline | Content is distributed in lines |
100% of parent's width | Occupies only content's space | Occupies only content's space |
Vertically, one after another | No line-breaks | No-line breaks |
Box-model applies as showed | Box-model applies as showed | - No heights and widths. Paddings and margins only horizontal (left and right) |
display: block (flex, list-item, table) |
display: inline-block |
display: inline |
Normal flow (default) | Floats | Absolute positioning |
---|---|---|
Default positioning scheme | Element is removed from the normal flow | Element is removed from the normal flow |
NOT floated or absolutely positioned | Text and inline elements will wrap around the floated element | No impact on surrounding elements |
Elements laid out according to their source | The container will not adjust its height to the element | We use top, bottom, left and right to offset the element from its relatively positioned container |
Default position relative |
float: left or float: right |
position: absolute or position: fixed |