[英]Equally space 3 divs inside a div using jQuery to make 4 equal sections
I have a table cell that I would like to split into 4 pixel perfect equal 'sections'. 我有一个表格单元格,我想将其拆分为4像素完美相等的“部分”。
This is being done by having 3 divs that are 1px wide (lines) these are then positioned using margin-left on top of another div. 这是通过使3个div宽度为1px的div(线)来完成的,然后将这些div使用margin-left放置在另一个div的顶部。
I'm doing this in jQuery as we need to account for the extra space taken up by the lines. 我正在jQuery中进行此操作,因为我们需要考虑这些行占用的额外空间。
I have this working fine.. but all of the sections are not exactly the same width. 我的工作正常。但是所有部分的宽度都不完全相同。 I have taken into account (I think) the 3 lines by removing 3px from the width of the cell and then dividing the remaining into 4 (4 sections...) As the cell gets bigger so does the difference in section size.
我考虑了(我认为)这3条线,方法是从单元格的宽度中删除3px,然后将其余部分分成4个(4个部分...)。随着单元格的变大,部分大小的差异也将变大。
This could of course be done in pure CSS by have the width of the lines in % instead of px. 当然,这可以在纯CSS中完成,方法是将行的宽度设置为%而不是px。 BUT having the lines at 0.5% does not render well.
但是线数为0.5%的渲染效果不好。 I prefer 1px.
我更喜欢1px。
I have a feeling I'm missing something simple? 我感觉自己缺少一些简单的东西?
Here is the fiddle. 这是小提琴。
http://jsfiddle.net/sQAg2/ http://jsfiddle.net/sQAg2/
Code: 码:
<style>
body {
padding: 30px;
}
.testTable {
margin: 0 auto;
border: 1px solid #f9f9f9;
width: 100%;
}
.testTable tr th {
padding: 10px;
border: 1px solid #c1c1c1;
box-sizing: border-box;
}
.testTable tr th:first-child {
width: 30%;
}
.testTable tr td {
background: #f9f9f9;
border: 1px solid #c1c1c1;
box-sizing:border-box;
}
.line {
position: relative;
background: #000;
width: 1px;
min-height: 20px;
float: left;
z-index: 9999;
}
</style>
<table id="" cellspacing="0" cellpadding="0" class="testTable">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div style="position: relative; width: 100%; overflow: hidden;">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="block" style="width: 50%; min-height: 20px; background: green; z-index: 1; margin-left: 30%;"></div>
<div class="" style="clear: both;"></div>
</div>
</td>
<td>Testing 2</td>
<td>Testing 3</td>
<td>Testing 4</td>
<td>Testing 5</td>
</tr>
</tbody>
</table>
<script>
var cellWidth,
lineMargin;
// Get the width of a cell and remove 3px due to 3 x 1px lines?
cellWidth = $('.testTable tbody tr:first').find('td').width() - 3;
// divide by 4 due to 4 sections
lineMargin = (cellWidth / 4);
$('.line').css('margin-left', lineMargin);
</script>
假设您不需要IE8。
margin-left: calc(25% - 1px);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.