Unable to focus on rectangle outside of toggle switch on selecting and I wanted to add the functionality to focus on the label class which is used for outside rectangle of toggle switch, but was completely stuck. I am trying to do, is if the switch is pressed, then focus should be on rectangle box. I am using below HTML and CSS. Any help appreciated.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.switch {
position: relative;
border: 1px solid rgb(112, 112, 112);
display: flex;
align-items: center;
width: 280px;
height: 70px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
left:234px;
position: absolute;
cursor: pointer;
top: 20px;
right: 0;
bottom: 0;
height: 24px;
width: 40px;
background-color: grey;
-webkit-transition: 4s;
transition: 4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 18px;
width: 18px;
left: 4px;
bottom: 4px;
background-color: white;
border-radius: 50%;
}
input:checked + .slider {
background-color: #2196F3;
}
.switch:hover{
box-shadow: 0 0 10px red;
}
.switch:active{
border-radius: 5 10px;
}
.switch:focus{
background-color: #0064D2;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(100%);
}
.switch label {
position: absolute;
left:2px;
width: 234px;
height: 22px;
color: rgb(65, 65, 65);
font-size: 16px;
font-weight: normal;
letter-spacing: 0.15px;
line-height: 22px;
}
</style>
</head>
<body>
<h2>Toggle Switch</h2>
<label class="switch">
<label for="toggle-switch"
>Switch label
</label>
<input type="checkbox" id="toggle-switch">
<span class="slider round"></span>
</label>
</body>
</html>
Related
I'm designing an element for a plugin called elementor. This project is really just to help me learn the functionality of developing for wordpress.
What I'm making is a "toggle content" slider that can toggle between text or predefined html. I've used the slider according to this guide: https://www.w3schools.com/howto/howto_css_switch.asp
Right Now the size of a switch is very big. I want a small switch. How Can change it? Can anyone help me out? Thanks.
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
</style>
</head>
<body>
<h2>Toggle Switch</h2>
<label class=" switch">
<input type="checkbox" checked>
<span class=" slider round"></span>
</label>
</body>
</html>
I used calc(); and var();, so you don't have to do a lot of work
Mozilla documentation: calc() explanation and var() explanation and --var: ; explanation .
just change ONE value the height of switch for change all responsively!!!
* {
--switch-height: 34px; /* change the value */
/* other code, pls see it then */
}
if you want a smaller switch, the 8px default padding, will take a lot of space,
so I simplified it for you! just change the --switch-padding: 8px; to something smaller...
automatically CSS calculates all things for you, for making the switch look good for all dimensions :)
the same thing if you want the switch to be bigger, remember to make the padding also bigger (the padding var)
I know This is not a site "we-are-doing-your-work.com" but I really want to help you!
here the complete fixed code:
* {
--switch-height: 34px;
--switch-padding: 8px;
--switch-width: calc((var(--switch-height) * 2) - var(--switch-padding));
--slider-height: calc(var(--switch-height) - var(--switch-padding));
--slider-on: calc(var(--switch-height) - var(--switch-padding));
}
.switch {
position: relative;
display: inline-block;
width: var(--switch-width);
height: var(--switch-height);
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
content: "";
position: absolute;
height: var(--slider-height);
width: var(--slider-height);
left: calc(var(--switch-padding) / 2);
bottom: calc(var(--switch-padding) / 2);
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked+.slider {
background-color: #2196F3;
}
input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked+.slider:before {
transform: translateX(var(--slider-on));
}
.slider.round {
border-radius: var(--slider-height);
}
.slider.round:before {
border-radius: 50%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>Toggle Switch</h2>
<label class="switch">
<input type="checkbox" checked>
<span class="slider round"></span>
</label>
</body>
</html>
* {
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.label{
background-color: #fea621;
display: flex;
border-radius: 50px;
height: 26px;
width: 50px;
align-items: center;
justify-content: space-between;
padding: 5px;
position: relative;
}
.checkbox{
opacity: 0;
position: absolute;
}
.checkbox + .label .ball{
content: url(323329.png);
background-color: #001e3d;
}
.checkbox:checked + .label .ball{
content: url(flag-ukraine-circle.svg);
background-color: #001e3d;
transform: translateX(24px);
}
.ball{
background-color: #001e3d;
border-radius: 50%;
height: 22px;
width: 22px;
position: absolute;
top: 2px;
left: 2px;
transition: transform .2s linear;
}
.ball:before:checked{
content: url(flag-ukraine-circle.svg);
left: 50px;
}
.ball:checked:after{
left: 0;
}
<div>
<input type="checkbox" id="checkbox" class="checkbox">
<label for="checkbox" class="label">
<i class="ukraine"></i>
<i class="usa"></i>
<div class="ball"></div>
</label>
</div>
You can change the size by changing the css
.switch {
position: relative;
display: inline-block;
width: 35px;
height: 22px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 10px;
width: 10px;
left: 4px;
bottom: 6px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(16px);
-ms-transform: translateX(16px);
transform: translateX(16px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
</style>
</head>
<body>
<h2>Toggle Switch</h2>
<label class=" switch">
<input type="checkbox" checked>
<span class=" slider round"></span>
</label>
</body>
</html>
try to experiment with different widths and heights
This question already has answers here:
'transform3d' not working with position: fixed children
(10 answers)
Closed 3 years ago.
I'm trying to position an absolutely position ::before of an element with respect to its relatively positioned grandparent. It's parent is not positioned, but it's top and left respect the parent and not the grandparent.
* {
box-sizing: border-box;
}
::-webkit-input-placeholder,
::-moz-placeholder {
color: #9b9b9b;
}
.field {
height: 43px;
display: flex;
flex-direction: column-reverse;
position: relative;
}
input,
label {
display: block;
}
input {
font-size: 14px;
border: 0;
border-bottom: 1px solid #F2F2F2;
border-radius: 0;
padding: 3px 0 10px 0;
background-color: transparent;
}
input:focus {
outline: 0;
border-bottom: 1px solid #D66C9C;
}
label {
font-size: 12px;
color: #9b9b9b;
transform: translateY(20px);
transition: all 0.2s;
}
input:focus + label {
color: #808080;
transform: translateY(0);
}
input:focus + label::before {
content: "";
width: 5px;
height: 5px;
display: block;
background-color: #d66c9c;
transform: rotate(45deg) translateY(-50%);
position: absolute;
left: 0;
bottom: 0;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="field">
<input type="text" id="fullname">
<label for="fullname">Name</label>
</div>
</body>
</html>
* {
box-sizing: border-box;
}
::-webkit-input-placeholder,
::-moz-placeholder {
color: #9b9b9b;
}
.field {
height: 43px;
display: flex;
flex-direction: column-reverse;
position: relative;
}
input,
label {
display: block;
}
input {
font-size: 14px;
border: 0;
border-bottom: 1px solid #F2F2F2;
border-radius: 0;
padding: 3px 0 10px 0;
background-color: transparent;
}
input:focus {
outline: 0;
border-bottom: 1px solid #D66C9C;
}
label {
font-size: 12px;
color: #9b9b9b;
transform: translateY(20px);
transition: all 0.2s;
position:relative;
}
input:focus + label {
color: #808080;
transform: translateY(0);padding:0 0 0 5px;
}
input:focus + label::before {
content: "";
width: 5px;
height: 5px;
display: block;
background-color: #d66c9c;
transform: rotate(45deg) translateY(-50%);
position: absolute;
left: -8px;
bottom:5px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="field">
<input type="text" id="fullname">
<label for="fullname">Name</label>
</div>
</body>
</html>
check this hope so this is working for you
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to move the label above the input field when focus like this.
But in my other code, it only changes font-size and is not moving up. What am I missing?
I use the same CSS code for both:
CSS
input:focus + label > span {
font-size: 12px;
transform: translate3d(0, -80%, 0);
transition-timing-function: cubic-bezier(0.2, 1, 0.3, 1);
}
Target The Span Within The Lable
If you target the span contained in the label, you can control it with the below code.
input + label > span {
position: relative;
display: block;
bottom: -1em
}
Your code needs a lot of work, but start here:
body {
background-color: #f0f0f0;
}
form {
margin-left: 40%;
margin-top: 11%;
}
label {
font-family: monospace; 'Montserrat', sans-serif;
font-size: 19px;
margin-left: -26%;
z-index: -1;
/* border: 5px solid black;*/
}
.head {
margin-left: 43%;
margin-top: 5%;
font-size: 25px;
font-family: monospace;
}
form {
position: relative; }
.input-field {
display:block;
background: transparent;
border-width: 2px;
border-top: 0;
border-left: 0;
border-right: 0;
border: 0;
background: transparent;
outline: 0;
height: 23px;
border-color: black;
width: 130px;
z-index: 1;
margin-left: 0%;
}
/*input:focus {
transform: scale3d(1, 1, 1);
transition: width 5s;
transition-delay: 3s;
transition-timing-function: cubic-bezier(0.2, 1, 0.3, 1);
z-index: -1;
background: green; /* ::after background *
} */
label::after {
content: '';
position: absolute;
top: 100%;
left: 0;
width: 30%;
height: 20%;
transition: transform 0.8s;
}
label::after {
z-index: -1;
background: green; /* ::after background */
transform: scale3d(1, 0.1, 1);
transform-origin: 0% 0%;
}
input:focus + label::after {
transform: scale3d(1, -2, 1);
transition-timing-function: linear;
}
input:focus + label > span {
font-size: 14px;
transform: translate3d(0, -200%, 0);
transition-timing-function: cubic-bezier(0.2, 1, 0.3, 1);
}
input + label > span {
position: relative;
display: block;
bottom: -1em
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<title>Test</title>
</head>
<body>
<p class="head">Sign In</p>
<form>
<div class="username">
<input type="text" name="name" class="input-field" id="user"/> <!--input filed-->
<label for="user">
<span class="username">Username</span>
</label>
</div>
</form>
</body>
</html
Filtered Code. You can Use This Code. This is working Fine.
.object {
position: relative;
z-index: 1;
display: inline-block;
width: 300px; /* input field width */
}
.input {
position: relative;
display: block;
float: right;
width: 30%;
border: none;
background: black;
color: yellow; /* font color when typing */
font-family: monospace;
/* for box shadows to show on iOS */
}
.input:focus {
outline: none;
}
.label {
display: inline-block;
color: black; /* label font color */
font-size: 18px;
font-family: monospace;
}
.label-content {
position: relative;
display: block;
padding: .6em 0;
}
.input--jiro {
padding: 0.85em 0.5em;
width: 100%; /* input width when focus */
background: blue ; /* input background color */
opacity: 0;
transition: opacity 0.3s;
}
.label-content--jiro {
transition: transform 0.3s 0.3s;
}
.label--jiro {
position: absolute;
left: 0;
padding: 0 0.85em;
width: 120%; /* underline width */
height: 100%;
}
label::after {
content: '';
position: absolute;
top: 100%;
left: 0;
width: 80%;
height: 100%;
transition: transform .8s;
}
label::after {
z-index: -1;
background: green; /* ::after background */
transform: scale3d(1, 0.1, 1);
transform-origin: 0% 0%; /* 0% 50%; * for middle */
}
input:focus + label::after {
transform: scale3d(1, -1, 1);
transition-timing-function: linear;
}
input:focus + label > span {
font-size: 12px;
transform: translate3d(0, -80%, 0);
transition-timing-function: cubic-bezier(0.2, 1, 0.3, 1);
}
<p class="head">Sign In</p>
<section>
<span class="object">
<input class="input input--jiro" type="text" id="input-10" /> <!-- input field -->
<label class="label label--jiro" for="input-10">
<span class="label-content label-content--jiro">Username</span> <!-- Label -->
</label>
</span>
</section>
There's a little royalblue background appearing (between border-bottom: 2px solid white; & ::after element) when I unfocus.
I don't know why is that happening.
body {
background-color: royalblue;/*#f0f0f0;*/
}
label {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
z-index: -1;
border: 0;
color: white;
position: relative;
}
.head {
color: white;
margin-left: 44%;
font-family: monospace;
font-size: 25px;
}
/*.content {
margin-top: 10%;
margin-left: 41%;
}*/
.password {
margin-top: 8%;
}
form {
position: relative;
}
input {
position: absolute;
font-family: 'Montserrat', sans-serif;
font-size: 15px;
background: transparent;
border: 0; /* BORDER yes/no */
border-bottom: 2px solid white;
background: transparent;
outline: 0;
height: 25px;
width: 180px;
z-index: 1;
margin-top: 5px;
}
/*input:focus {
outline: 1;
}*/
label::after {
content: '';
position: absolute;
top: -2px;
left: 0;
width: 180px;
height: 25px;
border-radius: 2px;
transition: transform .3s;
}
label::after{
z-index: -1;
background: beige; /*#a86bf;*/
transform: scale3d(1, 0, 1);
transform-origin: 0% 0%;
}
input:focus {
border-radius: 1px;
}
input:focus + label::after,
input:valid + label::after {
transform: scale3d(1, -1, 1);
transition-timing-function: linear;
}
span {
position: relative;
margin-top: -30px;
display: block;
padding: .6em 0;
padding-left: -5px;
transition: top .5s ease, font-size .5s ease;
/* transition: transform 1s 2s;*/
top: 0;
}
input:focus + label > span,
input:valid + label > span {
top: -20px;
font-size: 11px;
padding-bottom: 15px;
}
/* font-family: monospace;*/
/*transform: translate3d(0, -80%, 0); */
/* transition-timing-function: linear;*/
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<title>Test</title>
</head>
<body>
<p class="head">Sign In</p>
<form>
<div class="content">
<div class="username">
<input type="text" name="name" class="user-input" id="user" required /> <!--input field-->
<label class="user-label" for="user"><span>Username</span></label>
</div>
<div class="password">
<input type="text" name="name" class="pass-input" id="pass" required /> <!-- input field -->
<label class="pass-label" for="pass"><span>Password</span></label>
</div>
</div>
</form>
</body>
</html>
Update:
I changed to top: 1px;, also change border-bottom to black, now this is the result (full code). Why is there some excess scale at the bottom? how to remove those?
You have a top: -2px on label, and your problem is when you blur, so a solution is move top: -2px to input:focus + label::after and input:focus + label::after, so you dont have that gap when its not focused or validated
body {
background-color: royalblue;/*#f0f0f0;*/
}
label {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
z-index: -1;
border: 0;
color: white;
position: relative;
}
.head {
color: white;
margin-left: 44%;
font-family: monospace;
font-size: 25px;
}
/*.content {
margin-top: 10%;
margin-left: 41%;
}*/
.password {
margin-top: 8%;
}
form {
position: relative;
}
input {
position: absolute;
font-family: 'Montserrat', sans-serif;
font-size: 15px;
background: transparent;
border: 0; /* BORDER yes/no */
border-bottom: 2px solid white;
background: transparent;
outline: 0;
height: 25px;
width: 180px;
z-index: 1;
margin-top: 5px;
}
/*input:focus {
outline: 1;
}*/
label::after {
content: '';
position: absolute;
left: 0;
width: 180px;
height: 25px;
border-radius: 2px;
transition: transform .3s;
}
label::after{
z-index: -1;
background: beige; /*#a86bf;*/
transform: scale3d(1, 0, 1);
transform-origin: 0% 0%;
}
input:focus {
border-radius: 1px;
}
input:focus + label,
input:valid + label {
top: -2px;
}
input:focus + label::after,
input:valid + label::after {
transform: scale3d(1, -1, 1);
transition-timing-function: linear;
}
span {
position: relative;
margin-top: -30px;
display: block;
padding: .6em 0;
padding-left: -5px;
transition: top .5s ease, font-size .5s ease;
top: 0;
}
input:focus + label > span,
input:valid + label > span {
top: -20px;
font-size: 11px;
padding-bottom: 15px;
}
/* font-family: monospace;*/
/*transform: translate3d(0, -80%, 0); */
/* transition-timing-function: linear;*/
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<title>Test</title>
</head>
<body>
<p class="head">Sign In</p>
<form>
<div class="content">
<div class="username">
<input type="text" name="name" class="user-input" id="user" required /> <!--input field-->
<label class="user-label" for="user"><span>Username</span></label>
</div>
<div class="password">
<input type="text" name="name" class="pass-input" id="pass" required /> <!-- input field -->
<label class="pass-label" for="pass"><span>Password</span></label>
</div>
</div>
</form>
</body>
</html>
you have an extra code, you can see here in comment section
label::after {
content: '';
position: absolute;
/*top: -2px;*/
left: 0;
width: 180px;
height: 25px;
border-radius: 2px;
transition: transform .3s;
}
Add top:1px on label::after See Below.
label::after {
content: '';
position: absolute;
left: 0;
top: 1px;
width: 180px;
height: 25px;
border-radius: 2px;
transition: transform .3s;
}
I found a similar question to mine here but the accepted answer did not fix my issue.
Bring element to front using CSS
I have made custom check boxes and I am trying to bring them to the front. Here is my html:
<!DOCTYPE HTML>
<html>
<head>
<link href="stylesheet.css" rel="stylesheet" type="text/css">
<script src="javascript.js"></script>
</head>
<body>
<div id="Title">
<img src="" alt="WifiFinder.img" id="WifiFinderImg">
<input type="button" class="Headerbuttons" id="back" value="Actually, I already have an account!" onclick="document.location='searchpage.html';"/>
</div>
<div id="AccountRegistration">
<h2>Username</h2>
<input class="userinfo" type="text" value="Username Here"/>
<h2>Email Address</h2>
<input class="userinfo" type="email" value="Email Address"/>
<h2>Date of Birth</h2>
<input class="userinfo" type="date" value="DD/MM/YYYY"/>
<h2>Password</h2>
<input class="userinfo" type="password" value="Password"/>
<h2>Retype Password</h2>
<input class="userinfo" type="password" value="Password"/>
<label class="container">I have Read and Accept the Terms and Conditions
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="container">I have Read and Accept the Privacy Statement
<input type="checkbox">
<span class="checkmark"></span>
</label>
<input class="buttons" type="button" value="Create Account" onclick="alert('Hello World');"/>
</div>
</body>
The CSS for this is as follows:
:root{
--main-color: #0052CC;
--secondary-color: #172B4D;
--tertiary-color: #FFFFFF;
}
body {
background-color: var(--tertiary-color);
margin: 0%;
}
#Title {
position: absolute;
width: 100%;
height: 30%;
background-color: var(--main-color);
margin: 0%;
}
#WifiFinderImg{
position: absolute;
height: 100%;
width: 50%;
left: 0%;
bottom: 0%;
margin: 0%;
}
/*The main content on the account registration page*/
#AccountRegistration{
font-size: x-large;
position: absolute;
height: 70%;
overflow-y: scroll;
bottom: 0%;
color: var(--main-color);
width: 100%;
text-align: center;
border: var(--secondary-color) 1px solid;
}
/*Class to style user input feilds for account creation*/
.userinfo{
font-size: large;
width: 40%;
border: 0px;
border-bottom: 1px var(--main-color) solid;
text-align: center;
color: lightgrey;
}
/*Customize the label*/
.container {
display: block;
position: relative;
padding-left: 35px;
margin-top: 2%;
margin-bottom: 2%;
cursor: pointer;
font-size: x-large;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/*Hide the browser's default checkbox*/
.container input {
position: relative;
opacity: 0;
cursor: pointer;
}
/*Create a custom checkbox*/
.checkmark {
position: relative;
top: 0;
left: 35%;
height: 25px;
width: 25px;
background-color: lightgray;
}
/*On mouse-over, add a light grey background color*/
.container:hover input ~ .checkmark {
background-color: lightslategrey;
}
/*When the checkbox is checked, add a main colour background*/
.container input:checked ~ .checkmark {
background-color: var(--main-color);
}
/*Create the checkmark*/
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/*Show the checkmark when checked*/
.container input:checked ~ .checkmark:after {
display: block;
}
/*Style the checkmark*/
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid var(--tertiary-color);
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
/*Class to style all content buttons*/
.buttons{
position: relative;
margin-left: -10%;
margin-top: -5%;
margin-bottom: 2%;
top: 150%;
left: 5%;
font-size: x-large;
width: 20%;
height: 10%;
border: 1px var(--main-color) solid;
text-align: center;
background-color: var(--main-color);
color: var(--tertiary-color);
}
/*Hover action for content buttons*/
.buttons:hover{
background-color: var(--secondary-color);
border-color: var(--secondary-color);
}
/*Buttons in the header*/
.Headerbuttons{
width: 15%;
height: 10%;
position: absolute;
top: 10%;
right: 2%;
border: 1px var(--tertiary-color) solid;
text-align: center;
background-color: var(--main-color);
color: var(--tertiary-color);
}
/*Hover actions for buttons in the header*/
.Headerbuttons:hover{
background-color: var(--tertiary-color);
border-color: var(--tertiary-color);
color: var(--main-color);
}
It seems to work when I change the position of the checkbox from relative to absolute, but then the problem is that they are on different parts of the page for different sized screens.
Add display: inline-block;
Here is the working Snippet
:root {
--main-color: #0052CC;
--secondary-color: #172B4D;
--tertiary-color: #FFFFFF;
}
body {
background-color: var(--tertiary-color);
margin: 0%;
}
#Title {
position: absolute;
width: 100%;
height: 30%;
background-color: var(--main-color);
margin: 0%;
}
#WifiFinderImg {
position: absolute;
height: 100%;
width: 50%;
left: 0%;
bottom: 0%;
margin: 0%;
}
/*The main content on the account registration page*/
#AccountRegistration {
font-size: x-large;
position: absolute;
height: 70%;
overflow-y: scroll;
bottom: 0%;
color: var(--main-color);
width: 100%;
text-align: center;
border: var(--secondary-color) 1px solid;
}
/*Class to style user input feilds for account creation*/
.userinfo {
font-size: large;
width: 40%;
border: 0px;
border-bottom: 1px var(--main-color) solid;
text-align: center;
color: lightgrey;
}
/*Customize the label*/
.container {
display: block;
position: relative;
padding-left: 35px;
margin-top: 2%;
margin-bottom: 2%;
cursor: pointer;
font-size: x-large;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/*Hide the browser's default checkbox*/
.container input {
position: relative;
opacity: 0;
cursor: pointer;
}
/*Create a custom checkbox*/
.checkmark {
position: relative;
top: 4px;
left: 0;
height: 25px;
width: 25px;
background-color: lightgray;
display: inline-block; /* Added */
}
/*On mouse-over, add a light grey background color*/
.container:hover input ~ .checkmark {
background-color: lightslategrey;
}
/*When the checkbox is checked, add a main colour background*/
.container input:checked ~ .checkmark {
background-color: var(--main-color);
}
/*Create the checkmark*/
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/*Show the checkmark when checked*/
.container input:checked ~ .checkmark:after {
display: block;
}
/*Style the checkmark*/
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid var(--tertiary-color);
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
/*Class to style all content buttons*/
.buttons {
position: relative;
margin-left: -10%;
margin-top: -5%;
margin-bottom: 2%;
top: 150%;
left: 5%;
font-size: x-large;
width: 20%;
height: 10%;
border: 1px var(--main-color) solid;
text-align: center;
background-color: var(--main-color);
color: var(--tertiary-color);
}
/*Hover action for content buttons*/
.buttons:hover {
background-color: var(--secondary-color);
border-color: var(--secondary-color);
}
/*Buttons in the header*/
.Headerbuttons {
width: 15%;
height: 10%;
position: absolute;
top: 10%;
right: 2%;
border: 1px var(--tertiary-color) solid;
text-align: center;
background-color: var(--main-color);
color: var(--tertiary-color);
}
/*Hover actions for buttons in the header*/
.Headerbuttons:hover {
background-color: var(--tertiary-color);
border-color: var(--tertiary-color);
color: var(--main-color);
}
<!DOCTYPE HTML>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="javascript.js"></script>
</head>
<body>
<div id="Title"> <img src="" alt="WifiFinder.img" id="WifiFinderImg">
<input type="button" class="Headerbuttons" id="back" value="Actually, I already have an account!" onclick="document.location='searchpage.html';"/>
</div>
<div id="AccountRegistration">
<h2>Username</h2>
<input class="userinfo" type="text" value="Username Here"/>
<h2>Email Address</h2>
<input class="userinfo" type="email" value="Email Address"/>
<h2>Date of Birth</h2>
<input class="userinfo" type="date" value="DD/MM/YYYY"/>
<h2>Password</h2>
<input class="userinfo" type="password" value="Password"/>
<h2>Retype Password</h2>
<input class="userinfo" type="password" value="Password"/>
<label class="container">I have Read and Accept the Terms and Conditions
<input type="checkbox">
<span class="checkmark"></span> </label>
<label class="container">I have Read and Accept the Privacy Statement
<input type="checkbox">
<span class="checkmark"></span> </label>
<input class="buttons" type="button" value="Create Account" onclick="alert('Hello World');"/>
</div>
</body>
</html>
The problem is indeed that you've used position relative and absolute amateur-ly (throughout the code).
Since you've mentioned that the checkboxes are used at multiple places, you need to a way to apply different styles. For that, I will add another class here.
Replace this:
<span class="checkmark"></span>
and this
.checkmark {
position: relative;
top: 0;
left: 35%;
height: 25px;
width: 25px;
background-color: lightgray;
}
With this:
<span class="checkmark customCheckmark"></span>
and this
.checkmark {
position: relative;
top: 0;
left: 35%;
height: 25px;
width: 25px;
background-color: lightgray;
}
.checkmark.customCheckmark {
position: absolute;
top: auto;
left: auto;
height: 25px;
width: 25px;
background-color: lightgray;
}
CSS-Tricks might be able to explain this in more depth.
https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/
Note: As a general rule of thumb, try not to use too many position relatives and absolutes, they mess up your CSS in most cases. If you're a beginner in CSS, try using Flexbox, it's much easier to learn and implement than conventional CSS.