How To Make An Svg Scale With Its Parent Container ? - GeeksforGeeks
Có thể bạn quan tâm
- Tutorials
- Python Tutorial
- Taking Input in Python
- Python Operators
- Python Data Types
- Python Numbers
- Python String
- Python Lists
- Python Tuples
- Sets in Python
- Python Dictionary
- Python Loops and Control Flow
- Python Conditional Statements
- Python Loops
- Python Functions
- Python OOPS Concept
- Python Data Structures
- Python DSA
- Linked List
- Stack
- Queue
- Tree
- Heap
- Hashing
- Graph
- Sets
- Map
- Advance Data Structure
- Sorting Algorithms
- Searching Algorithms
- Python Exception Handling
- Python File Handling
- Python Exercises
- Python List Exercise
- Python String Exercise
- Python Tuple Exercise
- Python Dictionary Exercise
- Python Set Exercise
- Python Design Patterns
- Python Programming Examples
- Python Practice Questions
- Java
- Java Programming Language
- Java Tutorial
- Data Types
- Variables
- Operators
- Flow Control in Java
- Loops in Java
- Methods
- Strings
- Arrays
- OOPs Concepts
- OOPs Concepts
- Classes and Objects
- Access Modifiers
- Inheritance
- Abstraction
- Encapsulation
- Polymorphism
- Interface
- Packages
- Multithreading
- File Handling
- Regular Expression
- Java Collections
- Java Collections
- Collection Class
- List Interface
- ArrayList
- LinkedList Class
- Queue Interface
- Set Interface
- HashSet Class
- Map Interface
- HashMap Class
- HashTable Class
- Iterator
- Comparator
- Collection Interview Questions
- Java 8 Tutorial
- Java Programs
- Java Programming Examples
- Java Array Programs
- Java String Programs
- Java Date-Time Programs
- Java File Handling Programs
- Java Collection Programs
- Java JDBC Programs
- Java Apache POI Programs
- Java OpenCV Programs
- Java Interview Questions
- Java Interview Questions
- Core Java Interview Questions-Freshers
- Java Multithreading Interview Questions
- OOPs Interview Questions and Answers
- Java Exercises
- Java Quiz
- Java Quiz
- Core Java MCQ
- Java Projects
- Advance Java
- Spring Tutorial
- Spring Boot Tutorial
- Spring Boot Interview Questions
- Spring MVC Tutorial
- Spring MVC Interview Questions
- Hibernate Tutorial
- Hibernate Interview Questions
- Java Programming Language
- Programming Languages
- C
- C++
- JavaScript
- PHP
- R Tutorial
- C#
- SQL
- Scala
- Perl
- Go Language
- Kotlin
- System Design
- System Design Tutorial
- What is System Design
- Key Terminologies in System Design
- Analysis and Architecture of Systems
- Scalability in System Design
- Databases in System Design
- High Level Design or HLD
- Low Level Design or LLD
- Case Studies in Designing Systems
- Complete System Design Tutorial
- Software Design Patterns
- System Design Roadmap
- Top 10 System Design Interview Questions and Answers
- System Design Tutorial
- Interview Corner
- Company Preparation
- Top Topics
- Practice Company Questions
- Interview Experiences
- Experienced Interviews
- Internship Interviews
- Competitive Programming
- Multiple Choice Quizzes
- Aptitude for Placements
- Computer Science Subjects
- Operating System
- DBMS
- Computer Networks
- Engineering Mathematics
- Computer Organization and Architecture
- Theory of Computation
- Compiler Design
- Digital Logic
- Software Engineering
- DevOps
- GIT
- AWS
- Docker
- Kubernetes
- Microsoft Azure Tutorial
- Google Cloud Platform
- Linux
- Linux Tutorial
- Linux Commands A-Z
- Linux Commands Cheatsheet
- File Permission Commands
- Linux System Administration
- Linux File System
- Linux Shell Scripting
- Linux Networking
- Linux Interview Questions
- Software Testing
- Software Testing Tutorial
- Software Engineering Tutorial
- Testing Interview Questions
- Jira
- Databases
- DBMS Tutorial
- SQL Tutorial
- PostgreSQL Tutorial
- MongoDB Tutorial
- SQL Interview Questions
- MySQL Interview Questions
- PL/SQL Interview Questions
- Android
- Android Tutorial
- Android Studio Tutorial
- Kotlin For Android
- Android Projects
- Android Interview Questions
- 6 Weeks of Android App Development
- Excel
- MS Excel Tutorial
- Introduction to MS Excel
- Data Analysis in Excel
- Basic Excel Formulas & Functions
- Data Analysis in Advanced Excel
- Workbooks
- Statistical Functions
- Data Visualization in Excel
- Pivot Tables in Excel
- Excel Spreadsheets in Python
- Basic Excel Shortcuts
- Mathematics
- Number System
- Algebra
- Linear Algebra
- Trigonometry
- Set Theory
- Statistics
- Probability
- Geometry
- Mensuration
- Logarithms
- Calculus
- Python Tutorial
- DSA
- Data Structures
- Arrays
- Matrix
- Strings
- Linked List
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- Doubly Circular Linked List
- Linked List Tutorial
- Stack
- Queue
- Tree
- Generic Tree
- Binary Tree
- Binary Search Tree
- AVL Tree
- B Tree
- B+ Tree
- Red Black Tree
- Tree Data Structure Tutorial
- Heap
- Hashing
- Graph
- Set Data Structure
- Map Data Structure
- Advanced Data Structure
- Data Structures Tutorial
- Algorithms
- Analysis of Algorithms
- Design and Analysis of Algorithms
- Asymptotic Analysis
- Asymptotic Notations
- Worst, Average and Best Cases
- Searching Algorithms
- Linear Search
- Binary Search
- Searching Algorithms Tutorial
- Sorting Algorithms
- Selection Sort
- Bubble Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Counting Sort
- Radix Sort
- Bucket Sort
- Sorting Algorithms Tutorial
- Greedy Algorithms
- Dynamic Programming
- Graph Algorithms
- Pattern Searching
- Recursion
- Backtracking
- Divide and Conquer
- Mathematical Algorithms
- Geometric Algorithms
- Bitwise Algorithms
- Randomized Algorithms
- Branch and Bound
- Algorithms Tutorial
- Analysis of Algorithms
- DSA Tutorial
- Practice
- All DSA Problems
- Problem of the Day
- Company Wise Coding Practice
- Amazon
- Microsoft
- Flipkart
- Explore All
- GfG SDE Sheet
- Practice Problems Difficulty Wise
- School
- Basic
- Easy
- Medium
- Hard
- Language Wise Coding Practice
- CPP
- Java
- Python
- Curated DSA Lists
- Beginner's DSA Sheet
- Top 50 Array Problems
- Top 50 String Problems
- Top 50 DP Problems
- Top 50 Graph Problems
- Top 50 Tree Problems
- Competitive Programming
- Company Wise SDE Sheets
- Facebook SDE Sheet
- Amazon SDE Sheet
- Apple SDE Sheet
- Netflix SDE Sheet
- Google SDE Sheet
- DSA Cheat Sheets
- SDE Sheet
- DSA Sheet for Beginners
- FAANG Coding Sheet
- Product-Based Coding Sheet
- Company-Wise Preparation Sheet
- Top Interview Questions
- Puzzles
- All Puzzles
- Top 100 Puzzles Asked In Interviews
- Top 20 Puzzles Commonly Asked During SDE Interviews
- Data Structures
- Data Science
- Python Tutorial
- R Tutorial
- Machine Learning
- Data Science using Python
- Data Science using R
- Data Science Packages
- Pandas Tutorial
- NumPy Tutorial
- Data Visualization
- Python Data Visualization Tutorial
- Data Visualization with R
- Data Analysis
- Data Analysis with Python
- Data Analysis with R
- Deep Learning
- NLP Tutorial
- Web Tech
- HTML Tutorial
- CSS Tutorial
- JavaScript Tutorial
- PHP Tutorial
- ReactJS Tutorial
- NodeJS Tutorial
- AngularJS Tutorial
- Bootstrap Tutorial
- Typescript
- Web Development Using Python
- Django
- Django Tutorial
- Django Projects
- Django Interview Questions
- Flask
- Flask Tutorial
- Flask Projects
- Flask Interview Questions
- Postman
- Github
- Django
- Wordpress Tutorial
- Web Design
- Cheat Sheets
- HTML Cheat Sheet
- CSS Cheat Sheet
- JavaScript Cheat Sheet
- React Cheat Sheet
- Angular Cheat Sheet
- jQuery Cheat Sheet
- Bootstrap Cheat Sheet
- Learn Complete Web Development
- Courses
- Coding for Everyone
- DSA to Development
- Machine Learning & Data Science
- Generative AI & ChatGPT
- Become AWS Certified
- DSA Courses
- Data Structure & Algorithm(C++/JAVA)
- Data Structure & Algorithm(Python)
- Data Structure & Algorithm(JavaScript)
- Programming Languages
- CPP
- Java
- Python
- JavaScript
- C
- HTML Tutorial
- HTML Exercises
- HTML Tags
- HTML Attributes
- Global Attributes
- Event Attributes
- HTML Interview Questions
- HTML DOM
- DOM Audio/Video
- HTML 5
- HTML Examples
- Color Picker
- A to Z Guide
- HTML Formatter
SVG is the abbreviation for Scalable Vector Graphics. It is an Extensible Markup Language (XML) based vector image format for two-dimensional graphics. It supports interactivity and animation. This means that every attribute and every element present in the SVG file can be animated. SVG image behaviors are defined in XML text files. They can be searched, indexed, scripted, compressed, and can be created or edited using any text editor, as well as using drawing software such as Inkscape. Almost every modern web browser support SVG.
The reason why it is tricky to scale SVG is that it does not scale like the other image formats. SVG images have a clearly defined aspect ratio: the ratio of width to the height which makes it difficult to scale with the changing parent container. Other images scale easily because the browser determines the height, width, and aspect ratio of the image, and it adjusts everything accordingly. So giving these properties to SVG should be the first step to our requirement. Although setting height and width barely sets an aspect ratio but what we aim for is scaling which is beyond the aspect ratio. A viewbox can serve our purpose rightly. The viewBox is an attribute of the <svg> element which takes four parameters x, y, width, and height. x and y signify the origin of the SVG coordinate system and width and height signify the number of pixels or coordinates that should be scaled to fill the width and height respectively. Let us take a look at the following example:
First approach:
- At first, we create a container that occupies 30% of the total width of the screen and 20% of the total height of the screen.
- Next, we create an SVG image(rectangle) using the <rect> tag and specifying the height, width, and fill attributes.
- The <svg> element wraps the rectangle image. The SVG element occupies 100% width of the parent container and its height is auto-adjusted depending on screen size. We use viewBox to make the SVG image scalable.
- viewBox = “0 0 100 100”: defines a coordinate system having x=0, y=0, width=100 units and height=100 units.
- Thus, the SVG containing a rectangle having width=50px and height=50px will fill up the height and width of the SVG image, and all the dimensions get scaled equally. Changing the x and y coordinates can yield different results, but we will keep ourselves restricted to the stated values.
Example:
HTML
<!DOCTYPE html> <html> <body> <div class="container" style= "width:30%; height: 20%; border:1px dashed black;"> <svg width="100%" height="auto" viewBox="0 0 100 100"> <rect width="50" height="50" style="fill:rgb(0,170,0); stroke-width:1; stroke:rgb(0,0,0)" /> </svg> </div> </body> </html> |
Output:
- Full Screen:
![](https://media.geeksforgeeks.org/wp-content/uploads/20201015200548/2.png)
Full Screen
- Minimized Screen:
![](https://media.geeksforgeeks.org/wp-content/uploads/20201015200547/1-300x157.png)
Minimized Screen
Second approach:
- The second approach demonstrates scaling of an svg image which is present within the <img> tag.
- In this case, browsers automatically adjust the image aspect ratio based on the current screen size.
- It is important to mention either height or width as an attribute in the img tag else there is possibility that Internet Explorer may change width = ”auto” and height =”auto” to some value which is acceptable to it.
Example:
HTML
<!DOCTYPE html> <html> <body> <div class="container" style= "width:80%; height:80%; border:1px dashed black;"> <img src="E:\method-draw-image.svg" style="width:50%"> </div> </body> </html> |
Output:
- Full Screen:
![](https://media.geeksforgeeks.org/wp-content/uploads/20201015205125/3.png)
Full Screen
- Minimized Screen:
![](https://media.geeksforgeeks.org/wp-content/uploads/20201015205200/4.png)
Minimized Screen
S
Shreyasi_Chakraborty Follow Improve Next Article How to resize SVG to fill its container in ReactJS?Please Login to comment...
Similar Reads
How to resize SVG to fill its container in ReactJS? We can resize SVG to fill its container in ReactJS using the type props. We can add the following style to the SVG CSS Syntax: svg { width: inherit; height: inherit; } React Syntax: <svg style = {{ width: "inherit", height:"inherit"}} // Child elements </svg>Creating React Application: Step 1: Create a React application using the following 2 min read How to move a container inside of another container in Bootstrap 5 ? The container is used to set the content’s margin. It contains row elements and the row elements are containers of columns. This is known as the grid system. In this article, we will learn about How to move a container inside of another container in Bootstrap 5. We will move a container to the left in another container. Syntaxms-0 me-0 Table of Con 2 min read What is the Difference Between container and container-fluid in Bootstrap 5 ? In Bootstrap 5, the "container" class creates a responsive container with a fixed width, adjusting to different screen sizes while maintaining margins. On the other hand, the "container-fluid" class generates a full-width container that spans the entire viewport without any margins, providing a seamless layout across various devices. "Container" is 3 min read How to force child div to be 100% of parent div's height without specifying parent's height? When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem 3 min read SVG scale Attribute The scale attribute decides the displacement scale factor that must be used on a <feDisplacementMap> filter primitive. Only <feDisplacementMap> element is using this attribute. Syntax: scale = "number" Attribute Values: The scale attribute accepts the values mentioned above and described below number: It is either an integer or a number 2 min read SVG FEDisplacementMap.scale Property The SVG FEDisplacementMap.scale property returns the SVGAnimatedNumber object corresponding to the scale component of the FEDisplacementMap.scale element. Syntax: var a = FEDisplacementMap.scale Return value: This property returns the SVGAnimatedNumber object corresponding to the scale component of the FEDisplacementMap.scale element. Example 1: C/ 1 min read SVG <svg> Element SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. SVG graphics are supported by the <svg> element in HTML. SVG graphics feature a container in which we can create a variety of shapes such as boxes, pathways, text, graphic images, and circles. Syntax: <svg width="x" height="y"> sh 2 min read What is the Position of an element relative to its container in CSS ? In this article, we will learn what is the position of an element relative to its container. The position property in CSS tells about the method of positioning for an element or an HTML entity. There are five different types of position properties available in CSS: Fixed: Any HTML element with position: fixed property will be positioned relative to 4 min read How to position a div at the bottom of its container using CSS? To position a div at the bottom of its container in CSS, you can use various approaches. Using absolute positioning involves setting `position: absolute;` and `bottom: 0;`, aligning the div at the container's bottom. Flexbox achieves this by employing `display: flex;` and `margin-top: auto;`, while Grid utilizes `display: grid;` with a defined temp 2 min read How to remove parent element except its child element using jQuery ? Given an HTML document and the task is to remove the parent element except for its child element. Approach 1: Use contents() method to select all the direct children, including text and comment nodes for the selected element.Selected elements are stored in a variable.Now, use replaceWith() method to replace the content of parent element by its all 2 min read View More Articles Article Tags :- HTML-SVG
- HTML
- Web Technologies
![three90RightbarBannerImg](https://media.geeksforgeeks.org/auth-dashboard-uploads/Must-do-coding-questions-with-bg-%281%29.png)
![geeksforgeeks-suggest-icon](https://media.geeksforgeeks.org/auth-dashboard-uploads/suggestChangeIcon.png)
![geeksforgeeks-improvement-icon](https://media.geeksforgeeks.org/auth-dashboard-uploads/createImprovementIcon.png)
What kind of Experience do you want to share?
Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences Can't choose a topic to write? click here for suggested topics Write and publish your own ArticleTừ khóa » Html Scale Svg Element
-
How To Scale SVG | CSS-Tricks
-
Resizing SVG In Html? - Stack Overflow
-
Scale - SVG: Scalable Vector Graphics - MDN Web Docs
-
Basic Transformations - SVG: Scalable Vector Graphics | MDN
-
Intrinsic Sizing - SVG
-
How To Resize SVG In HTML - W3docs
-
Html Scale Svg Code Example - Code Grepper
-
Scale SVG Graphics With Only Css - Gists · GitHub
-
Resizing An SVG When The Window Is Resized In D3.js - Chartio
-
SVG Viewport And ViewBox (For Complete Beginners) - Web Design
-
Scaling SVG Elements - CodePen
-
HTML Svg ViewBox Attribute - Dofactory
-
Html – Scale An SVG Element Using A Transform Origin - ITecNote
-
How To Resize A SVG Image - Medium