How do I apply a border around a checkbox when checked? - html

I want to check a checkbox and apply a 1px border around it via an enclosing span tag.
I am a bit stuck on how to detect the checked css property and select the parent span to apply a property to the span? Can I achieve this with only CSS? Below is what I have so far.
CSS(Sass)
input.checkbox
opacity: 0
span.check
width: 16px
height: 16px
border: 2px solid black
display: inline-block
position: relative
input.checkbox:checked <--once i detect checked I am unsure how to apply the border to the parent span tag
background: black
position: absolute
top: -8px
left: 0px
HTML
<span class='check'><input type="checkbox" class="checkbox" name="" /></span>

Simply NO,
Cascading does not works like that way. You can't select parent in CSS. But yes you can do this by using sass/scss. But it will not work as you expected.
It will just create a parent wrapper class. But it will not behave using the state of the child element. Because after compiling it's just CSS
We might get a parent selector in the future, may be in CSS4. So we are keeping our fingers crossed. :)
e.g.
SASS
input.checkbox
opacity: 0
span.check
width: 16px
height: 16px
border: 2px solid black
display: inline-block
position: relative
input.checkbox:checked
background: black
position: absolute
top: -8px
left: 0px
span.check &
border: 2px solid black
Output CSS,
input.checkbox {
opacity: 0;
}
span.check {
width: 16px;
height: 16px;
border: 2px solid black;
display: inline-block;
position: relative;
}
input.checkbox:checked {
background: black;
position: absolute;
top: -8px;
left: 0px;
}
span.check input.checkbox:checked {
border: 2px solid black;
}
But You can achieve this by using CSS siblings selector,
HTML,
<label>
<input class="checkbox" type="checkbox">
<span class="check"></span>
</label>
CSS,
label {
position: relative;
}
input.checkbox {
opacity: 1;
position: absolute;
}
input.checkbox:checked + span.check {
width: 16px;
height: 16px;
border: 2px solid black;
display: inline-block;
position: absolute;
}
Working example: https://jsfiddle.net/HA3bQ/167/

No way to select a parent with CSS only (until CSS4), so you must use JS..
See this post that talking about it here.

I have not seen that done using CSS, only within the javascript wherever the checkbox resides.

You can put some element after input
<label>
<input class="checkbox" type="checkbox">
<span class="checkmark></span>
</label>
And style with pseudo class :checked
.checkbox:checked ~ .checkmark {
// There goes styles for custom checkmark
}
Recommend look at this site for full examples

the simple answer is just use outline in css style
input.checkbox:checked {
outline: 2px solid black;
}
<span class='check'><input type="checkbox" class="checkbox" name="" /></span>

Related

Custom checkbox stretches vertically when text wraps

I have a styled checkbox that is the child of a container along with some text. The reason the checkbox and text are children of a parent is so that they can sit next to each other and be centered vertically on the UI. This has worked fine for me; however, I've noticed that the checkbox starts to change from a perfect circle into more of an oval as text starts to wrap into multiple lines (on mobile the text is two lines long and on desktop it is only one line). How could I fix this so that the checkbox does not stretch as the text wraps into multiple lines? Below is my html and styling, thank you.
.opt-in {
display: flex;
align-items: center;
color: #f4a11e;
}
input[type='checkbox'] {
position: relative;
margin: 17px 15px 0 0;
cursor: pointer;
width: 20px;
height: 20px;
-webkit-appearance: none;
background: transparent;
border: 1px solid #f4a11e;
outline: none;
border-radius: 50%;
transition: 0.5s;
}
input[type='checkbox']:before {
content: '';
position: absolute;
top: 45%;
left: 50%;
width: 4px;
height: 10px;
opacity: 0;
border-right: 1px solid #f4a11e;
border-bottom: 1px solid #f4a11e;
transform: translate(-50%, -50%) rotateZ(40deg);
transition: 0.2s;
}
input:checked[type='checkbox']:before {
opacity: 1;
}
<div class="opt-in">
<input type="checkbox" v-model="optIn" id="checkbox" />
<label for="checkbox">Opt-in to receive the latest cloud insights and industry deep dives.</label>
</div>
In the CSS code in the input[type='checkbox'] {} section, try using min-width: 20px; rather than width: 20px;.
Worked for me in a test implementation with copy/pasted code although I had to set max-width in the properties of the opt-in div to test it.
I don't have a technical explanation but I believe it has something to do with the relative position or the display: flex overriding the specified width/height in pixels.

How can I create a clickable event that changes position attributes in the CSS file of an outer div using only html and CSS?

I have a DIV container. inside it, I have a button. I want the button to be used to change the DIV's position attributes. I want the button click to shift the entire container to the left.
I have to do this without any scripting; only CSS and HTML.
is this possible?
perhaps with buttonclick:active{stuff}?
You can use the checkbox hack
#move-div {
display: none;
}
#move-div:checked + .movable {
left: -50px;
}
.movable {
position: relative;
background-color: #FF0000;
padding: 10px;
}
.button {
display: inline-block;
padding: 5px;
background-color: #FFF;
border-radius: 5px;
color: #000;
box-shadow: 3px 3px 5px 3px #AAA;
}
<input id="move-div" type="checkbox">
<div class="movable">
<label class="button" for="move-div">Move the div</label>
</div>

How to constrain CSS to when a contained input has focus?

Assume I have a form input structured as follows:
<div class="wrapper">
<span class="icon icon-search"></span>
<input type="text"/>
</div>
is there a way, with CSS3, to apply a red border around the .wrapper div on a focus state on the input element?
.wrapper input:focus {
border solid thin red;
}
puts the border on the input field but I want it on the containing div.
You're looking for a css parent selector. Unfortunately that isn't currently available. The exact functionality you're looking for would need JavaScript or alternative HTML.
It looks like you want the border to surround the icon and the field but currently it is only surrounding the field? My suggestion would be to use the sibling selector like so: (From my example below i've moved the icon after the input)
* {
box-sizing: border-box; /* Helps with sizing calculations */
}
.wrapper {
position: relative;
}
.icon {
width: 20px;
height: 20px;
position: absolute;
background-color: blue;
left: 200px;
top: 0;
border: solid 1px transparent;
border-left: none;
}
input {
width: 200px;
height: 20px;
border: solid 1px blue;
border-right: none;
}
input:focus {
border-color: red;
outline: 0;
}
input:focus + .icon {
border-color: red;
}
<div class="wrapper">
<input type="text"/>
<span class="icon icon-search"></span>
</div>

How to disable :hover on descendants?

Link:
http://jsbin.com/EFAlace/3/edit?html,css,output
HTML:
<a href='#' class='tooltip-parent'>Hover over me!
<span class='tooltip-container'>
<span class='tooltip'>
<a style='href='#'>Weird link</a><br>
One<br>
Two<br>
Three<br>
Four<br>
</span>
</span>
</a>
.tooltip-container added for absolute positioning, for 'reset' tooltip position.
CSS (LESS):
.tooltip-container {
position: absolute;
}
a:hover {
background-color: grey;
}
.tooltip-parent {
display: inline-block;
.tooltip {
width: 150px;
display: none;
position:relative;
border:1px solid blue;
&:before, &:after {
content: '';
top: -20px;
left: 20%;
position: absolute;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-bottom: 20px solid white;
margin-left: -20px;
}
&:before {
border-left: 23px solid transparent;
border-right: 23px solid transparent;
border-bottom: 23px solid;
margin-left: -23px;
border-bottom-color: inherit; /* Can't be included in the shorthand to work */
top: -23px;
}
}
&:hover .tooltip {
padding: 10px;
display: inline-block;
top: 20px;
}
}
ul {
list-style:none;
margin: 0; padding:0;
li {margin: 0; padding:0;}
}
:before and :after: things are for triangle at the top of the tooltip. Google on 'CSS triangle pseudo elements'
I have been experimenting with CSS-only tooltip, which pops out on hover over a parent element. You can see working example at jsBin. But I encountered the strange issue - when I add anchor inside tooltip - html markup blows out, just uncomment this code <!--<a style='href='#'>Weird link</a><br>--> in HTML pane and you will see what Im talking about. And then see at markup structure - browser just places HTML content of .tooltip outside of and element to which that tooltip is attached.
Thats pretty unclear behavior, any thoughts will be appreciated.
first i see a problem of anchor inside anchor, that's not allowed by html. try to rearrange your html tags in a better way.
secondly about the weird link, which is:
<a style='href='#'>Weird link</a>
why is it
style='href='#'

CSS3 radio button not working in Firefox

I'm trying to style a radio button using css3 pseudo elements. In works great in chrome but it jumps back to the default styling in firefox. Any idea?
Here's a fiddle: JSFIDDLE
The HTML:
<input class="fancy" name="cc1" type="radio" checked/>
<input class="fancy" name="cc1" type="radio"/>
The CSS:
input[type="radio"].fancy:before {
content:"";
width: 24px;
height: 24px;
background-color: #1f1f1f;
display: block;
border-radius: 50%;
position: relative;
top: -6px;
left: -6px;
box-shadow: 0 1px 2px rgba(255,255,255,.1),
inset 0 2px 2px rgba(0,0,0,.8);
}
input[type="radio"].fancy:checked:before {
visibility: visible;
content:"";
width: 24px;
height: 24px;
background-color: #1f1f1f;
display: block;
border-radius: 50%;
position: relative;
top: -6px;
left: -6px;
box-shadow: 0 1px 2px rgba(255,255,255,.2),
inset 0 2px 2px rgba(0,0,0,.8);
}
input[type="radio"].fancy:checked:after {
visibility: visible;
content:"";
width: 14px;
height: 14px;
display: block;
border-radius: 50%;
position: relative;
top: -25px;
left: -1px;
background: yellowgreen;
}
I'm trying to avoid background-images
Unfortunately, what you're trying to do is not valid -- ::before and ::after do not work on input elements (any input; it's not just restricted to radio buttons).
Proof available here: http://jsfiddle.net/LvaE2/1/ -- even in the most simple case, it doesn't work.
It also doesn't work in IE or Opera. The fact that it does work in Chrome is because Chrome is going beyond the spec in allowing it.
Your best bet is to do your styling on a label element that is linked to the actual radio button using the for attribute, and then set the radio button itself to display:none;. This is how everyone else does it.
Related question here: Which elements support the ::before and ::after pseudo-elements?
Hope that helps.
CSS3 Input radio will working in Firefox: version > 80 if added style appearance
input[type=checkbox] { appearance:initial; }
Please see the Snippet:
input[type=radio] { appearance:initial; }
input[type=radio]:after{
content: "after content";
width: 100px; height: 100px; display:block;
background: red;
}
input[type=radio]:checked:after{
content: "after content";
width: 100px; height: 100px; display:block;
background: blue;
}
<input type="radio">
<input type="radio">
<input type="radio">