A client wants to have the table designed like this above.
But i cant seem to figure out how to alter the position of the text in the first row or better said in general, because things like margin-top: -20px;
just dont work…
Also client will have employees create tables with non-html knowledge like Gutenberg Editor Table. This means i dont really have the option to add an extra element inside the td like a span :/
Any idea how and if this is doable ? Thank you very much!
tbody > tr:nth-child(1) > td {
???
}
I tried
tbody > tr:nth-child(1) > td {
margin-top: -20px;
margin-left: -20px;
}
to alter the position but that doesnt work.
5
Answers
Try placing the texts on the first line in
<span>
and assigningposition: absolute
withtop: -20px
in them andposition: relative
in the<td>
EDIT:
Using javascript to wrap the text with span
To obtain the design you want you can simply modify the appearance of the
thead
element, I suggest you to useposition: relative;
andtop: -.5em;
.Of course, you can also act on
td
s directly, but be sure to encapsulate the content in a contanier, e.g. a<span>
.Here it is a snippet with some examples:
Check this out, use overflow-wrap: break-word; to get the overflowing text into a second line. and also give a width: 200px , height: 55px, max-width: 200px to the table column, td element.
HTML code:
CSS code:
As stated in comments and other answers, the point here is having the
<th>
content displaced relative to its box model. I’m doing my answer just for the sake of having a live snippet but I’m adding close to nothing to anything said already.There are mostly two ways to achieve that result:
<th>
asposition: relative
and then having a childelement styled as
position: absolute
so that its actual positionwill cross the border box of the container;
translate;
Here in this demo I’m showing the translate strategy. There are some tricks to keep things tidy up; for example the size of the
th
andtd
are stated statically to keep alignments correct and the word wrap is forced with abr
both in the header text and in some td contents. The font is the closest I could pick that could be taken for granted and yet look close to the picture given.. that’s clearly a sans serif font but of course it should be more condensed.And now 2 further notes:
html and you might be forced to create such child element inside each
th
via js. It sounds strange you can’t change the html by the wayeven if you are using a wysiwyg editor. But as suggested in the other
answer, you can just have a js function running on
DOMContentLoaded
that will replace the
th
content with a new element wrapping it;its box model… that’s not clear why and it may depend on how it’s
embedded in the page and you didn’t show in your question so far;
To say the whole truth there’s sill another way that would not need the html to be changed (well only slightly actually): using css rules adopting
::before
/::after
to add the displaced element on top of theth
. I didn’t add this way because it would be required only if the html way isn’t suitable in your scenario.Other answers seem to have concentrated on moving the text.
I would leave the text alone and instead alter the background of that first row.
The reason for doing it this way is that the system adjusts the height of the greyish bit to accommodate any extra lines that may be put in the column headings.