You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Div定位SVG?添加样式后SVG消失问题求助

问题分析与解决
  • 核心问题:给.button添加float:right后,元素脱离正常文档流,父容器.background未设置高度且未清除浮动,导致高度塌陷为0,视觉上SVG就像“消失”了;另外SVG中的<foreignobject>标签大小写错误,SVG对标签大小写敏感,小写的foreignobject不会被浏览器识别。

修复方案

1. 解决父容器高度塌陷

给父容器.background添加清除浮动的样式,有两种常用方式:

方式一:使用overflow属性

.background {
  background: red;
  overflow: auto; /* 让父容器包裹浮动子元素 */
}
.button-text {
  text-align: center;
}
.button{
  float: right;
  padding: 40px;
}

方式二:使用clearfix伪元素(更通用)

.background {
  background: red;
}
/* 添加清除浮动的伪元素 */
.background::after {
  content: "";
  display: table;
  clear: both;
}
.button-text {
  text-align: center;
}
.button{
  float: right;
  padding: 40px;
}

2. 修复SVG标签错误

将SVG中的<foreignobject>改为<foreignObject>(注意首字母O大写),确保浏览器能识别该元素:

<foreignObject width="260" height="39">
  <p class="button-text">SEE ALL NEWS</p>
</foreignObject>

内容的提问来源于stack exchange,提问作者jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 22:45:59