Adjusting the size of SVG rectangle shapes within a group container

I'm struggling to resize an SVG in my website. It is located within the header of the site, and previously I was resizing a regular image like this:

$("#block_175 img").height($("#header").height() / 2);

Now that I switched to using an SVG, I have been experimenting with different methods to resize it without success. Here is how the SVG is structured:

<div id='block_175' class=''>
<svg width="100" height="100">
    <g id="cross_svg">
        <rect id="Rectangle-1"  x="0" y="0" width="48" height="2" fill="transparent"></rect>
        <rect id="Rectangle-2"  x="0" y="14" width="48" height="2" fill="transparent"></rect>
        <rect id="Rectangle-4"  x="0" y="28" width="48" height="2" fill="transparent"></rect>
    </g>    
</svg> 

To attempt to style the SVG, here is the CSS code:

svg 
{
    width: 52px;
    height: 52px;
    z-index: 99999;
    transition: all .3s ease;
    cursor: pointer;
}

svg g 
{
    transition: all .3s ease;
    width: 100px;
    height: 100px;
    display: block;
    position: absolute;
    left: 50%;
    top: 50%;
    margin: auto;
    cursor: pointer;
}

svg rect 
{
    transition: all .3s ease;
    fill: #ffffff;
}

svg g 
{
    width: 100px;
    height: 100px;
}

The JavaScript involved for interaction with the SVG:

svg.on('click', function()
        {
            // Code for SVG interaction goes here
        });

In summary, I am trying to resize the entire SVG to half the height of #header, but I have not found a solution yet.

Thank you.

EDIT 1: (thank you, Pavel Gatnar)

Updated CSS:

    svg 
{

        width: 100%;
        height: 100%;
        z-index: 99999;
        transition: all .3s ease;
        cursor: pointer;
    }

    svg g 
    {
        transition: all .3s ease;
        width: 100%;
        height: 100%;
        display: block;
        position: absolute;
        left: 50%;
        top: 50%;
        margin: auto;
        cursor: pointer;
    }

    svg rect 
    {
        transition: all .3s ease;
        fill: #ffffff;
    }

I have also attempted to resize the container div instead of the SVG itself, but the G element continues to overflow from the container div...

Answer №1

Ensure you include the 'viewbox' attribute and define it as follows:

 <svg width='100' height='100' viewBox='0 0 100 100'>...</svg>

This will allow you to treat your SVG like an image and adjust its size accordingly.

If the viewbox is missing, resizing the width and height of your SVG will not scale it down properly.

Take a look at this demonstration comparing an SVG with a viewbox to one without: http://jsfiddle.net/k14qd88j/

Answer №2

Adjust the size of the container div so that the svg element maintains 100% height and width.

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

What is the process for creating an index signature for a type alias representing a Map in Typescript?

Suppose I have a custom type for a Map as follows: type MyCustomMap = Map<string, number>; Is there any way to add an index signature to this type so that I can set key-value pairs after initializing it? I have been able to achieve this with types ...

The Save button is unresponsive when using jQuery-UI Connected Sortable Lists

When attempting to Test Drag&Drop element with connectedSortable from sortable1 on the left side and sortable2 on the right side, I encountered an issue where the array data results are not being sent to save in my database upon clicking the Save Butto ...

Reset the AJAX object using jQuery

Currently, my code looks like this: object1 = $.ajax({ .. .. }); If an error occurs, I would like to have the ability to restart the ajax request. For instance, if the user's connection is lost, I want to be able to easily call the same ajax again w ...

The fade-in effect will initiate from the start once the mouse leaves the element (with a

Currently, I am in search of a solution for improving the navigation menu I am developing. By clicking on this JSFiddle link, you can view the code in action. The issue I am facing is that the fadeIn effect should only occur when hovering over the nav-ite ...

Empty response returned by Next.js API Routes

I have attempted various methods to retrieve data from a database using MySQL. Here's what I've tried: export default function handler(req, res) { const mysql = require('mysql') const dbConn = mysql.createConnection({ ho ...

What is the best way to interact with a checkbox that has a label using Selenium in Node.js?

My HTML document contains multiple checkbox classes with different texts for each checkbox name. Here is a snippet of the HTML code: <div class="col-md-12 syllabus-div-1"> <h4 class="vertical-spacing">Coaching<i class="fa fa-graduation- ...

Store user input in a paragraph

I want to create a unique program that allows users to input text in a field, and when they click "Start", the text will appear in a paragraph backwards. I plan to use Html, jQuery, and CSS for this project. Can anyone provide guidance on how to achieve th ...

How to Position Icon on the Right Side of an Input Field using CSS

Is there a way to position the magnifying glass icon to the right side of my input field? Link to my Fiddle. @import url("//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"); body { margin: 30px; } .search { position: relative; ...

Discovering an Element in jQuery through its ID using Spaces and Variables

My issue involves locating an element within another element using an ID and then adding a class when the ID is hardcoded. For example: var tableId = el.id; $('#' + tableId).find("[id='Checkout On']").addClass('highlight'); ...

Ways to verify if one div in jQuery contains identical text to another div

I need help with a jQuery script to remove duplicate text in div elements Here's the requirement: If div1 contains the text "hi" and div2 also contains "hi", then div2 should be removed Below is my current code snippet: <script src="https:/ ...

I need to save the API response in object form from Angular so that I can use it later. Can someone suggest the best way to store this response efficiently?

Angular: I need to find a way to save the API response, which is in object format, for future use and to reduce additional API requests. Can someone suggest a convenient method to handle this? Snippet of My Code (Service File): getList(){ .... .... ...

Managing active dropdown menus in VueJS

I'm having trouble figuring out why my navigation menu and method to open subitems on click are not working correctly. [![dropdown_menu][1]][1] new Vue({ el: '#app', data: { //menu "menu_title": "a", "child_ro ...

The DIV with looping functionality is not maintaining the table structure from the previous view

I have a table with a lot of ordering details. However, when I try to add a product from the admin page, the original table structure works fine. But as soon as jQuery adds an extra DIV to expand the table, I encounter issues. Can anyone suggest a jQuery m ...

Is it possible to include aria-controls in an anchor tag?

My primary navigation includes some elements that may have a secondary navigation. In order to optimize accessibility on my website, I am seeking the most effective way to show/hide the secondary nav. After brainstorming, here is what I have come up with: ...

Guide on aligning text along a baseline

CLICK HERE TO ENTER THE INTERACTIVE PLAYGROUND HTML: <div class="first">Text A</div> <div class="second">Text B</div> CSS: div { background-color: rgba(255, 0, 0, 0.3); /* For visualization only */ margin-top: 50px; ...

Display an additional div when hovering over form input

I am currently in the process of designing a search bar animation. Specifically, I want the search history to appear when hovering over the search bar. However, I am facing an issue where the code doesn't seem to work when I use .search-bar-input:hove ...

Transition into animated borders using <hr> lines

While working on a basic static website using Bootstrap 5, I encountered an issue with the animations in the full-screen carousel. The Navbar, Title text, and subtext fade in smoothly as intended. However, I faced challenges trying to apply similar animati ...

Creating a new component in Qt using an already imported type

When trying to create a component of an imported type, such as MyObject, the workaround is currently to create a separate MyObjectComponent.qml file within the application. This process is important for creating components from types found in installed QML ...

Navigating the waters of managing setInterval within a Nuxt environment

Within my component, I have a timer that is initiated using setInterval in the mounted() hook. Let's say this component is located at http://localhost:3000/some-route. What is the best approach to call clearInterval() when navigating to another rou ...

How can I incorporate a dashed vertical line in between select bars on a bar chart using Echarts?

I am currently utilizing Echarts and have successfully developed a bar chart. My goal is to incorporate two vertical dashed lines to distinguish between Source3 and Source4, as well as another dashed line to indicate the separation of SourceSix and SourceS ...