Learn how to make your website responsive on desktops, tablets, and mobile phones, using media queries in CSS.
What are media queries ?
In CSS, media queries are used to apply styles on your website based on the properties of the user’s device. It helps you make your website responsive on all devices (desktops, tablets, and mobile phones).
Video Tutorial
If you prefer following a video tutorial, here you are:
We will be using a grid column. So let’s say you have a row container with 12 columns in it.
It will check if the device width exceeds 992px, then it will divide the column into 12 parts. i.e. 12 / 1 = 12 columns
Media Query for Tablet
The following media query will be used for tablet devices.
/* for tablets */@media (min-width: 768px) and (max-width: 991px) {.col {grid-column: auto/span 3; /* 12 / 3 = 4 columns for tablet */ }}
Media Queries for Mobile and Small Devices
For mobile devices, we will show 2 columns, and for small phones (having a resolution of less than 767px) we will show 1 column.
/* for mobile */@media (max-width: 767px) {.col {grid-column: auto/span 6; /* 12 / 6 = 2 columns for mobile */ }}/* for small devices */@media (max-width: 320px) {.col {grid-column: auto/span 12; /* 12 / 12 = 1 column for small devices */ }}
That’s it. That’s how you can use media queries in CSS and make your website responsive for all devices. If you face any problem in following this, kindly do let me know.
In this article, we will discuss how you can hide or show any UI if the value of a variable is true in React JS. Following this tutorial, you will also learn how to use the if condition in React JS. You just need to do 3 things:
By default, you will not see the second button because the default value is false. Try changing the value of the variable from false to true, you will start seeing the UI.
Routing in React JS can be achieved using a module “react-router-dom”. This module provides all the functionality required for creating links and displaying components based on the current route.
Installing “react-router-dom”
First, you need to install this module by running the following command at the root of your project:
npminstallreact-router-dom
Creating routes
Then open the “src/App.js” file and import BrowserRouter from “react-router-dom”.
import Home from"./components/Home"import About from"./components/About"functionApp() {...}
Creating components
Create a folder named “components” inside your “src” folder and create 2 files in it.
src/components/Home.js
src/components/About.js
Following will be the code of these 2 files.
// src/components/Home.jsfunctionHome() {return ( <h1>Home</h1> )}exportdefault Home
// src/components/About.jsfunctionAbout() {return ( <h1>About</h1> )}exportdefault About
At this point, you will see the Home component at the main route.
Routing in React JS – Home route
Creating navigation links
Now we need to create navigation links for each route. So import the Link from “react-router-dom” and create 2 links from it.
import { BrowserRouter, Routes, Route, Link } from"react-router-dom"functionApp() {return ( <BrowserRouter> <divclassName="App"> <headerclassName="App-header"> <imgsrc={logo}className="App-logo"alt="logo" /> <p> Edit <code>src/App.js</code> and save to reload. </p> <Routes> <Routepath="/"element={ <Home /> } /> <Routepath="/About"element={ <About /> } /> </Routes> <Linkto="/"style={{ color: "white" }}> Home </Link> <Linkto="/About"style={{ color: "white" }}> About </Link> <aclassName="App-link"href="https://reactjs.org"target="_blank"rel="noopener noreferrer" > Learn React </a> </header> </div> </BrowserRouter> );}
If you refresh the page now, you will see 2 links. On clicking any link, you will see that its relevant component is displayed.
Routing in React JS – Navigation links
Additional: Lazy load components
You can also lazy load your components, which means that your component will only be imported when it is accessed. It is great for larger apps where a large number of components are being created. For that, first, you need to import lazy from “react”.
import { lazy } from"react"
Then you need to change the way you are importing your components.
// import Home from "./components/Home"// import About from "./components/About"const Home =lazy(function () {returnimport ("./components/Home")})const About =lazy(function () {returnimport ("./components/About")})
When lazy loading your components, you need to wrap your <Routes> tag inside Suspense. So import it and wrap your routes inside it.
fallback is used to display a UI component until the actual component gets loaded.
So that’s it. That’s how you can create routing in your React JS app. If you face any issues in following this, kindly do let me know. I created a Social Networking website in React JS that uses this routing mechanism. You can download it from here for free.
Many people want to learn React JS, but they don’t know where to start. The most basic thing is to start by creating a new project in React JS. To create a new React JS project, you first need to make sure you have Node.js installed in your system. If you do not have Node.js installed in your system, you can download it from here. Try downloading the LTS version.
Once you have Node.js installed in your system, run the following command:
npminstall-gnpx
This will install the NPX module globally. NPX is a command-line tool used to run NPM packages directly without needing to install them locally or globally.
Then run the following command to create a new folder for your React JS project:
npxcreate-react-appmy-react-app
Replace “my-react-app” with the folder name you want to be created. If you are planning to give spaces in the name of the folder, enclose them in double quotes “”. This will create a new folder named “my-react-app”. Open the terminal inside this folder and run the React JS project.
cdmy-react-appnpmstart
At this point, you will see the following page. It will automatically be opened in your browser:
Create new React JS project
We created a free Social Network project in React JS. You can download it and see the code to learn more.
To update all dependencies in your project using NPM, you need to use the module “npm-check-updates” (NCU) globally and run its command:
npminstall-gnpm-check-updatesncu-unpminstall
npm install -g npm-check-updates: This will install the “npm-check-updates” module globally in your system. So you will be able to run the command “ncu” directly in your terminal.
ncu -u: This will update your package.json
npm install: This will finally update all your modules inside the “node_modules” folder.
If you face any problem with this, kindly do let me know.