gpt4 book ai didi

css - 如何在不给它(或其父级)明确高度的情况下防止内联 SVG 以任意高度显示?

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:14 24 4
gpt4 key购买 nike

下面是一个使用 Bootstrap 3 的简化布局。该行的中间列包含一个内联 SVG,它提供了从蓝色框到绿色框的排序过渡。

我为什么要这样做?使用浏览器 CSS 设计 SVG 样式很酷!此外,这样做意味着我不必为我可能应用此设计元素的每个可能的行高和颜色组合生成一堆 PNG。

虽然我可以使用 JS 根据最高的不包含 SVG 的 div 动态设置 col-* div 的高度,但似乎应该有一种方法可以使用 CSS 来做到这一点。

请注意,我已尝试为 .row 元素设置 display: flex(它是在样式表中注释掉)。虽然它确实确保列具有相同的高度,但它具有增加内容 div 而不是缩小样式 div 的效果。

Plunker包括在内,以防您像我一样发现它比 StackOverflow 片段更容易使用。

html, body.stackoverflow-snippet {
color: white;
font-size: 25px;
height: 100%;
}

.row {
/*
* Ultimately I would use the following rule to ensure the columns are equal
* height, since their content is dynamic, but the problem is less obvious
* when this rule is in effect.
*/
/* display: flex; */
}

.row.desired-result div {
/*
* To see the desired effect, set the height to any known value. In my case, I
* can't set the height of the divs explicitly the because user-generated
* content populates these divs and may change their height.
*/
height: 35px;
}

.row .rounded {
padding-left: 0;
}

.blue {
background: blue;
}

.green {
background: green;
}

svg {
height: 100%;
}

svg circle {
fill: blue;
}
<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body class="stackoverflow-snippet">
<div class="container-fluid">
<!-- first row: desired result -->
<div class="row desired-result">
<div class="blue col-xs-6">Desired result</div>
<div class="green rounded col-xs-1">
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" class="inline-svg inlined-svg" viewBox="90 20 10 60" role="img">
<circle r="50" cy="50" cx="50"></circle>
</svg>
</div>
<div class="green col-xs-5">(explicit height)</div>
</div>
<!-- second row: just a visual break to separate the rows of interest -->
<div class="row">
<div class="col-xs-12">&nbsp;</div>
</div>
<!-- third row: natural height -->
<div class="row">
<div class="blue col-xs-6">Ack!</div>
<div class="green rounded col-xs-1">
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" class="inline-svg inlined-svg" viewBox="90 20 10 60" role="img">
<circle r="50" cy="50" cx="50"></circle>
</svg>
</div>
<div class="green col-xs-5">(natural height)</div>
</div>
</div>
</body>

</html>

最佳答案

使 SVG position: absolute。然后将根据其父容器测量“100%”高度。只要你还记得将父单元格设置为position: relative,使其成为合适的包含 block 。

.svg-container {
position: relative;
}

.inline-svg {
position: absolute;
left: 0;
}

我们还向 SVG 单元格添加了一个不间断空间,以防止在您将 SVG 设为绝对时它折叠到零高度。

.svg-container::before {
content: '\a0'
}

结果:

html, body.stackoverflow-snippet {
color: white;
font-size: 25px;
height: 100%;
}

.row {
/*
* Ultimately I would use the following rule to ensure the columns are equal
* height, since their content is dynamic, but the problem is less obvious
* when this rule is in effect.
*/
/* display: flex; */
}

.row.desired-result div {
/*
* To see the desired effect, set the height to any known value. In my case, I
* can't set the height of the divs explicitly the because user-generated
* content populates these divs and may change their height.
*/
height: 35px;
}

.row .rounded {
padding-left: 0;
}

.blue {
background: blue;
}

.green {
background: green;
}

svg {
height: 100%;
}

svg circle {
fill: blue;
}

.svg-container {
position: relative;
}

.svg-container::before {
content: '\a0'
}

.inline-svg {
position: absolute;
left: 0;
}
<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body class="stackoverflow-snippet">
<div class="container-fluid">
<!-- first row: desired result -->
<div class="row desired-result">
<div class="blue col-xs-6">Desired result</div>
<div class="green rounded col-xs-1">
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" class="inline-svg inlined-svg" viewBox="90 20 10 60" role="img">
<circle r="50" cy="50" cx="50"></circle>
</svg>
</div>
<div class="green col-xs-5">(explicit height)</div>
</div>
<!-- second row: just a visual break to separate the rows of interest -->
<div class="row">
<div class="col-xs-12">&nbsp;</div>
</div>
<!-- third row: natural height -->
<div class="row">
<div class="blue col-xs-6">Ack!</div>
<div class="green rounded col-xs-1 svg-container">
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" class="inline-svg inlined-svg" viewBox="90 20 10 60" role="img">
<circle r="50" cy="50" cx="50"></circle>
</svg>
</div>
<div class="green col-xs-5 svg-container">
(natural height)</div>
</div>
</div>
</body>

</html>

关于css - 如何在不给它(或其父级)明确高度的情况下防止内联 SVG 以任意高度显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51755453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com