I've encountered an interesting challenge with a project I'm working on. I need to inject markup into pages that I don't have control over, specifically within tables. The issue arises when my injected div causes the parent table to expand uncontrollably due to the lack of table-layout: fixed
. Here is an example setup:
<div style="width: 600px;">
<table width="100%">
<tr>
<td>
<div> <!-- my content --> </div>
</td>
</tr>
</table>
</div>
While setting table-layout: fixed
on the table would solve this problem, I am limited to making changes starting from the innermost div only.
I discovered a hack that works by applying
width: 100%; display: table; table-layout: fixed;
to my div. However, I'm unsure about its compliance with relevant specifications since the contents are all display: block
.
The provided markup showcases the issue and presents the hack in action:
<div class="outer">
<table class="bad-table">
<tr>
<td>
<div class="wrapper">
<div>Lorem ipsum dolor sit amet...</div>
<div class="target">
<ul class="menu">
<li style="background-color: #800;"></li>
<li style="background-color: #880;"></li>
<li style="background-color: #080;"></li>
<li style="background-color: #008;"></li>
</ul>
</div>
<div>Lorem ipsum dolor sit amet...</div>
</div>
</td>
</tr>
</table>
</div>
CSS:
.outer {
width: 200px;
border: 1px solid #0f0;
}
.bad-table {
width: 100%;
border: 1px solid #f00;
}
.target {
width: 100%;
overflow-x: auto;
overflow-y: hidden;
}
.wrapper {
width: 100%;
}
.wrapper.hack {
display: table;
table-layout: fixed;
}
This scenario highlights two main constraints:
- I cannot modify any markup outside of my injected div.
- The height of my injected div must be based on its content, preventing solutions using
position: absolute
.
Is the presented hack a suitable solution for this issue, or is there a more effective approach?