Is there a way to prevent an inline SVG from appearing at a random height without specifying its height or the height of its parent element?

Displayed here is a simplified Bootstrap 3 layout. The middle column in the row features an inline SVG that transitions from a blue box to a green box.

I've opted for styling SVG with browser CSS because I find it interesting! Additionally, this approach saves me from creating multiple PNGs for various row heights and color combinations where this design element might be applied.

Although I could use JavaScript to dynamically adjust the height of the col-* divs based on the tallest non-SVG-containing one, I believe there should be a CSS solution to achieve the same result.

It's worth noting that I experimented with setting display: flex for the .row element (currently commented out in the stylesheet). While this does ensure equal column heights, it expands content divs instead of shrinking the styled one.

In case you prefer working with Plunker over a StackOverflow snippet like I do, I have included a link to this Plunker.

HTML, body.stackoverflow-snippet {
  color: white;
  font-size: 25px;
  height: 100%;
}

.row {
  /*
   * Here, the following rule would help ensure equal column
   * heights, but the issue becomes less apparent with dynamic content.
   */
  /* display: flex; */
}

.row.desired-result div {
  /*
   * Set any known value for height to see the desired effect. In my case,
   * I can't explicitly set the height of these divs since user-generated
   * content fills them and may change their height.
   */
  height: 35px;
}

.row .rounded {
  padding-left: 0;
}

.blue {
  background: blue;
}

.green {
  background: green;
}

svg {
  height: 100%;
}

svg circle {
  fill: blue;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body class="stackoverflow-snippet">
  <div class="container-fluid">
    <!-- first row: desired result -->
    <div class="row desired-result">
      <div class="blue col-xs-6">Desired result</div>
      <div class="green rounded col-xs-1">
        <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" class="inline-svg inlined-svg" viewBox="90 20 10 60" role="img">
          <circle r="50" cy="50" cx="50"></circle>
        </svg>
      </div>
      <div class="green col-xs-5">(explicit height)</div>
    </div>
    <!-- second row: just a visual break to separate the rows of interest -->
    <div class="row">
      <div class="col-xs-12">&nbsp;</div>
    </div>
    <!-- third row: natural height -->
    <div class="row">
      <div class="blue col-xs-6">Ack!</div>
      <div class="green rounded col-xs-1">
        <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" class="inline-svg inlined-svg" viewBox="90 20 10 60" role="img">
          <circle r="50" cy="50" cx="50"></circle>
        </svg>
      </div>
      <div class="green col-xs-5">(natural height)</div>
    </div>
  </div>
</body>

</html>

Answer №1

To ensure that the SVG is correctly positioned, set its position property to absolute. This way, the height defined as "100%" will be relative to its parent container. Don't forget to also set the parent cell's position property to relative to create a suitable containing block.

.svg-container {
  position: relative;
}

.inline-svg {
  position: absolute;
  left: 0;
}

To prevent the SVG cell from collapsing to zero height when using absolute positioning, add a non-breaking space within the SVG container.

.svg-container::before {
  content: '\a0'
}

The final result:

html, body.stackoverflow-snippet {
  color: white;
  font-size: 25px;
  height: 100%;
}

.row {
  /*
   * In this case, I would normally use display:flex; to ensure equal heights
   * of columns with dynamic content. However, the issue is less noticeable
   * when this rule is not applied.
   */
  /* display: flex; */
}

.row.desired-result div {
  /*
   * To see the desired effect, specify a fixed height value. In my scenario,
   * setting explicit heights for divs is challenging because user-generated
   * content dictates their height.
   */
  height: 35px;
}

.row .rounded {
  padding-left: 0;
}

.blue {
  background: blue;
}

.green {
  background: green;
}

svg {
  height: 100%;
}

svg circle {
  fill: blue;
}

.svg-container {
  position: relative;
}

.svg-container::before {
  content: '\a0'
}

.inline-svg {
  position: absolute;
  left: 0;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body class="stackoverflow-snippet">
  <div class="container-fluid">
    <!-- first row: desired result -->
    <div class="row desired-result">
      <div class="blue col-xs-6">Desired result</div>
      <div class="green rounded col-xs-1">
        <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" class="inline-svg inlined-svg" viewBox="90 20 10 60" role="img">
          <circle r="50" cy="50" cx="50"></circle>
        </svg>
      </div>
      <div class="green col-xs-5">(explicit height)</div>
    </div>
    <!-- second row: just a visual break to separate the rows of interest -->
    <div class="row">
      <div class="col-xs-12">&nbsp;</div>
    </div>
    <!-- third row: natural height -->
    <div class="row">
      <div class="blue col-xs-6">Ack!</div>
      <div class="green rounded col-xs-1 svg-container">
        <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" class="inline-svg inlined-svg" viewBox="90 20 10 60" role="img">
          <circle r="50" cy="50" cx="50"></circle>
        </svg>
      </div>
      <div class="green col-xs-5 svg-container">
        (natural height)</div>
    </div>
  </div>
</body>

</html>

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

How can I adjust the vertical position of Material-UI Popper element using the popper.js library?

https://i.stack.imgur.com/ZUYa4.png Utilizing a material-ui (v 4.9.5) Popper for a pop-out menu similar to the one shown above has been my recent project. The anchorElement is set as the chosen ListItem on the left side. My goal is to have the Popper alig ...

I'm unable to adjust the font size of the final h3 element

I'm currently designing a webpage with various titles, but I've encountered an issue. I am unable to adjust the font size of the last h3 title without affecting the rest of the titles. I'm curious as to why this is happening and how I can re ...

The moving div suddenly halts before continuing its journey

Two overlapping divs create an interesting sliding effect. A gray div slides in from the top over a black div. Hovering your mouse over the black div causes the gray one to slide out of view. If you hover over the gray div, it also slides out but pauses ...

Creating an HTML element that can zoom, using dimensions specified in percentages but appearing as if they were specified in pixels

This question may seem simple, but I have been searching for an answer and haven't found one yet. Imagine we have an HTML element with dimensions specified in pixels: <div style="width:750px; height: 250px"></div> We can easily resize i ...

Toggle visibility of div content when hovering over a link by leveraging the data attribute, with the div initially visible

I have a collection of links: <p><a href="#" class="floorplan initial" data-id="king"><strong>King</strong></a><br> 4 Bedrooms x 2.5 Bathrooms</p> <p><a href="#" class="floorplan" data-id="wood">< ...

Comparing hardware-accelerated CSS3 animations, transitions, and jQuery animate for mobile devices

While developing my app using PhoneGap and jQuery, I encountered a dilemma regarding animations. Initially, I relied on what I knew best - jQuery animate - to create smooth movements. However, discussions about hardware acceleration caught my attention. ...

Hiding the author, category, and date information from displaying on WordPress posts

After creating a Wordpress blog site and adding the category widget to the right sidebar as a drop down, I realized that I needed to customize its layout in terms of colors and design. However, I am struggling to find out how it can be done, which .php fil ...

AngularJS: ng-show causing flickering issue upon page refresh

Recently, I encountered an issue with my code snippet: <body> <ng-view></ng-view> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script> <script src="http://ajax.googleapis.com/ajax/ ...

The CSS file is not appearing, causing the styling not to be applied

I've included the following line in my html file: <link rel="stylesheet" href="css/styles.css"> Despite having the css file linked, the styling is not visible on the page. The HTML remains plain. The structure of my files is as follows: file ...

The scroll function malfunctions when attempting to center-align content on the screen with the use of transform

Is there a way to center a div on the screen without encountering scrolling issues when the screen size is reduced? If you have any alternative solutions (excluding the use of transform), please share, as I've tried various approaches but have not be ...

What is causing the element to disappear in this basic Angular Material Sidenav component when using css border-radius? Check out the demo to see the issue in action

I have a question regarding the Angular Material Sidenav component. I noticed that in the code below, when I increase the border-radius property to a certain value, the element seems to disappear. <mat-drawer-container class="example-container" ...

Issue with MUI Grid item not functioning properly when using overflowY: "auto"

I am encountering an issue while using MUI with React. I have a <Paper> element wrapping a <Grid> with 3 children elements. The problem arises when I set the overflowY property of the bottom grid item to "auto" - instead of showing the scroll b ...

Strip the CSS styling from an image (the CSS being included in the generated code)

I need to remove the CSS styling from an image, but I can't modify the generated code where it's coming from. My actual code looks like this: <div class="bubble"> <img id="one" src="/static/webupload/MyronArtifacts/images/descarga.png ...

Guide on adding animation to an image in Bootstrap when hovering with the mouse

Is there a way to add animation to an image on my website when hovering over it without using CSS? Are there any specific bootstrap classes that allow for direct animations on elements? ...

Adjust the height to match the shortest sibling child div height

In my layout, I have multiple rows with each row containing multiple columns. Within each column, there is a div and a paragraph - the div contains an image. My goal is to set the div with the class cover-bg to the lowest height of cover-bg in the same row ...

Tips for making Google search results include query strings in the returned links

I need help figuring out how to make Google search results show a URL containing a query string. Here's an example from the project I am currently working on: Instead of this link, Google search returns: If anyone has any suggestions for fixing this ...

Explore the interactive feature of hovering over a component within a div that is enhanced with a transformative translate3d effect upon hover. Discover the seamless event queuing

Within a div, there is a modal component that includes a transform: translate3d effect with hover transition. The div is created using vue.js list rendering method. Upon opening the modal and hovering over it, it jerks between the parent div and its intend ...

The Android WebView display is consistently showing a blank white screen, failing to reflect CSS or HTML modifications, and exhibiting choppy animations

There seems to be a strange inconsistency when I make changes to CSS classes. Sometimes, after adding a down class name to a button using a touch event, the changes do not appear on the button or anywhere else on the page. It's frustrating how unpredi ...

Vertical and floating alignment

Can you help me with this HTML/CSS challenge? User: Support: Emily Johnson Alex Roberts I am looking to have two input boxes aligned vertically on both the left and right ...

The footer section of my website seems to have gone missing in the HTML/CSS coding

I'm having trouble with my website footer not displaying. I've tried using "position: absolute; top: 50px; left:100px;" in my HTML/CSS, but it's not working. Can anyone help me fix this issue? Here is a link to the code I'm working on: ...