如何使SVG带有一个<path>的标签,使一条水平线填满SVG的父容器的整个宽度.

https://codesandbox.io/s/charming-goldstine-wh7td4?file=/src/index.tsx

    .container {
      width: 400px;
      height: 100px;
      background: rgba(0, 0, 255, 0.2);
      position: relative;
    }

    svg {
      width: 100%;
      height: 100%;
      stroke: black;
      stroke-width: 20px;
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
    }
    svg path {
      width: 100%;
      height: 100%;
    }
          <h4>
            Line should fill 100% width of the container of the svg. (the svg should be absolute
            positioned)
          </h4>
          <div class="container">
            <svg viewBox="0 0 1000 1" xmlns="http://www.w3.org/2000/svg">
              <path
                d="M 0,0 H 100%"
                // d="M 0, 5 L 100, 5"
              />
            </svg>
          </div>

推荐答案

可以通过修改d="M 0,0 H 100%"%将其设置为d="M 0,0 H 100%"%.

例如,这里我想要为90%,所以我使用:d="M 0,0 H 900%"

附注:百分比与viewbox中的第三个参数相关:viewBox="0 0 1000 1"

.container {
      width: 400px;
      height: 100px;
      background: rgba(0, 0, 255, 0.2);
      position: relative;
    }

    svg {
      width: 100%;
      height: 100%;
      stroke: black;
      stroke-width: 20px;
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
    }
    svg path {
      width: 100%;
      height: 100%;
    }
<h4>
            Line should fill 100% width of the container of the svg. (the svg should be absolute
            positioned)
          </h4>
          <div class="container">
            <svg viewBox="0 0 1000 1" xmlns="http://www.w3.org/2000/svg">
              <path
                d="M 0,0 H 900%"
                // d="M 0, 5 L 100, 5"
              />
            </svg>
          </div>

Html相关问答推荐

我需要两个HTML标签,以便当另一个隐藏时,一个可见,反之亦然

一次悬停可触发同一分区中的另一次悬停

在一行中对齐文本和图标

根据按钮位置左/右对齐按钮

SVG动画只有在打开S开发工具的浏览器时才开始播放

带有图像的虚线边框

在 html 和 css 中绘制表格内的倾斜线

按钮位于主 div 的底部而不是内部 Div

如何在Rmarkdown中添加千位分隔符?

在HTML使用link标签导入外部CSS时出现元素link不允许使用尾斜杠错误

样式化单选按钮 背景 colored颜色

如何使用html css将图像显示为附加形状

如何使容器的大小适合其 position:absolute; 子元素的大小

在 CSS 中的 div 中获取文本以环绕其他文本?

Angular:从服务器下载 HTML 并打印

如何将两个平行 div 的页面内的表格居中?

如何让一个 div 元素粘在另一个元素上?

我如何使用 CSS nth-child(odd) nth-child(even) 来对齐 CSS 网格中的列?

如何制作一个空的网格模板行来填充剩余空间?

如何并排放置
的定义列表,但如果
变长,
会向下移动?