Is there a way to stop my div from overflowing when I dynamically add an svg to it?

I have a grid that looks similar to the image provided https://i.sstatic.net/mvAYp.png

Everything up to this point is working fine. Using d3.js, I am dynamically adding an svg, but I want to position it inside .container2 within #mysvg (I have outlined the area with a red border to indicate where I want the svg to go). However, when I do this, the .container2 expands in size and I want to prevent that. Strangely, the svg itself fits perfectly within the available space.

How can I resolve this issue? Is there a way to correct it?

let width = document.getElementById('mysvg').offsetWidth;
let height = document.getElementById('mysvg').offsetHeight;
let createSvg=d3.select("#mysvg").append("svg").attr("width", width).attr("height", height)
html,body{
 margin:0px;
 padding:0px;
 height:100%;
 width:100%;
 border:1px solid red;
}

*{
  box-sizing: border-box;  
}

svg{
 border:1px solid green;
}

h6{
  margin:0px;
  padding:0px;
}

.container1,.container2,.container3{
  border:1px solid black;
}

.container_flexbox{
  height:100%;
  display:flex;
  flex-direction:column;
}

.container_graph{
  flex-grow:1;
}

#mysvg{
 border:1px solid red;
 height:100%;
}

.container_grid{
  display: grid !important;
  grid-template-areas:
    "container1 container2"
    "container1 container3";
  grid-template-columns: 1fr 1fr;
  grid-gap: 10px;
  background-color: white;
  padding: 10px;
  height:100%;
}

.container1 {
  grid-area: container1;
}

.container2 {
  grid-area: container2;
}

.container3 {
  grid-area: container3;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div class="container_grid">
    <div class="container1">container 1</div>
    <div class="container2">
        <div class="container_flexbox">
            <div>
                <h6>title of container 2 </h6>
            </div>
            <div class="container_graph">
                <div id="mysvg">
                </div>
            </div>
        </div>
    </div>
    <div class="container3">container 3</div>
</div>

Answer №1

To prevent the auto sizing issue you are experiencing, make sure to explicitly set a height for your rows just like you did for your columns:

let width = document.getElementById('mysvg').offsetWidth;
let height = document.getElementById('mysvg').offsetHeight;
let createSvg=d3.select("#mysvg").append("svg").attr("width", width).attr("height", height)
html,body{
 margin:0px;
 padding:0px;
 height:100%;
 width:100%;
 border:1px solid red;
}

*{
  box-sizing: border-box;  
}

svg{
 border:1px solid green;
}

h6{
  margin:0px;
  padding:0px;
}

.container1,.container2,.container3{
  border:1px solid black;
}

.container_flexbox{
  height:100%;
  display:flex;
  flex-direction:column;
}

.container_graph{
  flex-grow:1;
}

#mysvg{
 border:1px solid red;
 height:100%;
}

.container_grid{
  display: grid !important;
  grid-template-areas:
    "container1 container2"
    "container1 container3";
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-gap: 10px;
  background-color: white;
  padding: 10px;
  height:100%;
}

.container1 {
  grid-area: container1;
}

.container2 {
  grid-area: container2;
}

.container3 {
  grid-area: container3;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div class="container_grid">
    <div class="container1">container 1</div>
    <div class="container2">
        <div class="container_flexbox">
            <div>
                <h6>title of container 2 </h6>
            </div>
            <div class="container_graph">
                <div id="mysvg">
                </div>
            </div>
        </div>
    </div>
    <div class="container3">container 3</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

Struggles with organizing tables in asp.net

My apologies for my lack of knowledge in this area, but I am struggling to make my table look correct on my webform. I begin by creating a table: <table> </table> And I want to add a border. The traditional way is to use the 'border&apo ...

Unusual dark streak beneath the Wordpress emblem

I recently created my website using Wordpress and decided to use a PNG image (140*82 px) as the logo instead of the default "site-title". However, I encountered an unusual issue where a black line appeared just below the image. After checking my "header.ph ...

What could be causing my Time Picker MUI to malfunction in my React project?

I am currently working on a React and Rails project where I am attempting to style my React components. However, I have encountered an issue with the MUI component Time and Date picker. Whenever I try to implement the time picker, I get a strange error reg ...

Showing pdf documents without relying on third-party software

Within my Angular application, I have integrated the following snippet into an HTML template: <embed src="../assets/AOK_T2DM.pdf" style="width: 100%;height: 500px" type="application/pdf"> The representation of this code ...

What methods can be utilized to ensure that my wistia video player/playlist is responsive on different devices

I need some assistance with making my Wistia player responsive while using a playlist. I want the video player to adjust its size based on the screen size. Here is an example: My current code utilizes their iframe implementation: <div id="wistia_1n649 ...

Cause an element to extend beyond others when the viewport reaches its limit

My design dilemma involves two squares: a blue one that will expand to contain things totaling 800px, and a red one that must always remain fully visible. When narrowing the viewport, the red square should overlap the blue square, rather than disappearing ...

Apply a specific class only when the user scrolls within the range of 200px to 300px

Is it possible to dynamically add a class to a div element based on the user's scrolling behavior? For example, I would like to add a class when the user scrolls 200px down the page, and then remove it when they scroll 300px down. Similarly, I want to ...

Issue with Jquery .scroll method not triggering display:none functionality

Styling Using CSS #acc-close-all, #to-top { position: relative; left: 951px; width: 29px; height: 42px; margin-bottom: 2px; display:none; } #acc-close-all a, #to-top a { position: absolute; float: right; display: block ...

What are the steps to implement a horizontal scroll feature on a website when the scroll width is set to 0?

My goal is to enable users to scroll in the web view similar to how they can drag to scroll on mobile devices Check out my sandbox here for reference I have created a sample sandbox with the following code: <!DOCTYPE html> <html lang="en&qu ...

CSS resetting can lead to tables appearing misaligned

After applying a CSS reset to my website's stylesheet (which is valid as CSS 2.0 and used with XHTML 1.0 transitional webpage), I noticed that a table on my website no longer looks correct. Specifically, the table is now positioned incorrectly and the ...

Adhering button for sliding side panel

Check out my JSFiddle HERE to see what I have done. I would really appreciate it if someone could help me figure out how to make the show button float with the sidr panel :) <style type="text/css"> #panel { position: fixed; top: 50%; r ...

Is it possible to merge elements into a carousel in the mobile display?

I have a collection of elements that I would like to combine into a swipeable carousel. Is there a way to achieve this, specifically to make it a carousel only in mobile view? If needed, here is the HTML code. Thank you for your assistance. <div class= ...

jQuery parent() Function Explained

checkout this code snippet - https://jsfiddle.net/johndoe1994/xtu09zz9/ Let me explain the functionality of the code The code contains two containers: .first and .second. The .first container has two default divs with a class of .item. The .second contai ...

What is the best way to showcase a ul element as inline-block?

Is there a way to keep the bottom menu aligned in one row on all screen sizes? I attempted using display: inline-block, but it doesn't seem to work for me. Below is the CSS code: footer #middle-footer { background: #F6F6F6; color: #000; font-size ...

Using CSS to position an element relative/absolute within text inline

Need help aligning caret icons next to dynamically populated text in a navbar menu with dropdown tabs at any viewport size. Referring to positioning similar to the green carets shown here: https://i.stack.imgur.com/4XM7x.png Check out the code snippet bel ...

The bottom section of the main page

I've successfully implemented a footer that sticks to the bottom of each page as intended. However, I'm facing an issue when there's a large amount of text on a particular page that requires scrolling. The scroll continues beyond the footer, ...

Unusual HTML Structure (content misplaced or out of order?)

Recently, I started learning CSS/HTML in school and we just delved into Javascript. Currently, we are working on a website project. However, while trying to integrate the content with the navbar, I encountered a strange issue. When resizing to 620px or le ...

Tips for creating a function to assign a CSS width using a JavaScript variable?

Here is the code snippet I have been working on: ...<script> function adjustElementSize() { var newSize = window.innerWidth - 600; document.getElementById("spin").style.width = newSize + "px"; document.getElementById("spin").style.height = newSize + ...

In Bootstrap 5, the Col elements do not behave as inline elements

I have been attempting to create a column with 3 elements in a row: 2 buttons and 1 select dropdown Initially, I was able to achieve this layout with three buttons. However, when I replaced one button with a select dropdown, it stopped functioning properl ...

Arrange a variety of images with different dimensions in a narrow row while keeping the width fixed and adjusting the height accordingly

Imagine you have a collection of 3 (or more) images, each with different sizes and aspect ratios, within a fixed-width div (for example width:100%): <div class="image-row"> <img src="1.png"> <img src="2.png"> <img src="3.png" ...