Pure CSS Animating Mobile menu activated using radiobutton - html

I have a Mobile menu that should be forming X when clicked, however it would not respond and is stuck on its shape. I don't know why it acted that why, but as far as I have analyzed, this code should animate the Menu to form X when clicked. I am attaching the code for reference.
These are the CSS and HTML
/* Checkbox Hack */
input[type=checkbox] {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
/* Default State */
.divko {
background: green;
width: 400px;
height: 100px;
line-height: 100px;
color: white;
text-align: center;
}
/* Toggled State */
.myspan {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}
.myspan:first-child {
transform-origin: 0% 0%;
}
.myspan:nth-last-child(2) {
transform-origin: 0% 100%;
}
input[type=checkbox]:checked ~ .myspan {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}
input[type=checkbox]:checked ~ .divko {
background: red;
}
<div for="toggle-1"> <span class="myspan"></span>
<span class="myspan"></span>
<span class="myspan"></span></div>
<input type="checkbox" id="toggle-1">
<div class="divko">I'm controlled by toggle. No JavaScript!</div>
Here is the dabblet
http://dabblet.com/gist/5b4667ecc7255c228cd488c080140d95

Put the checkbox before the div I've give the .menu class to, and apply the rule on input[type=checkbox]:checked ~ .menu > .myspan.
/* Checkbox Hack */
input[type=checkbox] {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
/* Default State */
.divko {
background: green;
width: 400px;
height: 100px;
line-height: 100px;
color: white;
text-align: center;
}
/* Toggled State */
.myspan {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 2px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}
input[type=checkbox]:checked ~ .menu > .myspan {
opacity: 1;
background: #232323;
}
input[type=checkbox]:checked ~ .menu > .myspan:first-child {
transform: rotate(45deg);
}
input[type=checkbox]:checked ~ .menu > .myspan:nth-child(2) {
transform: translate(-100px);
}
input[type=checkbox]:checked ~ .menu > .myspan:last-child {
transform: rotate(-45deg);
}
input[type=checkbox]:checked ~ .divko {
background: red;
}
<input type="checkbox" id="toggle-1">
<div for="toggle-1" class="menu">
<span class="myspan"></span>
<span class="myspan"></span>
<span class="myspan"></span>
</div>
<div class="divko">I'm controlled by toggle. No JavaScript!</div>

Related

How do I make a element visible when checkbox is checked using CSS only [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 7 months ago.
I have a navigation bar and a background (class="modal") which are set to
display: none;
How do I make it
display: block;
when the input checkbox menu-check is checked using only CSS.
[I have only shown the class modal since both navigation and modal classes act similarly]
.modal {
display: none;
position: absolute;
height: 100%;
top: 0;
left: 0;
right: 0;
background: #00827f;
}
.menu-btn {
position: fixed;
display: flex;
flex-direction: column;
gap: 5px;
bottom: 20px;
right: 20px;
padding: 15px;
border-radius: 20px;
z-index: 10;
background-color: #08ccc9;
cursor: pointer;
}
.menu-btn:hover>span:nth-of-type(1) {
transform-origin: right;
transform: scaleX(0.5);
}
.menu-btn:hover>span:nth-of-type(3) {
transform-origin: left;
transform: scaleX(0.5);
}
.menu-btn input {
display: none;
}
.menu-btn span {
width: 25px;
height: 4px;
border-radius: 999px;
background-color: black;
transition: all 0.3s ease;
}
.menu-btn input:checked~span:nth-of-type(1) {
transform: rotate(45deg) translate(5px, 7px);
transform-origin: center;
transition: all 0.3s ease, transform-origin 0s;
}
.menu-btn input:checked~span:nth-of-type(2) {
transform: translate(30px, 0px);
opacity: 0;
}
.menu-btn input:checked~span:nth-of-type(3) {
transform: rotate(-45deg) translate(5px, -7px);
transform-origin: center;
transition: all 0.3s ease, transform-origin 0s;
}
<label for="menu-check" class="menu-btn">
<input type="checkbox" id="menu-check">
<span></span>
<span></span>
<span></span>
</label>
<div class="modal"></div>
You need to change your HTML so that the checkbox is at the same level in the DOM as the modal. You can only select subsequent or child elements, not previous or ancestral elements.
#menu-check {display:none;}
.modal {
display: none;
position: absolute;
height: 100%;
top: 0;
left: 0;
right: 0;
background: #00827f;
}
.menu-btn {
position: fixed;
display: flex;
flex-direction: column;
gap: 5px;
bottom: 20px;
right: 20px;
padding: 15px;
border-radius: 20px;
z-index: 10;
background-color: #08ccc9;
cursor: pointer;
}
.menu-btn:hover>span:nth-of-type(1) {
transform-origin: right;
transform: scaleX(0.5);
}
.menu-btn:hover>span:nth-of-type(3) {
transform-origin: left;
transform: scaleX(0.5);
}
.menu-btn span {
width: 25px;
height: 4px;
border-radius: 999px;
background-color: black;
transition: all 0.3s ease;
}
/*Check Box CSS Manipulation*/
#menu-check:checked ~ .modal{
display:block;
}
#menu-check:checked ~ .menu-btn span:nth-of-type(1) {
transform: rotate(45deg) translate(5px, 7px);
transform-origin: center;
transition: all 0.3s ease, transform-origin 0s;
}
#menu-check:checked ~ .menu-btn span:nth-of-type(2) {
transform: translate(30px, 0px);
opacity: 0;
}
#menu-check:checked ~ .menu-btn span:nth-of-type(3) {
transform: rotate(-45deg) translate(5px, -7px);
transform-origin: center;
transition: all 0.3s ease, transform-origin 0s;
}
<input type="checkbox" id="menu-check">
<label for="menu-check" class="menu-btn">
<span></span>
<span></span>
<span></span>
</label>
<div class="modal"></div>

Burger menu not going to correct position

I have made a burger menu for my site but the dropdown menu/ element does not go inside the menu icon. It moves the wrong way. It goes from left to right but I need it to go from right to left. I can't figure out how to make the menu move the other way. I have tried to tweak some of the tags but either it's not worked or it's broken.
nav {
position: absolute;
top: -2%;
left: 93%;
}
#menuToggle {
display: block;
position: relative;
top: 50px;
left: 50px;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}
#menuToggle a {
text-decoration: none;
color: #232323;
transition: color 0.3s ease;
}
#menuToggle a:hover {
color: tomato;
}
#menuToggle input {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
#menuToggle span {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #ffffff;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}
#menuToggle span:first-child {
transform-origin: 0% 0%;
}
#menuToggle span:nth-last-child(2) {
transform-origin: 0% 100%;
}
#menuToggle input:checked~span {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}
#menuToggle input:checked~span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
#menuToggle input:checked~span:nth-last-child(2) {
transform: rotate(-45deg) translate(0, -1px);
}
#menu {
position: absolute;
width: 300px;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;
background: #ffffff;
list-style-type: none;
-webkit-font-smoothing: antialiased;
transform-origin: 0% 0%;
transform: translate(-100%, 0);
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0);
}
#menu li {
padding: 10px 0;
font-size: 22px;
}
#menuToggle input:checked~ul {
transform: none;
}
<nav role="navigation">
<div id="menuToggle">
<input type="checkbox" />
<span></span>
<span></span>
<span></span>
<ul id="menu">
<li>Home</li>
<li>About us</li>
<li>Rules</li>
<li>Support us</li>
</ul>
</div>
</nav>
any help is greatly appreciated, thank you

The page shows hidden items off the screen in mobile mode

I trying to resolve it, but i dont know that problems is with menu, or icon of chat(using tawk.to)
The page is also online on www.niebieskiczat.pl
I never had this problem, but i think i change something and now i got this like that, or maybe tawk.to change somethink? The hamburger menu should appear on the regular website and on the mobile. Now on PC working god, but on mobile i got problem like on picture. Here is code for CSS navbar
.menuToggle {
display: block;
position: absolute;
top: 40px;
right: 120px;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}
.menuToggle input {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
.menuToggle span {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1),
background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1), opacity 0.55s ease;
}
.menuToggle span:first-child {
transform-origin: 0% 0%;
}
.menuToggle span:nth-last-child(2) {
transform-origin: 0% 100%;
}
.menuToggle input:checked ~ span {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}
.menuToggle input:checked ~ span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
.menuToggle input:checked ~ span:nth-last-child(2) {
opacity: 1;
transform: rotate(-45deg) translate(0, -1px);
}
.menu {
position: absolute;
width: 300px;
margin: -80px 0 0 0;
padding: 50px;
padding-top: 105px;
right: -150px;
background: lightblue;
list-style-type: none;
-webkit-font-smoothing: antialiased;
transform-origin: 0% 0%;
transform: translate(100%, 0);
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1);
}
.menu li {
padding: 10px 0;
font-size: 1.2em;
}
.menuToggle input:checked ~ ul {
transform: none;
opacity: 1;
}
#media screen and (max-width: 768px) {
#menu {
transform: none;
opacity: 0;
transition: opacity 0.5s cubic-bezier(0.77, 0.2, 0.05, 1);
}
}
And HTML
<nav role='navigation'>
<div class="menuToggle">
<input type="checkbox" />
<span></span>
<span></span>
<span></span>
<ul class="menu">
<a href="pages/about.html">
<li>O nas</li>
</a>
<a href="pages/howtotalk.html">
<li>Jak rozmawiać?</li>
</a>
<a href="pages/contacts.html">
<li>Przydatne kontakty</li>
</a>
</ul>
</div>
</nav>
Add this to your menu class.
It will solve your problem.
.menu {
position: fixed;
right: 0;
}
try
#media and screen and (max-width:766px){ .menu{ display:none; } }

close checkbox hamburger menu, when clicked outside without javascript/jquery possibly

i have hamburger menu which i took from here. https://codepen.io/erikterwan/pen/EVzeRP
it is using only css by using a checkbox to expand and collapse the menu.
so what i want is when i click outside the menu (anywhere on the page), the menu should collapse. can someone help me.
this is the html code
<nav role="navigation">
<div id="menuToggle">
<input type="checkbox" />
<span></span>
<span></span>
<span></span>
<ul id="menu">
<li>Home</li>
<li>About</li>
<li>Info</li>
<li>Contact</li>
<li>Show me more</li>
</ul>
</div>
</nav>
this is the css code
body
{
margin: 0;
padding: 0;
background: #232323;
color: #cdcdcd;
font-family: "Avenir Next", "Avenir", sans-serif;
}
#menuToggle
{
display: block;
position: relative;
top: 50px;
left: 50px;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}
#menuToggle a
{
text-decoration: none;
color: #232323;
transition: color 0.3s ease;
}
#menuToggle a:hover
{
color: tomato;
}
#menuToggle input
{
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
#menuToggle span
{
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
opacity 0.55s ease;
}
#menuToggle span:first-child
{
transform-origin: 0% 0%;
}
#menuToggle span:nth-last-child(2)
{
transform-origin: 0% 100%;
}
#menuToggle input:checked ~ span
{
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}
#menuToggle input:checked ~ span:nth-last-child(3)
{
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
#menuToggle input:checked ~ span:nth-last-child(2)
{
transform: rotate(-45deg) translate(0, -1px);
}
#menu
{
position: absolute;
width: 300px;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;
background: #ededed;
list-style-type: none;
-webkit-font-smoothing: antialiased;
transform-origin: 0% 0%;
transform: translate(-100%, 0);
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}
#menu li
{
padding: 10px 0;
font-size: 22px;
}
#menuToggle input:checked ~ ul
{
transform: none;
}
You could use a label that expands to the whole width and height, and positioned under the nav: <label for="checkbox-id"></label>. You would need to add an id to your checkbox. Make sure to set the label background to transparent.
EDIT: You would show the label only when #checkbox-id is checked. This can be achieved using only CSS. You might need to restructure your HTML a little bit. TBH, your implementation can be much simpler than in the codepen you're drawing inspiration from. Please let me know in a comment if you want more details.
What have i done is i detected a click outside an element and setting the checkbox to false.
$(document).ready(function() {
$(document).on("click", function(event) {
if ($(event.target).closest("#checkbox").length === 0) {
$("#checkbox").prop("checked", false);
}
});
});

Checkbox CSS :checked styling

I'm trying to understand where the problem in my CSS is, but I really don't have any clue what is wrong.
Basically, I'm styling the default checkbox style with CSS. It works well until you try to check the checkbox.
The idea behind my code is when you check the checkbox, it should increase the size and change the background colour to red. In addition, it should keep the style till you unchecked the box (manually).
Do you have any idea where the problem is? In my opinion the problem is where the "input[type="checkbox"]:checked .check-box-effect {" begins.
Please find the code below
label {
display: inline-block;
color: #fff;
cursor: pointer;
position: relative;
}
label .check-box-effect {
display: inline-block;
position: relative;
background-color: transparent;
width: 25px;
height: 25px;
border: 2px solid #dcdcdc;
border-radius: 10%;
}
label .check-box-effect:before {
content: "";
width: 0px;
height: 2px;
border-radius: 2px;
background: #626262;
position: absolute;
transform: rotate(45deg);
top: 13px;
left: 9px;
transition: width 50ms ease 50ms;
transform-origin: 0% 0%;
}
label .check-box-effect:after {
content: "";
width: 0;
height: 2px;
border-radius: 2px;
background: #626262;
position: absolute;
transform: rotate(305deg);
top: 16px;
left: 10px;
transition: width 50ms ease;
transform-origin: 0% 0%;
}
label:hover .check-box-effect:before {
width: 5px;
transition: width 100ms ease;
}
label:hover .check-box-effect:after {
width: 10px;
transition: width 150ms ease 100ms;
}
input[type="checkbox"] {
display: none;
}
input[type="checkbox"]:checked .check-box-effect {
background-color: red !important;
transform: scale(1.25);
}
input[type="checkbox"]:checked .check-box-effect:after {
width: 10px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked .check-box-effect:before {
width: 5px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked:hover .check-box-effect {
background-color: #dcdcdc;
transform: scale(1.25);
}
input[type="checkbox"]:checked:hover .check-box-effect:after {
width: 10px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked:hover .check-box-effect:before {
width: 5px;
background: #333;
transition: width 150ms ease 100ms;
}
<label>
<input type="checkbox" id="chkProdTomove" />
<span class="check-box-effect"></span>
</label>
You need to change all checked selectors to this:
input[type="checkbox"]:checked + .check-box-effect:before
label {
display: inline-block;
color: #fff;
cursor: pointer;
position: relative;
}
label .check-box-effect {
display: inline-block;
position: relative;
background-color: transparent;
width: 25px;
height: 25px;
border: 2px solid #dcdcdc;
border-radius: 10%;
}
label .check-box-effect:before {
content: "";
width: 0px;
height: 2px;
border-radius: 2px;
background: #626262;
position: absolute;
transform: rotate(45deg);
top: 13px;
left: 9px;
transition: width 50ms ease 50ms;
transform-origin: 0% 0%;
}
label .check-box-effect:after {
content: "";
width: 0;
height: 2px;
border-radius: 2px;
background: #626262;
position: absolute;
transform: rotate(305deg);
top: 16px;
left: 10px;
transition: width 50ms ease;
transform-origin: 0% 0%;
}
label:hover .check-box-effect:before {
width: 5px;
transition: width 100ms ease;
}
label:hover .check-box-effect:after {
width: 10px;
transition: width 150ms ease 100ms;
}
input[type="checkbox"] {
display: none;
}
input[type="checkbox"]:checked + .check-box-effect {
background-color: red !important;
transform: scale(1.25);
}
input[type="checkbox"]:checked + .check-box-effect:after {
width: 10px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked + .check-box-effect:before {
width: 5px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked:hover + .check-box-effect {
background-color: #dcdcdc;
transform: scale(1.25);
}
input[type="checkbox"]:checked:hover + .check-box-effect:after {
width: 10px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked:hover + .check-box-effect:before {
width: 5px;
background: #333;
transition: width 150ms ease 100ms;
}
<label>
<input type="checkbox" id="chkProdTomove" />
<span class="check-box-effect"></span>
</label>
You need to target the next element when the checkbox is checked:
input[type="checkbox"]:checked + .check-box-effect {
background-color: red !important;
transform: scale(1.25);
}
Your current code is trying to target the .check-box-effect if it would be a child of the checkbox.