CSS

  1. Cascading Style Sheets
    1. Introduction to CSS
    2. CSS Properties
    3. Intermediate CSS
      1. The Cascade - Specificity and Inheritance
      2. Combining CSS Selectors
      3. CSS Positioning
        1. Static Positioning
        2. Relative Positioning
        3. Absolute Positioning
        4. Fixed Positioning
    4. Advanced CSS
      1. CSS Display
      2. CSS Float
      3. How to Create Responsive Websites
      4. Media Queries
      5. CSS Grid
      6. CSS Flexbox
        1. Display: Flex
        2. Flex Direction
        3. Flex Layout
        4. Flex Sizing
      7. Grid
        1. Grid Sizing
        2. Grid Item Placement
      8. External Frameworks
        1. Bootstrap Layout
        2. Bootstrap Container
        3. Bootstrap Components

Cascading Style Sheets

Introduction to CSS

CSS Properties

Intermediate CSS

The Cascade - Specificity and Inheritance

Combining CSS Selectors

How to target specific elements to style

<!-- file: "index.html" -->
<p>Yellow Text</p>
<div class="box inner-box">
  <p>White Text</p>
</div>
/* file: "style.css" */
p {
  color: yellow;
}
.inner-box p {
  color: white;
}

Targeting the paragraph inside a parent div’s class

Group = Apply to both selectors

selector, selector {
  color: white;
}

Child = Apply to direct child of left side

selector > selector {
  color: firebrick;
}

Descendant = Apply to a descendant of left side

selector selector {
  color: blue;
}

Chaining = Apply where ALL selectors and true

selectorselector {
  color: seagreen;
}

Combining Combiners

selector selectorselector {
  font-size: 0.5rem;
}

CSS Positioning

Relative, Absolute, Fixed, and Static Positioning

Static Positioning

HTML default flow

Relative Positioning

Position relative to default position

Absolute Positioning

Position relative to nearest positioned ancestor or top left corner of webpage

Fixed Positioning

Position relative to top left corner of browser window

Advanced CSS

CSS Display

Block, Inline, and Inline-Block

CSS Float

Only use float for wrapping text around images. Use flex and grid for crafting layouts.

How to Create Responsive Websites

Ways of creating responsive websites:

  1. Media Queries
  2. CSS Grid
  3. CSS Flexbox
  4. External Frameworks (e.g., Bootstrap)

Media Queries

Adding breakpoints to define responsive layouts

/* file: "style.css" */
div {
  background-color: blue;
  height: 200px;
  widows: 200px;
}

@media (max-width: 600px) {
  div {
    height: 100px;
    width: 100px;
  }
}

When a website is >=600px wide, use the nested styling

/* file: "style.css" */

@media (min-width: 600px) {
  h1 {
    font-size: 15px;
  }
}

If the screen is wider than 600px, use the nested styling to render

Combine

@media (min-width: 600px) and (max-width: 900px) {
  /* Styles for screen between 600px and 900px */
}
@media (max-width: 600px) and (min-width: 900px) {
  /* Styles for screen less than 600px and greater than 900px */
}

Device

@media print(orientation: landscape){
  /* Styles for landscape orientation */
}

MDN Docs - Media Query

/*file: style.css*/
@media (min-width: 319px) and (max-width: 480px) {
  body {
    background-color: lightsalmon;
  }
}
@media (min-width: 481px) and (max-width: 1200px) {
  body {
    background-color: powderblue;
  }
}
@media (min-width: 1201px) and (max-width: 1600px) {
  body {
    background-color: limegreen;
  }
}
@media (min-width: 1601px) {
  body {
    background-color: seagreen;
  }
}

Media query widths template

CSS Grid

<!-- file: "index.html" -->
<div class="grid-container">
  <div class="first card"></div>
  <div class="card"></div>
  <div class="card"></div>
  <div class="card"></div>
  <div class="card"></div>
</div>
/*file: "style.css"*/
.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 100px 200px 200px;
  gap: 30px;
}
.first {
  grid-column:  span 2;
}
.card {
  background-color: blue;
}

Basics of using CSS Grid.

CSS Flexbox

Display: Flex
<!-- file: "index.html" -->
<div class="flex-container">
  <div class="first card"></div>
  <div class="second card"></div>
  <div class="card"></div>
  <div class="card"></div>
</div>
/*file: "style.css"*/
.flex-container {
  display: flex;
}
.card {
  background: blue;
  border: 30px solid white;
  height: 100px;
  flex: 1;
}
.first {
  flex: 2;
}
.second {
  flex: 0.5;
}
Flex Direction

By default, flex-direction is set to row. This affects the direction of the “main axis”.

MDN Docs - CSS Selector Combinator MDN Docs - CSS Universal Selector

Flex Layout

Align, Justify, and Wrap

Properties:

.container {
  flex-direction: column;
  flex-wrap: nowrap;
  justify-content: flex-start;
  align-items: flex-start;          /* Aligns along the cross axis */
  height: 70vh;                     /* viewport height */
  align-content: center;            /* Only works if wrap is true */
}

.item {
  order: 0;
  flex-basis: auto;
  align-self: flex-start;
  flex-grow: 1;
  flex-shrink: 1;
}

Complete Guide to Flexbox Flexbox Froggy

Flex Sizing

Shrinking and Growing

Priority List of Sizing Properties

  1. min-width/max-width
  2. flex-basis
  3. width
  4. content-width

Grid

A Two-Dimensional Layout System

Grid Sizing
/*file: "style.css"*/
.grid-container {
  display: grid;
  grid-template: 100px 200px / 400px 800px;   /* rows / columns */
}

Grid size using minmax to limit the minimum and maximum grow/shrink size of the grid:

.grid-container {
  display: grid;
  grid-template-rows: 200px 400px;
  grid-template-columns: 200px minmax(400px, 2fr);
}

The minmax sets the minimum size that the column can shrink and grow.

Succinct way to write grid’s rows and columns:

.grid-container {
  grid-template-rows: repeat(2, 2fr);     /* (times, size) */
  grid-template-columns: repeat(2, 1fr);
}
Grid Item Placement

How to layout items in the grid

(grid-row) (grid-column)

Grid Garden

External Frameworks

Bootstrap Framework

<!-- file: "index.html" -->
<div class="container">
  <div class="row">
    <div class="card col-6">Card</div>
    <div class="card col-2">Card</div>
    <div class="card col-4">Card</div>
  </div>
</div>

Bootstrap Official Website

Bootstrap Layout

Understanding the 12 column Boostrap layout system

Bootstrap Container

Screen breakpoints

 Extra smallSmallMediumLargeX-LargeXX-Large
.container100%540px720px960px1140px1320px
.container-sm100%540px720px960px1140px1320px
.container-md100%100%720px960px1140px1320px
.container-lg100%100%100%960px1140px1320px
.container-xl100%100%100%100%1140px1320px
.container-xxl100%100%100%100%100%1320px
.container-fluid100%100%100%100%100%100%
 <576px>=576px>=768px>=992px>=1200px>=1400px

Breakpoints cheatsheet

Bootstrap Components

Learn to use the pre-built and pre-styled Components


© 2024. knznsmn. All rights reserved.