Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG in flexbox messes up height of other elements

Tags:

html

css

svg

I'm trying to use a svg element that resizes with the container size in a flexbox, but for some reason it messes up the size of a div (with text) below the svg. (How much changes when you resize the browser window)

example

Here are the basic css properties I'm using for all this:

[layout] {
    box-sizing: border-box;
    display: flex;
}

[layout=column] {
    flex-direction: column;
}

[layout=row] {
    flex-direction: row;
}

[flex] {
    box-sizing: border-box;
    flex: 1;
}

HTML:

<div class="content" style="height: 100%;" layout="row">

    <div class="card" layout="column" flex>
        <div class="toolbar" layout="column">
            <span>Test</span>
        </div>

        <div class="card-content" layout="column" flex>
            <div layout="column" flex>
                <div layout="column" flex>
                    <div layout="column" flex
                         style="background:green;">
                        <svg viewBox="0 0 50 50">
                            <circle r="25" cx="25" cy="25">
                        </svg>
                    </div>
                </div>
                <div>Text</div>
            </div>
        </div>
    </div>
</div>

Codepen: http://codepen.io/anon/pen/rVJepm

How can I fix the sizing when using a SVG?

like image 681
Staeff Avatar asked Jul 01 '15 10:07

Staeff


People also ask

Can you use flexbox with SVG?

Flexbox can organize non-SVG DOM elements: You can't use Flexbox to arrange elements within SVG. That is, if you have a bunch of rect elements within your SVG, Flexbox can't move them. But, if you have several, separate SVG elements (think small multiples), Flexbox can arrange them for you.

Can you set height of flexbox?

By default, a flex container has the following width and height assigned. width: auto; height: auto; But you can set a fixed height and width to the flex container.

How do I fix my flexbox size?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.


1 Answers

Your problem with flex-box arises in the usage of the flex shorthand property. You set it to: flex: 1 1 auto, which means: flex-grow: 1; flex-shrink: 1; flex-basis: auto;.

As you don't want the container having the content text to shrink. You only have to set flex-shrink: 0; to this node. Here is your forked demo: http://codepen.io/anon/pen/GJQqog

On a side note: In my opinion it's still illegal in html5 to make up new attributes. This is not the reason for the failure, but maybe it would be better to go for data-attributes.

like image 75
Nico O Avatar answered Oct 13 '22 05:10

Nico O