如何向 td 边框添加虚线或添加内边距

发布于 2025-01-10 02:00:44 字数 2358 浏览 1 评论 0原文

下图已经接近我正在寻找的解决方案(请参阅我的codepen)。唯一缺少的是 的轮廓边框与 的边框之间的填充/间隙;

带虚线的当前状态表

问题

  • 如果您必须创建类似于上图的内容,您会采取什么方法?
  • 我怎样才能实现虚线在 td (/ tr) 的边框与 tr 或 tbody 或 table 的轮廓之间有一些间隙?

代码

您可以查看codepen 上的代码。我的CSS方法是这样的

table {  width: 100%; }
/** outline: 0.5pt solid; outline-color: black; border-spacing: .2em 0em; **/

thead, tbody { outline: 0.5pt solid; outline-color: black; }

th.sgn { border-bottom: 1pt dotted blue; padding-top: 1ex;}    
td.sgn { border-bottom: 1pt dotted blue; padding-top: 1ex;}  
<table>
       <colgroup>
         <col width="20%">
         <col width="35%">                    
         <col width="15%">
         <col width="30%">                    
    </colgroup>
    <thead>
       <tr>
            <th>Werkzeugnr:</th>
            <th class="sgn"> &nbsp; </th>
            <th>Index</td><th class="sgn"></th>
       </tr>                            
       <tr class="sml">
            <th colspan="4"> &nbsp; </th>
       </tr>                            
    </thead>
    <tbody>
        <tr><th colspan="4"><h3>Bitte ausfüllen und abheften!</h3></th></tr>
    </tbody>
    <tbody>                                                
       <tr>
         <td>Einbaudatum:</td><td class="sgn"> &nbsp; </td>
         <td>Name</td><td class="sgn"> &nbsp;</td>
       </tr>
       <tr><td>Anlaufprobleme:</td><td><input type="checkbox"></td><td colspan="2">Bitte Grund angeben</td>
       </tr>
    </tbody>    
</table>

The below image is already close to the solution i am looking for (see my codepen). The only thing that is missing is a padding / gap between the outline border of <tbody> or <thead> and the border of the <td>

table with dotted lines current state

Questions

  • What approach would you take if you had to create something that looks like the image above?
  • How can i achieve that the dotted lines have some gap between the border of the td (/ tr) and the outline of either tr or tbody or table?

Code

You can take a look at the code on codepen. My CSS approach is this

table {  width: 100%; }
/** outline: 0.5pt solid; outline-color: black; border-spacing: .2em 0em; **/

thead, tbody { outline: 0.5pt solid; outline-color: black; }

th.sgn { border-bottom: 1pt dotted blue; padding-top: 1ex;}    
td.sgn { border-bottom: 1pt dotted blue; padding-top: 1ex;}  
<table>
       <colgroup>
         <col width="20%">
         <col width="35%">                    
         <col width="15%">
         <col width="30%">                    
    </colgroup>
    <thead>
       <tr>
            <th>Werkzeugnr:</th>
            <th class="sgn">   </th>
            <th>Index</td><th class="sgn"></th>
       </tr>                            
       <tr class="sml">
            <th colspan="4">   </th>
       </tr>                            
    </thead>
    <tbody>
        <tr><th colspan="4"><h3>Bitte ausfüllen und abheften!</h3></th></tr>
    </tbody>
    <tbody>                                                
       <tr>
         <td>Einbaudatum:</td><td class="sgn">   </td>
         <td>Name</td><td class="sgn">  </td>
       </tr>
       <tr><td>Anlaufprobleme:</td><td><input type="checkbox"></td><td colspan="2">Bitte Grund angeben</td>
       </tr>
    </tbody>    
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我最亲爱的 2025-01-17 02:00:44

我将使用 ::before::after 伪类来实现该间距。

  1. 首先,为想要赋予空间的元素指定一个唯一的类(例如.spaced)。
<td class="spaced sgn" colspan="3">   </td>
  1. 然后,给它一个相对位置,以便 ::before::after 相对于它定位:
.spaced {
  position: relative;
}
  1. 添加 ::before< /code> 和 ::after 与您想要的 width 作为空格,并将它们分别定位到左侧和右侧:
.spaced::before,
.spaced::after {
  content: "";
  position: absolute;
  width: 1rem;
  height: 1rem;
  background: white;
  bottom: -0.5rem;
}

.spaced::before {
  left: 0;
}

.spaced::after {
  right: 0;
}

CodePen: https://codepen.io/moaaz_bs/pen/xxPJoQL?editors=1100

I would achieve that spacing with ::before and ::after pseudo classes.

  1. First, give elements you want to give space a unique class (e.g. .spaced).
<td class="spaced sgn" colspan="3">   </td>
  1. Then, give it a relative position so ::before and ::after get positioned relative to it:
.spaced {
  position: relative;
}
  1. Add ::before and ::after with the width you want as space, and position them to left and right respectively:
.spaced::before,
.spaced::after {
  content: "";
  position: absolute;
  width: 1rem;
  height: 1rem;
  background: white;
  bottom: -0.5rem;
}

.spaced::before {
  left: 0;
}

.spaced::after {
  right: 0;
}

CodePen: https://codepen.io/moaaz_bs/pen/xxPJoQL?editors=1100

帅的被狗咬 2025-01-17 02:00:44

如果您必须创建类似于上图的内容,您会采取什么方法?

CSS 伪元素非常适合此用例。

.sgn::after {
  content: '';
  position: absolute;
  border-bottom: 2px dotted blue;
  width: 20%;
}

您可以根据自己的喜好进行调整,我无法弄清楚要使其发挥作用的宽度,但我相信您能够做到。

What approach would you take if you had to create something that looks like the image above?

CSS Pseudo elements are perfect for this use case.

.sgn::after {
  content: '';
  position: absolute;
  border-bottom: 2px dotted blue;
  width: 20%;
}

You can adjust this to your liking, I couldn't figure out what width to get it to work but I'm sure you will be able to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文