Can an SVG image/object be embedded in a way that it automatically adjusts to the size of its container like an inline element?

My inquiry is this: can an embedded SVG image or object be manipulated to behave like an inline element, stretching or shrinking to fit its container size?

I have searched on this and various Stackoverflow threads but have not found a definitive answer...

Below is the code I have been working with:

/*RESET RULES*/
*, ::before, ::after {
  box-sizing: border-box;
}
body {
  line-height: 1.5;
}
img {
  max-width: 100%;
  display: block;
}
  /*CUSTOM STYLES*/
  div.gcnt{
    position: relative;
        display: inline-block;
        min-height: 50px;
    max-height: 200px;
    min-width: 100px;
    max-width: 300px;
    width: 20%; height: 30%;

    border: 1px dashed greenyellow;
  }
  div.gcnt.inline svg, div.gcnt.ii img, div.gcnt.oi object {
    position: absolute;
    width: 100%; height: 100%;
  }
<div class="gcnt inline"><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 100 100" preserveAspectRatio="none"><circle cx="50%" cy="50%" r="40" stroke="black" stroke-width="1.2" fill="darkseagreen"/></svg></div>
inline SVG works fine!
<hr>
<div class="gcnt ii"><img src="a.svg" class="svgi"></div>
embedded SVG using img tag does not work
<hr>
<div class="gcnt oi"><object id="svg1" class="svgo" data="a.svg" type="image/svg+xml"></object></div>
embedded SVG using object tag also does not work

The inline `svg` works well, while using the `img` or `object` tags does not (even though a.svg contains identical code as the inline svg).

Here's a screenshot demonstrating the issue:

https://i.sstatic.net/jC7s2.png

I want to avoid using the `background-image` style rule for this.

Any ideas on how to achieve this goal? Your assistance would be greatly appreciated, thank you in advance

Also, I am not concerned about supporting Internet Explorer

EDIT:

After further investigation, it turns out that I was misled by a heading title in the article I referenced earlier in my question.

(This is the current state when writing this: https://i.sstatic.net/ULaQb.png)

As mentioned by @RobertLongson in the comment below: although using an *inline* SVG element works fine, external SVGs (included in the webpage via `img` or `object` tags) do not work correctly due to attribute name being case-sensitive. The correct case must be used for it to function properly. In my `a.svg` content, I incorrectly wrote `viewbox` instead of `viewBox` (which is the correct case). Simply correcting this made everything work as intended, so... this is definitely the solution in this scenario.

Answer №1

#custom-div {
width: 106px;
height: 137px;
resize: both;
overflow: auto;
}
<div id="custom-div">    
    <svg
    id="uniqueSvgId" 
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    version="1.1"
    x="0"
    y="0"
    width="100%"
    height="100%"
    viewBox="0 -90 800 600"
    preserveAspectRatio="none">
        <ellipse cx="350" cy="100" rx="350" ry="100" />
    </svg>
</div>

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

GWT Designer style issue plaguing development efforts

Currently, I am working on designing a login view using GWT Designer while incorporating styles from Twitter Bootstrap. The structure I have set up is as follows: However, the displayed result does not meet my expectations: Despite setting all CSS paddi ...

Is using CSS as an HTML attribute considered poor practice?

I've been noticing an increasing trend of combining HTML, CSS, and JavaScript together in web development. However, I was taught to keep them separate with HTML linking to the sources/scripts. While using the style attribute in HTML is sometimes acce ...

What is the best way to adjust the width and height of react-flip-page through CSS for different media queries?

Incorporating the react-flip-page npm package to add animation to a book on my web application has been exciting. However, I've encountered an issue with adjusting the width when using media queries due to the available props for setting size and colo ...

Adjusting the <div> size for optimal display on iPhone and iPad screens

I am having an issue with my jQuery Mobile app. I have a page with a header and footer, and the main content consists of 4 images arranged in a 2x2 grid format. Since I couldn't get JM grid to work, I created my own grid using a div container. Here is ...

Guide on aligning input and labels side by side in Bootstrap forms using CSS only

I've exhausted all the solutions provided here, including referencing this Bootstrap horizontal-form link. However, nothing seems to be effective. I am open to trying any method at this point, even if it involves just modifying the CSS. My main object ...

Keep Variable-Height Element Corners Rounded to Perfection

I am facing a challenge with creating buttons that have perfectly rounded corners. The button is 50px high and the border radius is set to 25px, resulting in a perfect half-circle on each side of the button: It's pretty straightforward to achieve thi ...

Row buttons in a mat-table that can be clicked individually

In my mat-table, each row represents an audio recording for a call. At the end of each row, there is a play button that plays the respective audio file when clicked. However, whenever I click any play button, the correct audio file plays but all the play b ...

PHP script not running as intended

I created an HTML form where users can input values and select options. Once the user has made their selections, these values are then sent to a PHP script on the server. The PHP script retrieves data from a MySQL database based on the user-selected values ...

Issue with using an <a> tag within a <li> element in Bootstrap

When I attempted to include an "a" tag within an "li" element, I noticed that the pointer cursor was missing and the href attribute wasn't functioning as expected. Which Bootstrap property is causing this issue? Could it be a lack of padding for the l ...

Difficulty arranging these elements in CSS

I'm attempting to achieve the following: (The black box represents a signup/login section, the blue is a navigation bar, and the red is a header area with some text content) I'm trying to reach this outcome with the following CSS: @import url(/ ...

The sequence of HTML5 DragDrop Enter and Leave events occur consecutively

I am encountering a problem with a simple drag & drop effect where the class name is changed when the drag enters or leaves, resulting in continuous entering and leaving. Take a look at this basic HTML code: <div class="box"> <div cla ...

Ways to determine if a parent element contains a child element

I am dealing with a parent element that contains a child element, but the child element is only revealed when a button is clicked. Here is what the initial parent element looks like: <div class="parent"> <!--v-if--> </div> ...

Replica of Spotify - Bottom Section - HTML and CSS Styling

I have been attempting to replicate the Spotify interface using only HTML and CSS. While I have successfully created the player section, I am struggling to center the player and incorporate the other two details as shown in the attached images. The 'G ...

What is the best way to center elements vertically within a table element?

I'm encountering some issues with my tables. I have created two tables, stacked one below the other, and I am trying to center the elements within them. However, I am facing difficulty in achieving vertical alignment and true centering, as shown in th ...

Styling a CSS property using React

I’m having trouble figuring out how to properly format the marginLeft value in my code. I’ve tried various methods, but haven’t been successful yet. Can someone please provide assistance on escaping the dash when defining properties this way? If it ...

Issue with the final transition of the last image in the Bootstrap carousel

I'm currently in the process of creating my own portfolio website. I've integrated a Carousel feature inspired by Bootstrap's example. However, I've noticed some glitches in the transitions between the first or second slides and the thi ...

What is the reason for preserving the height to width ratio in the <img> tag?

When I write a simple code like this: <img src="mycat.jpg" height="300px";> I noticed that if the image is very large, the height will be reduced to 300px, and the width will automatically adjust to fit the new height. I expected ...

Symfony: The Database Query Button with a Pop-up Feature

I am looking to create a button that will automatically search my database for all users with a "secouriste" attribute set and display their first name, last name, and phone number in a popup. After conducting research, here is what I have gathered: In m ...

Utilizing the Bootstrap grid system allows for input fields to automatically expand to 100% width once they reach

I am encountering some challenges while attempting to develop a responsive form using bootstrap grids. The issue seems to arise when resizing the browser window, as the grid classes behave unexpectedly by expanding and taking up the entire page width at ce ...

Tips for maintaining a single-line div while adding ellipses:

What is the CSS way to ensure that a div or class contains only one line of text, with ellipses added if it exceeds that limit? I am aware that setting a specific height and using overflow:hidden can achieve this, but it doesn't quite work for my need ...