Adjusting the border size of an SVG without using the viewbox

Currently, I am working with 2 SVGs where one has a viewbox and the other doesn't. They both are set to be 100% width of the parent element. The first SVG scales according to its viewbox while the second one scales according to the height of the parent due to a hover height transition.

The issue I'm facing is that I want both SVGs to have the same stroke-width. I want the stroke to scale just like it does with the SVG that has a viewbox, but the second SVG's stroke always remains at 2px.

How can I maintain the functionality of scaling the height of the second SVG with its parent while also achieving the same border width as the first SVG?

body > div{
  width: 100px;
  height: 400px;
}
svg{
  width: 100%;
  stroke-width: 2px;
  stroke: black;
}
.svg-with-viewbox rect{
  fill: red;
}
div > div{
  height: 50px;
  width: 100%;
  transition: 1.5s all;
}
div > div:hover{
  height: 200px;
}
.scale-height{
  fill: yellow;
  height: 100%;
}
<div>
  <svg class="svg-with-viewbox" viewbox="0 0 20 20">
    <rect height="20" width="20"/>
  </svg>
  <div>
    <svg class="scale-height">
        <rect width="100%" height="100%">
          
        </rect>
    </svg>
  </div>
    
</div>

Answer №1

To maintain the same size around the transparent edges of an SVG part even when rescaled, you can utilize the drop-shadow() filter.

Check out this example:

body>div {
  width: 100px;
  height: 400px;
}

svg {
  width: 100%;
  filter: /* on on each sides */
      drop-shadow( 2px 0   black) 
      drop-shadow(-2px 0   black) 
      drop-shadow( 0   2px black) 
      drop-shadow( 0  -2px black);
  margin: 2px;/* increase margin or padding to include the drop-shadow */
}

.svg-with-viewbox rect {
  fill: red;
}

div>div {
  height: 50px;
  width: 100%;
  transition: 1.5s all;
}

div>div:hover {
  height: 200px;
}

.scale-height {
  fill: yellow;
  height: 100%;
}
<div>
  <svg class="svg-with-viewbox" viewbox="0 0 20 20">
    <rect height="20" width="20"/>
  </svg>
  <div>
    <svg class="scale-height">
        <rect width="100%" height="100%">

        </rect>
    </svg>
  </div>

</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

Troubleshooting Vue.js nested v-for with <tr> tag problem

Why does Vue complain about undefined properties when I try nesting a <tr> inside a <tr> with a v-for binding on each? <table> <thead></thead> <tbody> <tr v-for="item in items"> <td>{{ item.nam ...

Concealing and revealing template URLs with AngularJS

I am currently working with a dynamic Array in my Controller that contains templates (html files) structured similarly to the example below: $scope.templates = [ { name: 'template1.html', url: 'template1.html'}, { name: ...

"Modify the MySQL database each time a user changes the value in a

As a student, I am looking to update value(s) whenever a student changes the value(s) in the correction or update form. So far, I have been able to retrieve and display values in text boxes based on the name selected from a dropdown list from the database ...

Having trouble incorporating custom CSS into my Rails/Bootstrap project

I’m having trouble figuring out what I’m doing wrong. I’ve added custom files and tried to override the existing ones. application.css.scss @import 'bootstrap-sprockets'; @import 'bootstrap'; /* * This manifest file will be co ...

What is the process for adding a box shadow beneath my header?

I am currently attempting to add a box shadow under my header, similar to the design featured in the project mockup at https://github.com/RaghavMangrola/the-brighton-times. After trying to implement the following CSS property and value: .header { ...

I am looking to develop a customizable table where the user can input their desired information

Looking to create an HTML page featuring a 10x10 table with alternating red and green squares. After loading the page, a pop-up window will prompt the user to input a word, which will then appear only in the red squares of the table. While I've succes ...

Styling the navigation menu for mobile devices

How can we transition from a menu like this : https://i.sstatic.net/qQ2k1.png to this when the screen size reaches a specific width: https://i.sstatic.net/AUM4v.png In essence, replacing certain texts with icons. Is pre-defining it and changing the dis ...

The boundary extends fully to the right

Recently, I created a calculator to convert kilograms to pounds and encountered an issue when trying to add a border to a p element. The problem was that the border extended all the way to the right side of the page. Here is the corresponding HTML code: &l ...

Getting the most out of fonts with Webpack sass-loader

I recently set up a custom font in my project like this: $font-path: '~@/assets/fonts/'; @font-face { font-family: 'mainFont'; font-weight: 300; font-style: normal; src: url('#{$font-path}mainFont/mainFont.eot&apos ...

To generate a new <div> element by clicking a button and then clicking on it using C# Selenium

Before clicking the button, this is the initial HTML code for the page: <div id="layerContainer"> </div> After clicking the button, the code changes as shown in the picture here I have been attempting to locate the new button, but I keep rec ...

Using jQuery to remove all inline styles except for a specific one

Just a quick inquiry: Our Content Management System (CMS) utilizes CKEditor for clients to modify their websites. The front-end styles include the use of a pre tag, which we have customized to our desired appearance. However, when their team members copy a ...

Removing a Dom element using stage.removeChild( )

When the number 6 is typed and entered into the game, the function correct() in the code snippet below determines what action to take. I would like to remove the DOM element gg (the equation 3+3=input) from the stage after typing 6 and pressing enter. How ...

The text sliding feature gradually moves further away from the left side with each iteration

I am in the process of transferring an existing slider from one website to another. Instead of creating a text slider from scratch, I decided to modify the code I found for the new site. However, the slider is not functioning correctly on the new site. Th ...

Are none of the page links clickable?

Currently, I am in the process of creating a portfolio website. This is my first attempt at coding HTML and CSS from scratch without the aid of a layout template. I've encountered an issue that has me stumped - the links within the container are not ...

Replicating a row in a table without disrupting a button within the row

As a novice in the world of coding, I am currently embarking on a project to revamp an existing website. The task at hand involves creating a table with a built-in save/edit feature, which I have successfully implemented. However, I am facing a roadblock w ...

Transferring the values of JavaScript objects to HTML as strings

My goal is to generate HTML elements based on the values of specific JavaScript objects that are not global variables. However, when attempting to execute the code below, I encounter an error stating "params is not defined." What I actually aim to achieve ...

The right alignment with Flexbox's justify content property is not being implemented as expected

I'm having trouble aligning my footer using flexbox - no matter what I try, it won't move all the way to the right. When I use justify-content: center, the items are centered, but when I change it to justify-content: right, everything shifts bac ...

Incorporating CSS and JS files into a WordPress theme

To incorporate Css & Js files into my website pages, I plan to insert the following code into the functions.php file: function cssjsloading(){ wp_enqueue_style('bootstrap-rtl', get_template_directory_uri() . '/css/bootstrap-rtl.css&apo ...

Personalized form outlines

I'm designing a Valentine's Day theme for my website and I was wondering if it's possible to create custom form borders, like a heart shape, without having to hard-code everything. I could always insert an image where needed, but that seems ...

Encountering unexpected behavior with the .data() method

I'm encountering an issue with my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv= ...