css only dynamic size tooltip - html

I think I'm close but I am struggling to get a tooltip working exactly as I want. I have a dynamically built HTML table and I want to have tooltips for the contents of the TDs. It needs to be purely css (not based on javascript or events) for performance. I would like the tooltip to appear above and in the center of the TD upon hover and with an arrow underneath. I've got this to work but my problem is the content of the tooltip can sometimes be several lines which increases the tooltip height. When this happens the tooltip is still in the center of the TD but not above it anymore, it has moved down. How can I have a tooltip that will always be above the TD by the same amount regardless of the height. Please help.
html example td:
<td>
<div class="tooltip">
<span>My Tooltip Text</span>
Text in the TD
</div>
</td>
my css:
.tooltip span
{
display: none;
color: #000;
text-decoration: none;
}
.tooltip:hover span
{
display: block;
position: absolute;
background-color: #292929;
margin: -2.8em -1em;
color: white;
padding: 0.5rem 1rem;
border-radius: 4px;
white-space: pre-line;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
text-align: left;
}
.tooltip:hover span::after
{
top: 100%;
left: 50%;
border: solid transparent;
content: '';
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-top: 8px solid #292929;
border-right: 8px solid transparent;
border-left: 8px solid transparent;
border-width: 10px;
margin-left: -40px;
}

for example
#keyframes pulse {
0% {
opacity: .5;
transform: translate(-50%, -100%) scale(.5);
}
50%{
opacity: 1;
transform: translate(-50%, -100%) scale(1.2);
}
100% {
transform: translate(-50%, -100%) scale(1);
}
}
.tooltip span
{
color: #000;
text-decoration: none;
transform: translate(-50%, -100%) scale(.5);
position: absolute;
background-color: #292929;
color: white;
padding: 0.5rem 1rem;
border-radius: 4px;
font-size: 1rem;
font-weight: 400;
text-align: center;
box-shadow: 0 0 4px 2px rgba(0,0,0,.2);
width: max-content;
max-width: 200px;
top:0;
left:50%;
margin-top: -18px;
visibility: hidden;
opacity: 0;
}
.tooltip:hover{
position: relative;
}
.tooltip:hover span
{
opacity: 1;
visibility: visible;
transform: translate(-50%, -100%) scale(1);
animation: pulse 1s;
}
.tooltip:hover span::after
{
top: 100%;
left: 50%;
transform: translateX(-50%);
border: solid transparent;
content: '';
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-top: 8px solid #292929;
border-right: 8px solid transparent;
border-left: 8px solid transparent;
border-width: 10px;
}
<br>
<br>
<br>
<br>
<table border="1">
<tr>
<td>Hello</td>
<td>
<div class="tooltip">
<span>My Tooltip Text</span>
Cool text
</div>
</td>
<td>
<div class="tooltip">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>
Long text
</div>
</td>
</tr>
</table>

Related

text-underline decoration not apply on all child element

I have a label "Show more", that display hidden content. I want all child element of this label to be underline including the arrow. The problem is that only the text has text-decoration and not the arrow. How can I solve this issue in order that also arrow will be in the same underline text.
Thanks
#arrow_create {
border: solid black;
border-width: 0 2px 2px 0;
display: inline-block;
padding: 2px;
}
.icos-angle-up {
margin-left: 3px !important;
transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
}
.icos-angle-down {
margin: 3px;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
.icos-angle-up {
visibility: hidden;
}
#read_more_checkbox:checked+label[for="read_more_checkbox"]>.icos-angle-up {
visibility: visible;
}
#read_more_checkbox:checked+label[for="read_more_checkbox"]>.icos-angle-down {
display: none;
}
.read_more_txt {
display: none;
}
#read_more_checkbox {
display: none;
}
#read_more_checkbox:checked~.read_more_txt {
display: block;
margin-top: 10px;
}
.read_more_label {
margin-left: 5px !important;
font-weight: bold;
text-transform: uppercase;
}
#read_more_checkbox~.read_more_label:before {
content: attr(read_more);
text-decoration: underline;
text-underline-offset: 4px;
text-decoration-thickness: 1px;
cursor: pointer;
width: 110px;
}
#read_more_checkbox:checked~.read_more_label::before {
text-decoration: underline;
text-underline-offset: 4px;
text-decoration-thickness: 1px;
content: attr(read_less);
cursor: pointer;
}
<label for="read_more_checkbox" class="read_more_label" read_more="Show more" read_less="Show less">
<span id="arrow_create" class="icos-angle-down"></span>
<span id="arrow_create" class="icos-angle-up"></span>
</label>
So instead of using text-decoration (which only applies to text) you can either use border-bottom or to keep it more complicated there is also the possibility of using a css pseudo class.
Using border bottom
.read_more_label {
border-bottom: 1px solid black;
}
#arrow_create {
border: solid black;
border-width: 0 2px 2px 0;
display: inline-block;
padding: 2px;
}
.icos-angle-down {
margin: 3px;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
<label class="read_more_label">
Show more
<span id="arrow_create" class="icos-angle-down"></span>
</label>
Using css pseudo classes
The benefit of using pseudo classes would be that you also have the chance to design it more and you can even add some litte animations (as I did in the example)
.read_more_label {
position: relative;
}
.read_more_label::after {
content: '';
position: absolute;
bottom: -1px;
left: 0;
width: 100%;
height: 1px;
background-color: black;
opacity: 0;
transition: all 0.2s;
}
.read_more_label:hover::after {
opacity: 1;
}
#arrow_create {
border: solid black;
border-width: 0 2px 2px 0;
display: inline-block;
padding: 2px;
}
.icos-angle-down {
margin: 3px;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
<p>HOVER IT: </p>
<label class="read_more_label">
Show more
<span id="arrow_create" class="icos-angle-down"></span>
</label>
If shape arrow doesn't matter.
span {
text-decoration: underline;
}
<html>
<body>
<span>SHOW MORE ∨</span>
</body>
</html>
You can use also other HTML Special Characters
Another way
span {
border-bottom: 1px solid black;
}
span::after {
content: "";
border: solid black;
border-width: 0 2px 2px 0;
display: inline-block;
padding: 2px;
margin: 3px;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
<html>
<body>
<span>SHOW MORE</span>
</body>
</html>
You can use hr tag to draw a line under any thing and then you can add css on hr according to your need.for example if you want to change color of that line you can simply add css on hr tag and change color.

Always show tooltip at the same position from containg dive

When I hover over the first Div, the tooltip is shown further away than if I hover over the following two divs. Obviously it is because the text inside the div is larger/longer. But I don't want to show the tooltip span not depending on the hover text, but relating to the containing div of the text, so it is shown always at the same position.
jQuery is not an option for anything though but I kind of think, that it's a CSS problem anyway.
.subPhaseContainer {
float: left;
margin: 0 auto;
}
.projectItem {
margin: 4px;
border: 2px solid black;
cursor: pointer;
height: 17px;
}
.projectItem.green {
background-color: green;
color: white;
}
.projectNumber {
position: relative;
display: inline-block;
width: 80px;
}
.projectNumber .tooltiptext {
visibility: hidden;
width: fit-content;
text-align: left;
padding: 5px;
top: -1px;
position: absolute;
z-index: 1;
margin-left: 34px;
transition: opacity 0.3s;
border: 1px solid black;
}
.projectNumber .tooltiptext::after {
content: "";
position: absolute;
top: 20%;
right: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent black transparent transparent;
}
.projectNumber:hover .tooltiptext.green {
background-color: green;
color: white;
visibility: visible;
opacity: 1;
}
.projectNumber:hover .tooltiptext.yellow {
background-color: yellow;
visibility: visible;
opacity: 1;
}
.projectNumber:hover .tooltiptext.red {
background-color: red;
color: white;
visibility: visible;
opacity: 1;
}
<div class="subPhaseContainer">
<div class="projectItem green">
<div class="projectNumber"><span>AAAA-00</span>
<span class="tooltiptext green">Tooltip Sample</span>
</div>
</div>
<div class="projectItem green">
<div class="projectNumber">
<span>BBB-11</span>
<span class="tooltiptext green">Tooltip Sample</span>
</div>
</div>
<div class="projectItem green">
<div class="projectNumber">
<span>CCC-22</span>
<span class="tooltiptext green">Tooltip Sample</span>
</div>
</div>
</div>
You need to specify either a 'left' or 'right' position for your tooltiptext span, otherwise its left/right position will be the same as it would have been had you kept the tooltiptext span positioned relative.
So just update your CSS for the tooltiptext to this:
.projectNumber .tooltiptext {
visibility: hidden;
width: fit-content;
text-align: left;
padding: 5px;
top: -1px;
right: -100%;
position: absolute;
z-index: 1;
margin-left: 34px;
transition: opacity 0.3s;
border: 1px solid black;
}

Shadowed underlined text with perspective

I have the following html structure where I style my titles with an underline and a shadow.
.title {
font-size: 28px;
line-height: 1;
letter-spacing: 0px;
text-shadow: 2px 2px #32c8ff;
text-decoration: underline;
text-underline-position: under;
}
<div class="container">
<div class="content">
<div class="title">MY TITLE
</div>
</div>
</div>
I tried to apply a perspective effect with the following css
.container {
-webkit-perspective: 150px;
perspective: 150px;
}
.content {
-webkit-transform: rotateX(25deg);
transform: rotateX(25deg);
}
<div class="container">
<div class="content">
<div class="title">MY TITLE
</div>
</div>
</div>
As you can see, it works fine on the text but on the underline the shadow is not displayed and the distance from the text (text-underline-position: under;) is ignored.
Is there a way to display the shadow and keep the correct distance of the underline from the text when using the perspective?
You can simulate the underline with the pseudo element before:
.title {
display: inline-block;
font-size: 28px;
line-height: 1;
letter-spacing: 0px;
position: relative;
text-shadow: 2px 2px #32c8ff;
text-decoration: none;
}
.title::before {
border-bottom: 2px solid #000;
bottom: 0;
content: "";
position: absolute;
left: 0;
right: 0;
box-shadow: 2px 2px #32c8ff;
}
See the example here
Another possibility: Fiddle
.title, .subtitle {
font-size: 28px;
line-height: 1;
letter-spacing: 0px;
text-shadow: 2px 2px #32c8ff;
text-align: center;
}
.title {
border-bottom: 2px solid;
box-shadow: 2px 2px #32c8ff;
}
.container {
display: table-cell;
-webkit-transform: rotateX(45deg);
transform: rotateX(25deg);
}
.content {
-webkit-transform: rotateX(45deg);
transform: rotateX(25deg);
}
<div class="container">
<div class="content">
<div class="title">GREENPEACE</div>
<div class="subtitle">THE SURFER</div>
</div>
</div>
Check this out:
https://jsfiddle.net/zzhn5gh7/
.container {
width: 200px;
height: 200px;
border: 1px solid #CCC;
margin: 0 auto 60px;
position: relative;
-webkit-perspective: 600px;
-moz-perspective: 600px;
-o-perspective: 600px;
perspective: 600px;
}
.title {
width: 100%;
height: 100%;
position: absolute;
background: green;
font-size: 35px;
line-height: 1;
letter-spacing: 0px;
text-shadow: 2px 2px #32c8ff;
text-decoration: underline;
text-underline-position: under;
}
#rotate-x .title {
-webkit-transform: rotateX( 45deg);
-moz-transform: rotateX( 45deg);
-o-transform: rotateX( 45deg);
transform: rotateX( 45deg);
}
<div id="rotate-x" class="container">
<div class="title">MY TITLE
</div>
The right solution for me was the following:
.title {
display: inline-block;
position: relative;
font-size: 28px;
line-height: 2.5;
letter-spacing: 0px;
text-shadow: 2px 2px #32c8ff;
text-decoration: none;
}
.title::before {
border-bottom: 4px solid #ffffff;
bottom: 0;
content: "";
position: absolute;
left: 0;
right: 0;
box-shadow: 2px 2px #32c8ff;
margin: 11px 0px;
}
.subtitle {
font-size: 28px;
line-height: 0.7;
letter-spacing: 0px;
text-shadow: 2px 2px #32c8ff;
}

CSS Tooltip (tooltip display even if display none)

I try to make a tooltip (hide/unhide span) when a div is hovered.
I don't know where is the problem because the span is always visible and if i add display:none, all content will be hidden.
Thank you very much!
CSS:
.tooltip {
display:relative;
}
.tooltip span:before {
content:'';
display:none;
border-right: 8px solid #000000;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
position:absolute;
z-index:8;
font-size:0;
line-height:0;
width:0;
height:0;
top: 30%;
left: 83%;
}
.tooltip span:after {
display:none;
position:absolute;
top:0%;
left:89%;
padding:5px 8px;
background: #000000;
color:#fff;
z-index:9;
font-size: 0.75em;
height:auto;
opacity: 0.8;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
white-space:nowrap;
word-wrap:normal;
}
.tooltip span:hover:before,
.tooltip span:hover:after {
display:block;
}
HTML:
<div class="tooltip">
<span>tooltip text</span>
<input type="button" class="button active" value = "HOVER ME" >
</div>
https://jsfiddle.net/dy6bjvbm/1/
Start with something like the following:
.tooltip {
position: relative;
display: inline-block;
}
.tooltip span {
display: none;
position: absolute;
top: 30px;
border: 1px solid #ccc;
padding: 5px;
background: #eee;
}
.tooltip:hover span {
display: block;
}
<div class="tooltip">
<span>tooltip text</span>
<input type="button" class="button active" value = "HOVER ME" >
</div>
See this fork of your fiddle: https://jsfiddle.net/b60fcyu1/
If you're up for a JavaScript/jQuery solution, you can use mouseover and mouseout with hide() and show() element.
$(document).ready(function() {
$("input.button").mouseover(function() {
$('div span').hide();
});
$("input.button").mouseout(function() {
$('div span').show();
});
});
.tooltip {
display: relative;
}
.tooltip span:before {
content: '';
display: none;
border-right: 8px solid #000000;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
position: absolute;
z-index: 8;
font-size: 0;
line-height: 0;
width: 0;
height: 0;
top: 30%;
left: 83%;
}
.tooltip span:after {
display: none;
position: absolute;
top: 0%;
left: 89%;
padding: 5px 8px;
background: #000000;
color: #fff;
z-index: 9;
font-size: 0.75em;
height: auto;
opacity: 0.8;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
white-space: nowrap;
word-wrap: normal;
}
.tooltip span:hover:before,
.tooltip span:hover:after {
display: block;
}
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<div class="tooltip">
<span>tooltip text</span>
<br>
<input type="button" class="button active" value="HOVER ME">
</div>
If you have constant values, you can do it like this. (for more effective with multiple rows)
.tooltip {
position: relative;
width: 300px;
height: 30px;
}
.tooltip span {
opacity: 0;
pointer-events: none;
transition: 0.3s;
width: 100%;
position: absolute;
background-color: rgba(0, 0, 0, 0.6);
color: #ffffff;
line-height: 30px;
bottom: 40px;
border-radius: 5px;
padding: 0 5px;
}
.tooltip span:after {
top: 100%;
left: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(0, 0, 0, 0);
border-top-color: rgba(0, 0, 0, 0.6);
border-width: 6px;
margin-left: -6px;
}
.tooltip:hover span {
opacity: 1;
pointer-events: inherit;
}
<p style="height:100px;">
<!-- for seperate -->
</p>
<div class="tooltip">
<span>tooltip text tooltip text tooltip text tooltip text tooltip text tooltip text tooltip text tooltip text</span>
<input type="button" class="button active" value="HOVER ME" style="width:100%;height:30px">
</div>

css break words into new lines and becomes off positioned

I'm trying to create a simple tooltip without the need for js.
I'm wondering what the best approach is. I want dynamic text to be able to fit in a tooltip container smoothly, responsively on top of its container, the issue with the current code is that it doesn't stretch on its own, it breaks words into new lines which changes the height of the whole thing and then the positioning is off and it overlaps its container.
table {
margin-top: 100px;
}
td {
border: solid 1px #000;
height: 25px;
width: 25px;
position: relative;
}
div {
position: absolute;
top: -25px;;
}
span {
display: inline-block;
border-radius: 5px;
background: #000;
color: #fff;
position: absolute;
font-size: 15px;
padding: 5px;
width: auto;
}
td:hover div span {
display: inline-block;
}
<table>
<tr>
<td>
something
<div>
<span>
text
</span>
</div>
</td>
<td>
something
<div>
<span>
some long text
</span>
</div>
</td>
</tr>
</table>
You mean something like? Let me know.
If you want your tooltip above hover item, add bottom:100% to tooltip element.
table {
margin-top: 100px;
}
td {
border: solid 1px #000;
height: 25px;
width: 150px;
position: relative;
/*style to show it better to you*/
display:block;
margin-right:50px;
float:left;
}
div {
position: absolute;
bottom: 100%;
left:50%;
width:60px;
margin-left:-30px;
}
span {
display: none;
border-radius: 5px;
background: #000;
color: #fff;
font-size: 15px;
padding: 5px;
width: auto;
margin-bottom: 5px;
overflow: hidden;
text-overflow: ellipsis;
word-wrap: normal;
white-space: nowrap;
width:100%;
text-align:center;
}
td:hover div span {
display: inline-block;
}
<table>
<tr>
<td>
something
<div>
<span>
text
</span>
</div>
</td>
<td>
something
<div>
<span>
some long text
</span>
</div>
</td>
</tr>
</table>
If you don't want to break text in tooltip you can use white-space: nowrap. You can also use transform: translate() to position tooltip in middle of td With arrow
table {
margin-top: 100px;
}
td {
border: solid 1px #000;
position: relative;
}
span {
border-radius: 5px;
background: #000;
color: #fff;
position: absolute;
white-space: nowrap;
padding: 5px;
top: 0;
left: 50%;
transform: translate(-50%, -100%);
opacity: 0;
transition: all 0.3s ease-in;
}
td:hover span {
opacity: 1;
}
<table>
<tr>
<td>something<span>text</span></td>
<td>something<span>some long text</span></td>
<td>Lorem ipsum dolor.<span>Lorem ipsum dolor sit amet, consectetur.</span></td>
</tr>
</table>
Or if you want tooltip to be same size as td you can use width: 100%
table {
margin-top: 100px;
}
td {
border: solid 1px #000;
position: relative;
}
span {
border-radius: 5px;
background: #000;
color: #fff;
position: absolute;
width: 100%;
padding: 5px;
top: 0;
left: 50%;
transform: translate(-50%, -100%);
opacity: 0;
transition: all 0.3s ease-in;
}
td:hover span {
opacity: 1;
}
<table>
<tr>
<td>something<span>text</span></td>
<td>something<span>some long text</span></td>
<td>Lorem ipsum dolor.<span>Lorem ipsum dolor sit amet, consectetur.</span></td>
</tr>
</table>
You can also use :after pseudo element to add small arrow under tooltip
table {
margin-top: 100px;
}
td {
border: solid 1px #000;
position: relative;
}
span {
border-radius: 5px;
background: #000;
color: #fff;
position: absolute;
width: 100%;
padding: 5px;
top: -10px;
left: 50%;
transform: translate(-50%, -100%);
opacity: 0;
transition: all 0.3s ease-in;
}
span:before {
width: 0;
height: 0;
content: '';
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-top: 7px solid black;
position: absolute;
left: 50%;
bottom: 0;
transform: translate(-50%, 100%);
}
td:hover span {
opacity: 1;
}
<table>
<tr>
<td>something<span>text</span></td>
<td>something<span>some long text</span></td>
<td>Lorem ipsum dolor.<span>Lorem ipsum dolor sit amet, consectetur.</span></td>
</tr>
</table>
table {
margin-top: 100px;
}
td {
border: solid 1px #000;
height: 25px;
width: 25px;
position: relative;
}
div {
position: absolute;
top: -25px;;
}
span {
display: inline-block;
border-radius: 5px;
background: #000;
color: #fff;
position: absolute;
font-size: 15px;
padding: 5px;
width: auto;
white-space: nowrap;
}
td:hover div span {
display: inline-block;
}
<table>
<tr>
<td>
something
<div>
<span>
text
</span>
</div>
</td>
<td>
something
<div>
<span>
some long text
</span>
</div>
</td>
</tr>
</table>
Try using white-space: nowrap;