Adjust the size and positioning of SVG elements within a flexbox layout

I am attempting to create a flexbox with an SVG element as a child. The goal is for the SVG element to be as large as possible in terms of both width and height.

.grid {
  display: grid;
  grid-template-columns: 2fr 2fr 2fr;
  gap: 1em;
}

.parent {
  aspect-ratio: 1 / 1;  
  background: lightgrey;
  display: flex;
  flex-direction: column;
}

.box {
  flex: 1;
  display: flex;
  flex-direction: column;
}

svg {
  flex: 1;
}
<div class="grid">

  <div class="parent">
    <div>no box around SVG</div>
    <div>SVG height larger than its width</div>
    <svg viewBox="0 0 1 3">
      <path d="M0 0 L1 3 L0 3" />
    </svg>
    <div>SVG has maximum height</div>
    <div>and centers horizontally</div>
  </div>
  
  <div class="parent">
    <div>no box around SVG</div>
    <div>SVG width equal to its height</div>
    <svg viewBox="0 0 1 1">
      <path d="M0 0 L1 1 L0 1" />
    </svg>
    <div>SVG has maximum height</div>
    <div>and centers horizontally</div>
  </div>

  <div class="parent">
    <div>no box around SVG</div>
    <div>SVG width larger than its height</div>
    <svg viewBox="0 0 3 1">
      <path d="M0 0 L3 1 L0 1" />
    </svg>
    <div>SVG has maximum width</div>
    <div>and centers vertically</div>
  </div>
  
  
  <div class="parent">
    <div>box around SVG</div>
    <div>SVG height greater than its width</div>
    <div class="box">
      <svg viewBox="0 0 1 3">
        <path d="M0 0 L1 3 L0 3" />
      </svg>
    </div>
    <div>SVG has maximum height and width</div>
    <div>but should be equal to box 1</div>
  </div>
  <div class="parent">
    <div>box around SVG</div>
    <div>SVG width equal to its height</div>
    <div class="box">
      <svg viewBox="0 0 1 1">
        <path d="M0 0 L1 1 L0 1" />
      </svg>
    </div>
    <div>SVG has maximum height and width</div>
    <div>but should be equal to box 2</div>
  </div>
  <div class="parent">
    <div>box around SVG</div>
    <div>SVG width greater than its height</div>
    <div class="box">
      <svg viewBox="0 0 3 1">
        <path d="M0 0 L3 1 L0 1" />
      </svg>
    </div>
    <div>SVG has maximum width</div>
    <div>and is vertically centered like box 3</div>
  </div>
</div>

While the first three boxes are functioning properly, the last three boxes are not behaving the same way. It appears that there is another flexbox affecting their behavior. How can I make the last set of boxes work the same way as the initial set?

It seems that the last three boxes always occupy the entire width available to them.

Answer №1

For optimal performance on Firefox: In a strange turn of events, inputting

height="1" or width="1"
, or any numerical value for that matter, into the svg element seems to do the trick when combined with adding svg {height: auto; width: auto;} to override the html attributes.

To ensure compatibility across Chrome and Firefox: It seems necessary to include both height="1" width="1" in the svg code and simply incorporate svg {width: auto;}.

The reasoning behind this method remains a mystery, but it gets the job done.

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

Tips for resizing a 100% div without displaying vertical scrollbars in the browser

I am facing an issue with a table that has three rows. I have a main #container div with a height of 100%, and inside it is a table with 3 rows. The first and last row contain fixed size content. However, in the second row, there is a #content div with a h ...

What is the best way to decrease the width of one of the four columns?

I trust all is well with you. I'm having trouble adjusting the width of one of my table data cells. In the image attached below, you'll notice that my + icon is the same size as the other columns. I'd like to make it much narrower and posit ...

Utilizing JavaScript to adjust the width of an HTML element

Struggling to manipulate the position and size of a div tag using a JavaScript function, particularly confused about how to retrieve the current width of the div. Here is the function in question function socialExt() { document.getElementById("Left") ...

Using jQuery to animate a div element with CSS3

After creating a loader using CSS3 and CSS3 Animations, I am looking to implement the actual loading function with jQuery based on a specified duration. For instance, setting it to load fully in 30 seconds. This is what I currently have: < ...

Tips for overlaying an image on a div regardless of its height

(!) Although this question may seem repetitive, I have not been able to find a suitable solution in any of the previous 10 topics. I apologize for the inconvenience and am actively seeking a resolution to this unique situation; Allow me to outline the iss ...

Formatting list items in HTML

I have just finished coding the following HTML code: My main goal is to align the list items - HOME | All About Kittens | Feeding Your Kitten in a proper layout. Currently, they are displayed as a simple list. However, I would like them to be aligned ...

Responsive design for iPad and larger screens - adjusting to different screen sizes

Does anyone know of a CSS code that can effectively target both iPad and desktop devices with a max-width of 768? I attempted the following code, however it seems to only work for desktops: @media only screen and (max-width: 768px) Due to this limitatio ...

Creating a button with multiple background gradients using CSS

When working on a button element that already boasts a gradient effect, my aim was to integrate a small play image directly in the center. However, achieving this without compromising the current layout and ensuring compatibility across various browsers pr ...

Using JavaScript to open a new window and display CSS while it loads

I am looking to utilize a new window for printing a portion of HTML content. var cssLink = document.getElementByTagName('link')[2]; var prtContent = document.getElementById('print_body'); var WinPrint = window.open(' ...

What is the best way to incorporate the parallax effect into a v-carousel?

Currently, I have a "v-carousel" containing multiple images and now I am looking to incorporate a parallax effect into it, similar to "v-parallax". <v-carousel cycle height="600" hide-delimiter-background show-arrows-on-hover> <v-carousel-i ...

CSS Alignment in React with Material UI

Is there a way to align one button to the left while centering the other two? To see an example in Codesandbox, please visit HERE <DialogActions sx={{ justifyContent: "center" }}> <Button>Left</Button> <Button on ...

Adjusting the size of content with CSS

I've been attempting to get an image to cover the entire screen on a device using CSS, but so far I haven't had any success. The image is within an :after pseudo element and content tag. Since it needs to be laid over the HTML, I can't use t ...

Struggling to position Bootstrap navbar links completely to the right side

The navigation bar link items are not aligning all the way to the right when using mr-auto. It appears like this Here is my HTML template: <nav class="navbar navbar-default navbar-expand-lg navbar-custom"> <div class="navbar-co ...

Overflow-y SweetAlert Icon

Struggling with a website modification issue where using Swal.fire(...) causes an overflow problem affecting the icon rendering. How can this be fixed? Here is the custom CSS code in question: * { margin: 0px; padding: 0px; font-family: &ap ...

Grid layout with columns of equal width in Bootstrap

I'm currently working on creating a dashboard with Angular and Bootstrap 4. I've utilized the equal-width columns from https://getbootstrap.com/docs/4.0/layout/grid and it's functioning well. However, I'm facing an issue where if the lo ...

Troubleshooting Problem with CSS Button Hover Effects

Is there a way to keep the white "Contact US" text active or white when the user hovers over the button and moves down to the list of links? It seems like a simple issue, but I can't figure it out. Currently, the text turns from red to white and back ...

What is causing the file to automatically insert white space whenever I insert an image?

Working on a Bootstrap 4 website with three images inside a .row div, I encountered an issue. To ensure all images have the same height, I created a custom class setting height: 20% and width: auto. Though the images display as desired, there is extra spac ...

Why is my CSS not showing up in Live Server when I preview it from Visual Studio?

Every time I use the "Go Live" option with Live Server (Visual Studio extension), I only get a preview of my plain HTML file. However, when I manually open the HTML file from the folder containing both HTML and CSS files, the page loads correctly with the ...

I'm noticing that when I refresh the page, my inputs seem to disappear from the page, yet I can see that they are saved in the console's local

If anyone can help me with this issue I'm facing, I would greatly appreciate it. My aim is to retain values in the text box along with local storage even after refreshing the page. Here is an example of the code I am working with: Link to my web appl ...

Use jQuery to delete the parent div when the element inside does not contain a specific class

I'm dealing with a grid that has 3 columns, each sharing the same div class. Inside each column is a span with either the class of class="simplefavorite-button" or class="simplefavorite-button.active". Here's how the markup looks: <div class= ...