top of page

CSS Functions

Updated: Jan 10, 2023



Functions in CSS are used to set the various CSS property. For example, the attr() function is used to retrieve the value of the HTML attribute.


Below is the list of CSS Functions with Example:


1. attr()

The attr() function is an inbuilt function in CSS which returns the value of an attribute of the selected elements

<!DOCTYPE html>
<html>
<head>
<style>
a:after {content: " (" attr(href) ")";}
</style>
</head>
<body>

<h1>The attr() Function</h1>

<p>Insert the value of the href attribute in parenthesis after each link:</p>

<p><a href="https://www.thetechplatform.com/">Visit our Website</a></p>

</body>
</html>

Output:










 


2. calc()

The calc() function is an inbuilt function in CSS which is used to perform calculations based on CSS property.

<html>
<head>
<style>
#div {
  position: absolute;
  left: 50px;
  width: calc(100% - 100px);
  border: 1px solid black;
  background-color: pink;
  padding: 5px;
  text-align: center;
}
</style>
</head>
<body>

<h1>The calc() Function</h1>

<p>Create a div that stretches across the window, with a 50px gap between both sides of the div and the edges of the window:</p>

<div id="div">Text</div>

</body>
</html>

 

3. max()

The max() function uses the largest value, from a comma-separated list of values, as the property value.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
#div {
  background-color: pink;
  height: 100px;
  width: max(50%, 300px);
}
</style>
</head>
<body>

<h1>The max() Function</h1>

<div id="div">Some text...</div>

<p>Resize the browser window to see the effect.</p>

</body>
</html>











 

4. min()

The min() function uses the smallest value, from a comma-separated list of values, as the property value.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
#div {
  background-color: pink;
  height: 200px;
  width: min(75%, 400px);
}
</style>
</head>
<body>

<h1>The min() Function</h1>

<div id="div">Text....</div>

<p>Resize the browser window to see the effect.</p>

</body>
</html>

Output:











 

5. hsl()

The hsl() function is an inbuilt function in CSS which is used to define the colors using the Hue-saturation-lightness model (HSL).

  • hue: This parameter is used to define the degree on the color wheel. Its value lies between 0 to 360 where 0 or 360 represents red, 120 represents green and 240 represents blue.

  • saturation: This parameter is used to define the saturation where 0% represents shade of gray and 100% represents full color.

  • lightness: This parameter is used to define the lightness where 0% represents black, 50% represents normal, and 100% represents white.

Code:

<!DOCTYPE html>
<html>
<head>
<style>
#p1 {background-color:hsl(0,100%,50%);}
#p2 {background-color:hsl(60,100%,50%);}
#p3 {background-color:hsl(120,100%,50%);}
#p4 {background-color:hsl(180,100%,50%);}
#p5 {background-color:hsl(240,100%,50%); color: white;}
#p6 {background-color:hsl(300,100%,50%);}
#p7 {background-color:hsl(0,0%,0%); color: white;}
</style>
</head>
<body>

<h1>The hsl() Function</h1>

<p>HSL colors:</p>
<p id="p1">Red</p>
<p id="p2">Yellow</p>
<p id="p3">Green</p>
<p id="p4">Light Blue</p>
<p id="p5">Dark Blue</p>
<p id="p6">Pink</p>
<p id="p7">Black</p>

</body>
</html>

Output:



 

6. hsla()

The hsla() function define colors using the Hue-saturation-lightness-alpha model (HSLA). HSLA color values are an extension of HSL color values with an alpha channel - which specifies the opacity of the color.

  • hue - Defines a degree on the color circle (from 0 to 360) - 0 (or 360) is red, 120 is green, 240 is blue

  • saturation - Defines the saturation; 0% is a shade of gray and 100% is the full color (full saturation)

  • lightness - Defines the lightness; 0% is black, 50% is normal, and 100% is white

  • alpha - Defines the opacity as a number between 0.0 (fully transparent) and 1.0 (fully opaque)

Code:

<!DOCTYPE html>
<html>
<head>
<style>
#p1 {background-color:hsla(0,0%,0%,1.0);color:white;}
#p2 {background-color:hsla(0,100%,50%,1.3); color:white;}
#p3 {background-color:hsla(180,100%,50%,1.0);}
#p4 {background-color:hsla(60,100%,50%,1.0);}
#p5 {background-color:hsla(120,100%,50%,1.0);}
#p6 {background-color:hsla(240,100%,50%,1.0); color:white;}
</style>
</head>
<body>

<h1>The hsla() Function</h1>

<p>HSL colors with opacity:</p>
<p id="p1">Black</p>
<p id="p2">Red</p>
<p id="p3">Aqua</p>
<p id="p4">Yellow</p>
<p id="p5">Lime</p>
<p id="p6">Blue</p>

</body>
</html>

Output:


 

7. counter()

The counter() function returns the current value of the named counter, as a string.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
body {
  counter-reset: Chapter;
}

h2::before {
  counter-increment: Chapter;
  content: "Chapter " counter(Chapter) ": ";
}
</style>
</head>
<body>

<h1>Using CSS Counters:</h1>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h2>JavaScript Tutorial</h2>

</body>
</html>









 

8. cubic-bezier()

The cubic-bezier() function defines a Cubic Bezier curve. A Cubic Bezier curve is defined by four points P0, P1, P2, and P3. P0 and P3 are the start and the end of the curve and, in CSS these points are fixed as the coordinates are ratios. P0 is (0, 0) and represents the initial time and the initial state, P3 is (1, 1) and represents the final time and the final state. The cubic-bezier() function can be used with the animation-timing-function property and the transition-timing-function property.


Code:

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background: blue;
  transition: width 3s;
  transition-timing-function: cubic-bezier(1.1, 1.1, 1.0, 1.1);
}

div:hover {
  width:300px;
}
</style>
</head>
<body>

<h1>The cubic-bezier() Function</h1>

<p>Hover over the element to see the transition effect.</p>

<div></div>

</body>
</html>









 

9. linear-gradient()

The linear-gradient() function is an inbuilt function in CSS which is used to set the linear gradient as the background image.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
#linear {
  height: 200px;
  background-image: linear-gradient(Blue, Red, Yellow, green, Black);
}
</style>
</head>
<body>

<h3>Linear Gradient - Top to Bottom</h3>
<p>This linear gradient starts at the top.</p>

<div id="linear"></div>

</body>
</html>


10. repeating-linear-gradient()

The repeating-linear-gradient() function is an inbuilt function in CSS which is used to repeat linear gradients.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
#grad {
  height: 200px;
  background-image: repeating-linear-gradient(Black, yellow 10%, green 20% );
}
</style>
</head>
<body>

<h3>Repeating Linear Gradient</h3>

<div id="grad"></div>

</body>
</html>

Output:


 

11. radial-gradient()

The radial-gradient() function is an inbuilt function in CSS which is used to set a radial gradient as the background image. It starts at a single point and emanates outward. By default, the first color starts at the center position of the element and then fade to the end color towards the edge of the element. Fade happens at an equal rate until specified.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
  height: 200px;
  width: 300px;
  background-image: radial-gradient(red, white, black);
}
</style>
</head>
<body>

<h3>Radial Gradient - Evenly Spaced Color Stops</h3>
<div id="grad1"></div>

</body>
</html>

Output:













12. repeating-radial-gradient()

The repeating-radial-gradient() function is an inbuilt function in CSS which is used to repeat radial gradients.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
  height: 200px;
  width: 300px;
  background-image: repeating-radial-gradient(blue, yellow 10%, blue 20%);
}
</style>
</head>
<body>

<h3>Repeating Radial Gradient</h3>

<div id="grad1"></div>

</body>
</html>

Output:


 













13. repeating-conic-gradient()

The repeating-conic-gradient() function is used to repeat conic gradients.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
  height: 150px;
  width: 150px; 
  background-image: repeating-conic-gradient(pink 10%, red 20%);
  border-radius: 50%;
}

#grad2 {
  height: 150px;
  width: 150px;
  background-image: repeating-conic-gradient(yellow 10%, red 20%, yellow 30%);
  border-radius: 50%;
}
</style>
</head>
<body>

<h1>Repeating Conic Gradient</h1>
<h3>Using 2 Colors</h3>
<div id="grad1"></div><br>
<h3>Using 3 Colors</h3>
<div id="grad2"></div>

</body>
</html>


Output:


 

14. rgb()

The rgb() function is an inbuilt function in CSS which is used to define the colors using the Red Green Blue (RGB) model.

  • red: This parameter is used to define the intensity of red color. It is an integer value lies between 0 to 255, or as a percentage value between 0% to 100%.

  • green: This parameter is used to define the intensity of green color. It is an integer value lies between 0 to 255, or as a percentage value between 0% to 100%.

  • blue: This parameter is used to define the intensity of blue color. It is an integer value lies between 0 to 255, or as a percentage value between 0% to 100%.

Code:

<!DOCTYPE html>
<html>
<head>
<style>
#p1 {background-color:rgb(255,0,0);}
#p2 {background-color:rgb(0,255,0);}
#p3 {background-color:rgb(0,0,255);color: white;}
#p4 {background-color:rgb(192,192,192);}
#p5 {background-color:rgb(255,255,0);}
#p6 {background-color:rgb(255,0,255);}
</style>
</head>
<body>

<h1>The rgb() Function</h1>

<p>RGB colors:</p>
<p id="p1">Red</p>
<p id="p2">Green</p>
<p id="p3">Blue</p>
<p id="p4">Grey</p>
<p id="p5">Yellow</p>
<p id="p6">Cerise</p>

</body>
</html>

Output:


 

15. rgba()

The rgba() function is an inbuilt function in CSS that is used to define the colors using the Red-Green-Blue-Alpha (RGBA) model. It is an extension of rgb() color values containing an alpha channel that specifies the transparency of color.

  • red: This parameter is used to define the intensity of the red color. It is an integer value that lies between 0 to 255, or as a percentage value between 0% to 100%.

  • green: This parameter is used to define the intensity of the green color. It is an integer value that lies between 0 to 255, or as a percentage value between 0% to 100%.

  • blue: This parameter is used to define the intensity of the blue color. It is an integer value that lies between 0 to 255, or as a percentage value between 0% to 100%.

  • alpha: This parameter is used to define the opacity and the value lies between 0.0 (completely transparent) to 1.0 (completely opaque).

Code:

<!DOCTYPE html>
<html>
<head>
<style>
#p1 {background-color:rgba(255,0,0,0.3);}
#p2 {background-color:rgba(0,255,0,0.3);}
#p3 {background-color:rgba(0,0,255,0.3);}
#p4 {background-color:rgba(192,192,192,0.3);}
#p5 {background-color:rgba(255,255,0,0.3);}
#p6 {background-color:rgba(255,0,255,0.3);}
</style>
</head>
<body>

<h1>The rgba() Function</h1>

<p>RGB colors with opacity:</p>
<p id="p1">Red</p>
<p id="p2">Green</p>
<p id="p3">Blue</p>
<p id="p4">Grey</p>
<p id="p5">Yellow</p>
<p id="p6">Cerise</p>

</body>
</html>

Output:


 

16. var()

The var() function in CSS is used to insert a value for custom property.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
:root {
  --main-bg-color: aqua;  
}

#div1 {
  background-color: var(--main-bg-color);
  padding: 5px;  
}

#div2 {
  background-color: var(--main-bg-color);
  padding: 5px;
}

#div3 {
  background-color: var(--main-bg-color);
  padding: 5px;
}
</style>
</head>
<body>

<h1>The var() Function</h1>

<div id="div1">Technology is the result of accumulated knowledge and application of skills, methods, and processes used in industrial production and scientific research.</div>
<br>

<div id="div2">Technology is embedded in the operation of all machines, with or without detailed knowledge of their function, for the intended purpose of an organization.</div>
<br>

<div id="div3">There are three kinds of technological innovations such as the Semi- Radical, Incremental and Disruptive. This kind of technology typically relies on the existing knowledge about technology. However, it uses knowledge in such ways that it differs importantly into the past.</div>

</body>
</html>

Output:


 

17. conic-gradient()

The conic-gradient() function sets a conic gradient as the background image. A conic gradient is a gradient with color transitions rotated around a center point. To create a conic gradient you must define at least two color stops.


Code:

<!DOCTYPE html>
<html>
<head>
<style>
#cronic {
  height: 200px;
  width: 200px;
  background-color: red; 
  background-image: conic-gradient(blue, red, yellow, black);
}
</style>
</head>
<body>

<h1>Conic Gradient - Four Colors</h1>

<div id="cronic"></div>

</body>
</html>









 

Sofia Sondh

The Tech Platform

0 comments

Recent Posts

See All

Comentários


bottom of page