The sizing of the SVG element does not dynamically adjust when the window is resized

There appears to be a peculiar issue with the sizing of the rect inside the svg element when the page initially loads. Even when resizing the window, which should resize the svg as well, the width of the rect remains unchanged. However, toggling off and on the height or width in the element inspector does update the width of the rect. It's worth noting that this issue is specific to Chrome and Firefox, as it does not occur in Safari.

I'm wondering if there might be a better approach in terms of HTML and CSS to achieve the desired effect. Essentially, I am aiming for a dashed stroke around the box. Unfortunately, using a dashed border is not an option due to the dashes being too narrow.

.box {
  background: black;
  min-height: 300px;
  padding: 65px;
  border-radius: 20px;
  position: relative;
  max-width: 700px;
  margin: auto;
}

svg {
  width: calc(100% - 46px);
  height: calc(100% - 46px);
  fill: none;
  stroke: white;
  stroke-dasharray: 8px;
  position: absolute;
  top: 23px;
  left: 23px;
  pointer-events: none;
}

svg rect {
  width: calc(100% - 2px);
  height: calc(100% - 2px);
}
<div class="box">
  <svg xmlns='http://www.w3.org/2000/svg'>
    <rect x='1' y='1' rx='5' />
  </svg>
</div>

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

Answer №1

To ensure the inner <rect> of the svg is responsive, you can apply the border-radius and padding properties using relative units.

body {
}

*{
   box-sizing:border-box
}

.resize {
  resize: both;
  overflow: auto;
  padding: 1em;
  border: 1px solid red;
  width: 100%;
}

.box{
  width: 100%;
  height:100%;
}

svg {
  display: block;
  width: 100%;
  height:100%;
  padding: 23px;
  background: #000;
  border-radius: 20px;
  overflow:visible;
}

rect {
  stroke: white;
  stroke-dasharray: 8px;
  stroke-width:1px;
}
<div class="resize">
  <div class="box">
    <svg xmlns='http://www.w3.org/2000/svg'>
      <rect x='0' y='0' rx='5' width="100%" height="100%" />
    </svg>
  </div>
</div>
<p>Resize me</p>

It's crucial to include the overflow:visible property to prevent stroke clipping and set box-sizing: border-box to avoid padding overflows.

This method eliminates the need for using calc() for width/height values, which can sometimes lead to issues in certain browsers when applied to svg elements.

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

A tutorial on implementing a "Back to Top" button that appears dynamically while scrolling in Angular

I've developed a scroll-to-top feature for my Angular project, but I'm facing an issue. The scroll icon appears immediately upon page load instead of only showing after the user scrolls down. Any ideas or suggestions on how to achieve this? Here ...

Preventing a webpage's CSS from affecting an iframe loading an HTML document

Whenever I load an iframe, it seems to change a bit. Do I need to apply some kind of reset or adjustment? How can I ensure that the content within the iframe looks consistent no matter what page it's loaded into? ...

Issues with the animation of letters bouncing in css3

I wanted to implement an animated effect in CSS, specifically the "jumping letters" effect. However, it seems that the current implementation is not working as intended. The entire word moves upward instead of each letter bouncing individually. Can you h ...

A guide on retrieving information from a database with PHP and transferring it to Javascript

As I work on creating a website using HTML, CSS, MySQL, and JavaScript, my goal is to allow users to log in and engage in a quiz with 40 questions. Below is the JavaScript code for a countdown timer that includes the "questions" variable. After 40 seconds ...

Utilize XML and Javascript to create a captivating donut chart

I am currently experimenting with XML and JavaScript to generate a donut chart. I attempted to implement this JS code. However, I am struggling to create XML that is compatible with the JS. var SCIENCE = 0; var ART = 0; var MATH = 0; $(document).ready ...

The dimensions of the image are not conforming to the div's size

I am experiencing an issue with a div that contains an image. The image is not respecting the size of the div and is overlapping it. .bs-col-md-6 { box-sizing: border-box; -ms-flex-positive: 0; flex-grow: 0; 0; */ -ms-flex-negativ ...

Using canvas transformation changes the way drawImage is applied

I have been working on a game as a hobby and you can find it at . I have managed to get most aspects of the game working well, such as transformation, selection, movement, and objects. However, there is one particular challenge that I am struggling with. ...

What is the best way to access a local variable within a POST method in Node.js?

Below are some of the modules that I have utilized: const mysql = require('mysql'); const express = require('express'); const session = require('express-session'); const path = require('path'); const bodyParser = req ...

The formatting of the list is not being correctly displayed in the Ajax Jquery list

Trying to dynamically generate an unordered list from XML onto a jQuery mobile page has been a bit of a challenge for me. While I can display the items on the page, the styling never seems to work properly, only showing up as plain text with blue links. Is ...

Retain the contents of the shopping cart even when the page is refreshed

For a course project, I am recreating a grocery store website and need assistance on how to retain the shopping cart values even after refreshing the webpage. Please inform me if more information is required... <button type="button" id= ...

I am looking to save the session value into the search box of the appTables application by utilizing jQuery

Is there a way to save the session value in the search box of appTables by using jQuery? <?php $session = \Config\Services::session(); ?> <script type="text/javascript"> $(document).ready(function () { var searc ...

Tips for customizing CSS hover effects using HTML and jQuery scripts

Just a heads up before I ask my question - the code I'm about to share is sourced from this video on YouTube, so all credit goes to the creator. JS $(".product-colors span").click(function(){ $(".product-colors span"). ...

Is there a glitch with the External Style Sheet in the CodeIgniter PHP framework?

I inserted the following code into my search.php view page, which had an external style sheet. To include the style sheet in this file, I used: <head> <link rel="stylesheet" type="text/css" href="mystyle.css" /> </head> However, it doe ...

Squarespace | Tips for Completely Removing the Footer in the Pacific Template

I must say, the footer on this website is incredibly stubborn. Let's take a look at subject A: URL: Hello there! I'm currently working on a website and I'm facing a challenge with the background not stretching to fit the entire window when ...

"Clicking on the dropdown menu will consistently result in it collapsing

When it comes to a DropDown menu, the typical expectation is that when an option is selected, the menu will collapse. However, in my situation, I do not want the dropdown menu to collapse if the user is attempting to log in and clicks on the Username and P ...

Using setTimeout to click and hold on an element in Javascript

I am in need of adding a feature to my web app where a specific action is triggered when the user clicks and holds on an element, similar to the long press on Android. Here is the HTML code for my div: <div id="myDiv" onmousedown="press()" onmouse ...

At 400 pixels screen size, the buttons in the appBar component are spilling out

In my appBar component, I have three buttons that appear fine on large screens. However, when the screen size reaches 400px, they start stretching out of the appBar. Here is how they look on large screens: https://i.sstatic.net/l3nJM.png And this is how ...

Is it possible to use jQuery/JS to automatically format currency input as it is typed, adding a 1000 separator and ensuring

I'm working on a text input field that needs to format the value as it is being typed, with restrictions of 2 decimal places and 1000 separators. This field should only allow for digits to be entered. Specifically, this input is meant for users to ent ...

Having issue with jQuery popup not functioning correctly on the right side with the contact form. Is there anyone who knows how

Looking for a side slide popup that only accepts paragraph content? Want to add a contact form to it? Check out this fiddle - link here For a working example, visit - $(function() { // Slide from right to left $('#test2').PopupLayer({ ...

Is there a way to incorporate two Bootstrap navbars on a single page that are of varying heights?

Utilizing Bootstrap 3.0 with dual navbars on a single page that adjust in height using the same variable for both .navbar blocks. Despite attempting to incorporate an additional class to alter the height of the initial navbar, it remains unaffected. Check ...