gpt4 book ai didi

html - SVG 在加载时超大,直到 CSS 启动

转载 作者:行者123 更新时间:2023-11-28 13:55:34 24 4
gpt4 key购买 nike

我在固定大小的 div 中有一个 SVG(宽度:350px;高度:400px;),页面加载后一切正常,但在加载过程中,SVG 过大且损坏直到 CSS 开始:

screenshot

这是代码,首先是 HTML:

<div class="dashboard-tasks-completed">
<figure>
<svg width="100%" height="100%" viewBox="0 0 42 42" class="donut">
<circle class="donut-hole" cx="21" cy="21" r="15.91549430918954" fill="#fff"></circle>
<circle class="donut-ring" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#19222a" stroke-width="5"></circle>
<circle id="active" class="donut-segment" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#206996" stroke-width="5" stroke-dasharray="66.66666666666666 33.33333333333333" stroke-dashoffset="25"></circle>
<circle id="completed" class="donut-segment" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#B8E1FA" stroke-width="5" stroke-dasharray="33.33333333333333 66.66666666666666" stroke-dashoffset="58.33333333333334"></circle>
<g class="chart-text">
<text x="50%" y="50%" class="chart-number">3</text>
<text x="50%" y="50%" class="chart-label"> Tasks </text>
</g>
</svg>
<figcaption class="figure-key">
<ul class="figure-key-list" aria-hidden="true" role="presentation">
<li>
<span class="shape-circle shape-blue"></span>2 Active (66.67%)
</li>
<li>
<span class="shape-circle shape-lightblue"></span>1 Completed (33.33%)
</li>
</ul>
</figcaption>
</figure>
</div>

CSS:

.dashboard-tasks-completed {
width: 350px;
height: 400px;
}

.chart-text {
font: 16px/1.4em "Montserrat", Arial, sans-serif;
fill: #000;
transform: translateY(0.25em);
}

.chart-number {
font-size: 0.6em;
line-height: 1;
text-anchor: middle;
transform: translateY(-0.25em);
}

.chart-label {
font-size: 0.2em;
font-weight: bold;
text-transform: uppercase;
text-anchor: middle;
transform: translateY(0.7em);
}

.figure-key-list {
list-style: none;
}

.figure-key-list li {
margin: 0 0 8px;
font-size: 14px;
}

.shape-circle {
display: inline-block;
vertical-align: middle;
width: 32px;
height: 32px;
border-radius: 50%;
margin-right: 10px;
}

.shape-blue { background-color: #206996; }

.shape-lightblue { background-color: #B8E1FA }

我已经创建了一个最小的例子,不幸的是它在 JSFiddle 或 Codepen 中都不可重现,但这里是 demo无论如何。

我该如何解决这个问题?

最佳答案

正如其他人所说,您让 SVG 填充页面直到 CSS 加载并且它变成正确的大小。

  • 如果您知道您的 SVG 将是 width: 350px; height: 400px; ,那么您可以改为在 SVG 上设置这些尺寸。

    <svg width="350px" height="400px" ...>

    然后在CSS中把它放回你想要的。

    .dashboard-tasks-completed svg {
    width: 100%;
    height: 100%;
    }
  • 或者您可以在您的文档中内嵌一些基本样式,这将设置一些重要的尺寸,直到加载其余的 SVG。

    <head>
    <style>
    .dashboard-tasks-completed {
    width: 350px;
    height: 400px;
    }
    .chart-text {
    font: 16px/1.4em "Montserrat", Arial, sans-serif;
    fill: #000;
    transform: translateY(0.25em);
    }
    </style>
    ...
  • 如果你的字体大小永远不会改变,那为什么不在你的 <text> 上设置这些属性呢?元素而不是使用 CSS?

    <g class="chart-text" style="font: 16px/1.4em "Montserrat", Arial, sans-serif; fill: #000; transform: translateY(0.25em);">

关于html - SVG 在加载时超大,直到 CSS 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58633404/

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