My end goal is to have a speech bubble on hover of a button appear on the highest z-index/layer of the page. The button will be part of a bigger piece of text content that is inside a div with a max-height restriction.
Sometimes, the text content can spill over this max-height restriction, and ideally I'd like a scroll bar to be created so the content still neatly fits inside the parent div. However when the text content does not fit, I would like a scroll bar created in order to not have the content bleed over the parent div.
Here is where my issue begins
Adding a property of overflow-y: scroll
to the parent div in order to handle edge cases of long text content pushes the bubble beneath the div layer and automatically creates an x-axis scroll.I would like a y-axis scroll with no x-axis scroll and to force the bubble on hover to appear on top even when there is longer text content.
Below is the codepen to better describe my issue. Removing overflow-y: scroll
from .demo element is exactly what I want, except for when the content is too large for the parent div restrictions...
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 64px auto;
text-align: center;
max-width: 640px;
width: 94%;
background-repeat: no-repeat;
height: 100vh;
background-color: #8bc6ec;
background-image: linear-gradient(135deg, #8bc6ec 0%, #9599e2 100%);
}
.demo {
background-color: white;
padding: 2rem;
border-radius: 0.5rem;
max-height: 400px;
overflow-y: scroll;
}
.demo,
.demo p {
margin: 4em 0;
text-align: center;
}
.button {
background-color: #ea4c89;
border-radius: 8px;
border-style: none;
box-sizing: border-box;
color: #ffffff;
cursor: pointer;
display: inline-block;
font-family: "Haas Grot Text R Web", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
font-weight: 500;
height: 40px;
line-height: 20px;
list-style: none;
margin: 0;
outline: none;
padding: 10px 16px;
position: relative;
text-align: center;
text-decoration: none;
transition: color 100ms;
vertical-align: baseline;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
}
.button:hover,
.button:focus {
background-color: #f082ac;
}
/**
* Tooltip Styles
*/
/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
position: relative;
z-index: 2;
cursor: pointer;
}
/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
pointer-events: none;
}
/* Position tooltip above the element */
[data-tooltip]:before {
content: attr(data-tooltip);
background-color: white;
color: black;
position: absolute;
width: max-content;
border: solid black 1.5px;
padding: 5px 10px;
border-radius: 10px;
left: 102%;
top: 115%;
}
/* Show tooltip content on hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
<div >
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum
is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model
text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to
popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the
more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum"
(The Extrem<button data-tooltip="I’m the tooltip text on the right of the button that extends out pretty far">I’m a button with a tooltip</button></p>
</div>
CodePudding user response:
Instead of opacity: 0
use display: none;
so the :before
is only there on :hover
. This will make it so there is only overflow-x: scroll;
when the tooltip is displayed.
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 64px auto;
text-align: center;
max-width: 640px;
width: 94%;
background-repeat: no-repeat;
height: 100vh;
background-color: #8bc6ec;
background-image: linear-gradient(135deg, #8bc6ec 0%, #9599e2 100%);
}
.demo {
background-color: white;
padding: 2rem;
border-radius: 0.5rem;
max-height: 400px;
overflow-y: scroll;
}
.demo,
.demo p {
margin: 4em 0;
text-align: center;
}
.button {
background-color: #ea4c89;
border-radius: 8px;
border-style: none;
box-sizing: border-box;
color: #ffffff;
cursor: pointer;
display: inline-block;
font-family: "Haas Grot Text R Web", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
font-weight: 500;
height: 40px;
line-height: 20px;
list-style: none;
margin: 0;
outline: none;
padding: 10px 16px;
position: relative;
text-align: center;
text-decoration: none;
transition: color 100ms;
vertical-align: baseline;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
}
.button:hover,
.button:focus {
background-color: #f082ac;
}
/**
* Tooltip Styles
*/
/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
position: relative;
z-index: 2;
cursor: pointer;
}
/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
display: none;
pointer-events: none;
}
/* Position tooltip above the element */
[data-tooltip]:before {
content: attr(data-tooltip);
background-color: white;
color: black;
position: absolute;
width: max-content;
border: solid black 1.5px;
padding: 5px 10px;
border-radius: 10px;
left: 102%;
top: 115%;
}
/* Show tooltip content on hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100);
display: block;
}
<div >
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum
is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model
text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to
popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the
more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum"
(The Extrem<button data-tooltip="I’m the tooltip text on the right of the button that extends out pretty far">I’m a button with a tooltip</button></p>
</div>