Discover how a tiny code change can make your website icons look perfect everywhere!
Why SVG icon usage in Bootsrap? - Purpose & Use Cases
Imagine you want to add small pictures (icons) to your website buttons or menus. You download many tiny images and place each one manually in your code.
Every time you want to change the icon size or color, you must edit each image separately. This is slow and can cause inconsistent looks. Also, images may look blurry on different screen sizes.
Using SVG icons lets you embed scalable, sharp graphics directly in your code. You can easily change their size and color with simple code changes, keeping your site fast and consistent.
<img src="icon.png" width="16" height="16">
<svg class="bi" width="16" height="16"><use xlink:href="#icon-name" /></svg>
SVG icons enable crisp, flexible visuals that adapt perfectly to any screen and style with minimal effort.
On a shopping site, the cart icon stays sharp and changes color when hovered, all without loading new images or slowing the page.
Manual image icons are hard to resize and style consistently.
SVG icons scale perfectly and can be styled with CSS.
Bootstrap's SVG icon system makes adding and customizing icons easy and efficient.
Practice
Solution
Step 1: Understand SVG icon properties
SVG icons are vector graphics, meaning they can scale to any size without losing sharpness or quality.Step 2: Compare with traditional images
Unlike raster images, SVGs do not pixelate when enlarged, making them ideal for responsive designs.Final Answer:
They scale smoothly without losing quality on any screen size. -> Option AQuick Check:
SVG icons scale well = A [OK]
- Thinking SVG files are always smaller than images
- Assuming SVGs add animations automatically
- Ignoring accessibility needs for icons
Solution
Step 1: Identify how SVG color matches text
Usingfill="currentColor"makes the SVG icon inherit the current text color automatically.Step 2: Check other options
Options A and D set fixed colors, not dynamic. <svg color="text"></svg> uses an invalid attribute for SVG color.Final Answer:
<svg fill="currentColor"></svg> -> Option AQuick Check:
Use fill="currentColor" for text color match [OK]
- Using fixed color codes instead of currentColor
- Trying to use CSS color property inside SVG fill
- Confusing fill with stroke attribute
<svg class="bi bi-star" width="2rem" height="2rem" fill="currentColor"><use xlink:href="#star" /></svg>
Solution
Step 1: Understand the use of <use> with xlink:href
The <use> element references an SVG symbol with id "star" defined elsewhere in the document or sprite.Step 2: Check if #star is defined
Since the snippet does not define the symbol with id "star", the icon will not render and show empty space or nothing.Final Answer:
An empty box because the #star symbol is not defined in the snippet. -> Option BQuick Check:
Undefined symbol reference = empty icon [OK]
- Assuming <use> works without symbol definition
- Thinking fill="currentColor" forces black color
- Believing SVG <use> is unsupported
<svg role="img" aria-label="Search icon"><path d="M10 10 L20 20" /></svg>
Solution
Step 1: Check SVG accessibility attributes
The SVG has role="img" and aria-label="Search icon", which is good for screen readers.Step 2: Check for sizing attributes
SVG icons should have width and height attributes to define visible size; missing here means it may not display properly.Final Answer:
Missing width and height attributes for sizing. -> Option CQuick Check:
SVG needs width and height to show correctly [OK]
- Thinking fill attribute is mandatory for visibility
- Assuming aria-label replaces need for sizing
- Believing <path> usage is incorrect here
Solution
Step 1: Use fill="currentColor" for color inheritance
Setting fill="currentColor" makes the SVG icon inherit the text color of its container.Step 2: Change button text color on hover with CSS
By changing the button's text color on hover, the SVG icon color changes automatically without extra code.Final Answer:
Set fill="currentColor" on the SVG and change the button's text color with CSS on hover. -> Option DQuick Check:
fill="currentColor" + CSS hover color = dynamic icon color [OK]
- Using fixed fill colors that don't change on hover
- Trying to change SVG fill with inline styles on hover
- Replacing SVG files instead of using CSS color changes
Ready to prove it?
Pass the test and this pattern moves unknown
