SVG icons let you add sharp, colorful pictures that look good on any screen size. They keep your website fast and clear.
SVG icon usage in Bootsrap
<svg class="bi" width="32" height="32" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"> <path d="..."/> </svg>
fill="currentColor" attribute to make the icon color follow the text color.width and height to control icon size.<svg class="bi bi-circle-fill" width="24" height="24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"> <path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0z"/> </svg>
<svg class="bi bi-heart-fill" width="32" height="32" fill="red" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M8 1C5.243 1 3 3.243 3 6c0 3.314 5 8 5 8s5-4.686 5-8c0-2.757-2.243-5-5-5z"/> </svg>
<svg class="bi bi-star" width="40" height="40" fill="gold" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"> <path d="M8 12.146l-3.717 2.19 1-4.28-3.283-2.85 4.356-.377L8 3.5l1.644 3.33 4.356.377-3.283 2.85 1 4.28z"/> </svg>
This page shows three Bootstrap SVG icons with different colors and sizes. Each icon is inside a section with a label for screen readers. The icons scale well and keep sharp edges.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap SVG Icon Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> <style> body { font-family: Arial, sans-serif; padding: 2rem; background-color: #f8f9fa; color: #212529; } .icon-example { display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem; } .icon-example svg { flex-shrink: 0; } </style> </head> <body> <h1>Bootstrap SVG Icon Usage</h1> <section class="icon-example" aria-label="Circle icon example"> <svg class="bi bi-circle-fill" width="32" height="32" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" aria-hidden="true"> <path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0z"/> </svg> <p>This is a circle icon that matches the text color and is 32x32 pixels.</p> </section> <section class="icon-example" aria-label="Heart icon example"> <svg class="bi bi-heart-fill" width="32" height="32" fill="red" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" aria-hidden="true"> <path fill-rule="evenodd" d="M8 1C5.243 1 3 3.243 3 6c0 3.314 5 8 5 8s5-4.686 5-8c0-2.757-2.243-5-5-5z"/> </svg> <p>This heart icon is red and stands out on the page.</p> </section> <section class="icon-example" aria-label="Star icon example"> <svg class="bi bi-star" width="40" height="40" fill="gold" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" aria-hidden="true"> <path d="M8 12.146l-3.717 2.19 1-4.28-3.283-2.85 4.356-.377L8 3.5l1.644 3.33 4.356.377-3.283 2.85 1 4.28z"/> </svg> <p>The gold star icon is bigger to catch attention.</p> </section> </body> </html>
Always add aria-hidden="true" to decorative SVG icons for accessibility. Do not add role="img" to decorative icons.
Use fill="currentColor" to make icons inherit text color, so they fit your design easily.
Bootstrap icons can be customized with CSS for hover effects or color changes.
SVG icons are sharp and scale well on all devices.
Use Bootstrap SVG icons with fill="currentColor" to match text color.
Always include accessibility labels and roles for better user experience.
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
