Radio buttons don't apply CSS on check - html

I am trying to create two radio buttons that change color once the user selects them. However, once I select one of them they don't apply the supposed CSS:
.poller-nature-radio input[type="radio"] {
opacity: 0;
position: fixed;
width: 0;
}
#id_poller_nature label {
display: inline-block;
background-color: #ddd;
padding: 10px 20px;
font-family: sans-serif, Arial;
font-size: 16px;
border: 2px solid #444;
border-radius: 4px;
}
#id_poller_nature {
display: flex;
}
#id_poller_nature li {
list-style-type: none;
}
#id_poller_nature input[type="radio"]:checked + label {
background-color:#bfb;
border-color: #4c4;
}
<div class="fieldWrapper poller-nature-radio">
<label for="id_poller_nature_0">Nothing changes on select:</label>
<ul id="id_poller_nature">
<li><label for="id_poller_nature_0"><input type="radio" name="poller_nature" value="Choice 1" required="" id="id_poller_nature_0">
Question</label>
</li>
<li><label for="id_poller_nature_1"><input type="radio" name="poller_nature" value="Choice 2" required="" id="id_poller_nature_1">
Information</label>
</li>
</ul>
</div>

Since you used + css selector, the label should come as a sibling and come after the input. Check the below snippet.
.poller-nature-radio input {
position: fixed;
visibility: hidden;
}
#id_poller_nature label {
display: inline-block;
background-color: #ddd;
padding: 10px 20px;
font-family: sans-serif, Arial;
font-size: 16px;
border: 2px solid #444;
border-radius: 4px;
}
#id_poller_nature {
display: flex;
}
#id_poller_nature li {
list-style-type: none;
}
#id_poller_nature input[type="radio"]:checked + label {
background-color:#bfb;
border-color: #4c4;
}
<div class="fieldWrapper poller-nature-radio">
<label for="id_poller_nature_0">Click the button to see the change:</label>
<ul id="id_poller_nature">
<li>
<input
type="radio"
name="poller_nature"
value="Choice 1"
required=""
id="id_poller_nature_0"
/>
<label for="id_poller_nature_0"> Question</label>
</li>
<li>
<input
type="radio"
name="poller_nature"
value="Choice 2"
required=""
id="id_poller_nature_1"
/>
<label for="id_poller_nature_1"> Information</label>
</li>
</ul>
</div>

.poller-nature-radio input[type="radio"] {
opacity: 0;
position: fixed;
width: 0;
}
#id_poller_nature label {
display: inline-block;
background-color: #ddd;
padding: 10px 20px;
font-family: sans-serif, Arial;
font-size: 16px;
border: 2px solid #444;
border-radius: 4px;
}
#id_poller_nature {
display: flex;
}
#id_poller_nature li {
list-style-type: none;
}
#id_poller_nature_1:checked {
background-color:#bfb;
border-color: #4c4;
}
<div class="fieldWrapper poller-nature-radio">
<label for="id_poller_nature_0">Nothing changes on select:</label>
<ul id="id_poller_nature">
<li><label for="id_poller_nature_0"><input type="radio" name="poller_nature"
value="Choice 1" required="" id="id_poller_nature_0">
Question</label>
</li>
<li><label for="id_poller_nature_1"><input type="radio" name="poller_nature" value="Choice 2" required="" id="id_poller_nature_1">
Information</label>
</li>
</ul>
</div>
Try to access the input element using the individual id then handle the :check state

Change your HTML like this.
<ul id="id_poller_nature">
<li>
<input type="radio" name="poller_nature" value="Choice 1" required="" id="id_poller_nature_0">
<label for="id_poller_nature_0">`enter code
here`Question</label>
</li>

Related

Custom radio selector not functioning

I've created this custom radio selector for my site and the first radio selector is working but the 2nd field isn't selecting. Is there another class that I need to create or have each input named a different ID so they don't conflict with one another? I'm trying to create multiple options for a customer to select an option.
input[type=radio] {
position: absolute;
visibility: hidden;
display: none;
}
label {
display: inline-block;
cursor: pointer;
font-weight: bold;
padding: 5px 20px;
}
input[type=radio]:checked+label {
color: #fff;
background: #444;
border-radius: 5px;
}
label+input[type=radio]+label {
border-left: solid 3px #444;
}
.radio-group {
display: inline-block;
overflow: hidden;
}
<div class="radio-group">
<input type="radio" id="lessthan6months" name="timeline"><label for="lessthan6months">6 mo</label>
<input type="radio" id="6to12months" name="timeline"><label for="6to12months">6-12 mo</label>
<input type="radio" id="12to24months" name="timeline"><label for="12to24months">12-24 mo</label>
<div class="line"></div>
</div>
<div class="radio-group">
<input type="radio" id="tradein_y" name="tradein" value="Yes"><label for="trade in yes">Yes</label>
<input type="radio" id="tradein_n" name="tradein" value="No"><label for="trade in no">No</label>
<div class="line"></div>
</div>
The for of the label content has to match the id of the input.
This will work, like this:
input[type=radio] {
position: absolute;
visibility: hidden;
display: none;
}
label {
display: inline-block;
cursor: pointer;
font-weight: bold;
padding: 5px 20px;
}
input[type=radio]:checked+label {
color: #fff;
background: #444;
border-radius: 5px;
}
label+input[type=radio]+label {
border-left: solid 3px #444;
}
.radio-group {
display: inline-block;
overflow: hidden;
}
<div class="radio-group">
<input type="radio" id="lessthan6months" name="timeline"><label for="lessthan6months">6 mo</label>
<input type="radio" id="6to12months" name="timeline"><label for="6to12months">6-12 mo</label>
<input type="radio" id="12to24months" name="timeline"><label for="12to24months">12-24 mo</label>
<div class="line"></div>
</div>
<div class="radio-group">
<input type="radio" id="tradein_y" name="tradein" value="Yes"><label for="tradein_y">Yes</label>
<input type="radio" id="tradein_n" name="tradein" value="No"><label for="tradein_n">No</label>
<div class="line"></div>
</div>

How can I align text to the right side of radio and checkbox inputs?

I'm trying to have my checkboxes and radio inputs appear on the left side of the form tag with their labels appearing next to them. However no matter how many examples I find, none of the solutions seem to help with me.
All the displays are set to be flex. I've also commented out a couple of CSS lines as they are meant to work with JavaScript (which I haven't written yet). I doubt they were relevant as one sets the overall page display and the other hides elements with the class "hide-question", but I included them just in case there was something I was missing.
I hope this is enough information. Any help would be appreciated.
And here's my code:
* {
margin: 0;
padding: 0;
}
h2,
p,
label {
color: #231f20;
}
h2,
p {
font-weight: bold;
}
body {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
padding: 10px;
}
form {
display: flex;
/*height: 100vh;*/
justify-content: center;
align-items: center;
background-color: #fac08e;
}
header {
width: 100%;
background-color: #fac08e;
overflow: hidden;
}
input,
select {
margin-top: 6px;
}
li {
list-style: none;
margin: 10px 0;
}
textarea {
resize: none;
font-family: Arial, Helvetica, sans-serif;
padding: 5px;
}
/*.hide-question {
display: none;
}*/
.question {
background-color: #fcd6b5;
display: flex;
flex-direction: column;
padding: 10%;
border-radius: 20px;
}
.question-title {
font-size: 20px;
margin-bottom: 5px;
}
.question-title span {
font-size: 24px;
margin-right: 12px;
}
#question-20 input,
#question-21 input {
display: block;
}
<body>
<form action="results.html" method="GET" enctype="multipart/form-data">
<div class="question">
<div id="question-4" class="hide-question">
<p class="question-title"><span>4</span> Are you a Citizen?</p>
<ul>
<li>
<label for="idyesCitizen">Yes</label>
<input type="radio" name="Citizenship" id="idyesCitizen" value="Yes">
</li>
<li>
<label for="idnoCitizen">No</label>
<input type="radio" name="Citizenship" id="idnoCitizen" value="No">
</li>
</ul>
</div>
<div id="question-6" class="hide-question">
<p class="question-title"><span>6</span> What other Languages can you speak?</p>
<ul>
<li>
<label for="idmandarin">Mandarin</label>
<input type="checkbox" name="Mandarin" id="idmandarin">
</li>
<li>
<label for="iditalian">Italian</label>
<input type="checkbox" name="Italian" id="iditalian">
</li>
<li>
<label for="idarabic">Arabic</label>
<input type="checkbox" name="Arabic" id="idarabic">
</li>
<li>
<label for="idcantonese">Cantonese</label>
<input type="checkbox" name="Cantonese" id="idcantonese">
</li>
<li>
<label for="idgreek">Greek</label>
<input type="checkbox" name="Greek" id="idgreek">
</li>
</ul>
</div>
</div>
</form>
</body>
If you want the labels after the inputs, why are all the label elements before the inputs in your markup?
Also, with both radio buttons and checkboxes, labels can wrap inputs. This eliminates the need for the for attributes and IDs, and it makes the labels clickable.
* {
margin: 0;
padding: 0;
}
h2,
p,
label {
color: #231f20;
}
h2,
p {
font-weight: bold;
}
body {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
padding: 10px;
}
form {
display: flex;
/*height: 100vh;*/
justify-content: center;
align-items: center;
background-color: #fac08e;
}
header {
width: 100%;
background-color: #fac08e;
overflow: hidden;
}
input,
select {
margin-top: 6px;
}
li {
list-style: none;
margin: 10px 0;
}
textarea {
resize: none;
font-family: Arial, Helvetica, sans-serif;
padding: 5px;
}
/*.hide-question {
display: none;
}*/
.question {
background-color: #fcd6b5;
display: flex;
flex-direction: column;
padding: 10%;
border-radius: 20px;
}
.question-title {
font-size: 20px;
margin-bottom: 5px;
}
.question-title span {
font-size: 24px;
margin-right: 12px;
}
#question-20 input,
#question-21 input {
display: block;
}
<body>
<form action="results.html" method="GET" enctype="multipart/form-data">
<div class="question">
<div id="question-4" class="hide-question">
<p class="question-title"><span>4</span> Are you a Citizen?</p>
<ul>
<li>
<label>
<input type="radio" name="Citizenship" value="Yes">
Yes
</label>
</li>
<li>
<label>
<input type="radio" name="Citizenship" value="No">
No
</label>
</li>
</ul>
</div>
<div id="question-6" class="hide-question">
<p class="question-title"><span>6</span> What other Languages can you speak?</p>
<ul>
<li>
<label>
<input type="checkbox" name="Mandarin">
Mandarin
</label>
</li>
<li>
<label>
<input type="checkbox" name="Italian">
Italian
</label>
</li>
<li>
<label>
<input type="checkbox" name="Arabic">
Arabic
</label>
</li>
<li>
<label>
<input type="checkbox" name="Cantonese">
Cantonese
</label>
</li>
<li>
<label>
<input type="checkbox" name="Greek">
Greek
</label>
</li>
</ul>
</div>
</div>
</form>
</body>
Simply put the label after the checkbox:
* {
margin: 0;
padding: 0;
}
h2,
p,
label {
color: #231f20;
}
h2,
p {
font-weight: bold;
}
body {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
padding: 10px;
}
form {
display: flex;
/*height: 100vh;*/
justify-content: center;
align-items: center;
background-color: #fac08e;
}
header {
width: 100%;
background-color: #fac08e;
overflow: hidden;
}
input,
select {
margin-top: 6px;
}
li {
list-style: none;
margin: 10px 0;
}
textarea {
resize: none;
font-family: Arial, Helvetica, sans-serif;
padding: 5px;
}
/*.hide-question {
display: none;
}*/
.question {
background-color: #fcd6b5;
display: flex;
flex-direction: column;
padding: 10%;
border-radius: 20px;
}
.question-title {
font-size: 20px;
margin-bottom: 5px;
}
.question-title span {
font-size: 24px;
margin-right: 12px;
}
#question-20 input,
#question-21 input {
display: block;
}
<body>
<form action="results.html" method="GET" enctype="multipart/form-data">
<div class="question">
<div id="question-4" class="hide-question">
<p class="question-title"><span>4</span> Are you a Citizen?</p>
<ul>
<li>
<input type="radio" name="Citizenship" id="idyesCitizen" value="Yes">
<label for="idyesCitizen">Yes</label>
</li>
<li>
<input type="radio" name="Citizenship" id="idnoCitizen" value="No">
<label for="idnoCitizen">No</label>
</li>
</ul>
</div>
<div id="question-6" class="hide-question">
<p class="question-title"><span>6</span> What other Languages can you speak?</p>
<ul>
<li>
<input type="checkbox" name="Mandarin" id="idmandarin">
<label for="idmandarin">Mandarin</label>
</li>
<li>
<input type="checkbox" name="Italian" id="iditalian">
<label for="iditalian">Italian</label>
</li>
<li>
<input type="checkbox" name="Arabic" id="idarabic">
<label for="idarabic">Arabic</label>
</li>
<li>
<input type="checkbox" name="Cantonese" id="idcantonese">
<label for="idcantonese">Cantonese</label>
</li>
<li>
<input type="checkbox" name="Greek" id="idgreek">
<label for="idgreek">Greek</label>
</li>
</ul>
</div>
</div>
</form>
</body>
To be honest, if I were you I will just put labels after input tags so it will automatically be next to each other after applying flex.
However If you do not want to change you html. all you need is flex-direction:row-reverse``. and justify-content:flex:end```.
I have added class reverseLabels to uls and applied below css.
.reverseLabels>li {
display: flex;
align-items: center;
flex-direction: row-reverse;
justify-content: flex-end;
}
* {
margin: 0;
padding: 0;
}
h2,
p,
label {
color: #231f20;
}
h2,
p {
font-weight: bold;
}
body {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
padding: 10px;
}
form {
display: flex;
/*height: 100vh;*/
justify-content: center;
align-items: center;
background-color: #fac08e;
}
header {
width: 100%;
background-color: #fac08e;
overflow: hidden;
}
input,
select {
margin-top: 6px;
}
li {
list-style: none;
margin: 10px 0;
}
textarea {
resize: none;
font-family: Arial, Helvetica, sans-serif;
padding: 5px;
}
/*.hide-question {
display: none;
}*/
.question {
background-color: #fcd6b5;
display: flex;
flex-direction: column;
padding: 10%;
border-radius: 20px;
}
.question-title {
font-size: 20px;
margin-bottom: 5px;
}
.question-title span {
font-size: 24px;
margin-right: 12px;
}
#question-20 input,
#question-21 input {
display: block;
}
/*To acheive desire output use this css if you do not want to change html*/
.reverseLabels>li {
display: flex;
align-items: center;
flex-direction: row-reverse;
justify-content: flex-end;
}
.reverseLabels>li input {
margin-top: 0;
}
<body>
<form action="results.html" method="GET" enctype="multipart/form-data">
<div class="question">
<div id="question-4" class="hide-question">
<p class="question-title"><span>4</span> Are you a Citizen?</p>
<ul class="reverseLabels">
<li>
<label for="idyesCitizen">Yes</label>
<input type="radio" name="Citizenship" id="idyesCitizen" value="Yes">
</li>
<li>
<label for="idnoCitizen">No</label>
<input type="radio" name="Citizenship" id="idnoCitizen" value="No">
</li>
</ul>
</div>
<div id="question-6" class="hide-question">
<p class="question-title"><span>6</span> What other Languages can you speak?</p>
<ul class="reverseLabels">
<li>
<label for="idmandarin">Mandarin</label>
<input type="checkbox" name="Mandarin" id="idmandarin">
</li>
<li>
<label for="iditalian">Italian</label>
<input type="checkbox" name="Italian" id="iditalian">
</li>
<li>
<label for="idarabic">Arabic</label>
<input type="checkbox" name="Arabic" id="idarabic">
</li>
<li>
<label for="idcantonese">Cantonese</label>
<input type="checkbox" name="Cantonese" id="idcantonese">
</li>
<li>
<label for="idgreek">Greek</label>
<input type="checkbox" name="Greek" id="idgreek">
</li>
</ul>
</div>
</div>
</form>
</body>

ordered list numbers are not aligned to the list

I have two doubts to be cleared.
my ordered list has numbering of 1 2 3 4 5 6 etc but am not able to bring that aligned with the questions this is going outside the box.
I have a solution button for each questions but I don't know how to place it on the side of the questions.
here i have attached the code and the expected output. if possible kindly complete the code and post.
* {
margin: 0;
padding: 0;
}
body {
font-family: sans-serif;
font-size: 15px;
height: 100%;
width: 100%;
background-color: antiquewhite;
}
input[type="radio"] {
margin-right: 10px;
}
p {
line-height: 30px;
padding: 30px;
}
.play{
position: absolute;
top: 150px;
left: 300px;
padding: 10px;
border-radius: 10px;
font-family: sans-serif;
background-color: darkgreen;
color: #fff;
}
form[name=quiz] {
margin-left: 10%;
margin-right: 10%;
}
ol{
padding: 10px;
list-style-position: inside;
}
ol li{
background-color: #fff;
border-radius: 15px;
margin: 10px;
box-shadow: 0px 1.5px 2px 0px rgb(91, 91, 91);
}
<form name="quiz" id="quiz">
<ol>
<li>
<div class="q1">
<p><strong>The value of \(\frac{1}{{{{\log }_4}120}} + \frac{1}{{{{\log }_5}120}} + \frac{1}{{{{\log }_6}120}}\) is</strong><br><br>
<input type="radio" name="question1" value="0">A.0<br>
<input type="radio" name="question1" value="1">B.1<br>
<input type="radio" name="question1" value="24">C.24<br>
<input type="radio" name="question1" value="120">D.120<br>
</p>
</div>
<div>
<button class="play">Play Solution</button>
</div>
</li>
<li>
<div>
<p><strong>For a 3x3 matrix A, |A| = 4 and adj A = \(\left( {\begin{array}{*{20}{c}}1&p&3\\1&3&3\\2&4&4\end{array}} \right)\), then the value of p is</strong><br><br>
<input type="radio" name="question2" value="4">A.4<br>
<input type="radio" name="question2" value="11">B.11<br>
<input type="radio" name="question2" value="5">C.5<br>
<input type="radio" name="question2" value="0">D.0<br>
</p>
</div>
</li>
Your question isn't aligning with the number of your ordered list because you're using a <p> element for it which is set to display: block; by default. Removing the <p> element will fix the problem.
You can use a <table> to structure your answers and the solution buttons.
* {
margin: 0;
padding: 0;
}
body {
font-family: sans-serif;
font-size: 15px;
height: 100%;
width: 100%;
background-color: antiquewhite;
}
form[name=quiz] {
margin-left: 10%;
margin-right: 10%;
}
ol {
padding: 10px;
list-style-position: inside;
}
ol li {
background-color: #fff;
border-radius: 15px;
margin: 10px;
padding: 30px;
box-shadow: 0px 1.5px 2px 0px rgb(91, 91, 91);
}
li table {
width: 100%;
}
li table td {
vertical-align: top;
padding-top: 20px;
}
li table td:first-child {
width: 70%;
}
td p {
padding-bottom: 10px;
}
td button {
width: 100%;
padding: 10px;
margin-bottom: 5px;
border-radius: 10px;
background-color: darkgreen;
color: #fff;
}
<form name="quiz" id="quiz">
<ol>
<li class="q1">
<strong>The value of \(\frac{1}{{{{\log }_4}120}} + \frac{1}{{{{\log }_5}120}} + \frac{1}{{{{\log }_6}120}}\) is</strong>
<table>
<tr>
<td>
<p>
<input type="radio" name="question1" id="question1" value="0">
<label for="question1">A. 0</label>
</p>
<p>
<input type="radio" name="question1" id="question2" value="1">
<label for="question2">B. 1</label>
</p>
<p>
<input type="radio" name="question1" id="question3" value="24">
<label for="question3">C. 24</label>
</p>
<p>
<input type="radio" name="question1" id="question4" value="120">
<label for="question4">D. 120</label>
</p>
</td>
<td>
<button class="play">Play Solution</button>
<button class="play">Text Solution</button>
</td>
</tr>
</table>
</li>
</ol>
</form>

Aligning label with input type text

Good day all. I am currently doing a form in html and css. I tried researching on the internet but somehow the code there is would either cause my textbox to lose its design and look plain or it would be very ugly.
I am trying to align the textbox with the labels so that it would look neat.
I also want to make the submit and reset button align in the center next to each other.
And the radio buttons to make them in a straight line instead of being seperated on 2 lines.
When I removed the code for the class .firstform select, type, I lose my design properties for the textboxes but the buttons managed to be align.
body {
background: url(ewp.jpg);
background-size: cover;
}
.firstform {
order-radius: 5px;
background: green;
padding: 20px;
width: 550px;
margin: auto;
color: #fff;
font-size: 16px;
font-family: verdana;
margin-top: 100px;
opacity: 0.8;
}
.firstform h1 {
text-align: center;
margin: 0;
padding: 0;
}
.firstform input,
select {
width: 50%;
padding: 12px 20px;
margin-left: 16em;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
font-size: 18px;
background: black;
color: white;
opacity: 0.9;
}
.container {
display: block;
position: relative;
padding-left: 25px;
margin-bottom: 12px;
margin-left: 24em;
cursor: pointer;
font-size: 12px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide browser default radio button */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 15px;
width: 15px;
background-color: #eee;
border-radius: 50%;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196f3;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the indicator (dot/circle) when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container .checkmark:after {
top: 6px;
left: 6px;
width: 4px;
height: 4px;
border-radius: 50%;
background: white;
}
.buttonHolder {
display: block;
margin: 0 auto;
}
.firstform input[type="submit"]:hover {
background: #45a049;
transparent: 0.6s;
}
.firstform input[type="reset"]:hover {
background: #45a059;
transparent: 0.6s;
}
<div class="firstform">
<h1>Student Registration Form</h1>
<form action="Form1.php" method="post">
<p><label>First Name: <input type="text" name ="fname" placeholder="First Name" maxlength="25"></label></p>
<p><label>Last Name: <input type="text" name="lname" placeholder="Last Name" maxlength="25"></label></p>
<p><label>Age: <input type="number" name= "age" min="0" max="150"></label></p>
<p><label> Date of Birth:<input type="date" name="date"></label></p>
<p>Gender:
<label class="container">
<input type="radio" name="gender" value="Male">Male
<span class="checkmark"></span>
</label>
<label class="container">
<input type="radio" name="gender" value="Female">Female
<span class="checkmark"></span>
</label>
</p>
<p> Nationality:
<label>
<select name="nationality">
<option selected>Malaysia</option>
<option>Bangladesh</option>
<option>India</option>
<option>African Nations</option>
<option>South East Asia nations</option>
<option>others</option>
</select>
</label>
</p>
<p><label>Address:<input type="text" name="address" size="30" /></label></p>
<p><label>Postcode: <input type="number" name="postcode" min="0" maxlength="5" oninput="this.value=this.value.slice(0,this.maxLength)"></label></p>
<p><label>State:
<input name="state" list="state">
<datalist id="state">
<option value="Selangor">
<option value="Kuala Lumpur">
<option value="Kelantan">
<option value="Johor">
<option value="Malacca">
<option value="Perak">
<option value="Pahang">
<option value="Negeri Sembilan">
<option value="Sabah">
<option value="Sarawak">
<option value="Perlis">
<option value="Kedah">
<option value="Terengganu">
<option value="Penang">
</datalist>
</label></p>
<p>
<label>Email:
<input type="email" name="email">
</label>
</p>
<p>
<label> Tel:
<input type="tel" name="tel" placeholder="(###) ###-####" oninput="this.value=this.value.slice(0,this.maxLength)" maxlength="10">
</label>
</p>
<div class="buttonHolder">
<input type="submit" name="Insert">
<input type="reset" name="Clear">
</div>
</form>
</div>
Working fiddle: https://jsfiddle.net/jennifergoncalves/nut5mzgj/10/
body {
background: url(ewp.jpg);
background-size: cover;
}
.firstform {
order-radius: 5px;
background: green;
padding: 20px;
width: 550px;
margin: auto;
color: #fff;
font-size: 16px;
font-family: verdana;
margin-top: 100px;
opacity: 0.8;
}
.firstform h1 {
text-align: center;
margin: 0;
padding: 0;
}
.firstform input,
select {
width: 50%;
padding: 12px 20px;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
font-size: 18px;
background: black;
color: white;
opacity: 0.9;
/* removed margin-left */
}
.container {
display: block;
position: relative;
padding-left: 25px;
margin-bottom: 12px;
margin-left: 24em;
cursor: pointer;
font-size: 12px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide browser default radio button */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 15px;
width: 15px;
background-color: #eee;
border-radius: 50%;
}
/* On mouse-over, add a grey background color */
.container:hover input~.checkmark {
background-color: #ccc;
}
/* When the radio button is checked, add a blue background */
.container input:checked~.checkmark {
background-color: #2196f3;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the indicator (dot/circle) when checked */
.container input:checked~.checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container .checkmark:after {
top: 6px;
left: 6px;
width: 4px;
height: 4px;
border-radius: 50%;
background: white;
}
.buttonHolder {
display: block;
margin: 0 auto;
}
.firstform input[type="submit"]:hover {
background: #45a049;
transparent: 0.6s;
}
.firstform input[type="reset"]:hover {
background: #45a059;
transparent: 0.6s;
}
label > span, p > span {
width: 50%;
display: inline-block;
}
<div class="firstform">
<h1>Student Registration Form</h1>
<form action="Form1.php" method="post">
<p><label><span>First Name: </span><input type="text" name ="fname" placeholder="First Name" maxlength="25"></label></p>
<p><label><span>Last Name: </span><input type="text" name="lname" placeholder="Last Name" maxlength="25"></label></p>
<p><label><span>Age: </span><input type="number" name= "age" min="0" max="150"></label></p>
<p><label><span>Date of Birth:</span><input type="date" name="date"></label></p>
<p><span>Gender:</span>
<label class="container">
<input type="radio" name="gender" value="Male">Male
<span class="checkmark"></span>
</label>
<label class="container">
<input type="radio" name="gender" value="Female">Female
<span class="checkmark"></span>
</label>
</p>
<p><label><span>Nationality:</span><select name="nationality">
<option selected>Malaysia</option>
<option>Bangladesh</option>
<option>India</option>
<option>African Nations</option>
<option>South East Asia nations</option>
<option>others</option>
</select>
</label>
</p>
<p><label><span>Address:</span><input type="text" name="address" size="30" /></label></p>
<p><label><span>Postcode: </span><input type="number" name="postcode" min="0" maxlength="5" oninput="this.value=this.value.slice(0,this.maxLength)"></label></p>
<p><label><span>State:</span><input name="state" list="state">
<datalist id="state">
<option value="Selangor">
<option value="Kuala Lumpur">
<option value="Kelantan">
<option value="Johor">
<option value="Malacca">
<option value="Perak">
<option value="Pahang">
<option value="Negeri Sembilan">
<option value="Sabah">
<option value="Sarawak">
<option value="Perlis">
<option value="Kedah">
<option value="Terengganu">
<option value="Penang">
</datalist>
</label></p>
<p>
<label><span>Email:</span><input type="email" name="email">
</label>
</p>
<p>
<label><span>Tel:</span><input type="tel" name="tel" placeholder="(###) ###-####" oninput="this.value=this.value.slice(0,this.maxLength)" maxlength="10">
</label>
</p>
<div class="buttonHolder">
<input type="submit" name="Insert"><input type="reset" name="Clear">
</div>
</form>
</div>
The issues were that you had a margin to the left of the inputs. This was preventing the elements from being inline because it added to the horizontal space needed.
Secondly, your inputs are inside your labels. This is fine practice since you can avoid using for attributes for accessibility. However, you can't target just the label, so I surrounded the actual label text with a span. Now you can just target the actual label text. Setting this to 50% and having the inputs at 50% with display:inline-block on both will solve this issue.
I suggest looking into flexbox for these kind of issues. It gives you the ability to be completely flexible about how you align your elements. Here you can find a good explanation:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
3 issues here, to fix the problem
You have enclosed the <input> inside the <label>. Move it out, close the </label> and then start the <input>
remove the margin-left of the input
give display: inline-block; width: 50%; to the label
So your code looks like this now:
jsfiddle: https://jsfiddle.net/vg3ts1m0/3/
jsfiddle (full screen): https://jsfiddle.net/vg3ts1m0/3/show
I have edited first few items in it. It will give you an idea on how to proceed.
I hope it helps.
I am trying to align the textbox with the labels so that it would look neat.
// Layout will resolve 90% of vertical alignment issues.
// To unify form controls (ex. `<input>`, `<select>`, etc) apply the following
input, select, label {display: inline-block; font: inherit}
I also want to make the submit and reset button align in the center next to each other.
/*
Wrap the buttons in a block element (ex. `<fieldset>`, `<div>`, etc)
and center the block
*/
fieldset {width: fit-content; margin: 0 auto}
<fieldset><input type='reset'><input type='submit'></fieldset>
And the radio buttons to make them in a straight line instead of being seperated on 2 lines.
/*
Dimensions should be adjusted as a square (ex. width:4vw; height:4vh;)
The following styles align everything vertically. Keep the height and
line-height the same.
*/
[type=radio] {height: 5vh; line-height: 5vh; vertical-align: middle}
Intrinsic lengths (vw and vh) are used to make the whole form so that it's responsive (see demo in full page mode.
Demo
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
font: 700 5vh/1.1 Verdana;
}
body {
overflow-x: hidden;
overflow-y: scroll;
}
form {
width: 100%;
height: 100%;
padding: 0;
margin: 0 auto;
}
fieldset {
padding: 5px;
margin: 1vh auto;
border-radius: 6px;
}
.set1>legend {
margin-bottom: -7px
}
legend {
margin-top: 10px;
}
legend+hr {
width: 30vw;
}
[type=radio] {
width: 4vw;
height: 4vh;
}
input,
select,
label {
display: inline-block;
font: inherit;
margin: 2vh 0 0 0;
height: 6vh;
line-height: 6vh;
vertical-align: middle;
padding: 1px 2px;
border-radius: 4px;
}
#first,
#last {
width: 77vw;
}
#dob {
width: 33vw
}
.set2 label {
min-width: 9vw;
}
.set3 label {
min-width: 10vw;
}
#male {
margin-left: 3vw;
}
label[for=nationality] {
width: 19vw
}
#nationality {
height: 8vh;
line-height 8vh;
}
#address {
width: 71vw;
}
#state {
width: 25vw;
}
#email {
width: 75vw;
}
[type=number],
[type=tel],
[type=date] {
font-family: Consolas;
font-size: 6vh
}
[type='reset'],
[type='submit'] {
width: 15vw;
height: 8vh;
cursor: pointer;
}
[type=submit] {
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
margin: 0 0 0 -3px;
padding: 0 0 2px 0;
}
[type=reset] {
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
margin: 0 0 0 -3px;
padding: 0 0 2px 0;
}
.set4 {
padding: 4px;
width: fit-content;
margin: 0px auto;
}
hr {
margin-top: 8px;
}
<form id='main'>
<fieldset name='set1' class='set1 set'>
<legend>Student Registration</legend>
<hr>
<fieldset name='set2' class='set2 set'>
<legend>Personal Information</legend>
<label for='first'>First: </label>
<input id='first' name='first' type='text'><br>
<label for='last'>Last: </label>
<input id='last' name='last' type='text'><br>
<label for='dob'>DOB: </label>
<input id='dob' name='dob' type='date'>
<input id='male' name='gender' type='radio' value='male'><label for='male'> Male </label>
<input id='female' name='gender' type='radio' value='female'><label for='female'> Female</label> <br>
<label for='nationality'>Nationality: </label>
<select id='nationality' name='nationality'>
<option value=''>---------------------------</option>
<optgroup label='North America'>
<option value='US'>American</option>
<option value='CA'>Canadian</option>
<option value='MX'>Mexican</option>
</optgroup>
<optgroup label='South America'>
<option value='BR'>Brazilian</option>
<option value='CR'>Costa Rican</option>
<option value='PR'>Peruvian</option>
</optgroup>
<optgroup label='Asia'>
<option value='CN'>Chinese</option>
<option value='JP'>Japanese</option>
<option value='IN'>Indian</option>
</optgroup>
<optgroup label='Europe'>
<option value='GB'>British</option>
<option value='DE'>German</option>
<option value='FI'>Finnish</option>
</optgroup>
<optgroup label='Africa'>
<option value='NG'>Nigerian</option>
<option value='EG'>Egyptian</option>
<option value='ML'>Malian</option>
</optgroup>
<option value='AU'>Australian</option>
</select>
</fieldset>
<hr>
<fieldset name='set3' class='set3 set'>
<legend>Contact Information</legend>
<label for='address'>Address: </label>
<input id='address' name='address' type='text'><br>
<label for='state'>State: </label>
<input id='state' name='state' list='states'>
<datalist id="states">
<option value="California">
<option value="Oregon">
<option value="Washington">
<option value="Nevada">
<option value="Arizona">
</datalist>
<label for='zipcode'>Zip Code: </label>
<input id='zipcode' name='zipcode' type='number' min='85000' max='98999'><br>
<label for='email'>Email: </label>
<input id='email' name='email' type='email'><br>
<label for='tel'>Telephone: </label>
<input id='tel' name='tel' type='tel'>
</fieldset>
<fieldset name='set4' class='set4 set'>
<input type='submit'>
<input type='reset'>
</fieldset>
</fieldset>
</form>

Using a checkbox within an checkbox accordion?

I am trying to create a filter for a website, I made an accordion using check boxs, and I would like to put check boxes within it using
<input name="name" value="value" type="checkbox"/>
However the check box itself wont show.
Here is the code:
<div id="container">
<section id="accordion">
<div>
<input type="checkbox" id="check-1" />
<label for="check-1">Continent</label>
<form action="">
Europe<input name="animal" value="Cat" type="checkbox"/>
</form>
</div>
<div>
<input type="checkbox" id="check-2" />
<label for="check-2">Country</label>
<form action="">
UK<input name="animal" value="Cat" type="checkbox"/>
</form>
</div>
<div>
<input type="checkbox" id="check-3" />
<label for="check-3">League / Competition</label>
<form action="">
Premier League<input name="animal" value="Cat" type="checkbox"/>
</form>
</div>
<div>
<input type="checkbox" id="check-4" />
<label for="check-4">Date</label>
<form action="">
march 13th<input name="animal" value="Cat" type="checkbox"/>
</form>
</div>
</section>
and the CSS:
* {
font-family: Arial, sans;
margin: 0;
padding: 0;
}
h1, h2 {
margin: 1em 0 0 0;
text-align:left;
}
h2 {
margin: 0 0 1em 0;
}
#container {
margin: 10px;
width: 20%;
}
#accordion input {
display: none;
}
#accordion label {
background: #eee;
border-radius: .25em;
cursor: pointer;
display: block;
margin-bottom: .125em;
padding: .25em 1em;
z-index: 20;
}
#accordion label:hover {
background: #ccc;
}
#accordion input:checked + label {
background: #ccc;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
color: white;
margin-: 0;
}
#accordion form{
background: #f7f7f7;
height:0px;
overflow:hidden;
z-index:10;
}
#accordion from input{
padding: 1em;
}
#accordion input:form {
}
#accordion input:checked ~ form {
border-bottom-left-radius: .25em;
border-bottom-right-radius: .25em;
height: auto;
margin-bottom: .125em;
}
DEMO here: http://codepen.io/anon/pen/gPdzWK
So you have a line #accordion input { display: none }, this will hide all the inputs you have.
If you only want to display the input in the form and hide the other ones you can do: #accordion > div > input { display: none } which selects the input that is a direct child of div which itself is a direct child of #accordion.