sakibcy / responsive-web-design

simple and effective notes to make responsive sites

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool





Normalize Css

Browsers apply styles to elements before you've written any CSS at all, and sometimes those styles vary.
Normalizing your CSS lets you rest assured, knowing that you have the same base layer of styles applied across all browsers. It should be noted that I am talking about normalization and not resetting.



Box Model


We shoud use

  • margin for separating elements
  • padding for giving space inside element



Position Property

The position property specifies the type of positioning method used for an element
There are five different position values:

Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the position value.

Static

HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page.

Relative

An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.

Fixed

An element with position: fixed; is positioned relative to the viewport,
which means it always stays in the same place even if the page is scrolled.
The top, right, bottom, and left properties are used to position the element.
A fixed element does not leave a gap in the page where it would normally have been located.

Absolute

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

Sticky

An element with position: sticky; is positioned based on the user's scroll position.
A sticky element toggles between relative and fixed, depending on the scroll position.
It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).



Which to use? Flexbox or Grid?

CSS Grid is a multi-dimension layout module, which means that it has columns and rows. Flexbox can lay out its child items either as columns or rows, but not both



Ask yourself those questions:

  • How the component child items are displayed? Inline or as columns and rows?
  • How the component is expected to work on various screen sizes?

Most of the time, if the component you are viewing has all of its child items displayed inline, then most probably flexbox is the best solution here. Consider the following example:

However, if you see columns and rows, then CSS grid is the solution for your case.

To learn more read this article πŸ‘‰ Grid for layout, Flexbox for components




FlexBox

When you're building a house, you need a blueprint. In the same way, we need a blueprint when we're making websites. And Flexbox is the blueprint.

The Flexbox model allows us to layout the content of our website. Not only that, it helps us create the structures needed for creating responsive websites for multiple devices.



Flexbox Architecture

Flexbox Chart



Star using Flex

For start using flex first declare:

display: flex;



flex-direction property

This property allows us to set the direction and orientation in which our flex-items should be distributed inside the flex-container.



flex-wrap

This property helps you set the number of flex-items you want in a line or row.



flex-flow

This is the shorthand for the flex-direction and flex-wrap properties:



justify-content property

This property arranges flex-items along the MAIN AXIS inside the flex-container.



text-align

Arrange text to left, center, right, start, end



align-content property

This property arranges flex-items along the CROSS AXIS inside the flex-container. This is similar to justify-content. Please note that without the flex-wrap property, this property doesn't work. Here's a demo:



place-content

This is the shorthand for the align-content and justify-content properties:



align-items property

This property distributes Flex-items along the Cross Axis.



align-self property

This property works on the child classes. It positions the selected item along the Cross Axis.

In total we have 6 values:

  • flex-start
  • flex-end
  • center
  • baseline
  • stretch
  • auto



The order property

In addition to reversing the order in which flex items are visually displayed, you can target individual items and change where they appear in the visual order with the order property.



gap (grid-gap)

The gap CSS property sets the gaps (gutters) between rows and columns. It is a shorthand for row-gap and column-gap.

gap: 10px;



flex - grow | shrink | wrap | basis properties

The properties will work when we resize the window.

flex-basis

The initial size of a flex item
This is similar to adding width to a flex-item, but only more flexible. flex-basis: 10em, for example, will set the initial size of a flex-item to 10em. Its final size will be based on the available space, flex-grow, and flex-shrink.

flex-grow

This property grows the size of a flex-item based on the width of the flex-container.

flex-shrink

This property helps a flex item shrink based on the width of the flex-container. It's the opposite of flex-grow.

Please note that flex-grow and flex-shrink work on child classes. So, we will target all our boxes like this:

.box-1 {
  flex-grow: 1;
}
.box-2 {
  flex-grow: 5;
}
.box-1 {
  flex-grow: 1;
}



flex shorthand

This is the shorthand for the flex-grow, flex-shrink and flex-basis properties combined.




Grid

The Grid items [Contents] are distributed along the main axis and cross axis.



Grid Architecture



Grid Chart

Grid properties are divided into:

  • Parent properties
  • Child Properties

Note: The red colored text denotes the shorthand properties:



Start using Grid

to use grid on items first declare it:

display: grid;




Grid Parent

grid-template-columns

You use this property to define the number and width of columns.

  • You can either individually set the width of each column,
  • or set a uniform width for all columns using the repeat() function.

Note:

  • The pixel values will be an exact measurement. The "auto" keyword will cover the available space.
  • If you use fr (fraction unit) as a unit of measurement, all the boxes will be equal in size.
  • for repeat() you can use
    grid-template-columns: repeat(3, 1fr);

    or

    grid-template-columns: repeat(3, 100px);



grid-template-rows

define the number and height of rows. You can either individually set the height of each row, or set a uniform height for all rows using the repeat() function.



grid-template

This is the shorthand of 2 properties:

  • grid-template-rows
  • grid-template-columns

An example

grid-template-rows: 100px 100px;
grid-template-columns: 200px 200px;

/* The shorthand */
grid-template: 100px 100px / 200px 200px;



grid-template-areas

use this property to specify the amount of space a grid cell should carry in terms of columns and rows across the parent container.

Call it the blueprint(template) of our layoutπŸ‘‡

  • grid-template-areas: The parent property that will create the blueprint
  • grid-area: the child property that will follow the blueprint.

First, create the blueprint

Like this πŸ‘‡ inside the parent .container class:

.container {
  display: grid;
  gap: 20px;
  height: 100vh;

  grid-template-areas:
    "A A A A   A A A A   A A A A"
    "B B B B   B B B B   B B C C"
    "B B B B   B B B B   B B C C";
}



grid-area


Implement the blueprint

Target all our child .box-\* classes and set the values like this

.box-1 {
  grid-area: A;
}
.box-2 {
  grid-area: B;
}
.box-3 {
  grid-area: C;
}



column-gap

column-gap works with grid-template-columns



row-gap

row-gap works with grid-template-rows



justify-items

You use this property to position grid-items (children) inside grid containers along the X-Axis [Main Axis]. The 4 values are πŸ‘‡ Alt Text



justify-content

You use this property to position your grid [Basically everything] inside the grid container along the X-Axis [Main Axis]. The 7 values are πŸ‘‡



align-items

use this property to position grid-items (children) inside the grid container along the Y-Axis [Cross Axis].



align-content

use this property to position our grid [Basically everything] inside the grid container along the Y-Axis [Cross Axis].


place-items

short form of

  • justify-items
  • align-items

place-content

short form of

  • justify-content
  • align-content




Grid Children

The illustration below πŸ‘‡ shows the start and end points of rows and columns of a single cell.



grid-column-start - grid-column-end

join multiple Columns together
Write this code in your CSS:

.container {
  display: grid;
  gap: 20px;
  height: 100vh;

  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(12, 1fr);
}

The result looks like this:

As we are dealing with child properties, we need to target our .box-* classes like this:

.box-1 {
}
.box-2 {
}
.box-3 {
}
.box-4 {
}

The default scale of every .box-* class is:

grid-column-start: 1;
grid-column-end: 10;

/* The shorthand -> */
grid-column: 1 / 10;
/* or */
grid-column: span 10;

The result looks like this: πŸ‘‡



grid-column

short form of

  • grid-column-start
  • grid-column-end
grid-column-start: 1;
grid-column-end: 2;

/* The shorthand -> */
grid-column: 1 / 10;
/* or */
grid-column: span 10;



grid-row-start - grid-row-end

use these two properties to join multiple ROWS together.

.box-1 {
  grid-row-start: 1;
  grid-row-end: 11;

  /* shorthand */
  grid-row: 1/11;
}




Media Queries



CSS Media Query Syntax

Here's the syntax of a Media Query:

@media screen and (max-width: 768px) {
  .container {
    //Your code's here
  }
}


We declare media queries with @media first.

@media type

the media type, min-width, and max-width functions are basically conditions we are giving to the browser. We don't write the "and" operator if we have one condition. Like this ->

@media screen {
  .container {
    // Your code here
  }
}

We write the and operator if we have two conditions, like this:

@media screen and (max-width: 768px) {
  .container {
    // Your code here
  }
}

You can also skip the media type and work with just min-width & max-width, like this:

//Targeting screen sizes between 480px & 768px

@media (min-width: 480px) and (max-width: 768px) {
  .container {
    // Your code here
  }
}

If you have three conditions or more, you can use a comma, like this:

//Targeting screen sizes between 480px & 768px

@media screen, (min-width: 480px) and (max-width: 768px) {
  .container {
    // Your code here
  }
}



min-width & max-width

there's no such thing as a standard screen break-point guide because there are so many screen sizes on the market



max-width

Using this function, we are creating a boundary. This will work as long as we are inside the boundary. Here's a sample πŸ‘‡

Our Boundary is 500px:
Notice how the light purple color gets Disabled when we hit above 500px.


min-width:

We are also creating a boundary here. But this will work if we go outside the boundary. Here's a sample: πŸ‘‡

Our boundary is 500px:
Notice how the light purple color gets enabled after we hit above 500px width.


desktop first approach vs mobile first approach

  • mobile first approach : min-width
  • desktop first approach : max-width




Css Units



Absolute unit (fixed)

  • px

Relative unit (change on some condition)

  • % relative to the size of the container
  • vw, vh relative to the viewport
  • em, rem relative to the font size



REM, EM, VW, VH are relative units

Font using the REM unit

πŸ‘‡ Notice font size is changing when we resize the window.

pixels are absolute units.

πŸ‘‡ Notice that the font size of 50px doesn't change when we resize the window.



REM Unit

The REM unit depends on the root element [the HTML element]. Here's an image to show you how it works:πŸ‘‡

How to change the root font-size

By default root fon-size is 16px. But you can change it πŸ‘‡

html {
  font-size: 40px; /* Change here */
}

.text {
  font-size: 1rem;
}



How to Make Responsive Websites with REM Units

Write your styles in rem units instead of the pixels and change the root elements at different breakpoints using media queries.

// large screen

@media (max-width: 1400px) {
  html {
    font-size: 25px;
  }
}

// Tablet screen

@media (max-width: 768px) {
  html {
    font-size: 18px;
  }
}

// Mobile screen

@media (max-width: 450px) {
  html {
    font-size: 12px;
  }
}

Now, set the .text class to 3 rem units, like this:

.text {
  font-size: 3rem;
}

And here's the result: πŸ‘‡

Here are the calculations:

  • For the large screen -> 3 rem * 25px = 75px
  • For tablet screen -> 3 rem * 18px = 54px
  • For mobile screen -> 3 rem * 12px = 36px
  • Default setting -> 3rem * 16px = 48px




EM Units

Don't use the EM unit 😡❌

Using the EM unit is not worth the effort because:

  • you have a high chance of making a calculation error
  • you have to write a lot of code in media queries while trying to make the website responsive on all screen sizes
  • it's too time-consuming.

The EM unit is the same as the REM unit but it depends on the parent font size. Here's a demo. πŸ‘‡

Note: make sure you remove all the media queries.

html {
  font-size: 16px;
}

.text {
  font-size: 3em;
}

/** Calculations
  font-size should be 
  3 em * 16px = 48px
**/

Now, let's try adding 3em padding to the .text class.

html {
  font-size: 16px;
}

.text {
  font-size: 3em;
  padding: 3em;
}

/** Calculations
text    => 3em * 16px = 48px
padding => 3em * 3em * 16px = 144px
**/

Instead of being 48px of padding, we are getting 144px padding. As you can see, it is getting multiplied by the previous number.

Here's the computed part from the developer console: πŸ‘‡



VW unit - viewport width

It works like the percentage unit. Specifying 50vw is equivalent to occupying 50% of entire visible screen width

.text {
  display: none;
}

.box {
  width: 50vw;

  height: 300px;
  /* display: none; */
}

If you look carefully, you can see that 50vw means 50%, which will cover half of the entire screen width.

resizing box which is 50vw in size. It is taking 50% of entire screen even if we resize the window.



VH unit - viewport height

It works like the percentage unit as well. Specifying 50vh is equivalent to occupying 50% of entire visible screen height

.text {
  display: none;
}

.box {
  width: 300px;

  height: 50vh;
  /* display: none; */
}

And here's the result: πŸ‘‡

As you can see, it will always cover that much space even if we resize the window.




Css Articles




Resources from

About

simple and effective notes to make responsive sites