html - 如何创建相对于其他元素的响应式嵌套 SVG 线?

标签 html css svg

我有一个包含 3 个元素的嵌套 SVG。位于左右两侧的 2 个三 Angular 形和中间的一条线。我想在水平和垂直调整大小时都获得一条响应线(线应该只有三 Angular 形之间的可用宽度)。我曾尝试以百分比设置宽度,但仅在水平调整大小时有效。当我垂直调整大小时,它不起作用,因为三 Angular 形的宽度发生了变化。这是代码笔链接:https://codepen.io/roppazvan/pen/dyyPKKL?editors=1100

    <svg
        class='input-source'
        stroke='black'
        stroke-width='0'
        fill="black">
        <rect  x="20" y="20%" width="100%" height="60%" 
            stroke='black'
            stroke-width='0'
        >
        </rect>
              <!-- The right head --> 
        <svg class='head input-source' id='right' 
            height="100%"
            width='100%' 
            viewBox="0 0 20 40"
            preserveAspectRatio="xMaxYMid"
            >
            <rect width="100%" height="100%"/>
        </svg>

        <!-- The left head --> 
        <svg class='head input-source' id='left' 
            height="100%"
            width='100%' 
            viewBox="0 0 15 30"
            preserveAspectRatio="xMinYMid"
            >
            <rect width="100%" height="100%"/>
        </svg>
    </svg>   
</svg>


<svg width="110px" height="40px" version="1.0" state='normal'>
    <svg
        class='input-source'
        stroke='black'
        stroke-width='0'
        fill="black">
        <rect  x="20" y="20%" width="100%" height="60%" 
            stroke='black'
            stroke-width='0'
        >
        </rect>
              <!-- The right head --> 
        <svg class='head input-source' id='right' 
            height="100%"
            width='100%' 
            viewBox="0 0 20 40"
            preserveAspectRatio="xMaxYMid"
            >
            <rect width="100%" height="100%"/>
        </svg>

        <!-- The left head --> 
        <svg class='head input-source' id='left' 
            height="100%"
            width='100%' 
            viewBox="0 0 15 30"
            preserveAspectRatio="xMinYMid"
            >
            <rect width="100%" height="100%"/>
        </svg>
    </svg>   
</svg>

最佳答案

实现和理解的最简单和最容易的方法可能就是使用 flex-box。

#svg-container {
  margin-top: 100px;
  width: 100%;
  border: 1px solid #bada55;

  display: flex;
  flex-direction: row;
}

svg {
  height: 10vh;
}

/* stretch the middle box */
svg:nth-child(2) {
  flex: 1;
}
<div id="svg-container">
<!--    left head  -->
  <svg viewBox="0 0 14 14" preserveAspectRatio="xMinYMid" opacity="0.5">
     <polygon points="0,7 14,0 14,14 " />
  </svg>
<!--     line -->
  <svg viewBox="0 0 14 14" preserveAspectRatio="none">
    <rect y="30%" width="100%" height="40%" />
  </svg>
<!--     right head -->
  <svg viewBox="0 0 14 14" preserveAspectRatio="xMaxYMid" opacity="0.5">
    <polygon points="14,7 0,0 0,14 "/>
  </svg>
</div>

关于html - 如何创建相对于其他元素的响应式嵌套 SVG 线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58285105/

相关文章:

javascript - 如何访问使用 LINK 标签加载的第 3 方样式表的源代码?

html - 检测宽度与检测浏览器

html - 用图像和动画渐变背景遮盖 div

svg - 尝试使用 SVG 作为背景时模块解析失败

html - 表格形式的数据溢出IE 8中的div,表格中断

javascript - Bootstrap 弹出窗口定位 - 不起作用

javascript - 字体系列 iFrame 正文

css - 是否有定位的简写属性?

javascript - 单击更改 svg 元素的属性填充

html - 如何在剩余空间中 float 图像并居中标题?