.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: -15px;
left: 110%;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 50%;
right: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent black transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
<h2>Right Tooltip w/ Left Arrow</h2>
<div class="tooltip">Hover over me
<ul class="tooltiptext">
<li>Tooltip text 1</li>
<li>Tooltip text 2</li>
<li>Tooltip text 3</li>
</ul>
</div>
Works perfectly with one Tooltip message, but not with multiple message.
It should always be vertically center regardless the no of tooltip messages.
Feel free to ask question if anyone didn't understand the scenario.
Instead of using margin values to adjust / place the toopltip.
Use top:50% and a transform to vertically center by dragging it back up 50% of its own height.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
margin-top: 50px;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: 50%;
transform: translateY(-50%);
left: 120%;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 100%;
border-width: 5px;
border-style: solid;
border-color: transparent black transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
<div class="tooltip">Hover over me
<ul class="tooltiptext">
<li>Tooltip text 1</li>
<li>Tooltip text 2</li>
<li>Tooltip text 3</li>
</ul>
</div>
<br/>
<div class="tooltip">Hover over me
<ul class="tooltiptext">
<li>Tooltip text 1</li>
</ul>
</div>
Related
I have a dropdown-component that when you click the button and dropdown is shown.
I want the triangle on the dropdown to always point to the center of the button that opened it regardless of how big it was.
Is this possible using css?
In the example below I am using a css-variable to achieve this. But in my real scenario I am unable to do this since the width of the container/button is decided by its contents.
Is it possible to reference the width of the container in any other way? I see that percentages used in left-positioning and the transform is based on the width of the menu it self. So that doesn't really help.
body {
margin: 2rem 50%;
--button-size: 100px;
}
.container {
position: absolute;
display: inline-block;
}
.button {
position: relative;
width: var(--button-size);
border: 1px solid black;
}
.button:after {
display: block;
position: absolute;
content: "";
width: 1px;
height: 200%;
top: 0;
left: 50%;
transform: translateX(-50%);
background: red;
}
.item {
white-space: pre;
padding: 1rem;
}
.item + .item {
border-top: 1px solid black;
}
.menu {
padding: 0;
list-style: none;
position: absolute;
border-radius: 4px;
border: 1px solid black;
left: 100%;
transform: translateX(calc(-75% - var(--button-size)/2));
}
.menu:after,
.menu:before {
content: "";
position: absolute;
left: 75%;
transform: translateX(-50%);
width: 0;
height: 0;
border-style: solid;
border-width: 8px;
border-color: transparent;
}
.menu:before {
border-bottom-color: black;
top: -17px;
}
.menu:after {
border-bottom-color: white;
top: -16px;
}
<div class="container">
<div class="button">Centered on me(but using css-variables)</div>
<ul class="menu">
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li class="item">Item 3</li>
</ul>
</div>
I think using a margin: auto; on the element that you want in the center could be useful as it automatically adjusts and places it's element in the center depending on the space available to it in any given situation.
I have a close button symbol which shows a top tooltip on hover pointing downwards. I have taken this example from https://www.w3schools.com/css/tryit.asp?filename=trycss_tooltip_arrow_bottom
<div id="middle">
<div class="posts">
<i class="fas fa-times fa-lg"><span class="tooltiptext">Exit</span></i>
</div>
</div>
CSS
.posts {
background:white;
border:1px solid #c0c0c0;
border-radius:4px;
padding:20px;
position:relative;
}
.posts a i.tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 150%;
left: 50%;
margin-left: -60px;
}
.posts a i.tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: black transparent transparent transparent;
}
.posts a:hover i.tooltiptext {
visibility: visible;
}
Tooltip is not showing.
Please add style to posts class
.posts{
position:relative;
margin:60px;
text-align:center;
}
.posts{
position:relative;
margin:60px;
text-align:center;
}
.posts a .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 150%;
left: 50%;
margin-left: -60px;
}
.posts a .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: black transparent transparent transparent;
}
.posts a:hover .tooltiptext {
visibility: visible;
}
<script src='https://kit.fontawesome.com/a076d05399.js'></script>
<div id="middle">
<div class="posts">
<i class="fas fa-times fa-lg"><span class="tooltiptext">Exit</span></i>
</div>
</div>
I was trying to make, when mouse go over the text "Hover over me", appear/disappear the text "Tooltip text" whit this code:
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
div .tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
<body style="text-align:center;">
<h2>Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="wrapper">
<div class="header">
<div class="head">
<div class="nav">
<div class="tooltip"> Hover over me
</div>
</div>
<div class="test">
<span class="tooltiptext">Tooltip text</span>
</div>
</div>
</div>
</div>
</body>
After a couple of attempts I came to the conclusion that it does not work because they are in two different <div>. So I was wondering if it was possible to make it work, Thanks.
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
div .tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
.tooltiptext {
visibility:hidden;
}
.tooltip:hover + .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
<body style="text-align:center;">
<h2>Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="wrapper">
<div class="header">
<div class="head">
<div class="tooltip"> Hover over me
</div>
<div class="tooltiptext">Tooltip text</div>
</div>
</div>
</div>
</body>
</html>
i am trying to implement a tooltip like div using css.
i am refering
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_tooltip
above example shows a top tool tip.
can any one help me change this to bottom tool tip. I am very weak in CSS and design.
Here you go this could help you.
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: -40px;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -5px;
border-width: 0px 10px 10px 10px;
border-style: solid;
border-color: #555 transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
<body style="text-align:center;">
<h2>Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="tooltip">Hover over me
<span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
Here it is...
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
top: 100%;
left: 50%;
margin-left: -60px;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
<!DOCTYPE html>
<html>
<body style="text-align:center;">
<div class="tooltip">Hover over me
<span class="tooltiptext">Tooltip text</span>
</div>
</body>
</html>
Here is how you can do it with some changes in CSS.
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: -215%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltiptext::after {
bottom: 100%;
left: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(136, 183, 213, 0);
border-bottom-color: #555;
border-width: 6px;
margin-left: -6px;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
<div style="text-align:center;">
<h2>Tooltip</h2>
<p>Move the mouse over the text below:</p>
<div class="tooltip">Hover over me
<span class="tooltiptext">Tooltip text</span>
</div>
</div>
Try this...!!!
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px solid black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
top: 100%;
left: 50%;
margin-left: -60px;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<div class="tooltip">Hover Me
<span class="tooltiptext">bottom tooltip</span>
</div>
</body>
</html>
I have a little "caret" that is put on my currently hovered menu item ,and I now realize the caret created it not centered. I have a bunch of dynamically sized menu items.
The class menu-caret is applied when hovered by a little bit of jquery, and I have some css like so :
.menu-caret::before {
content:"";
position: absolute;
margin-left: 20px;
bottom: 0;
width: 0%;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid black;
}
See a quick example here - https://jsfiddle.net/keL4zhky/3/.
Is it possible to have the carets be centered on the bottom? Thanks!
EDIT: is it possible to do this without relative? I have a full width sub menu that if i change the ul/li to relative will lose it's effect - See example here http://codepen.io/ajmajma/pen/KgGxWL
Just make the parent position: relative; and add left: 50%; and margin-left: -5px; to the pseudo element:
ul {
width: 100%;
position: relative;
display: inline-block;
list-style-type: none;
}
li {
padding: 20px;
float: left;
position: relative;
}
.menu-caret::before {
content: "";
position: absolute;
left: 50%;
bottom: 0;
width: 0%;
margin-left: -5px;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid black;
}
<ul>
<li class="menu-caret">one</li>
<li class="menu-caret">larger</li>
<li class="menu-caret">larrrrger</li>
</ul>
Instead of left: 50%; and margin-left: -5px;you can also just use left: calc(50% - 5px);
The li has to have position:relative to provide positioning context.
The pseudo-element is then positioned absolutely 50% left and then pulled back 50% of it's own width by the transform. This will work for any size caret and does not require any magic numbers.
ul {
width: 100%;
position: relative;
display: inline-block;
list-style-type: none;
}
li {
padding: 20px;
float: left;
}
.menu-caret {
position: relative;
}
.menu-caret::before {
content: "";
position: absolute;
transform: translateX(-50%);
left: 50%;
bottom: 0;
width: 0%;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid black;
}
<ul>
<li class="menu-caret">one</li>
<li class="menu-caret">larger</li>
<li class="menu-caret">larrrrger</li>
</ul>
Instead of absolute positioning and float, I prefer:
ul {
width: 100%;
position: relative;
display: inline-block;
list-style-type: none;
}
li {
padding: 20px;
text-align:center;
display: inline-block;
}
.menu-caret::after {
text-align: center;
display: block;
content: "";
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid black;
clear: both;
width: 1px;
margin: 0 auto;
}
<ul>
<li class="menu-caret">one</li>
<li class="menu-caret">larger</li>
<li class="menu-caret">larrrrger</li>
</ul>