css - SVG中心横竖线
问题描述
如何创建一个包含两条线(一条水平线,一条垂直线)的 SVG,从一侧的中间到另一侧的中间,从而使加号与 SVG 容器的大小相同?
这就是我现在所拥有的,但是当更改 SVG 大小时,行长不会改变
<svg width="10px" height="10px">
<line class="point" x1="0" x2="10" y1="5" y2="5" />
<line class="point" x1="5" x2="5" y1="0" y2="10" />
</svg>
解决方案
要使 SVG 的内容随其大小缩放,它需要有一个viewBox
.
svg {
background-color: linen;
}
.point {
stroke: black;
stroke-width: 1;
}
<svg width="10px" height="10px" viewBox="0 0 10 10">
<line class="point" x1="0" x2="10" y1="5" y2="5" />
<line class="point" x1="5" x2="5" y1="0" y2="10" />
</svg>
<svg width="30px" height="30px" viewBox="0 0 10 10">
<line class="point" x1="0" x2="10" y1="5" y2="5" />
<line class="point" x1="5" x2="5" y1="0" y2="10" />
</svg>
<svg width="100px" height="100px" viewBox="0 0 10 10">
<line class="point" x1="0" x2="10" y1="5" y2="5" />
<line class="point" x1="5" x2="5" y1="0" y2="10" />
</svg>
在此示例中,所有内容都会缩放,包括线宽。如果这不是您想要的,那么您可以使用@SirExotic 的方法(使用百分比坐标),或者您可以使用vector-effect: non-scaling-stroke
.
svg {
background-color: linen;
}
.point {
stroke: black;
stroke-width: 1;
vector-effect: non-scaling-stroke;
}
<svg width="10px" height="10px" viewBox="0 0 10 10">
<line class="point" x1="0" x2="10" y1="5" y2="5" />
<line class="point" x1="5" x2="5" y1="0" y2="10" />
</svg>
<svg width="30px" height="30px" viewBox="0 0 10 10">
<line class="point" x1="0" x2="10" y1="5" y2="5" />
<line class="point" x1="5" x2="5" y1="0" y2="10" />
</svg>
<svg width="100px" height="100px" viewBox="0 0 10 10">
<line class="point" x1="0" x2="10" y1="5" y2="5" />
<line class="point" x1="5" x2="5" y1="0" y2="10" />
</svg>
推荐阅读
- bash - 在 powershell 中重用启动的程序实例
- python - OSError:/lib64/libm.so.6:在 CentOS 7 中为 Python 3 使用 pygel3d 时找不到版本“GLIBC_2.27”
- javascript - for 循环函数未运行
- drupal - Drupal 站点迁移
- javascript - 将 API 信息保存到数据库中 - 在 Javascript 中使用 Django 和 Fetch API
- typescript - 为什么当映射泛型构造函数的 ConstructorParameters 时,Typescript 不再确定它是一个元组?
- azure - “az acr build” Azure CLI 命令,是否有等效的 Powershell cmdlet 命令?
- webpack - TailwindCSS JIT Webpack HMR 不断用 themekit 重新编译
- angular - 在命名插座中传递参数不起作用
- c - 在 MPI 中按顺序运行 for 循环的某些部分