Skip to content

sakibcy/responsive-web-design

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

42 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation





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-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-basis

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 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


REM, EM, VW, VH are relative units

Font using the REM unit

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published