With pure CSS I think you won’t get the effect, so I suggest a pure Javascript solution. Follow the steps below:
First include TD text in two tags: div
and p
, in this way:
<td><div><p>Contratação de empresa para fornecimento<p></div></td>
Then use the CSS below. I’ve included some lines and changed some of your original CSS. You can compare to see what’s changed. Basically it was removing the spacing of the p
and others related to div
maid.
table{
float: right;
margin: 50px 300px auto 0px;
width: 65%;
}
th{
border: 0px solid #ffffff;
color: #ffffff;
text-align: center;
font-family: Verdana, Arial, Helvetica;
font-size: 14px;
text-align: center;
background-color: #007cc2;
}
td{
border: 3px solid #ffffff;
color: #5d5d5d;
font-size: 14px;
font-weight: normal;
font-family: Verdana, Arial, Helvetica, sans-serif;
background-color: #efeeee;
text-align: center;
table-layout: fixed;
}
td div{
height: 50px;
overflow: hidden;
transition: height 0.4s;
}
table tr td p{
margin: 0;
position: relative;
}
Now include the script below that will do all the control of the effect and necessary adjustments:
var tds = document.querySelectorAll("tr:not(:first-child)"); // seleciona todas as TRs menos a primeira
var normalHeight = 50; // altura padrão (mesmo que no height do CSS -> td div)
for(var x=0; x<tds.length; x++){
var ps = tds[x].querySelector(":nth-child(3) p"); // selecione todos os parágrafos da 3ª coluna
// centralizo apenas os que forem menor que a altura padrão
if(ps.offsetHeight < normalHeight){
ps.style.cssText = "top: 50%; transform: translateY(-50%);";
}
// aumenta a div ao passar o mouse na TR
tds[x].addEventListener("mouseover", function(){
var tdDiv = this.querySelector("td:nth-child(3) p");
var divHeight = tdDiv.offsetHeight;
if(divHeight > normalHeight) tdDiv.parentNode.style.height = divHeight+"px";
});
// volta ao normal ao retirar o mouse da TR
tds[x].addEventListener("mouseleave", function(){
var tdDiv = this.querySelector("td:nth-child(3) p");
tdDiv.parentNode.style.height = normalHeight+"px";
});
}
Let’s see it working (preferably run in full screen):
var tds = document.querySelectorAll("tr:not(:first-child)"); // seleciona todas as TRs menos a primeira
var normalHeight = 50; // altura padrão (mesmo que no height do CSS -> td div)
for(var x=0; x<tds.length; x++){
var ps = tds[x].querySelector(":nth-child(3) p"); // selecione todos os parágrafos da 3ª coluna
// centralizo apenas os que forem menor que a altura padrão
if(ps.offsetHeight < normalHeight){
ps.style.cssText = "top: 50%; transform: translateY(-50%);";
}
// aumenta a div ao passar o mouse na TR
tds[x].addEventListener("mouseover", function(){
var tdDiv = this.querySelector("td:nth-child(3) p");
var divHeight = tdDiv.offsetHeight;
if(divHeight > normalHeight) tdDiv.parentNode.style.height = divHeight+"px";
});
// volta ao normal ao retirar o mouse da TR
tds[x].addEventListener("mouseleave", function(){
var tdDiv = this.querySelector("td:nth-child(3) p");
tdDiv.parentNode.style.height = normalHeight+"px";
});
}
table{
/* comentado para exibição no snippet
float: right;
margin: 50px 300px auto 0px; */
width: 65%;
}
th{
border: 0px solid #ffffff;
color: #ffffff;
text-align: center;
font-family: Verdana, Arial, Helvetica;
font-size: 14px;
text-align: center;
background-color: #007cc2;
}
td{
border: 3px solid #ffffff;
color: #5d5d5d;
font-size: 14px;
font-weight: normal;
font-family: Verdana, Arial, Helvetica, sans-serif;
background-color: #efeeee;
text-align: center;
table-layout: fixed;
}
td div{
height: 50px;
overflow: hidden;
transition: height 0.4s;
}
table tr td p{
margin: 0;
position: relative;
}
<div>
<table>
<tr>
<th>Publicação</th>
<th>Abertura</th>
<th>Obejeto</th>
<th>Editar</th>
<th>Comunicado</th>
<th>ATA</th>
<tr>
<td>13/04/2018</td>
<td> 26/04/2018 14:00</td>
<td><div><p>Contratação de empresa para fornecimento de: equipamentos hidraúlicos, em atendimento a solicitação do engenheiro quimico, conforme specificado no TERMO DE REFERÊNCIA – (ANEXO I).Contratação de empresa para fornecimento de: equipamentos hidraúlicos.<p></div> </td>
<td>ARQUIVO</td>
<td>Aguardando Publicação</td>
<td>Aguardando Publicação</td>
</tr>
<tr>
<td>13/04/2018</td>
<td> 26/04/2018 14:00</td>
<td><div><p>Contratação<p></div></td>
<td>ARQUIVO</td>
<td>Aguardando Publicação</td>
<td>Aguardando Publicação</td>
</tr>
<tr>
<td>13/04/2018</td>
<td> 26/04/2018 14:00</td>
<td><div><p>Contratação de empresa para fornecimento de: equipamentos hidraúlicos, em atendimento a solicitação do engenheiro quimico, conforme specificado no TERMO DE REFERÊNCIA – (ANEXO I).Contratação de empresa para fornecimento de: equipamentos hidraúlicos, em atendimento a solicitação do engenheiro quimico, conforme specificado no TERMO DE REFERÊNCIA – (ANEXO I).<p></div> </td>
<td>ARQUIVO</td>
<td>Aguardando Publicação</td>
<td>Aguardando Publicação</td>
</tr>
</table>
</div>
It looks pretty cool Victor
– hugocsl
@hugocsl thanks Hugo, very good to receive this feedback from you! : D We kind of ended up doing pretty much the same thing in our answers :P
– Victor Jordan
Mano I really liked what you did but would there be some way to leave the text with more content ? 'Cause it sits on one line and it doesn’t look very pretty rsrs.
– Lucca Fernandes
For nothing Young, got very creative. He keeps jumping between a cell with more content and another, but not all are flowers in CSS rss. Try to copy the code you made in Codepen and put here in the Stackoverflow Snipper that gets even better! If you can’t tell me I’ll edit your answer with the code there
– hugocsl
@Luccafernandes fear that leaving with more content, with two lines for example, only with CSS would not be possible... What you could do to leave more content would be to increase the size a little
max-width
. Maybe if you wanted something more elaborate, you would have to go for a solution with Javascript– Victor Jordan
@hugocsl Ready! Really now it’s easier! Thanks again! : ) Feel free to edit if you find something that can improve!
– Victor Jordan
Got it, helped me a lot, thank you XD
– Lucca Fernandes