如何在不使用溢出隐藏的情况下将边框添加到与边框半径完美匹配的表格列

Posted

技术标签:

【中文标题】如何在不使用溢出隐藏的情况下将边框添加到与边框半径完美匹配的表格列【英文标题】:How do i add border to the table column that matches the border radius perfectly without using overflow hidden 【发布时间】:2022-01-01 17:24:18 【问题描述】:

我正在尝试在下图中将这种设计实现为表格第一个 td 上的边框或框阴影。 最重要的是我不能使用溢出隐藏,因为它会弄乱里面的所有下拉菜单。

我尝试了使用 boxshadow、border 的不同方法,但无法准确指出这一点。 这是截图,它使用了 box-shadow,但它并不能完全反映上图的设计。

.wrapper 
  background-color: #f1f4f8;
  height: 100vh;


.table 
  border-collapse: separate;
  border-spacing: 1px 2px;
  border: none;


.table td,
.table th 
  white-space: nowrap;
  border: none !important;


.table thead th 
  border: none;
  padding: 7.5px 10px;
  background-color: #f1f4f8;
  z-index: 1;


.table td 
  padding: 10px;
  height: 50px;
  background-color: #fff;


.table td:first-child 
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;


.table td:last-child 
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;


.table tbody tr:hover td:first-child 
  position: relative;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  box-shadow: inset 2px 0px 0px blue;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.10.2/umd/popper.min.js" integrity="sha512-nnzkI2u2Dy6HMnzMIkh7CPd1KX445z38XIu4jG1jGw7x5tSL3VBjE44dY4ihMU1ijAQV930SPM12cCFrB18sVw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="wrapper">
  <div class="container">
    <table class="table">
      <thead>
        <th>
          Items
        </th>
        <th>
          Price
        </th>
        <th>
          Quantity
        </th>
      </thead>
      <tbody>
        <tr>
          <td>
            <div class="btn-group" role="group" aria-label="Button group with nested dropdown">
  <button type="button" class="btn btn-secondary">1</button>
  <button type="button" class="btn btn-secondary">2</button>

  <div class="btn-group" role="group">
    <button id="btnGroupDrop1" type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown
    </button>
    <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
      <a class="dropdown-item" href="#">Dropdown link</a>
      <a class="dropdown-item" href="#">Dropdown link</a>
    </div>
  </div>
</div>
          </td>
          <td>
            $200
          </td>
          <td>
            3
          </td>
        </tr>
        <tr>
          <td>
            Chocolates
          </td>
          <td>
            $200
          </td>
          <td>
            3
          </td>
        </tr>
        <tr>
          <td>
            Chocolates
          </td>
          <td>
            $200
          </td>
          <td>
            3
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

我可以使用这种技术实现设计,但 overflow: hidden; 如下面的 sn-p 但 overflow: hidden;会弄乱表格列中的其他下拉内容。这是使用 psuedo 选择器的外观。

.wrapper 
  background-color: #f1f4f8;
  height: 100vh;


.table 
  border-collapse: separate;
  border-spacing: 1px 2px;
  border: none;


.table td,
.table th 
  white-space: nowrap;
  border: none !important;


.table thead th 
  border: none;
  padding: 7.5px 10px;
  background-color: #f1f4f8;
  z-index: 1;


.table td 
  padding: 10px;
  height: 50px;
  background-color: #fff;


.table td:first-child 
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  position: relative;
  overflow:hidden;


.table td:last-child 
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;


.table tbody tr:hover td:first-child::after 
  
          width: 2px;
          height: 100%;
          background-color: #2c66ee;
          position: absolute;
          left: 0;
          content: "";
          top: 0;
          bottom: 0;
        
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="wrapper">
  <div class="container">
    <table class="table">
      <thead>
        <th>
          Items
        </th>
        <th>
          Price
        </th>
        <th>
          Quantity
        </th>
      </thead>
      <tbody>
        <tr>
          <td>
            <div class="btn-group" role="group" aria-label="Button group with nested dropdown">
  <button type="button" class="btn btn-secondary">1</button>
  <button type="button" class="btn btn-secondary">2</button>

  <div class="btn-group" role="group">
    <button id="btnGroupDrop1" type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown
    </button>
    <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
      <a class="dropdown-item" href="#">Dropdown link</a>
      <a class="dropdown-item" href="#">Dropdown link</a>
    </div>
  </div>
</div>
          </td>
          <td>
            $200
          </td>
          <td>
            3
          </td>
        </tr>
        <tr>
          <td>
            Chocolates
          </td>
          <td>
            $200
          </td>
          <td>
            3
          </td>
        </tr>
        <tr>
          <td>
            Chocolates
          </td>
          <td>
            $200
          </td>
          <td>
            3
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

有没有什么方法可以在不使用overflow: hidden 的情况下实现这一点?

谢谢。

【问题讨论】:

3d 旋转怎么样? 为什么不使用 linear-gradientbackground-img: linear-gradient(to right, blue 2px, white 2px); 【参考方案1】:

我会使用线性渐变(Amaury Hanser 也提到过)并使用 css 变量来处理悬停时的偏移量

td:first-child 
   background: linear-gradient(
     to right, 
     #2c66ee var(--marker, 0), 
     #fff 0
  );

tr:hover td:first-child 
    --marker: 2px;

【讨论】:

以上是关于如何在不使用溢出隐藏的情况下将边框添加到与边框半径完美匹配的表格列的主要内容,如果未能解决你的问题,请参考以下文章

当父级具有边框半径并且子级具有动画时,CSS溢出隐藏在chrome中不起作用

如何在不将Dock设置为Fill的情况下将Label置于Panel内部

如何在不使用边框间距和空行的情况下在带有边框的表格行之间添加空格

Webkit 不考虑溢出:以边界半径隐藏

边界半径和溢出:隐藏(离子应用程序)

如何在表格行上添加边框半径