Friday, 29 November 2024

Radial Gradients in CSS: A Complete Guide with HTML Examples

Radial gradients in CSS allow you to create smooth transitions between two or more colors radiating from an origin. Unlike linear gradients, which transition colors along a straight line, radial gradients transition colors in a circular or elliptical pattern.

 

Syntax

background: radial-gradient([shape size at position], start-color, ..., last-color);

 

1.   shape: Defines the shape of the gradient. It can be circle or ellipse.

2.   size: Defines the size of the gradient. Options include closest-side, farthest-side, closest-corner, farthest-corner.

3.   position: Defines the center of the gradient. It can be specified in keywords (center, top, bottom, left, right) or percentages.

4.   colors: Defines the colors to be used in the gradient. You can specify multiple colors, and optionally add color stops.

 

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Basic Radial Gradient</title>
    <style>
        .gradient {
            width: 300px;
            height: 300px;
            background: radial-gradient(red, green);
        }
    </style>
</head>
<body>
    <div class="gradient"></div>
</body>
</html>

Above snippet generate below screen.


 

Radial Gradient with Shape and Size

This example shows a radial gradient with an elliptical shape and a specific size.

 

withShapeSize.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Radial Gradient with Shape and Size</title>
    <style>
        .gradient {
            width: 500px;
            height: 300px;
            background: radial-gradient(ellipse closest-side at center, yellow, green);
        }
    </style>
</head>
<body>
    <div class="gradient"></div>
</body>
</html>

 

Above snippet generate below screen.


 

Radial Gradient with Multiple Colors

This example demonstrates a radial gradient with multiple color stops.

 

multipleColors.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Radial Gradient with Multiple Colors</title>
    <style>
        .gradient {
            width: 300px;
            height: 300px;
            background: radial-gradient(circle at 50% 50%, red, yellow, green, blue);
        }
    </style>
</head>
<body>
    <div class="gradient"></div>
</body>
</html>

 


Radial Gradient with Positioning

This example shows a radial gradient with a custom position.

 

positioning.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Radial Gradient with Positioning</title>
    <style>
        .gradient {
            width: 300px;
            height: 300px;
            background: radial-gradient(circle at 70% 30%, red, blue);
        }
    </style>
</head>
<body>
    <div class="gradient"></div>
</body>
</html>

 

  

Previous                                                    Next                                                    Home

Functions in JavaScript

Functions are one of the fundamental building blocks in JavaScript. They allow you to encapsulate code into reusable blocks, making your programs more modular, maintainable, and readable.

What is a Function?

A function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it).

 

Declaring a Function

You can declare a function in JavaScript using the function keyword. Here’s a basic example:

function greet() {
    console.log('Hello, world!!!!');
}

 

In this example:

 

1.   function is the keyword used to declare a function.

2.   greet is the name of the function.

3.   The code inside the curly braces {} is the function's body, which is executed when the function is called.

 

How to call a function?

To execute the code inside a function, you need to call the function by its name followed by parentheses.

greet();

 

helloWorld.js

function greet() {
    console.log('Hello, World!!!!');
}

greet();
greet();
greet();

 

Output

Hello, World!!!!
Hello, World!!!!
Hello, World!!!!

 

 

Previous                                                    Next                                                    Home

Using the mark Tag for highlighting Text in HTML

The <mark> element in HTML is used to highlight text. In general, it is used to emphasize certain content or indicate something that is important or relevant in the context of the surrounding text, such as search results or important keywords in an article.

The <mark> element can be useful in the following cases.

 

1.   Search Results: To highlight search terms in a result set.

2.   Important Annotations: When you want to emphasize specific portions of text for the user's attention.

3.   Educational Purposes: Highlighting key terms or important points in tutorials, lessons, or articles.

 

The <mark> element usually renders text with a yellow background by default, but you can change this styling using CSS.

 

mark-element.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Mark Example</title>
</head>

<body>
  <p>HTML is not a programming language; it's a <mark>markup language.</mark> 
    This means it uses <mark>tags</mark> to define elements within a document. 
    These tags tell the web browser how to display the content.</p>
</body>

</html>

Above snippet generate below screen.

 


‘<mark>’ element ensures that the highlighted text is accessible and meaningful for screen readers or other assistive technologies, enhancing the experience for users with disabilities.


  

Previous                                                    Next                                                    Home

Thursday, 28 November 2024

Rebuild the counter application using Functional State Setter functions

This is continuation to my previous post.

In React, functional state setter functions are used to update the state of a component.

 

When you use the useState hook, it returns an array with two elements: the current state and a function to update that state.

const [count, setCount] = useState(0);

Updating State with Functional Updates

Now, let's focus on how to update the state correctly using the state setter function (setCount).

 

Basic Update: Normally, you would call setCount with a new value like below.

setCount(count + 1);

 

This works fine, but if you have multiple state updates in quick succession, this could lead to issues because the state might not be up-to-date when you calculate the new value. React batches state updates for performance, which means that it may not immediately reflect the latest state.

 

Functional Update: To handle this correctly, you can pass a function to setCount. This function receives the current state (in this case, prevCount) as its argument:

setCount(prevCount => prevCount + 1);

 

How prevCount is injected?

When you pass a function to setCount, React internally manages this function.  When you call setCount, React queues this state update and later invokes the function you provided with the latest state value. So, when the function prevCount => prevCount + 1 is called, React automatically passes the most recent value of count as prevCount.

 

1.   Suppose the current value of count is 2.

2.   You call setCount(prevCount => prevCount + 1).

3.   React takes the current value (2) and passes it to your function. So, prevCount will be 2.

4.   Inside your function, prevCount + 1 evaluates to 3, and React sets the new value of count to 3.

 

Let’s build the counter application using Functional State setter function.

 

Follow below step-by-step procedure to build the complete working application.

 

Step 1: Go to the post (Quickly Set Up a React Project with Vite: A Step-by-Step), and set up a React Project ‘counter-app-with-functional-state-setter’.

 

Project structure looks like below.



Step 2: Create components folder in src/ folder and define Counter component.

 

Counter.jsx

import { useState } from "react";

export default function Counter() {
  const [count, setCount] = useState(0);

  function incrementCount() {
    setCount((prevCount) => prevCount + 1);
  }

  function decrementCount() {
    setCount((prevCount) => prevCount - 1);
  }

  function resetCount() {
    setCount((prevCount) => 0);
  }

  return (
    <>
      <p>Count = {count}</p>
      <button onClick={incrementCount}>Increment</button> &nbsp;
      <button onClick={decrementCount}>Decrement</button> &nbsp;
      <button onClick={resetCount}>Reset</button> &nbsp;
    </>
  );
}

 

Step 3: Update main.jsx like below.

 

main.jsx

 

import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import Counter from './components/Counter'

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <Counter />
  </StrictMode>,
)

 

Step 4: Build and run the Application by executing below commands.

  cd counter-app-with-functional-state-setter
  npm install
  npm run dev

 

Open the url ‘http://localhost:5173/’ in browser, you will see below screen.



Experiment with the button Increment, Decrement and Reset to understand the functionality.

 

You can download this application from below link. 

Previous                                                    Next                                                    Home