Expanding the width of a single svg element within a block of three

Is there a way to stretch one of the three SVGs in a block without any space between them? The first and last SVG should always be 100px width, while the second SVG should have varying widths like 100px, 200px, or 1000px but must be adjacent to each other.

 HTML
 <div class="container">
  <svg viewBox="0 0 100 100">
    <rect class="st0" width="100px"  height="100px"/>
  </svg>
  <svg viewBox="0 0 500 100" preserveAspectRatio="none">
    <rect class="st1" width="500px"  height="100px"/>
  </svg>
  <svg viewBox="0 0 100 100">
    <rect class="st2" width="100px"  height="100px"/>
  </svg>
 </div>
CSS
 svg {
  width: 100px;
  height: 100px;
 }
 svg rect.st2 {
  fill: red;
 }
 svg rect.st1 {
  fill: green;
  width: 500px;
 }
 svg rect.st0 {
  fill: blue;
 }
 .container {
  display: flex;
 }

JSFIDDLE

Answer №1

To make the svg element grow within a flex container, simply include flex-grow:1; in its styling:

 svg {
  width: 100px;
  height: 100px;
 }
 svg rect.st2 {
  fill: red;
 }
 svg rect.st1 {
  fill: green;
 }
 svg rect.st0 {
  fill: blue;
 }
 .container {
  display: flex;
 }
 
 .container svg:nth-child(2) {flex-grow:1;}
<div class="container">
  <svg viewBox="0 0 100 100">
    <rect class="st0" width="100px"  height="100px"/>
  </svg>
  <svg viewBox="0 0 500 100" preserveAspectRatio="none">
    <rect class="st1" width="500px"  height="100px"/>
  </svg>
  <svg viewBox="0 0 100 100">
    <rect class="st2" width="100px"  height="100px"/>
  </svg>
 </div>

If you require specific widths of 100, 200, and 1000, then media queries should be used to set these widths based on conditions.

Answer №2

Here's an alternative solution for controlling individual SVG elements with CSS. It may or may not be helpful, but it's worth a try.

.container {
  display: flex;
  align-items:center;
}
svg {
  width: 100%;
  height: 100px;
}
svg:nth-child(2) {
  width: 1000px;
}
svg rect.st2 {
  fill: red;
}
svg rect.st1 {
  fill: green;
  width: 2000px;
}
svg rect.st0 {
  fill: blue;
}

Check out the JSFIDDLE demo here.

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

One CSS file linked, but only one is applied; the other is left unused

After including two CSS files, I am experiencing an issue where only one of them is being utilized. The other file does not seem to be included and the reason behind this remains unknown. My website has a standard header file that is present on all pages. ...

The header one tag does not wrap around content when the window size is adjusted; instead, it

I have been struggling to position my page title in the center of the header image both horizontally and vertically on my portfolio pages. I managed to get the h1 title where I want it but now I need to make it larger. The issue arises when I resize the wi ...

"HTML Parsing with Simple DOM: Dealing with Class Names Containing Spaces

I am currently utilizing PHP Simple HTML DOM to extract elements from the source code of a website (which is not my own). However, when attempting to locate a ul class named "board List", it is not being found. It seems like there may be an issue with spac ...

Retrieving the data-id attribute from dynamically rendered div elements

How can I modify my checkPair function to retrieve the data-id attribute when clicking on a dynamically created card? Currently, I am encountering an error stating "The name 'getAttribute' could not be found." function createHTMLElementFromHtml( ...

JavaScript Bug Report

Currently, I am immersed in a project that encompasses various languages like HTML, JS, and PHP. While working on one of the PHP functions, I stumbled upon an unexpected anomaly. To dissect it better, I decided to break it down into simpler functions: &l ...

PHP 5's HTML Form Library

Currently in search of an alternative to QuickForm. I have encountered performance problems with QF, specifically when dealing with a large number of options in combobox. I prefer something that is more object-oriented like Zend_Form, but without the exc ...

Error: The function replace cannot be used on elem

Here is what I am attempting to achieve: for loop inside the append is not working Below is my code: $('body').append( $('<section>').append( $('<form>') .attr('class', "ac-custom a ...

Stop the MatSort feature from activating when the space key is pressed in Angular Material

I am currently using angular material tables and have implemented filters for each column. The filter inputs are located in the row header of each column, which is working fine. However, I am facing an issue where whenever I type and press the space key, ...

The mask effect is not only darkening the background image but also affecting the color of my header content

Why is the mask I applied making not only my background image darker but also my h1 and button? <!-- Header - set the background image for the header in the line below--> <header class="bg-image d-flex justify-content-center al ...

Issues arise when HTML components are not functioning correctly upon calling JSON

I'm encountering a challenging issue that's difficult to articulate. Here is a link to a JSFiddle that demonstrates the problem: https://jsfiddle.net/z8L392ug/ enter code here The problem stems from my utilization of a news API for F1 stories. W ...

The PHP script was unable to load the CSS stylesheet from the specified base URL

I am having trouble loading the awesome.css file in my PHP file. The code I am using in my PHP file looks like this: <link rel="stylesheet" type="text/css" href="<?php echo base_url('resources/web2/lib/css/awesome.css'); ?>"/> Upon ...

Using jQuery to clear a textarea when a select input is changed

Currently, I am in the process of developing a small text editor that enables users to edit files and create new ones. Here is how I have set up my select menu. <select name="reportname" id="reportname" class="form-control"> <option value="zr ...

What is the best way to create a personalized image as the background in WordPress using CSS for a client?

I have this in my style.css .showcase{ background: url("x.jpg") no-repeat 0; } My website is built on WordPress and I have implemented advanced custom fields for the client to edit text. However, I am struggling to find a way for them to change the ...

Utilizing JavaScript to retrieve input names from arrays

This is the HTML form that I am currently working with: <form action="#" method="post"> <table> <tr> <td><label>Product:<label> <input type="text" /></td> <td><label>Price:<label> ...

Grabbing only the button element using jQuery when a click event occurs

HEY THERE! I have a simple question that I need help with. To better explain the issue, I've included a small code snippet below. The problem I'm facing is related to grabbing the id of a button. Everything works fine except when I click on any ...

Objects and strings cannot be inserted into a JavaScript array

For hours, I've been attempting to troubleshoot this code. Currently, it successfully finds the number of anchors, and I have an array that is of undetermined size. Within the for loop, it retrieves the anchor and extracts the .href. I've confirm ...

Video is not visible in safari browser initially, but becomes visible only after scrolling for a little while

Having issues with a video not displaying correctly in Safari? The problem is that the video only becomes visible after scrolling the browser window. Want to see an example of this issue in action? Click here and select the red bag. Check out the code sni ...

Chrome does not display the play button when the width of the audio tag is reduced in HTML 5

I'm looking to implement the HTML 5 audio tag that works on both Chrome and Firefox: <audio controls preload="metadata" style=" width:50px; border-radius: 100px;margin-top: 15px; float: left;"> <source src="https://s4.uuploa ...

Is there a way to display one of the divs in sequence each time the page is reloaded?

Is there a way to display the divs sequentially when the page is refreshed? For instance, on the initial load, only div 1 should appear, and with each refresh it should cycle through divs 2, 3, 4, and 5 before starting over. Below is an example I'm c ...

Update the loading animation to utilize a custom image instead

I found a preloader code on this website that rotates fontawesome icons and displays the percentage after a few seconds. However, I am looking to replace the fontawesome icon with an image but have been unsuccessful in my attempts. Is there a way to easil ...