草庐IT

html - 表格行边框 - 一半在里面,一半在外面

coder 2023-08-11 原文

我正在尝试使用我认为实现起来相当简单的样式来设置表格样式,但遇到了一个小问题。

表格将在每一行的左侧显示一个彩色指示器,因此我使用 border-left: 5px solid red; 来添加它。然而,尽管边界适用 - 一半在行内,一半在行外。我尝试添加 border-collapse: collapse 无济于事,我也在使用 box-sizing: border-box 但仍然有同样的问题。

最后,我也尝试将边框添加到第一个子单元格 (td),但出现同样的问题。

我已经设置了一个正在发生的事情的例子 - 我已经放置了一个超大的边框来突出显示这个问题:

http://www.cssdesk.com/TVa67

有没有人遇到过这个问题或有任何解决方案?

body {
  background: blue;
}
table {
  border-collapse: collapse;
  box-sizing: border-box;
  table-layout: fixed;
  width: 100%;
}
th,
td {
  background-color: #fff;
  padding: 10px 15px 8px;
}
th {
  border-bottom: 1px solid blue;
  font-weight: normal;
  text-align: left;
}
td {
  border-bottom: 1px solid gray;
}
tr.low {
  border-left: 25px solid red;
}
<table style="
    border-collapse: collapse;
">
  <tbody>
    <tr>
      <th>#</th>
      <th>Status</th>
      <th>Title</th>
      <th>Project</th>
      <th>Assigned To</th>
      <th>Age</th>
    </tr>

    <tr class="low">
      <td>1</td>
      <td>New</td>
      <td>This is an example ticket</td>
      <td>Something</td>
      <td>Something</td>
      <td>2 days ago</td>
    </tr>
  </tbody>
</table>

最佳答案

However, although the border applies - half of it is inside the row and half outside

此行为是预期的并且符合规范。引用:http://www.w3.org/TR/CSS2/tables.html#collapsing-borders它说:

Borders are centered on the grid lines between the cells...

它还用 description 的图表说明了这一点.

Has anyone run into this before or have any solutions?

是的,它可以很容易地在这个 fiddle 中演示:http://jsfiddle.net/abhitalks/xs7L9wn1/1/ 和下面的片段:

* { box-sizing: border-box; }
table {
    border-collapse: collapse;
    border: 1px solid gray;
    table-layout: fixed; width: 70%; 
    margin: 0 auto;
}
th, td {
    border: 1px solid gray;
    padding: 6px;
    text-align: center;
}
tbody > tr:nth-child(1) > td:first-child { border-left: 16px solid red; }
tbody > tr:nth-child(2) > td:first-child { border-left: 8px solid green; }
tbody > tr:nth-child(3) > td:first-child { border-left: 24px solid blue; }

tbody > tr:nth-child(1) > td:last-child { border-left: 16px solid red; }
tbody > tr:nth-child(2) > td:last-child { border-left: 8px solid green; }
tbody > tr:nth-child(3) > td:last-child { border-left: 24px solid blue; }
<table>
    <thead>
        <tr>
            <th>#</th>
            <th>Header 1</th>
            <th>Header 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Caption</td>
            <td>Description</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Caption</td>
            <td>Description</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Caption</td>
            <td>Description</td>
        </tr>
    </tbody>
</table>

解决方案:

只需为所有行添加一个相同宽度的透明边框即可。这样边框宽度将相同并且会整齐对齐。 (更新:向第一列添加了白色 border-left 以隐藏突出显示的单元格上的悬挂边框。正如您的评论所指出的。)

th, td { border-left: 15px solid transparent; }
tr > td:first-child, tr > th:first-child { border-left: 5px solid #fff; }
tr.low > td:first-child { border-left: 5px solid red; }

示例 fiddle :https://jsfiddle.net/abhitalks/s9taanz7/5/

片段:

* { box-sizing: border-box; }
body { background-color: blue; }
table {
    border-collapse: collapse;
    table-layout: fixed; width: 100%;
}
th, td {
    background-color: #fff;
    padding: 10px 15px 8px 8px;
    border-left: 5px solid transparent;
    border-bottom: 1px solid gray;  
}
th {
    border-bottom: 1px solid blue;
    font-weight: normal; text-align: left;
}
tr > td:first-child, tr > th:first-child { border-left: 10px solid #fff; }
tr.low > td:first-child { border-left: 10px solid red; }
<table>
	<thead>
		<tr>
			<th>#</th>
			<th>Status</th>
			<th>Title</th>
			<th>Project</th>
			<th>Assigned To</th>
			<th>Age</th>
		</tr>
	</thead>
	<tbody>
		<tr class="">
			<td>1</td>
			<td>New</td>
			<td>This is an example ticket</td>
			<td>Something</td>
			<td>Something</td>
			<td>2 days ago</td>
		</tr>
		<tr class="low">
			<td>2</td>
			<td>New</td>
			<td>This is an example ticket</td>
			<td>Something</td>
			<td>Something</td>
			<td>2 days ago</td>
		</tr>
		<tr class="">
			<td>3</td>
			<td>New</td>
			<td>This is an example ticket</td>
			<td>Something</td>
			<td>Something</td>
			<td>2 days ago</td>
		</tr>
	</tbody>
</table>

但是,这种方法会产生隐藏 border-bottom 的副作用,因为 border-left 与它重叠。


方案二:

您可以在左侧有一个额外的单元格用作指示器。然后,您可以使用 colgroup 来控制它。这种方法比上面的更简洁,并且还要求您在 css 中只指定一次宽度。

示例 fiddle 2:http://jsfiddle.net/abhitalks/z7u1nhwt/1/

片段 2:

* { box-sizing: border-box; }
body { background-color: blue; }
table {
    border-collapse: collapse;
    table-layout: fixed; width: 100%;
}
th, td {
    background-color: #fff;
    padding: 10px 15px 8px 8px;
    border-bottom: 1px solid gray;  
}
th {
    border-bottom: 1px solid blue;
    font-weight: normal; text-align: left;
}
.col1 { width: 10px; }
tr.low > td:first-child {
    background-color: #f00;
}
<table>
	<colgroup>
        <col class="col1" />
        <col class="coln" span="6" />
	</colgroup>
	<thead>
		<tr>
			<th></th>
			<th>#</th>
			<th>Status</th>
			<th>Title</th>
			<th>Project</th>
			<th>Assigned To</th>
			<th>Age</th>
		</tr>
	</thead>
	<tbody>
		<tr class="">
			<td></td>
			<td>1</td>
			<td>New</td>
			<td>This is an example ticket</td>
			<td>Something</td>
			<td>Something</td>
			<td>2 days ago</td>
		</tr>
		<tr class="low">
			<td></td>
			<td>2</td>
			<td>New</td>
			<td>This is an example ticket</td>
			<td>Something</td>
			<td>Something</td>
			<td>2 days ago</td>
		</tr>
		<tr class="">
			<td></td>
			<td>3</td>
			<td>New</td>
			<td>This is an example ticket</td>
			<td>Something</td>
			<td>Something</td>
			<td>2 days ago</td>
		</tr>
	</tbody>
</table>


当然,您也可以尝试使用@misterManSam 提出的使用伪元素的方法,具体取决于您的实现难易程度。

关于html - 表格行边框 - 一半在里面,一半在外面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31504421/

有关html - 表格行边框 - 一半在里面,一半在外面的更多相关文章

  1. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  2. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  3. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  4. ruby-on-rails - Ruby url 到 html 链接转换 - 2

    我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.

  5. ruby-on-rails - capybara ::ElementNotFound:无法找到 xpath "/html" - 2

    我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'

  6. ruby-on-rails - Prawn - 表格单元格内的链接 - 2

    我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c

  7. ruby - 如何使用 Ruby 将 CSV 文件读入 HTML 表格? - 2

    我正在尝试将一个简单的CSV文件读入HTML表格以在浏览器中显示,但我遇到了麻烦。这就是我正在尝试的:Controller:defshow@csv=CSV.open("file.csv",:headers=>true)end查看:输出:NameStartDateEndDateQuantityPostalCode基本上我只获取标题,而不会读取和呈现CSV正文。 最佳答案 这最终成为最终解决方案:Controller:defshow#OpenaCSVfile,andthenreaditintoaCSV::Tableobjectforda

  8. ruby - 如何使用 Nokogiri 解析纯 HTML 表格? - 2

    我想用Nokogiri解析HTML页面。页面的一部分有一个表,它没有使用任何特定的ID。是否可以提取如下内容:Today,3,455,34Today,1,1300,3664Today,10,100000,3444,Yesterday,3454,5656,3Yesterday,3545,1000,10Yesterday,3411,36223,15来自这个HTML:TodayYesterdayQntySizeLengthLengthSizeQnty345534345456563113003664354510001010100000344434113622315

  9. ruby-on-rails - 连接字符串时如何在 <%=%> block 内输出 html_safe? - 2

    考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://

  10. ruby-on-rails - prawnto 显示新页面时不会中断的表格 - 2

    我有可变数量的表格和可变数量的行,我想让它们一个接一个地显示,但如果表格不适合当前页面,请将其放在下一页,然后继续。我已将表格放入事务中,以便我可以回滚然后打印它(如果高度适合当前页面),但我如何获得表格高度?我现在有这段代码pdf.transactiondopdf.table@data,:font_size=>12,:border_style=>:grid,:horizontal_padding=>10,:vertical_padding=>3,:border_width=>2,:position=>:left,:row_colors=>["FFFFFF","DDDDDD"]pdf.

随机推荐