CSS / Javascript:如何在内联元素周围绘制最小边框?

时间:2022-05-14 23:17:19

Consider the following HTML:

请考虑以下HTML:

<p>This is a potentially large paragraph of text, which <span>may get 
wrapped onto several lines when displayed in the browser.
I would like to be able to draw a minimal</span> box round the span</p>

I would like to draw a minimal border round the span.

我想在跨度上绘制一个最小边框。

That is:

  • If the span is rendered on a single line, the border is equivalent to setting a CSS style of border: 1px solid black;
  • 如果跨度渲染在一行上,则边框相当于设置边框的CSS样式:1px solid black;

  • If the span is rendered on multiple lines, the border is drawn around the outermost edges of the span, not between the lines which it crosses. This is equivalent to setting a CSS background color on the span, and drawing the line around the edges of the highlighted area.
  • 如果跨度在多条线上渲染,则边框将围绕跨度的最外边缘绘制,而不是在跨越的线之间绘制。这相当于在跨度上设置CSS背景颜色,并在突出显示区域的边缘周围绘制线条。

I am fairly confident this cannot be done with raw CSS alone (in the second case). Solutions involving javascript libraries, or those which are Firefox-specific, are acceptable.

我相信这不能单独使用原始CSS(在第二种情况下)。涉及javascript库的解决方案或特定于Firefox的解决方案是可以接受的。

This is a mock-up of how the second scenario should look:

这是第二个场景应该如何看的模型:

CSS / Javascript:如何在内联元素周围绘制最小边框?

1 个解决方案

#1


13  

Consider adding an outline, not border http://jsfiddle.net/tarabyte/z9THQ/

考虑添加一个大纲,而不是边框​​http://jsfiddle.net/tarabyte/z9THQ/

span {
  outline: 2px solid black;   
}

Firefox draws outline between lines. There is a workarond: http://jsfiddle.net/z9THQ/2/

Firefox在线之间绘制轮廓。有一个工作方式:http://jsfiddle.net/z9THQ/2/

HTML:

<p>
   This is a potentially large paragraph of text, which 
      <span class="wrapped"><span> 
        may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal
      </span></span> 
   box round the span
 </p>

And CSS:

.wrapped {
   outline: 2px solid black;
}

.wrapped span {
    border: 1px solid white;
    background-color: white;
    position: relative;
    z-index: 1000;
}

#1


13  

Consider adding an outline, not border http://jsfiddle.net/tarabyte/z9THQ/

考虑添加一个大纲,而不是边框​​http://jsfiddle.net/tarabyte/z9THQ/

span {
  outline: 2px solid black;   
}

Firefox draws outline between lines. There is a workarond: http://jsfiddle.net/z9THQ/2/

Firefox在线之间绘制轮廓。有一个工作方式:http://jsfiddle.net/z9THQ/2/

HTML:

<p>
   This is a potentially large paragraph of text, which 
      <span class="wrapped"><span> 
        may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal
      </span></span> 
   box round the span
 </p>

And CSS:

.wrapped {
   outline: 2px solid black;
}

.wrapped span {
    border: 1px solid white;
    background-color: white;
    position: relative;
    z-index: 1000;
}