search
HomeWeb Front-endCSS TutorialWhat are the real-world applications of CSS and SVG?

What are the real-world applications of CSS and SVG?

Developers use CSS to style web content and represent it correctly. It can be used to make any content attractive.

The full form of SVG is Scalable Vector Image. SVG is an image type like jpg or png. A jpg or png is a raster image created using a grid of pixels. If we zoom in on a raster image, it starts to get blurry.

Vector images are created using mathematical functions that draw vectors and connect them to form shapes. Since it's not a pixel grid, it never becomes blurry even if we zoom in over 100x.

Whenever we use CSS with vector images, it makes it more attractive and we can create powerful images on the web page. In this tutorial, we'll learn CSS and SVG in action.

Example 1 (Adding basic styles to SVG images)

In the example below, we create a circle in SVG format. We use the "svg" HTML tag to create SVG images. Additionally, we set the size of the view box. Additionally, we set the x and y positions of the circle in the view frame.

We use the "circle" tag to access and style the svg image in CSS. Users can observe that they can control fill color, stroke color, and stroke width through CSS. However, they can also add additional styling using various CSS properties.

<html>
<head>
   <style>
      circle {
         fill: blue;
         stroke: red;
         stroke-width: 3;
      }
   </style>
</head>
<body>
   <h3> Using the CSS with SVG <i> to style the SVG image </i> </h2>
   <svg viewBox="0 0 100 100">
      <circle cx="20" cy="20" r="10" />
   </svg>
</body>
</html>

Example 2 (Add hover effect on SVG image)

In the example below, we have created two squares in SVG vector format. Additionally, we assigned a class name to each shape. We use the class name in CSS to access the HTML element and set the fill color. Additionally, we set up a hover effect for the shape. In the output, hover over the shape and you can observe how its color changes.

<html>
<head>
   <style>
      .shape {fill: green;}
      .shape:hover {fill: #ff0000;}
   </style>
</head>
<body>
   <h4 id="Using-the-CSS-with-SVG-i-to-add-hover-effect-on-the-SVG-image-i"> Using the CSS with SVG <i> to add hover effect on the SVG image. </i> </h4>
   <svg viewBox="0 0 960 600">
      <g id="shapes">
         <path class="shape" d="M100,100 L150,50 L200,100 L150,150 Z" />
         <path class="shape" d="M400,100 L450,50 L500,100 L450,150 Z" />
      </g>
   </svg>
</body>
</html>

Example 3 (Add animation to SVG image)

In the example below, we add animation to an SVG image. Here we have created a circle using SVG. In CSS, we access the circle using its id and add the "move" keyframe as animation.

In the Move keyframe, we change the vertical position of the circle, which shows us a bouncing circle.

<html>
<head>
   <style>
      #ball {
         animation: move 3s infinite;
         transform-origin: center bottom;
      }
      @keyframes move {
         0% {transform: translateY(0);}
         50% {transform: translateY(-30px);}
         100% {transform: translateY(0);}
      }
   </style>
</head>
<body>
   <h3 id="Using-the-CSS-with-SVG-i-to-add-hover-effect-on-the-SVG-image-i"> Using the CSS with SVG <i> to add hover effect on the SVG image. </i> </h3>
   <svg viewBox="0 0 100 100">
      <circle id="ball" cx="30" cy="30" r="15" fill="aqua" />
   </svg>
</body>
</html>

We learned to use CSS with SVG images. In this first example, we learned the basic usage of CSS and SVG. In the second example, we added a hover effect to the SVG image; in the previous example, we added animation to the SVG image.

In actual development, users can use CSS with SVG to add animations and create GIFs.

The above is the detailed content of What are the real-world applications of CSS and SVG?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Draggin' and Droppin' in ReactDraggin' and Droppin' in ReactApr 17, 2025 am 11:52 AM

The React ecosystem offers us a lot of libraries that all are focused on the interaction of drag and drop. We have react-dnd, react-beautiful-dnd,

Fast SoftwareFast SoftwareApr 17, 2025 am 11:49 AM

There have been some wonderfully interconnected things about fast software lately.

Nested Gradients with background-clipNested Gradients with background-clipApr 17, 2025 am 11:47 AM

I can't say I use background-clip all that often. I'd wager it's hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis,

Using requestAnimationFrame with React HooksUsing requestAnimationFrame with React HooksApr 17, 2025 am 11:46 AM

Animating with requestAnimationFrame should be easy, but if you haven’t read React’s documentation thoroughly then you will probably run into a few things

Need to scroll to the top of the page?Need to scroll to the top of the page?Apr 17, 2025 am 11:45 AM

Perhaps the easiest way to offer that to the user is a link that targets an ID on the element. So like...

The Best (GraphQL) API is One You WriteThe Best (GraphQL) API is One You WriteApr 17, 2025 am 11:36 AM

Listen, I am no GraphQL expert but I do enjoy working with it. The way it exposes data to me as a front-end developer is pretty cool. It's like a menu of

Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading IndicatorWeekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading IndicatorApr 17, 2025 am 11:26 AM

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

Various Methods for Expanding a Box While Preserving the Border RadiusVarious Methods for Expanding a Box While Preserving the Border RadiusApr 17, 2025 am 11:19 AM

I've recently noticed an interesting change on CodePen: on hovering the pens on the homepage, there's a rectangle with rounded corners expanding in the back.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.