position a tooltip always bottom right - html

I created a simple tooltip
[tooltip]:before {
content: attr(tooltip);
position: absolute;
opacity: 0;
transition: all 0.5s ease;
}
[tooltip]:hover:before {
opacity: 1;
color: #ffffff;
background: #333333;
padding: 10px;
}
/* ################ */
/* No need for this */
/* ################ */
div {
background: cyan;
margin: 20px;
padding: 10px;
}
body {
background: white;
}
<div tooltip="Tooltip">Div with tooltip</div>
As you can see the tooltip always covers the div content. I want to put the tooltip always at the bottom right side.
I add
bottom: 0;
right: 0;
to the CSS but then the tooltip appears at the bottom of the page. How can I fix this?

Because you first need to set your container position as relative : absolute positioning refers to the last non-static element.
After that, you don't want to use bottom: 0 but top: 100% : using bottom will align the tooltip on the bottom, while top will place it underneath your container.
[tooltip]:before {
content: attr(tooltip);
position: absolute;
opacity: 0;
right: 0;
top: 100%;
transition: all 0.5s ease;
}
[tooltip]:hover:before {
opacity: 1;
color: #ffffff;
background: #333333;
padding: 10px;
}
[tooltip] {
position: relative;
}
/* ################ */
/* No need for this */
/* ################ */
div {
background: cyan;
margin: 20px;
padding: 10px;
}
body {
background: white;
}
<div tooltip="Tooltip">Div with tooltip</div>

Simply add position:relative; to the element where the tooltip should be placed in the bottom right;
[tooltip] {
position:relative;
}
[tooltip]:before {
content: attr(tooltip);
position: absolute;
opacity: 0;
transition: all 0.5s ease;
bottom:0;
right:0;
}
[tooltip]:hover:before {
opacity: 1;
color: #ffffff;
background: #333333;
padding: 10px;
}
/* ################ */
/* No need for this */
/* ################ */
div {
background: cyan;
margin: 20px;
padding: 10px;
}
body {
background: white;
}
<div tooltip="Tooltip">Div with tooltip</div>

Related

Make CSS transition left to right

I am trying to make a menu bar open and close from right to left. I have looked at documentation and other posts and have not been able to get it to work.
As you can see the menu is on the right of the screen but opens from the wrong direction, I tried changing it to relative and changing all the values to no avail.
function openNav() {
document.getElementById("mySidebar").style.width = "250px";
document.getElementById("main").style.marginRight = "250px";
}
function closeNav() {
document.getElementById("mySidebar").style.width = "0";
document.getElementById("main").style.marginRight = "0";
}
#import url("https://fonts.googleapis.com/css?family=Nunito+Sans:900");
* {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
body {
font-family: "Nunito Sans";
overflow-x: hidden;
font-size: 60px;
}
video{
width: 100vw;
height: 100vh;
object-fit: cover;
top: 0;
left: 0;
}
.header {
position: absolute;
font-size: 130px;
color: #2C3939;
top: 50%;
left: 50%;
transform: translate(-0%, -50%);
}
.header2 {
position: absolute;
z-index: 1;
font-size: 30px;
color: #2C3939;
top: 40%;
left: 61%;
transform: translate(-0%, -50%);
}
/* The sidebar menu */
.sidebar {
height: 100%; /* 100% Full-height */
width: 0; /* 0 width - change this with JavaScript */
position: fixed; /* Stay in place */
z-index: 1; /* Stay on top */
top: 0;
left: 1200px;
background-color: #111; /* Black*/
overflow-x: hidden; /* Disable horizontal scroll */
padding-top: 60px; /* Place content 60px from the top */
transition: 0.5s; /* 0.5 second transition effect to slide in the sidebar */
}
/* The sidebar links */
.sidebar a {
float:right;
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
/* When you mouse over the navigation links, change their color */
.sidebar a:hover {
color: #f1f1f1;
}
/* Position and style the close button (top right corner) */
.sidebar .closebtn {
position: relative;
top: 0;
right: 25px;
font-size: 36px;
margin-right: 50px;
}
/* The button used to open the sidebar */
.openbtn {
position: fixed;
top: 5%;
left: 95%;
transform: translate(-50%, -50%);
font-size: 80px;
cursor: pointer;
color: 2C3939;
padding: 10px 15px;
border: none;
z-index: 1;
}
.openbtn:hover {
background-color: #444;
}
/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
#media screen and (max-height: 450px) {
.sidebar a {font-size: 18px;}
}
<div id="mySidebar" class="sidebar">
×
About
Services
Clients
Contact
</div>
<div id="body">
<button class="openbtn" onclick="openNav()">
<img src="assets/SidebarIcon.png" width="26.25" height="43.75">
</button>
</div>
you need to set the container position to absolute, and give it :
right:0
top:0
.sidebar {
height: 100%; /* 100% Full-height */
width: 0; /* 0 width - change this with JavaScript */
z-index: 1; /* Stay on top */
position: absolute; /* Stay in place */
top: 0;
right: 0;
background-color: #111; /* Black*/
overflow-x: hidden; /* Disable horizontal scroll */
padding-top: 60px; /* Place content 60px from the top */
transition: 0.5s; /* 0.5 second transition effect to slide in the sidebar */
}
replace this with your sidebar class

How to combine linear gradient in background image, opacity and text color change on hover?

Div tag contains background image with linear gradient of black and transparent from bottom. On mouse hover state, image changes opacity and the text over the image changes color simultaneously. How to do all this three things aforementioned. I want to achieve this similar effect.
Here is my code:
.container{
position: relative;
cursor: pointer;
}
.container::after{
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
position: absolute;
background: linear-gradient(
0deg,
rgba(0,0,0, 0.8) ,
transparent 30%,
transparent
);
}
.container p{
position: absolute;
bottom: 2rem;
padding: 0 2rem;
text-transform: uppercase;
font-family: 'Josefin Sans', sans-serif;
font-size: 2rem;
font-weight: 300;
color: var(--white);
}
.container p:hover{
color: var(--black);
}
.container:hover{
opacity: 0.4;
}
<div class="container">
<img id="image1" src="https://images.unsplash.com/photo-1451187580459-43490279c0fa?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1472&q=80" alt="Deep Earth">
<p id="p1">Deep earth</p>
</div>
You just needed to do some re-organizing in your CSS so you're selectors were targeting the correct elements. I also added a z-index to the text so it wasn't under the gradient.
.container {
display: block;
position: relative;
cursor: pointer;
width: fit-content;
}
.container::after {
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
position: absolute;
background: linear-gradient(0deg,
rgba(0, 0, 0, 0.8),
transparent 30%,
transparent);
}
.container p {
display: block;
position: absolute;
bottom: 2rem;
padding: 0 2rem;
text-transform: uppercase;
font-family: 'Josefin Sans', sans-serif;
font-size: 2rem;
font-weight: 300;
color: white;
z-index: 2;
}
.container:hover p {
color: black;
}
.container:hover img {
opacity: 0.4;
}
<div class="container">
<img id="image1" src="https://images.unsplash.com/photo-1451187580459-43490279c0fa?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1472&q=80" alt="Deep Earth">
<p id="p1">Deep earth</p>
</div>
I've changed up the CSS a fair bit, I've commented in the CSS code what I've done and why.
/* Body Margin 0 is just for the snippet */
body {margin: 0}
/* Added hidden overflow */
.container {
cursor: pointer;
overflow: hidden;
position: relative;
}
/* Use both before and after */
.container::before,
.container::after {
content: '';
position: absolute;
/* Same as top, right, bottom, left = 0 */
inset: 0;
/* z-index to control layer depth */
z-index: 1;
}
/* Use before to simulate the opacity change */
/* Stops the text from fading as well as the container */
.container::before {
background: white;
opacity: 0;
/* Place above dark gradient so that text is more visible */
z-index: 2;
/* Transition to make it look smooth */
transition: opacity 512ms 0ms ease-in-out;
}
.container::after {
background: linear-gradient(
0deg,
rgba(0,0,0, 0.8) ,
transparent 30%,
transparent
);
}
/* Removes bottom margin/padding from last element */
.container > *:last-child {
margin-bottom: 0;
padding-bottom: 0;
}
/* Set the img to always be 100% of the viewport width */
.container img { width: 100vw }
.container p {
color: white;
position: absolute;
/* Same as bottom: 2rem; left: 2rem; */
inset: auto auto 2rem 2rem;
text-transform: uppercase;
font-family: 'Josefin Sans', sans-serif;
font-size: 2rem;
font-weight: 300;
/* Make sure the text wraps in the right place */
max-width: calc(100% - 4rem);
/* Forces text to be above before/after */
z-index: 3;
/* Linear transition here! */
/* White to black text can look out of sync otherwise */
transition: color 512ms 0ms linear;
}
.container:hover p { color: black }
.container:hover::before { opacity: 0.4 }
<div class="container">
<img id="image1" src="https://images.unsplash.com/photo-1451187580459-43490279c0fa?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1472&q=80" alt="Deep Earth">
<p id="p1">Deep earth</p>
</div>

Hover effect on CSS button [duplicate]

This question already has answers here:
Keep the pseudo element in between the background and main content
(1 answer)
Avoid z-index working relative to the parent element
(1 answer)
Closed 4 years ago.
I am trying create a hover effect using CSS. Here is the link: http://creativeartbd.com/demo/test.html
Here is the code:
/* GENERAL BUTTON STYLING */
button,
button::after {
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
-o-transition: all 0.3s;
transition: all 0.3s;
}
button {
background: none;
border: 3px solid red;
border-radius: 5px;
color: red;
display: block;
font-size: 1.6em;
font-weight: bold;
margin: 1em auto;
padding: 2em 6em;
position: relative;
text-transform: uppercase;
}
button::before,
button::after {
background:red;
content: '';
position: absolute;
z-index: -1;
}
button:hover {
color: black;
}
/* BUTTON 5 */
.btn-5 {
overflow: hidden;
}
.btn-5::after {
/*background-color: #f00;*/
height: 100%;
left: -35%;
top: 0;
transform: skew(50deg);
transition-duration: 0.6s;
transform-origin: top left;
width: 0;
}
.btn-5:hover:after {
height: 100%;
width: 135%;
}
<button class="btn-5">Button 5</button>
now if you run it you can see that there is style when you hover over the button. Now I want to set initial background for this button. So that IF I set the background here:
button {
background: orange;
}
If I do so then the effect is not showing.
Can you tell me why and how can I solve it?
JSFiddle
add z-index:0 to the element to create a stacking context and keep the pseudo element inside. You can then add background
/* GENERAL BUTTON STYLING */
button,
button::after {
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
-o-transition: all 0.3s;
transition: all 0.3s;
}
button {
background: none;
border: 3px solid red;
border-radius: 5px;
color: red;
display: block;
font-size: 1.6em;
font-weight: bold;
margin: 1em auto;
padding: 2em 6em;
position: relative;
z-index:0;
background:orange;
text-transform: uppercase;
}
button::before,
button::after {
background:red;
content: '';
position: absolute;
z-index: -1;
}
button:hover {
color: black;
}
/* BUTTON 5 */
.btn-5 {
overflow: hidden;
}
.btn-5::after {
/*background-color: #f00;*/
height: 100%;
left: -35%;
top: 0;
transform: skew(50deg);
transition-duration: 0.6s;
transform-origin: top left;
width: 0;
}
.btn-5:hover:after {
height: 100%;
width: 135%;
}
<button class="btn-5">Button 5</button>
You can also simplify your code like follow:
button {
background: none;
border: 3px solid red;
border-radius: 5px;
color: red;
display: block;
font-size: 1.6em;
font-weight: bold;
margin: 1em auto;
padding: 2em 6em;
background:
linear-gradient(50deg,red 50%,transparent 50.5%),
orange;
background-size:250% 100%;
background-position: right;
text-transform: uppercase;
transition: all 0.5s;
}
button:hover {
color: black;
background-position: left;
}
<button class="btn-5">Button 5</button>

Adding border on hover shifts surrounding elements

Just hover on 'a headline' in the snippet below and you will see how elements are moving. Why?
There's no margin .. And they're only moving when I add border to the inline-block element. Try to add more border width in section.twelve a like:
section.twelve a {
border-bottom: 10px solid #FFFAFF;
}
But if you remove the border everything's fine.. Why is this behavior ? and is it only for border?
I just want to add any styles to the element without effecting the others.
section{
position: relative;
height: 300px;
padding: 15px 80px;
z-index: 1;
}
section h1{
font-size:3em;
font-weight: 100;
line-height: 1.3;
}
section a {
position: relative;
display: inline-block;
text-decoration: none;
transition: all 0.3s ease-in-out;
vertical-align: bottom;
}
section.twelve {
background: #121A5A;
color: #FFFAFF;
}
section.twelve a {
color:#D8315B;
font-weight: 700;
overflow: hidden;
padding: 0px 5px;
transition all 0.2s ease;
border-bottom: 5px solid #FFFAFF;
}
.twelve a:before{
content: "";
top:0; left: 0;
position: absolute;
width:100%; height: 100%;
background: #FFFAFF;
z-index: -1;
transition: all 0.2s ease;
transform: translateX(100%);
}
.twelve a:hover::before {
transform: translateX(-95%);
background: #D8315B;
}
.twelve a:hover{
color: #FFFAFF;
transform: translateX(5px);
border-bottom: 1px solid #FFFAFF;
}
<section class="twelve">
<h1>Write a headline that makes people do kind of a double take whenthey read it.</h1>
</section>
When you add, or change the width, of a border, that changes the size of the element. Hence, by adding the border on hover, the box grows to occupy more space, which naturally shifts the position of surrounding text / elements.
One method to resolve this issue is to always have the border present, so the size of the box is fixed. When the border shouldn't be visible, it's transparent.
Here's an example:
section {
position: relative;
height: 300px;
padding: 15px 80px;
z-index: 1;
}
section h1 {
font-size: 3em;
font-weight: 100;
line-height: 1.3;
}
section a {
position: relative;
display: inline-block;
text-decoration: none;
transition: all 0.3s ease-in-out;
vertical-align: bottom;
}
section.twelve {
background: #121A5A;
color: #FFFAFF;
}
section.twelve a {
color: #D8315B;
font-weight: 700;
overflow: hidden;
padding: 0px 5px;
transition all 0.2s ease;
border-bottom: 5px solid transparent; /* ADJUSTMENT */
}
.twelve a:before {
content: "";
top: 0;
left: 0;
position: absolute;
width: 100%;
height: 100%;
background: #FFFAFF;
z-index: -1;
transition: all 0.2s ease;
transform: translateX(100%);
}
.twelve a:hover::before {
transform: translateX(-95%);
background: #D8315B;
}
.twelve a:hover {
color: #FFFAFF;
transform: translateX(5px);
border-bottom: 5px solid white; /* ADJUSED */
}
<section class="twelve">
<h1>Write a headline that makes people do kind of a double take whenthey read it.</h1>
</section>
Yes, on hover you are changing element's border, so, element's total height also changes

How to hide tooltip?

I am using css to create tooltip, 4 tooltips are placed sequentially, 2nd tooltip appears on the popup content of 1st tooltip
The following code is placed in between jsp file in liferay, every jsp file has the tooltip code in it. If I open the 1st tooltip, other tooltips appear on the popup content of 1st tooltip,
I need to show the popup contents without any interruption. How do I do that?
My code is
.help-tip {
position: absolute;
top: 34px;
right: 120px;
text-align: center;
background-color: #a3c2c2;
border-radius: 50%;
width: 14px;
height: 10px;
font-size: 10px;
line-height: 16px;
cursor: default;
}
.help-tip:before {
content: '?';
font-weight: normal;
font-size: 10px;
color: #fff;
}
.help-tip:hover p {
display: block;
transform-origin: 100% 0%;
-webkit-animation: fadeIn 0.3s ease-in-out;
animation: fadeIn 0.3s ease-in-out;
}
.help-tip p {
/* The tooltip */
display: none;
text-align: center;
/*background-color: #a3c2c2;*/
padding: 5px;
width: 170px;
position: absolute;
border-radius: 6px;
box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.4);
right: -100px;
/*color: #000000;*/
font-size: 14px;
line-height: 1.4;
color: #000000;
background: #FBF5E6;
background: -webkit-linear-gradient(top, #FBF5E6, #FFFFFF);
background: linear-gradient(top, #FBF5E6, #FFFFFF);
border: 1px solid #CFB57C;
}
.help-tip p:before {
/* The pointer of the tooltip */
position: absolute;
content: '';
width: 0;
height: 0;
border: 6px solid transparent;
border-bottom-color: #66ccff;
right: 100px;
top: -12px;
}
.help-tip p:after {
/* Prevents the tooltip from being hidden */
width: 100%;
height: 40px;
content: '';
position: absolute;
top: -40px;
left: 0;
}
/* CSS animation */
#-webkit-keyframes fadeIn {
0% {
opacity: 0;
transform: scale(0.6);
}
100% {
opacity: 100%;
transform: scale(1);
}
}
#keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 100%;
}
}
<div class="help-tip">
<p>This is the inline edit help tip!</p>
</div>
Due to using the exact same code for each tooltip, every tooltip will have the CSS property, position: absolute; and that could be causing the problem. Try changing all the positions to relative.