I have a checkbox and i would like to style it. When the checkbox is unchecked it should have a black border and when the checkbox is checked the hole checkbox should be black without hook. Below you see what i have done so far. Sadly it wont work.
My Code:
<form>
<input class="newsletter" type="checkbox" value="text">Ich möchte den Newsletter bekommen
</form>
input[type=checkbox]:not(old){
width : 13px;
margin : 0;
padding : 0;
opacity : 0;
}
.newsletter:unchecked{
width:13px;
height: 13px;
border: 2px solid black;
border-radius:3px;
}
.newsletter:checked{
width:13px;
height: 13px;
border: 2px solid black;
background-color:black;
border-radius:3px;
}
The first part of my code should hide the current checkbox. The second part should be the checkbox when the box is unchecked and the third part when the box is checked. I thought this is how you are styling these checkboxes. What am i doing wrong?
This may help you
.checkbox input[type=checkbox] {
display: none;
}
.checkbox input[type=checkbox]:not(:checked) + label:before {
content: "";
border: 2px solid #000;
height: 10px;
width: 10px;
display: inline-block;
}
.checkbox input[type=checkbox]:checked + label:before {
content: "";
border: 2px solid #000;
height: 10px;
width: 10px;
background: #000;
display: inline-block;
}
<form>
<div class="checkbox">
<input id="check" class="newsletter" type="checkbox" value="text">
<label for="check"> Ich möchte den Newsletter bekommen</label>
</div>
</form>
The first thing you need to do with your code is add a label, e.g.:
<form>
<input class="newsletter" type="checkbox" value="text" id="newsletter" name="newsletter">
<label for="newsletter">Ich möchte den Newsletter bekommen</label>
</form>
I've also added an id and name attribute to the checkbox. The id needs to be the same as the for attribute on the label for this trick to work.
Next, we need to visibly hide the checkbox. I tend to use a class of sr-only in the CSS to visibly hide things based on Yahoo's example:
.sr-only{
height: 1px;
width: 1px;
overflow: hidden
clip: rect(1px,1px,1px,1px);
position: absolute;
}
Next we create a pseudo element before the label. For the style rules I'm using the adjacent sibling selector (+) to apply the rules when the label is an adjacent sibling of the checkbox. I'm keeping your class name so it'll only target labels that're siblings of class newsletter:
.newsletter:not(checked) + label:before{
content:" ";
display: inline-block;
width:13px;
height: 13px;
border: 2px solid black;
border-radius:3px;
}
The :not(checked) means apply these rules when newsletter is not checked.
To change the styles when newsletter is checked we change the background colour like this:
.newsletter:checked + label:before{
background-color:black;
}
This way clicking our pseudo checkbox or the text in the label will check the box (clicking the label at all sends focus to the field, so with a checkbox will check it).
Related
I want to make a CSS that will change the style of input type=radio. However, when I click another radio, the first one will still looked like that it is checked.
Here is my code and I tried to make the code as short as I can:
input[class=b][type=radio]:checked::before {
content: "";
display: block;
position: relative;
width: 12px;
height: 12px;
border: 2px solid red;
}
input[class=b][type=radio]:not(checked)::before {
content: "";
display: block;
position: relative;
width: 12px;
height: 12px;
border: 2px solid;
border-radius: 25px;
background: blue;
}
input[class=b][type=radio]:not(checked)::after {
content: "";
display: block;
visibility: hidden;
position: relative;
}
input[class=b][type=radio]:checked::after {
content: "";
display: block;
visibility: visible;
width: 5px;
height: 5px;
border: solid 2px red;
border-width: 5px 5px 5px 5px;
background: red;
border-radius: 25px;
position: relative;
transform: translateY(-15px)
}
HTML code:
<input type="radio" class="b" id="check1"><label for="check1">test</label>
<br>
<input type="radio" class="b" id="check2"><label for="check2">test2</label>
<p>They will both look like checked if you click each of them</p>
I wonder how to make it switch between checked and not checked by just using CSS. Also, I want the CSS event(such as :hover) of checked and not checked, because I want to change the style depending on its state.
The radio group must have share the same name (the value of the name attribute) to be treated as a group. Once the radio group is created, selecting any radio button in that group automatically deselects any other selected radio button in the same group. You can have as many radio groups on a page as you want, as long as each group has its own name.
<input type="radio" class="b" id="check1" name="test"><label for="check1">test</label>
<br>
<input type="radio" class="b" id="check2" name="test"><label for="check2">test2</label>
<p>They will both look like checked if you click each of them</p>
I've stumbled upon an interesting challenge in css which I can't seem to figure out.
I'm trying to make a form field, quite similar to what google does in their login form, where the label is moved relatively to to the top when the form field has focus, covering part of the form field's border. This is rather easy when the background is white, as I can just set background-color: #fff on the label.
In my case, however, I have a full-screen image background, which means my label background has to be transparent for the background-image, but has to cover the border of the form field. Is this possible?
This is my form markup:
<form>
<div class="form-field__container">
<div class="form-field__wrapper">
<label class="form-field__label input--active">Email</label>
<input class="form-field__input form-field__input--text"
type="text">
</div>
</div>
</form>
The form-field has a border around it:
.form-field__input {
border: 2px solid #e6e6e6;
}
The input--active class is set when the form field has focus, which adds the following styles to the label:
top: -10px;
left: 10px;
z-index: 1;
This moves my label over the top-border of the form field. Normally, I would then just add a background-color to the label which is the same as the page background, set a display: blockon that, and the label would cover the part of the form field border, which would solve my issue.
I do however have an image as a page background, which means I can't set a background-color on the label, because this would also cover a part of the page background. Is there any css property which allows me to have the label behave in a way that cuts out the part of the top border of the form-field which is below the label but doesn't cut away any of the background-image?
Below is an image of what I've got so far, for clarification:
I'd really appreciate the help.
Greetz derelektrischemoench
That's what you have fieldset and Legend for:
<fieldset>
<legend>
<label class="form-field__label input--active">Email</label>
</legend>
<input class="form-field__input form-field__input--text" type="text">
</fieldset>
here's an alternative hack that hides the top border completely and uses additional elements to create border to the left and right of the label text...
it's different than your approach and element structure, but it may give you some hints in how to use other elements to emulate a top border
body {
font-family: sans-serif;
background: linear-gradient(to top right, pink, orange);
}
input {
border: 2px solid black;
border-top: 0;
background: transparent;
}
input:focus {
outline: none;
border-color: blue;
}
label {
display: flex;
flex-direction: column;
}
label:focus-within .top-border-replacement:before,
label:focus-within .top-border-replacement:after {
border-color: blue;
}
label:focus-within .label-text {
color: blue;
}
.top-border-replacement {
display: flex;
}
.label-text {
position: relative;
bottom: -7px;
display: inline-block;
font-size: 12px;
padding: 0 4px;
font-weight: bold;
}
.top-border-replacement:before {
content: '';
width: 10px;
border-bottom: 2px solid black;
}
.top-border-replacement:after {
content: '';
width: 100%;
border-bottom: 2px solid black;
}
<label>
<span class="top-border-replacement">
<span class="label-text">TEST</span>
</span>
<input type="text" />
</label>
I use custom radio buttons which I need to verify via HTML5 form verification. Each option has the required attribute. The CSS :invalid selector should then color the border of the span covering the button in red as soon as the user clicks the form submit button. Unfortunately, the border gets colored on-load of the page, submit button hasn't been even clicked. Any ideas?
input[type="radio"] {
opacity: 0;
position: absolute;
z-index: -1;
}
label input[type="radio"]:checked+.form-btn-radio {
background-color: blue;
text-decoration: none;
color: white;
border: 2px solid blue;
}
label input[type="radio"]:invalid+.form-btn-radio {
border: 2px solid red;
}
.form-btn-radio {
border: 2px solid black;
color: black;
padding: 10px 25px 10px 25px;
min-width: 60px;
background-color: white;
text-decoration: none;
text-align: center;
display: inline-block;
cursor: pointer;
}
<label for="apply">
<input type="radio" id="apply" name="apply" value="easy" required>
<div class="form-btn-radio">Option 1 Name</div>
</label>
<label for="apply-external">
<input type="radio" id="apply" name="apply" value="url" required>
<div class="form-btn-radio">Option 2 Name</div>
</label>
found the issue: "If any one of the radio buttons in a group is required, the :invalid pseudo-class is applied to all of them if none of the buttons in the group is selected. (Grouped radio buttons share the same value for their name attribute.)"
developer.mozilla.org/en-US/docs/Web/CSS/:invalid
So JS is the only solution if you don't want to have one of the radios pre-selected.
I'm trying to create a custom checkbox since its hard to style the native html checkbox so below is what I have tried. The checkbox is checked and unchecked when I click the label but I cant change the border color of the label when the input checkbox is checked and unchecked. Any help, ideas, suggestions, recommendations is greatly appreciated.
input[type="checkbox"]{display:none;}
label{
border:1px solid red;
width: 15px;
height:15px;
display:block;
}
label + input:checked{
border-color: blue;
}
<div>
<label>
<input type="checkbox">
</label>
</div>
You can't do what you want with that hierarchy (without JavaScript) as there is no parent selector. You'd need to move the label after the checkbox and then use the adjacent sibling combinator +:
input[type="checkbox"] {
display: none;
}
label {
border: 1px solid red;
width: 15px;
height: 15px;
display: block;
}
input:checked + label{
border-color: blue;
}
<div>
<input id="cb" type="checkbox">
<label for="cb"></label>
</div>
I wanted to use custom checkboxes on a website so for starters, I worked with a span that I gave a custom background-color and once checked, I changed the background-color in CSS and this worked fine, the background-color changed when the checkbox was clicked on. However, once I replaced the color values by an image background url, it shows the background image in the initial state but it won't change the background-image after the value is checked. The checkbox flashes for a brief time but then continues to have the original background image.
The URL is correct, I can open the image in my browser. I've tried by giving the label rather than the span inside it the background properties but this has the same result. In the inspector in Google Chrome, the new background property is also 'active' (and the old one 'crossed out') so I just can't understand why it continues to show the old one.
The HTML:
<input type="checkbox" id="unlimited" name="unlimited" value="unlimited" />
<label for="unlimited"><span></span>Unlimited amount</label>
CSS:
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label span {
display:inline-block;
width:30px;
height:30px;
margin:-1px 4px 0 0;
vertical-align:middle;
background: url("../../static/img/checkbox1.jpg");
cursor:pointer;
}
input[type="checkbox"]:checked + label span {
background: url("../../static/img/checkbox2.jpg");
}
Edit: Fixed typo. Here is the code with just background colors, which works as you'd expect, once clicked the color changes to green, you click again it changes to blue.
CSS:
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label span {
display:inline-block;
width:30px;
height:30px;
margin:-1px 4px 0 0;
vertical-align:middle;
background: blue;
cursor:pointer;
}
input[type="checkbox"]:checked + label span {
background: green;
}
You have a typo
.input[type="checkbox"] + label span
See the full-stop/period in front of input?
input[type="checkbox"] {
display: none;
}
input[type="checkbox"] + label>span {
display: inline-block;
width: 30px;
height: 30px;
margin: -1px 4px 0 0;
vertical-align: middle;
background-image: url(http://lorempixel.com/output/abstract-q-c-30-30-4.jpg);
cursor: pointer;
}
input[type="checkbox"]:checked + label span {
background: url(http://lorempixel.com/output/people-q-c-30-30-3.jpg);
}
<input type="checkbox" id="unlimited" name="unlimited" value="unlimited" />
<label for="unlimited"><span></span>Unlimited amount</label>
The text in your question sounds like you're accidentally double clicking. But a checkbox doesn't have a double click event, so you're simply clicking twice, unselecting the checkbox the second time.
However, the code in your question shows an error in the CSS for the unchecked one, namely a extraneous dot in front of the input. If I remove that, it works normally.
(Note that I needed to replace the images by something generic.)
input[type="checkbox"] {
display: none;
}
input[type="checkbox"] + label span {
display: inline-block;
width: 30px;
height: 30px;
margin: -1px 4px 0 0;
vertical-align: middle;
background: url("http://dummyimage.com/30/FF0/000.png&text=%20");
cursor: pointer;
}
input[type="checkbox"]:checked + label span {
background: url("http://dummyimage.com/30/FF0/000.png&text=✔");
}
<input type="checkbox" id="unlimited" name="unlimited" value="unlimited" />
<label for="unlimited"><span></span>Unlimited amount</label>