1 of 29

4 - Advanced CSS.

Speck Academy 2021

2 of 29

Media queries

Mobile first design

BEM methodology

CSS Grid

CSS Flexbox

1

2

3

4

5

Overview

3 of 29

1

Media queries

4 of 29

Media queries overview

About

Media types

Media features

Logical operators

5 of 29

About

Media queries are useful when you want to modify your site or app depending on a device’s general type (such as print vs. screen) or specific characteristics and parameters (such as screen resolution or browser viewport width).

  • Media queries can be combined in various ways by using logical operators
  • Media queries are case-insensitive

In your HTML head, add following code for media queries support:

  • <meta name="viewport" content="width=device-width, initial-scale=1">

6 of 29

Media types

Media types describes general category of a device. If not defined then all type will be implied.

List of media types:

  • all - suitable for all devices
  • print - documents viewed on a screen in print mode
  • screen - intended primarily for screen
  • speech - intended for speech synthesizers

7 of 29

Media features

Media features describe specific characteristics of the user agent, output device, or environment. Each media feature expression must be surrounded by parentheses.

Most common:

8 of 29

Logical operators

The logical operators can be used to compose a complex media query.

List of logical operators:

  • and - used for combining multiple media features together, and it’s also used for joining media features with media types.
  • not - used to negate a media query (media type must be specified)
  • only - used to apply a style only if an entire query matches (media type must be specified)
  • , (comma) - used to combine multiple media queries into a single rule (it behaves like logical or operator)

9 of 29

2

Mobile first design

10 of 29

Mobile first design

Mobile first design means that we first write CSS for the smallest screen sizes (mobile) and override them for larger screens (desktop) when needed.

Reasons:

  • Code for larger screens is usually more complicated than the codes for smaller screens, on that way we can reduce the code size.
  • Less overrides.
  • Mobile traffic is higher than desktop traffic in most cases.

11 of 29

Mobile first design

Example of mobile first approach.

This can be used to create single column grid on mobile devices, two column grid on tablets and 3 column grid on desktop.

In most cases you will use min-width media feature inside breakpoints.

/* Mobile */�.articles-grid {� display: block;�}

/* Tablet */�@media only screen and (min-width: 768px){� .articles-grid {

display: grid;� grid-template-columns: repeat(2, 1fr);

}

}

/* Desktop */�@media only screen and (min-width: 1024px){� .articles-grid {� grid-template-columns: repeat(3, 1fr);

}

}

12 of 29

3

BEM methodology

13 of 29

BEM overview

Introduction

Block

Element

Modifier

Naming convention

14 of 29

Introduction

BEM (Block, Element, Modifier) is a component-based approach to web development.

The idea behind it is to divide the user interface into independent blocks.

This makes interface development easy and fast with a complex UI, and it allows reuse of existing code without copying and pasting.

For more information visit this link.

15 of 29

Block

Logically and functionally independent page component.

A block encapsulates behaviour (JS), templates and styles (CSS).

Blocks being independent allows for their re-use, as well as facilitating the project development and support process.

The implementation of blocks as independent entities makes it possible to change their position on the page and ensures their proper functioning and appearance.

Blocks can be nested inside any other blocks.

16 of 29

Block

Block name describes its purpose (“What is it?” - menu or button), not its state (“What does it look like?” - red or big).

It shouldn’t influence its environment, meaning you�shouldn’t set the external geometry (margin)�or positioning for the block.

You shouldn’t use CSS tag�or ID selector.

Example:

  • search-form, logo, menu, etc.

17 of 29

Element

A constituent part of block that can’t be used outside of it.

For example, a menu item is not used outside of the context of a menu block, therefore it is an element.

The element name describes its purpose, �and it’s separated from the block name�with a double underscore (__).

Example:

  • search-form__input
  • search-form__button

18 of 29

Modifier

A BEM entity that defines the appearance, state and behaviour of a block or an element.

  • Appearance - “What size?” or “Which theme?” and so on (size_s, theme_islands, etc.).
  • State - “How it’s different from the others” (disabled, focused, etc.)
  • Behaviour - “How does it behave?” or “How does it respond to the user?” (directions_left-top)

Modifier name is separated from the block or element name by a single underscore (_).

A modifier can’t be used alone!

19 of 29

Modifier

Boolean modifier

  • If a Boolean modifier is present, its value �is assumed to be true
  • block-name_modifier-name
  • block-name__element-name_modifier-name

Key-value modifier

  • Used when the modifier value is important
  • block-name_modifier-name_modifier-value
  • block-name__element-name_modifier-name_modifier-value
  • Example: menu_theme_islands

20 of 29

Naming convention

The name of a BEM entity is unique.

The same BEM entity always has the same name in all technologies (CSS, JS, HTML).

Naming rules:

  • Names are written in lowercase Latin letters.
  • Words are separated by hyphen.
  • The block name defines the namespace for its elements and modifiers.
  • The element name is separated from the block name by a double underscore (__).
  • The modifier name is separated from the block or element name by single underscore (_).
  • The modifier value is separated from the modifier name by a single underscore (_).
  • For boolean modifiers, the value is not included in the name.

21 of 29

4

CSS Grid

22 of 29

CSS Grid

CSS Grid is a two-dimensional grid-based layout system, meaning it can handle both columns and rows.

You work with Grid layout by applying CSS rules both to parent element (which becomes the Grid Container) and to that element’s children (which become Grid Items).

As of March 2017, most browsers shipped native, unprefixed support for CSS Grid (see more on Caniuse).

Find out more on MDN and css-tricks.com!

23 of 29

5

CSS Flexbox

24 of 29

CSS Flexbox

Flexbox layout module aims at providing a more efficient way to lay out, align and distribute space among items in a container.

The main idea behind flex layout is to give the container the ability to alter its items width/height (and order) to best fill the available space.

Flex container expands items to fill available free space or shrinks them to prevent overflow.

Flexbox layout is most appropriate to the components of an application, and small-scale layouts, while Grid layout is intended for larger scale layouts.

25 of 29

Useful resources

26 of 29

6

Hands-on

27 of 29

Hands-on

28 of 29

7

Homework

29 of 29

Homework