CSS - Fundamental

CSS

CSS

Outline

  • What is CSS?
  • How to use CSS?
  • CSS selector
  • Beautify web pages (e.g., text, hyperlinks, lists, gradients)
  • box model
  • float
  • position
  • web animation

1.1 What is CSS?

  • CSS - cascading style sheet
  • CSS - appearance (beautify web pages)
  • font, color, position

Advantage:

  • Separation of content and presentation
  • Web page structure is unified and can be reused
  • Rich in style
  • Highly recommend using CSS and HTML independently
  • Using SEO, Web easy to be indexed by search engines

1.2 History of CSS

  • CSS 1.0
  • CSS 2.0 - DIV(block) + Propose the idea of separating HTML and CSS structure to make web pages simpler, SEO (Search Engine Optimization) - Use search engine rules to improve your website's organic ranking in relevant search engines
  • CSS 2.1 - float, position
  • CSS 3.0 - Rounded corners, shadows, animations, browser compatibility

1.3 Quick start

1.3.1 3 ways to import CSS

2. Selector

Select a certain element or a certain category of elements

2.1 Basic selector

  • Tag selector --> tag{}
  • Class selector --> .class{}
  • ID selector: globally unique --> #id{}

Priority: id > class > tag

2.3 Level selector

  • descendant selector --> Selects all descendants after an element
  • child selector --> Select only descendants of the current element
  • adjacent sibling selector --> Select only one adjacent element of the same generation (start from current element )
  • general sibling selector --> Select all elements of the same generation (strat from current element)

VScode tips: shift + option + up to copy and paste current code

2.4 Structure pseudo-class selector

2.5 Attribute selector

Syntax:

attribute[regular expression]

= --> absolutely eqaul to

*= --> includes

^= --> start with

$= --> end with

3. Elements for beautifying Web pages

3.1 Purpose of beautifying web pages

  • Effectively deliver page information
  • Attract users
  • Highlight page information
  • Improve users experiences

3.2 Font style

  • font-size
  • font-family
  • font-weight
  • color
  • font: bold 20px/50px Arial;
  • font: oblique bolder 16px Arial

3.3 Text style

  • color
  • text-indent
  • text-decoration
  • line-height (can achieve top and bottom centering)
  • vertical-align (center the text relative to the image )
  • a:hover{}
  • a:link{}
  • a:visitied{}
  • 5rta:active{}

3.5 Shadow

  • text-shadow: horizontal offset, vertical offset, color

3.6 List style

  • Delete dots of list

  • Add img into list

3.7 Gradient

https://www.grabient.com/

4. Box model

  • Margin, boarder, padding

    image-20220609001555453

5. Display and Float

  • Standard document flow: top down
  • Block element: h1-h6, p, div, list
  • Inline-block: span, img, stronger

5.1 Display

  • Inline elements can be contained within block-level elements, but not vice versa --> display can change this,e.g.,

5.2 Float

5.3 Father element border collapses - The position of the floating element changes as the page size changes

  • Solution1: Increase the height of father elements

  • Solution2:

    1. add a empty div after floating elements
    2. clear floating
  • Solution3: overflow

  • Solution4: add a psuedo-class after father (most popular)

5.4 display vs. float

  • Display: can not determine direction, but no collpase issues
  • Float: can determine direction, but there is collpase issue --> solutions(above 4)

6. Position

6.1 Relative positioning

  • Offeset from its original position

  • Still in the standard documentation flow

  •  

Practice

image-20220609111423805

6.3 Absolute positioning and Fixed positioning

  • Abosolute: offset relative to the browser or father element, out of standard document flow

  • Fixed: positioning in a fixed position

    Example:

    6.4 Z-index

image-20220609220757237

Comments