0

I want to assign height to parent div based on the child content (here SVG is my child which can have different content based on data). If I assign fixed height either to parent or the child, it gives issue when content changes. It works fine for the width (automatically alter div width based on content) but not for height. Here is my code snippet.

React.useEffect(() => {
    // calling legend function and passing div id to function
    colorLegend("#legend");
}, [dep]);

function colorLegend(legend: string) {
    // logic
    if (colorLegend) {
       select(legend)
       .append("svg")
       .attr("overflow","visible")
       .attr("width", 150 + "px")
       .call(colorLegend);
    }
}

return (
    <div style={{position: "absolute",right: 16,top: 
      10,backgroundColor: "black",borderRadius: "5px",padding: 
    "10px"}}>
        <label style={{ color: "#6F6F6F" }}>
          {name}
        </label>
        <div id="legend"></div>
    </div> 
);

Fiddle link: https://jsfiddle.net/1sv3Lwar/

1 Answer 1

0

I think the reason your parent <div> isn't "growing" vertically is because you are using:

position: "absolute"

Using absolute positioning or floats will cause this.

Although, assuming you need the absolute positioning, so adding: overflow: hidden should do the trick.

5
  • No use. overflow: hidden do not work in my case
    – Eric
    Commented Oct 21, 2021 at 13:58
  • possible to work up a codepen/ working example of your issue?
    – cts
    Commented Oct 21, 2021 at 13:59
  • It will be bit difficult to make it work in code pen but I can try sharing the image for better understanding. In the image you can see the background black is not covering whole content. I cannot assign fixed height as well to parent because content may vary.
    – Eric
    Commented Oct 21, 2021 at 19:20
  • Also you can see that I have added the overflow: visible to the SVG, if I do not add that then rest of the content will get hidden inside parent div and not shown fully. I want the background black color should cover all the content and its height should be until label "H4", sadly I cannot specify fixed height since content may vary.
    – Eric
    Commented Oct 21, 2021 at 19:57
  • Here is the fiddle link : jsfiddle.net/1sv3Lwar
    – Eric
    Commented Oct 24, 2021 at 20:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.