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
Related
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>
I am trying to position a close button (x) to the top-right corner of an image.
That's just before the footer of my page:
<div class="modal" id="modal">
<span class="close" id="close">✖</span>
<img class="modal-content" id="modal-content">
</div>
How I show the modal:
function showModal(name) {
modal.style.display = "block";
modalImg.src = document.getElementById(name).src;
}
And here is my styling:
/* Background when image is shown */
#modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(119, 119, 119); /* Fallback color */
background-color: rgba(119, 119, 119, 0.9); /* Black w/ opacity */
}
/* Image */
#modal-content {
/* Horizontally center image */
margin: auto;
/* Sizing */
display: block;
max-width: 90%;
max-height: calc(100vh * 0.5);
width: auto;
/* Zoom (image gets bigger) on open */
animation-name: zoom;
animation-duration: 0.6s;
/* Style */
border: 10px solid #ffffff;
box-shadow: rgba(0, 0, 0, 0.54) 0px 0px 7px 4px;
/* Vertically center image */
position: relative;
top: 40%;
transform: translateY(-40%);
}
#keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
/* Close Button */
#close {
/* Position */
position: absolute;
top: 10px;
right: 10px;
/* Style */
color: #ffffff;
font-size: 20px;
font-weight: bold;
transition: 0.3s;
background-color: #000000;
border: 3px solid #ffffff;
box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 25px 3px;
/* Makes the Button round */
border-radius: 50%;
padding-left: 7px;
padding-right: 7px;
padding-bottom: 3px;
}
/* Change cursor when pointing on button */
#close:hover,
#close:focus {
text-decoration: none;
cursor: pointer;
}
/* 100% image width on smaller screens */
#media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}
The problem is that
position: absolute;
top: 10px;
right: 10px;
positions the close button in the top-right corner of the page, not at the top right corner of the image.
How can I fix that?
The close element it's positioning relative to the closest positioned ancestor, in your case the modal div instead of the modal-content (as you wanted).
I recomend you to wrap either the modal-content and close "button" in a (relative) div, like this:
<div class="modal" id="modal">
<div id='modal-content' class='modal-content">
<span class="close" id="close">✖</span>
<img> //make img height/width to 100%
</div>
</div>
This should position the close button in the top-right position of the absolute div and the image inside this div.
If the close button doesn't appear, make sure it has a higher z-index than the image.
I made a codepen: https://codepen.io/jpaulet/pen/RwGxpxy
I tried making a sidenav, and wanted to make it a dropdown menu, but when I stop hovering the main item, the dropdown goes away.
Does anyone know how to solve my problem?
(also a notice, this can't be fixed by clicking the main item instead of hovering, because clicking the main item links to another page).
Here is my code:
/* Style the links inside the sidenav */
#mySidenav a {
position: absolute; /* Position them relative to the browser window */
left: -80px; /* Position them outside of the screen */
transition: 0.3s; /* Add transition on hover */
padding: 15px; /* 15px padding */
width: 100px; /* Set a specific width */
text-decoration: none; /* Remove underline */
font-size: 12px; /* Increase font size */
color: white; /* White text color */
border-radius: 0 5px 5px 0; /* Rounded corners on the top right and bottom right side */
}
/* The about link: 20px from the top with a green background */
.Home {
top: 20px;
background-color: #4CAF50;
}
#Home a:hover {
left: 0; /* On mouse-over, make the elements appear as they should */
}
.Info {
top: 80px;
background-color: #2196F3; /* Blue */
}
#Info a:hover {
left: 0; /* On mouse-over, make the elements appear as they should */
}
.Producten {
top: 140px;
background-color: #f44336; /* Red */
}
#Producten a:hover {
left: 0; /* On mouse-over, make the elements appear as they should */
}
#Producten a:hover ~ DropdownGroepstraining {
visibility: visible;
width: 100px;
height: 45px;
}
.DropdownGroepstraining {
top: 185px;
margin-left: 140px;
background-color: #f44336; /* Red */
display: none;
}
.DropdownPrivetraining {
top: 230px;
margin-left: 140px;
background-color: #f44336; /* Red */
display: none;
}
.DropdownTrajectopmaat {
top: 275px;
margin-left: 140px;
background-color: #f44336; /* Red */
display: none;
}
.Trainers {
top: 200px;
background-color: #555 /* Light Black */
}
#Trainers a:hover {
left: 0; /* On mouse-over, make the elements appear as they should */
}
.DropdownPieter {
top: 245px;
margin-left: 140px;
background-color: #555;
display: none;
}
.DropdownLaura {
top: 290px;
margin-left: 140px;
background-color: #555;
display: none;
}
.DropdownYannick {
top: 335px;
margin-left: 140px;
background-color: #555;
display: none;
}
.DropdownSander {
top: 380px;
margin-left: 140px;
background-color: #555;
display: none;
}
.DropdownNick {
top: 425px;
margin-left: 140px;
background-color: #555;
display: none;
}
.Contact {
top: 260px;
background-color: purple;
}
#Contact a:hover {
left: 0; /* On mouse-over, make the elements appear as they should */
}
.DropdownContactform {
top: 305px;
margin-left: 140px;
background-color: purple;
display: none;
}
.DropdownFacebook {
top: 350px;
margin-left: 140px;
background-color: purple;
display: none;
}
.DropdownInsta {
top: 395px;
margin-left: 140px;
background-color: purple;
display: none;
}
.FAQ {
top: 320px;
background-color: rgba(245, 240, 15, 0.7)
}
#FAQ a:hover {
left: 0; /* On mouse-over, make the elements appear as they should */
}
a:hover ~ a {
display: block;
}
<nav>
<div id="mySidenav" class="sidenav">
<div id="Home">Home</div>
<div id="Info">Info</div>
<div id="Producten">Producten
Groepstraining
Privétraining
Traject op maat
</div>
<div id="Trainers">Trainers
Pieter Geerts
Laura Van Baekel
Yannick Maes
Sander Geerts
Nick Van Camp
</div>
<div id="Contact">Contact
Contactformulier
<img id="navfacebooklogo" src="images/facebooklogo.png" />Facebook
<img id="navinstagramlogo" src="images/instagramlogo.png" />Instagram
</div>
<div id="FAQ">FAQ</div>
</div>
</nav>
Does anyone know how to solve my problem?
(also a notice, this can't be fixed by clicking the main item instead of hovering, because clicking the main item links to another page).
Thanks for your time,
Tom
The problem is you are selecting the hover of anchor tag which is sibling of the sub menu.
change your
#Producten a:hover {
to
#Producten:hover a {
This way, you are selecting the hover of the parent, so the menu will be visible when hovering both menu and sub menu
My side-nav appears and functions perfectly in Chrome but not in Safari. Trying to find the code that is making it break. Here are some photos that show the two side-navs. Chrome is the one that works and Safari is the blank one.
I've already tried going into dev tools on both Safari and Chrome to check if any CSS styling attributes have been crossed out but they all seem to be there.
<div id="mySidenav" class="sidenav custom-underline">
×
<div class="nav-items">
Home
Videos
Art
Graphic Design
Contact
</div>
</div>
and CSS
.nav-items a:hover{
background-color: rgb(214, 0, 0);
}
i{
color: white;
}
/* The side navigation menu */
.sidenav {
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; /* Stay at the top */
left: 0;
background-color: rgb(0, 0, 0); /* 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 sidenav */
}
/* The navigation menu links */
.sidenav a {
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 */
.sidenav a:hover {
color: #f1f1f1;
}
/* Position and style the close button (top right corner) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
.nav-logo{
height: 200px;
display: block;
margin: auto;
margin-bottom: 50px;
}
.closebtn{
margin-top: 8px;
}
#video-icon{
float:left;
margin-right:10px;
position: relative;
top:9px;
}
/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
transition: margin-left .5s;
padding: 20px;
color: black;
background-color: rgb(0, 0, 0);
height: 200px;
}
/* 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) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
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>