大网数据广告活动
Web 标准与前端开发

x2

· 52CE 我爱测

The **`x2`** attribute is used to specify the second x-coordinate for drawing an SVG element that requires more than one coordinate. Elements that only need one coordinate use the x attribute instead.

The x2 attribute is used to specify the second x-coordinate for drawing an SVG element that requires more than one coordinate. Elements that only need one coordinate use the x attribute instead.

Elements

You can use this attribute with the SVG elements described in the sections below.

<line>

For line, x2 defines the x coordinate of the ending point of the line.

<table class="properties"> <tbody> <tr> <th scope="row">Value</th> <td> <strong ><a href="/en-US/docs/Web/SVG/Guides/Content_type#length" ><length></a ></strong > | <strong ><a href="/en-US/docs/Web/SVG/Guides/Content_type#percentage" ><percentage></a ></strong > | <strong ><a href="/en-US/docs/Web/SVG/Guides/Content_type#number" ><number></a ></strong > </td> </tr> <tr> <th scope="row">Default value</th> <td><code>0</code></td> </tr> <tr> <th scope="row">Animatable</th> <td>Yes</td> </tr> </tbody> </table>

html,
body,
svg {
  height: 100%;
}
<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
  <line x1="5" x2="1" y1="1" y2="9" stroke="red" />
  <line x1="5" x2="5" y1="1" y2="9" stroke="green" />
  <line x1="5" x2="9" y1="1" y2="9" stroke="blue" />
</svg>

line

<linearGradient>

For linearGradient, x2 defines the x coordinate of the ending point of the gradient vector used to map the gradient stop values. The exact behavior of this attribute is influenced by the gradientUnits attributes

<table class="properties"> <tbody> <tr> <th scope="row">Value</th> <td> <strong ><a href="/en-US/docs/Web/SVG/Guides/Content_type#length" ><length></a ></strong > </td> </tr> <tr> <th scope="row">Default value</th> <td><code>100%</code></td> </tr> <tr> <th scope="row">Animatable</th> <td>Yes</td> </tr> </tbody> </table>

html,
body,
svg {
  height: 100%;
}
<svg viewBox="0 0 20 10" xmlns="http://www.w3.org/2000/svg">
  
  <linearGradient x2="100%" id="g0">
    <stop offset="0" stop-color="black" />
    <stop offset="100%" stop-color="red" />
  </linearGradient>

  <rect x="1" y="1" width="8" height="8" fill="url(#g0)" />

  
  <linearGradient x2="20%" id="g1">
    <stop offset="0" stop-color="black" />
    <stop offset="100%" stop-color="red" />
  </linearGradient>

  <rect x="11" y="1" width="8" height="8" fill="url(#g1)" />
</svg>

linearGradient

Examples

html,
body,
svg {
  height: 100%;
}
<svg viewBox="0 0 25 25" xmlns="http://www.w3.org/2000/svg">
  <line x1="2" x2="2" y1="5" y2="20" stroke="red" />
  <line x1="2" x2="12" y1="5" y2="20" stroke="green" />
  <line x1="2" x2="22" y1="5" y2="20" stroke="blue" />
</svg>

Examples

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。