Is it possible for an SVG to derive its dimensions from the containing element?

I am currently in the process of transitioning a Vue/Vuetify application from using web fonts for Material Design Icons to utilizing SVG icons instead.

With the web font, an example of a heading that includes an icon looks like this:

<template>
  <h1>
    <v-icon>mdi-format-list-bulleted</v-icon>
    My Heading
  </h1>
</template>

The icon's height automatically adjusts to match the heading due to the CSS properties of the generated <i> element:

font-size: inherit;
color: inherit;

After switching to SVG icons, an example of a heading with an icon would be:

<template>
  <h1>
    <v-icon>{{ iconPath }}</v-icon>
    My Heading
  </h1>
</template>

<script>
  import { mdiFormatListBulleted } from '@mdi/js'

  export default {
    data: () => ({
      iconPath: mdiFormatListBulleted
    })
  }
</script>

This results in the following markup:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img"
    aria-hidden="true" class="v-icon__svg">
    <!-- path element omitted -->
</svg>

The CSS class for the SVG has fixed height and width properties:

.v-icon__svg {
    height: 24px;
    width: 24px;
    fill: currentColor;
}

Since the icon's height is fixed at 24px, it does not match the height of the parent element.

Although the <v-icon> component offers various props like size, large, small to adjust the icon size, they set a specific value rather than inheriting the parent's size.

Is there a method for an SVG to inherit its size from the parent element? Particularly, I want the SVG's height and width to align with the parent element's dimensions.

Demo

I have created a demonstration showcasing the implications of using SVG icons. It might take around 30 seconds to load, and disabling third-party cookies could be necessary for it to function properly.

Answer №1

To make the SVG height match the font-size of the context, you can set it to one of the relative units and then use vertical-align if needed.

h1,h2 {
  color: navy;
}

.height-ch {
    height: 1ch;
    fill: currentColor;
}

.height-em {
    height: 1em;
    fill: currentColor;
}

.height-rem {
    height: 1rem;
    fill: currentColor;
}
<h1>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img"
    aria-hidden="true" class="height-em">
    <path d="M 0 0 L 24 0 L 24 24 L 0 24 Z" />
</svg>
Heading em
</h1>
<h2>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img"
    aria-hidden="true" class="height-em">
    <path d="M 0 0 L 24 0 L 24 24 L 0 24 Z" />
</svg>
Heading em
</h2>
<h2>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img"
    aria-hidden="true" class="height-ch">
    <path d="M 0 0 L 24 0 L 24 24 L 0 24 Z" />
</svg>
Heading ch
</h2>
<h2>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img"
    aria-hidden="true" class="height-rem">
    <path d="M 0 0 L 24 0 L 24 24 L 0 24 Z" />
</svg>
Heading rem
</h2>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Achieving Flexbox alignment in a responsive layout

Struggling with achieving a responsive layout using Flexbox. Picture a page filled with panels. The main objective is to display a certain number of panels per row, aligned both horizontally and vertically like the rest. The main issue I am encountering ...

The collapsed feature of the Bootstrap 4 Navbar is not functioning as

Recently, I delved into the world of Bootstrap 4 and decided to create a collapsing menu. Everything seemed to be working fine when I clicked the button and saw the content display perfectly. However, things took a turn for the worse when I tried to collap ...

Implement a fadeout effect by using a timeout function that adds a class to the div element in

I implemented a loader on my webpage that animates for 3 seconds before a function kicks in to modify the styles and make it invisible. I am looking to add a fadeout effect when the 'waa' style is applied to the 'load' div. setTimeou ...

Navigation shadow in Bootstrap not showing up on nav.shadow

I've created a navigation bar component using vue.js and am styling it with bootstrap by including the framework in the main HTML file. Although I've added the desired styling to the navbar component, I'm facing an issue where adding the sh ...

Tips for connecting elements at the same index

.buttons, .weChangeColor { width: 50%; height: 100%; float: left; } .weChangeColor p { background: red; border: 1px solid; } .toggleColor { background: green; } <div class="buttons"> <p><a href="#">FirstLink</a></ ...

What steps should I take to execute a unique function the very first time a user scrolls to a specific element?

The issue at hand: I am working with a sophisticated looping image carousel that needs to initiate - starting from a specified initial slide - as soon as the user scrolls to a specific division. It must not restart if the user scrolls up or down and then ...

The jQuery click function triggers immediately upon the loading of the page

Despite my best efforts to resolve this issue independently and conduct extensive research on Google, I am still unable to identify the root of the problem. It seems that the click function continues to activate upon page load. Kindly elucidate the under ...

css - Showcasing images with a horizontal scrollbar

I'm having an issue with displaying multiple images in a fixed-width div horizontally. I want to have a horizontal scroll bar for the images that exceed the width of the div. Currently, the images are appearing vertically instead of side-by-side. Is ...

CSS can be utilized to craft intricate and dynamic shapes

Currently, I am attempting to produce a trapeze-like design utilizing various techniques in order to achieve the best possible outcome. The shape I am aiming to create is similar to this: (the content inside the shape will consist of images and text) Th ...

The integration of a Bootstrap modal within the side bar's rendering

Struggling with a modal appearing inside my side div and can't find any solutions in the bootstrap5 documentation or online forums. https://i.stack.imgur.com/gHsBi.png I just want the modal to display centered on the page with the background fade ef ...

How can I switch between different images using jQuery?

HTML <div class="image_rollover"> <img id="image_one" src=image_one.png"> <img id="image_two" src="image_two.png"> <img id="image_three" src="image_three.png"> <img id="image_four" src="image_four.png"> ...

"Utilize jQuery's append method to dynamically add elements to the DOM and ensure

I am currently facing an issue with my AJAX function that appends HTML to a page using the jQuery append method. The HTML being appended is quite large and contains cells with colors set by CSS within the same document. The problem I'm encountering i ...

Is there a way to update the content of both spans within a button component that is styled using styled-components in React?

I am interested in creating a unique button component using HTML code like the following: <button class="button_57" role="button"> <span class="text">Button</span> <span>Alternate text</span> ...

What is the best way to differentiate the behavior of two identical classes using a single JavaScript function?

Can someone help me figure out how to make two circles move differently with the same JavaScript function? I'm new to JavaScript and struggling to differentiate between classes in HTML to achieve this. My goal is to have each circle move randomly and ...

Styling used on the ofx.com website

I am attempting to recreate the CSS effects used on ofx.com, which includes a picture of London and a grey box with text. However, my version is not working properly. I am unsure of what might be missing in my code. Here is the snippet: .generic-hero-bl ...

What is the best way to override default CSS properties for a:hover on linked images?

I have a printer-friendly/PDF image that looks like this: Simple enough. However, when hovering over it, the background turns grey due to default hyperlink styles set as follows: #footer a:hover { color: white; background-color: #636363; -moz-transition: ...

Enhance your navigation bar with hover styles in React Router Dom v6

I'm having an issue with my navbar where the active styles are overriding the hover state. I want to maintain my hover state styles even on the active nav link. How can I achieve this? Here is what's currently happening: Active nav styles (look ...

Left-aligned and centered - Bootstrap

I need help figuring out how to center a list of items on the page, but have them aligned left. I am using bootstrap. I want the text to be in the center of the page but aligned left. <ul class='text-center'> <li class=& ...

The jquery datepicker is malfunctioning after switching to another component

My current setup includes the following versions: jQuery: 3.3.1 jQuery UI: 1.12.1 AngularJS: 6 Here's a snippet of my code: <input id="test" type="text" class="form-control" value=""> In my component (component.t ...

Inline styles are effective, but external stylesheets are not functioning properly (see JsFiddle for reference)

http://jsfiddle.net/xw0vvo9e/4/ Trying to specify a background color for the navBar. In the provided jsfiddle, the CSS includes: div .navBar { width: 100%; height: 45px; background-color: #FF0000; top: 0px; position: fixed; } However, the background ...