【问题标题】:How do you draw a line from a border?你如何从边界画一条线?
【发布时间】:2015-03-17 05:52:59
【问题描述】:

我目前有这个 HTML:

#circle {
  background-color: orange;
  max-width: 40px;
  margin-right: 20px;
  margin-bottom: 20px;
  min-width: 40px;
  min-height: 40px;
  font-size: 12px;
  border-radius: 50%;
  font-weight: bold;
  text-align: center;
  vertical-align: middle;
  line-height: 40px;
  float:left;
}
#innerContent {
  border: solid 2px black;
  padding: 3px;
}
#pointerDiv{
    float:left;
}
<div id="circle"><span id='innerContent'>123</span></div><div id='pointerDiv'>text goes here</div>

我正在努力实现这个效果:

基本上,一条从边框指向元素的线我可以用解释数字的文本填充。我该怎么做呢?

【问题讨论】:

  • 包含文本的元素在哪里?
  • 糟糕,我的错。固定!
  • 行的长度是否始终相同(或者)包含文本的元素在屏幕上的位置是否可以变化?
  • 行的长度可以始终相同,因为文本始终保持不变。

标签: css css-shapes


【解决方案1】:

你总是可以使用伪元素吗?我在下面创建了一个 basic 模型:

.circle {
  height: 55px;
  width: 55px;
  border-radius: 50%;
  background: orange;
  position: relative;
  display: inline-block;

}
.circle:before {
  position: absolute;
  content: "hi";
  height: 60%;
  top: 20%;
  left: 20%;
  width: 60%;
  border: 2px solid black;
}
.circle:after {
  position: absolute;
  content: "";
  width: 100%;
  right: -85%;
  top: 50%;
  border-bottom: 1px solid black;
}
.tooltip {
  display: inline-block;
  margin-left: 55px;
    height: 60px;
  width:60px;
}
.wrapper{
  display:block;
  }
<div class="wrapper">
  <div class="circle"></div>
  <div class="tooltip">text goes here</div>
</div>

【讨论】:

  • 几乎相似的想法配对,但 OP 只能接受一个答案,所以我想奖励你的努力。请不要认为是同情,而更像是对相似想法的相互欣赏。
  • @Harry:我们的大多数答案都相当相似(嗯,大部分时间无论如何)。所以它通常是在你我 Web-tiki 和 chipChocolate.py 之间发布的最快答案:P
【解决方案2】:

您可以使用 CSS 边框或 SVG 画线(可能与某些浏览器不兼容)

#circle {
  background-color: orange;
  max-width: 40px;
  margin-bottom: 20px;
  min-width: 40px;
  min-height: 40px;
  font-size: 12px;
  border-radius: 50%;
  font-weight: bold;
  text-align: center;
  vertical-align: middle;
  line-height: 40px;
  float: left;
}
#innerContent {
  border: solid 2px black;
  padding: 3px;
}
#pointerDiv {
  float: left;
  line-height: 40px;
}
#line-svg {
  float: left;
  margin-top: 20px;
  margin-left: -6px;
  width: 100px;
}
<div id="circle"><span id='innerContent'>123</span>
</div>
<svg id="line-svg">
  <line x1="0" y1="0" x2="100%" y2="0" style="stroke:rgb(0,0,0);stroke-width:4" />
</svg>
<div id='pointerDiv'>text goes here</div>

【讨论】:

  • 一个有趣的答案,谢谢!但是,出于兼容性原因,我宁愿使用 CSS。
  • SVG 非常像 CSS3,所以最好使用老式 CSS
  • 不错的替代想法伙伴 :)
【解决方案3】:

下面是一个示例方法,通过使用伪元素然后根据需要绝对定位它来实现这一点。

left: -58px 的原因是因为margin-right(我已将其从有问题的 20px 修改为 50px 以作说明)是 50px + 框的边框在圆圈内有几个 px所以这也必须考虑。使行的width 小于left 的值,以使行在pointerDiv 之前结束。

请注意,我还在#circle 中添加了clear: both,以防万一您想在另一个下方添加更多此类条目。如果不需要,可以将其删除。

#circle {
  background-color: orange;
  max-width: 40px;
  margin-right: 50px;
  margin-bottom: 20px;
  min-width: 40px;
  min-height: 40px;
  font-size: 12px;
  border-radius: 50%;
  font-weight: bold;
  text-align: center;
  vertical-align: middle;
  line-height: 40px;
  float: left;
  clear: both;
}
#innerContent {
  border: solid 2px black;
  padding: 3px;
}
#pointerDiv {
  float: left;
  position: relative;
  height: 40px;
  line-height: 40px;
}
#pointerDiv:before {
  content: '';
  position: absolute;
  border: 1px solid black;
  top: 50%;
  left: -58px;
  width: 55px;
}
<div id="circle"><span id='innerContent'>123</span>
</div>
<div id='pointerDiv'>text goes here</div>

<div id="circle"><span id='innerContent'>123</span>
</div>
<div id='pointerDiv'>some lengthy text goes here</div>

<div id="circle"><span id='innerContent'>123</span>
</div>
<div id='pointerDiv'>short txt</div>

【讨论】:

  • 根据我的情况调整了pointerDiv:before设置,但现在效果很好,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 2011-05-17
  • 1970-01-01
  • 2017-03-25
  • 2013-02-23
相关资源
最近更新 更多