[英]Make td containing normal text as wide as if it was bold
I'm working on a html table. 我正在研究一个html表。 To increase the readability, I want the current row to be bold.
为了提高可读性,我希望当前行为粗体。 I do that like that:
我这样做:
.displayTable tr:hover { color: #000000; font-weight: bold; }
But the table cells change their width and the whole table resizes when I hover rows with content filling the whole cell (because bolded text takes up more space). 但是当我悬停具有填充整个单元格的内容的行时,表格单元格会改变它们的宽度并且整个表格会调整大小(因为粗体文本会占用更多空间)。 So how can I prevent this by making the cell as wide as if it was bold without acually making t bold?
那么我怎样才能通过使细胞像粗体一样宽而不是粗体来防止这种情况呢?
You could give a like-bold style using a text-shadow
effect, eg 您可以使用
text-shadow
效果给出类似粗体的样式,例如
td:hover {
text-shadow: 0 0 1px #999;
}
so it won't affect the width of the text. 所以它不会影响文本的宽度。 Example code: http://codepen.io/anon/pen/cEqJK
示例代码: http : //codepen.io/anon/pen/cEqJK
I'm not sure that there is an ideal HTML/CSS solution for this problem, so I used some JavaScript to solve it. 我不确定这个问题是否有理想的HTML / CSS解决方案,所以我使用了一些JavaScript来解决它。
The result can be found in this fiddle . 结果可以在这个小提琴中找到。
HTML: HTML:
<table class="displayTable">
<tr><td>Row 1, Col 1</td><td>Row 1, Col 2</td></tr>
<tr><td>Row 2, Col 1</td><td>Row 2, Col 2</td></tr>
</table>
CSS: CSS:
.displayTable {
border: 1px solid #000;
}
.displayTable td {
border: 1px solid #000;
}
.displayTable tr:hover { color: #000000; font-weight: bold; }
JavaScript (using jQuery): JavaScript(使用jQuery):
$(function() {
var td = $(".displayTable tr td");
td.css("font-weight", "bold");
td.each( function () {
var width = $(this).width(),
height = $(this).height();
$(this).css("width", width+"px");
$(this).css("height", height+"px");
});
td.css("font-weight", "");
});
In my code, on page load the JavaScript essentially sets the font-weight to bold, checks to see what the width and height of the td
elements are, and sets their width and height properties to those values, and then removes the font-weight property so that it goes back to whatever (if anything) the stylesheet sets for it. 在我的代码中,在页面加载时,JavaScript基本上将font-weight设置为粗体,检查
td
元素的宽度和高度,并将它们的width和height属性设置为这些值,然后删除字体权重属性,以便它返回到样式表为它设置的任何(如果有的话)。 This should work. 这应该工作。
Make your table size static. 使您的表格大小为静态。 Add
width
and height
to <td>
. 将
width
和height
添加到<td>
。
HTML HTML
<table class="displayTable">
<tr>
<td>Hello</td>
<td>Word</td>
</tr>
<tr>
<td>Hello</td>
<td>Word</td>
</tr>
</table>
CSS CSS
.displayTable tr:hover {
color: #000000;
font-weight: bold;
}
tr{ background:yellow; }
td{
width:100px;
height:50px;
text-align:center;
}
Well, i tried to put in mind that your text that will be populated in each td
would be long and short. 好吧,我试着记住,你的每个
td
填充的文本都会很长很短。 So for that i use a separate div
to bind the text and set their overflow
with the basis of the width and height
of your td
因此,我使用一个单独的
div
来绑定文本,并根据您的td
的width and height
设置它们的overflow
div{
width:100px;
height:50px;
overflow-y:scroll;
}
td{
padding:5px;
}
tr:hover{
font-weight:bolder;
color:red;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.