Web Technologies

CSS #2

Claudia Chiriță . 2026/2027

Pseudo-classes

Pseudo-classes

selector:pseudo-class { 
  property: value;
}
  • keyword added to a selector to specify a special state of the selected element
  • eg. used for defining
    • the style of an element when the mouse is over it
    • different styles for visited and unvisited links

Pseudo-classes

:link    /* links that haven't been visited */
:visited /* links that have been visited  */
:hover   /* elements when we move the mouse over them */
:active  /* active links */
:not()   /* elements that are not an argument of not */

Pseudo-class :link

Pseudo-class :hover

Pseudo-class :not

Pseudo-class :first-child

selects an element that is the first child of another element

Pseudo-class :first-child

Pseudo-classes

  • :first-of-type
        selects the element that is the first child of the specified type of another element

  • :nth-of-type(n)
        selects the element that is the n-th child of the specified type of another element

  • :nth-child(n)
        selects the element of the specified type that is the n-th child of another element

n can be a natural number, a keyword (odd, even), formula (an+b, with n starting from 0)

Pseudo-classes

Pseudo-classes

pseudo-elements

pseudo-elements (::)

  • used to define the style of a part of an element
  • virtual elements, created with CSS, which do not appear in the HTML tree of the page
::first-letter    /* first letter of a text */
::first-line      /* first line of a block  element */

::before, ::after /* used to add content */
                  /* before/after an HTML element; */
                  /* used with the content property */ 	
	

pseudo-elements (::)

CSS Variables

  • custom CSS properties
    (nonstandard, defined by users)
  • defined to be reused inside a document

CSS Variables

  • global: valid in the entire document
    :root {
    --name-property: value;}
  • local: valid in the element (and all its descendents) in which they were defined
    element {
    --name-property: value;}
  • used together with the function
    var(--name-property)

CSS Layout

position property

  • type of positioning of an element in the page
  • can take the values:
    position:static;
    position:relative;
    position:absolute;
    position:fixed;
    position:sticky;
  • for positioning we use the properties
    left, right, top and bottom

position:static

  • the implicit position of any HTML element
  • with static position, the element will have a normal flux in the page
  • the elements with static position are not affected by left, right, top, bottom properties

position:relative

  • an element with position:relative is positioned relatively to the position it would have normally had in the page
  • for positioning we use the properties
    left, right, top and bottom

position:absolute

  • the element is positioned relatively to the first parent that has position:absolute/relative/fixed
  • the element is taken out of the flux of the document
  • it can overlap with other elements (exit Flatland!)

position:fixed

  • the element has a fixed position in the browser window
  • the element is taken out of the flux of the document and is not affected by the scroll
  • for positioning we use the properties
    left, right, top and bottom

position:sticky

  • the element commutes between relative and fixed positioning depending on the scroll
  • if the position of the scroll does not go pass the element, it will be positioned relatively, otherwise it behaves like a fixed element

overlaps: z-index

z-index: ...,-100 /* 0, 100,... */
  • through positioning, the elements can overlap; there can appear stacks of elements
  • the order of elements in the stack is given by z-index
  • most visibile elements have higher z-index
  • z-index can be set only for elements that are have an absolute, relative or fixed position

float property

float: left; /* right, none */
  • elements with float property are taken out of the flux of the document and positioned according to the given value, to the left or to the right
  • they affect the other elements: these arrange themselves by "wrapping" the float elements
  • the elements with an absolut position ignore the float property

clear property

clear: left; /* right, none, both */
  • determines if an element must be positioned below float elements that preceed it
  • elements with clear property do not arrange themselves around the float elements; they go below them

flexbox

  • unidimensional layout method for arranging items un rows and columns
  • items extend (flex) to fill the available space or shrink to fit in smaller spaces

flexbox

  • the flexible layout offers an efficient method for positioning, aligning and distributing the space between the elements in the document even when the viewport and element dimension is unknown or dynamic

flexbox tutorial

flexbox

  • the flexible layout consists in defining
    two types of elements:
    • flex container - parent element
    • flex items - child elements
flex

flex container

parent properties:
display
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content

display flex

display: flex; /* inline-flex */
  • defines a flexible container, inline or block, depending on the given value
  • creates a flexible context for all its direct children

flex direction

flex-direction: row; /* row-reverse, column, column-reverse*/
  • defines the direction in which the items are placed in the container: horizontally (left-right or right-left) or vertically (up-down or down-up)

flex wrap

flex-wrap: wrap; /* nowrap, wrap-reverse*/
  • establishes if the items will be arranged on a single line (column) or several
  • implicitly, the items will be arranged on a single line (column)

flex flow

flex-flow: flex-direction flex-wrap;
  • shorthand for properties flex-direction, flex-wrap
  • implicitly: flex-flow: row nowrap

justify content

justify-content: start; /* end, center, space-between, 
	                      space-around, space-evenly */
  • specifies the way in wich items are aligned along the main axis

align items

align-items: stretch; /* start, end, center, baseline */
  • specifies the way in wich items are aligned along the secondary axis (the axis perpendicular to the main axis)

align content

align-content: stretch; /*  start, end, center,
	                      space-between, space-around */
  • distributes the space between and around items
    on the secondary axis
  • has no effect if there is only a single line in the container

flex items

children properties:
order
flex-grow
flex-shrink
flex-basis
flex
align-self

order

order: <integer>; /* implicitly 0*/
  • establishes the order in which the items will appear in the flexible container
  • implicitly, items appear in the order in which they are written in the HTML document

flex grow

flex-grow: <number>; /* implicitly 0*/
  • establishes the capacity of items to extend their dimension if there is available space inside the container
  • if all items have a flex-grow of 1, they will share the available space equally

flex shrink

flex-shrink: <number>; /* implicitly 1*/
  • the capacity of items to shrink if necessary

flex basis

flex-basis: <length>; /* auto (implicitly) */
  • defines the implicit dimension of an element before the distribution of the available space

flex

flex: flex-grow flex-shrink flex-basis;
  • shorthand for properties flex-grow, flex-shrink and flex- basis
  • implicitly flex: 0 1 auto

align self

align-self: stretch; /* start, end, center, baseline, auto */
  • allows individual alignment of items on the secondary axis
  • overrides the alignment specified with align-items

why flex?

typical situations in which we use flexbox

grid layout

  • bidimensional layout system, based on a grid, with rows and columns, used for positioning, aligning and distributing the space between the elements in a document

grid tutorial

grid layout

  • CSS grid consists in defining two types of elements:
    • grid container - parent element
    • grid items - child elements
flex

grid container

parent properties:
display
grid-template-columns & grid-template-rows
grid-template-areas
column-gap & row-gap
justify-content
align-content
justify-items & align-items

display grid

display: grid; /* inline-grid */
  • defines the element as a grid container, inline or block, depending on the given value
  • establishes a grid format context for all its direct children

grid template

grid-template-columns
grid-template-rows

  • defines the number of columns and rows in which the grid is divded and their width/height
    (units: %, px, auto, fr)

grid-template-areas

gaps

column-gap
row-gap

  • defines the space between columns and rows

mind the gap?

justify content

justify-content: start; /* end, center, space-between, 
	                      space-around, space-evenly */
  • specifies the way in which the grid is aligned inside the container
  • the alignment is done horizontally

align content

align-content: stretch; /*  start, end, center,
	                      space-between, space-around, space-evenly */
  • specifies the way in which the grid is aligned inside the container, vertically
place-content: align-content justify-content

justify & align items

justify-items: stretch; /* start, end, center */
  • specifies the way in which the items are aligned on the main axis
align-items: stretch; /* start, end, center, baseline */
  • specifies the way in which the items are aligned on the secondary axis
place-items: align-items justify-items

grid items

children properties:
grid-column & grid-row
grid-area
justify-self & align-self
place-self

grid column & row

grid-column: start-column / end-column
  • defines the columns in which an element is placed
  • shorthand for properties grid-column-start and grid-column-end
grid-row: start-line / end-line
  • defines the rows in which an element is placed
  • shorthand for properties grid-row-start and grid-row-end

grid area

grid-area: start-line / start-column
           / end-line / end-column
  • shorthand for properties
    grid-row-start, grid-column-start, grid-row-end, grid-column-end

grid area

grid-area: name-item
  • can name an item of the grid which is then referred using the property grid-template-areas for the container

justify & align self

justify-self: stretch /* start, end, center */
  • aligns an item of the grid inside a cell, on the main axis
align-self: stretch /* start, end, center */
  • aligns an item of the grid inside a cell, on the secondary axis
place-self: align-self justify-self

flexbox vs. grid


          
                          (\ /)   
                          ( . .)  
                          C(")(") 
              
                         questions?