This question already has answers here:
CSS: bolding some text without changing its container's size
(14 answers)
Inline elements shifting when made bold on hover
(30 answers)
Closed 6 years ago.
I've been working with cool designs lately. One of the features works in the way that while checkbox is selected, then its font changes to bold. The problem is that while changing the weight of the font it moves another elements a little bit. Any ideas how to prevent it without positioning labels it with absolute position. I've created a snipped which shows the problem.
li{
list-style:none;
display:inline-block;
margin-right:20px
}
input[type="checkbox"]:checked + label{
font-weight:bold;
}
<ul>
<li>
<input type="checkbox" id="test">
<label for="test"> My label 1
</li>
<li>
<input type="checkbox" id="test2">
<label for="test2"> My label 2
</li>
<li>
<input type="checkbox" id="test3">
<label for="test3"> My label 2
</li>
</ul>
Use text-shadow to mimic the effect:
li {
list-style:none;
display:inline-block;
margin-right:20px
}
input[type="checkbox"]:checked + label{
text-shadow: 1px 0 0 currentColor;
}
<ul>
<li>
<input type="checkbox" id="test">
<label for="test"> My label 1
</li>
<li>
<input type="checkbox" id="test2">
<label for="test2"> My label 2
</li>
<li>
<input type="checkbox" id="test3">
<label for="test3"> My label 2
</li>
</ul>
You can use a small CSS Hack for this.
Just like (this will be font specific):
input[type="checkbox"]:checked + label{
font-weight: bold;
display: inline-block;
margin-right: -2.8px; /* cancel the margin on right that is produced */
}
Have a look at the snippet below:
li{
list-style:none;
display:inline-block;
margin-right:20px
}
input[type="checkbox"]:checked + label{
font-weight: bold;
display: inline-block;
margin-right: -2.8px;
}
<ul>
<li>
<input type="checkbox" id="test">
<label for="test"> My label 1</label>
</li>
<li>
<input type="checkbox" id="test2">
<label for="test2"> My label 2</label>
</li>
<li>
<input type="checkbox" id="test3">
<label for="test3"> My label 2</label>
</li>
</ul>
Hope this helps!
maybe you will try bootstrap, with it you can say:
<div class="col-md-4">
<!--CHECKBOX HERE-->
</div>
<div class="col-md-4">
<!--CHECKBOX HERE-->
</div>
<div class="col-md-4">
<!--CHECKBOX HERE-->
</div>
Or you do it by yourself, and say:
html:
<div class="myClass">
<!--CHECKBOXES HERE-->
</div>
<div class="myClass">
<!--CHECKBOXES HERE-->
</div>
<div class="myClass">
<!--CHECKBOXES HERE-->
</div>
css:
.myClass {
width:33.33333%;
}
hope i was able to help you :)
EDIT
You can also give a div several classes:
<div class="myClass1 myClass2 myClass3">
</div>
So you dont have to add the width in every css rule.
Related
I made a label because I wanted to apply css to the checkbox.
However, the label is not working except for the checkbox whose id is 'checkall'.
<div class="divCenter cartDiv">
<ul>
<li class="selectallforcart">
<div class="hanadiv">
<input type="checkbox" id="checkall" class="check" value="0"/>
<label for="checkall"></label> selelct All
</div>
</li>
</ul>
<% ArrayList<LikePdVO> likeList = (ArrayList<LikePdVO>)(request.getAttribute("likeList"));
for(LikePdVO vo : likeList) {
String arr[] = vo.getPhoto().split("\\*");
%>
<div class="cartProduct" id="<%=vo.getPd_id()%>">
<div class="fl cartCheck">
<input type="checkbox" class="check" name="<%=vo.getName()%>"
value="<%=vo.getOrder_price()%>" id="<%=vo.getPd_id()%>">
<label for="<%=vo.getPd_id()%>">
</label>
</div>
this is my code.
.cartDiv input[type=checkbox] + label {
display: inline-block;
width: 20px;
height: 20px;
border: 2px solid #bcbcbc;
cursor: pointer;
}
.cartDiv input[type=checkbox]:checked + label {
background-color: #866744;
}
.cartDiv input[type=checkbox] {
display: none;
}
And this is CSS.
When you mean that the label is not working, I assume you mean that clicking on it doesn't cause the checkbox to be checked?
Either way, the label's text needs to be within the element, so it should be:
<label for="checkall">Select all</label>
And your other labels contain no text, so the label itself is probably quite small, despite the CSS, and therefore not clickable.
You need to set label text in the label tag not out side of it.
<div class="divCenter cartDiv">
<ul>
<li class="selectallforcart">
<div class="hanadiv">
<input type="checkbox" id="checkall" class="check" value="0" />
<label for="checkall">selelct All</label>
</div>
</li>
</ul>
</div>
This HTML code shows two checkboxes next to each other
<div id="mr_mrs">
<ul class="mr_mrs form-no-clear">
<li id="mr" class="popular-category">
<label for="Mr" id="mr">Mr</label>
<span><input name="Mr" id="Mr" type="checkbox" /></span>
<div class="clearboth"></div>
</li>
<li id="mrs" class="popular-category">
<label for="Mrs" id="mrs">Mrs</label>
<span><input name="Mrs" id="Mrs" type="checkbox" /></span>
<div class="clearboth"></div>
</li>
</ul>
</div>
I want to customize the checkboxed to square and on selecting the checkbox fill some color to it. IMAGE BELOW
How can this be done ?
To sit the two controls next to each other, set their display to inline block: display: inline-block. (Default for div is block display.)
To enable the user to check the box by clicking the label text, there are a couple options. Both are shown in the example below.
<label> is parent to the text and checkbox.
Use <label for="myId">some text</label><input id="myId"> to associate the label to the input.*
*Note: when using the for/id method, it's label-for and input-id. I noticed in the example the id was applied to both elements, an id should be unique to the page.
.checkbox {
margin: 4px;
display: inline-block;
}
<div class="checkbox">
<label>Box 1<input type="checkbox"></label>
</div>
<div class="checkbox">
<label for="myId">Box 2</label>
<input id="myId" type="checkbox">
</div>
May be i don't understand fully plus selector,
What i want, when user click on radio button home, div one should get displayed,
and when user click on radio button about, div two should get displayed, it did not work,
So i strip down the code, where is the problem, with this code i accepted div one to get displayed as home is by default checked. But it did not happened, so i know where is the problem but i dont know why,
Please read the comment, in the code, as i said which line is giving the problem hint it's css last section,
HTML CODE
<div class="container">
<input type="radio" name="option" id="home" checked />
<input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
CSS CODE
.navigation {margin-top:20px;}
.link{cursor:pointer;}
/*making div display*/
.one,.two{
display:none;
}
/*
###This line is not working## want to display div, if user click on radio
button
*/
#home:checked +.container > .one{
display:block;
}
if you want to run the code here is the code pen link https://codepen.io/arif_suhail_123/pen/KvdWey
.container is not a sibling of #home.
To select the element in question, when #home is checked, you can use the ~, which is the general sibling selector:
#home:checked ~ .display > .one
.navigation {margin-top:20px;}
.link {cursor:pointer;}
.one, .two {
display:none;
}
#home:checked ~ .display > .one {
display:block;
}
#about:checked ~ .display > .two {
display: block;
}
<div class="container">
<input type="radio" name="option" id="home" checked />
<input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
The + is the adjacent sibling combinator. Which requires:
The elements to be siblings
The selector on the left of + is the first positioned element
The selector on the right of + is the selector that follows.
There must be no other elements between them.
In the following demo:
Each radio was moved in front of the div it's associated with.
Each radio is display:none since there's no need to show them because the interface are the labels.
Demo
input[name='option'],
.one,
.two {
display: none
}
#home:checked+.one {
display: block;
}
#about:checked+.two {
display: block;
}
<div class="container">
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<input type="radio" name="option" id="home" checked />
<div class="one">
<h3>This is first</h3>
</div>
<input type="radio" name="option" id="about" />
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
I believe for the plus operator to work the element has to be the immediate next sibling - So in this case the .one div would have to immediately follow the #home label, and the css would have to be:
#home:checked + .one{
display:block;
}
The html:
<div class="container">
<input type="radio" name="option" id="home" checked />
<div class="one">
<h3>This is first</h3>
</div>
<input type="radio" name="option" id="about" />
...
+ Selector : The element+element selector is used to select elements that is placed immediately after (not inside) the first specified element.
~ Selector : The element1~element2 selector matches occurrences of element2 that are preceded by element1.
So,you must use ~ instead of +.
.navigation {
margin-top:20px;
}
.link{
cursor:pointer;
}
.one,.two{
display:none;
}
#home:checked ~ .display > .one{
display:block;
}
#about:checked ~ .display > .two{
display:block;
}
<div class="container">
Home: <input type="radio" name="option" id="home" checked />
About: <input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
The order of elements is important when using this selector.
So to use the ~ operator the element should be after the first part.
Ex.
input[type=radio]:checked ~ label {
display: none;
}
The Html should be:
<div class="radio-groupe">
<input type="radio" name="water" id="choice-2" value="more-than-8-cups-a-day">
<label for="choice-2">More</label>
</div>
and not:
<div class="radio-groupe">
<label for="choice-2">More</label>
<input type="radio" name="water" id="choice-2" value="more-than-8-cups-a-day">
</div>
Bear with me as I'm not well versed with front end design. How can I get the following checkboxes aligned and stacked neatly as in the picture?
Right now I have them in a row of 9 checkboxes but they're not aligned and are spaced according to the length of the text.
As seen here:
JSFiddle
HTML:
<div class="tags">
<div class="col">
<label>
<input type="checkbox" rel="accounting" />Accounting</label>
<label>
<input type="checkbox" rel="courier" />Courier</label>
<label>
<input type="checkbox" rel="project-management" />Project Management</label>
<label>
<input type="checkbox" rel="video-games" />Video Games</label>
<label>
<input type="checkbox" rel="dentistry" />Dentistry</label>
<label>
<input type="checkbox" rel="librarian" />Librarian</label>
<label>
<input type="checkbox" rel="programmer" />Programmer</label>
<label>
<input type="checkbox" rel="architect" />Architect</label>
</div>
<div class="col">
<label>
<input type="checkbox" rel="photographer" />Photographer</label>
<label>
<input type="checkbox" rel="it" />IT</label>
<label>
<input type="checkbox" rel="artist" />Artist</label>
<label>
<input type="checkbox" rel="web-developer" />Web Developer</label>
<label>
<input type="checkbox" rel="web-designer" />Web Designer</label>
<label>
<input type="checkbox" rel="neurologist" />Neurologist</label>
<label>
<input type="checkbox" rel="veterinarian" />Veterinarian</label>
<label>
<input type="checkbox" rel="teacher" />Teacher</label>
</div>
<div class="col">
<label>
<input type="checkbox" rel="character-animator" />Character Animator</label>
<label>
<input type="checkbox" rel="salesman" />Salesman</label>
<label>
<input type="checkbox" rel="telemarketing" />Telemarketeing</label>
<label>
<input type="checkbox" rel="construction" />Construction</label>
<label>
<input type="checkbox" rel="lawyer" />Lawyer</label>
<label>
<input type="checkbox" rel="actor" />Actor</label>
<label>
<input type="checkbox" rel="policeman" />Policeman</label>
<label>
<input type="checkbox" rel="forestry" />Forestry</label>
</div>
</div>
<!-- end tags -->
CSS:
.filter {
width: 850px;
padding: 25px;
border: 1px solid black;
margin: 25px;
float: left;
}
.col {
width: 100%;
display: block;
margin-right: 10px;
}
label{
vertical-align: top;
float: left;
width: 160px;
}
Edit
I'd use a table layout. Simply add the following CSS:
.tags {
display: table;
}
.col {
display: table-row;
}
.col label {
display: table-cell;
}
Updated fiddle: http://jsfiddle.net/5sz6qdos/13/
Others might use flexbox but I have never used it myself, so not sure how to implement that.
I would probably go a different route, however, to make it more semantic and setup to be more flexible:
<ul class="tags">
<li class="tag"><label>...
....<!--put ALL of your label elements in a single parent element-->
</ul>
and
.tag {
display: block;
float: left;
width: 25%; /*for if you want 4 columns, or*/
width: 120px; /*if you want specific widths*/
}
.tags:after { content: ""; display: table; clear: both; } /*clearfix*/
Using percentages will flex all the columns, or using fixed pixels will allow the elements to flow (my preference).
first of all change your html code like that:
<input type="checkbox" name="accounting" id="accounting" rel="accounting" />
<label for="accounting">Accounting</label>
Do this for every checkbox.
Then Do the follwoing css
.tags{clear:both}
.col{float:left;width:120px;/*adjust your width */}
.col lable, .col input{display:inline-block;}
.col label{width:120px;/*adjust your width */}
.col input{width:20px;/*adjust your width */}
For starters, you're using the label tag wrong. They should not have an input inside them. And how I would do what you're trying to do is something like this:
<div class="col">
<label>Job</label>
<input>
<label>Job</label>
<input>
</div>
<div class="col">
<label>Job</label>
<input>
<label>Job</label>
<input>
</div>
Then in your CSS...
.col {
display: inline-block;
width: 33.3%;
vertical-align: top;}
label {
display: inline-block;
width: 100px;}
input {
display: inline-block;
width: calc(100% - 100px);}
To make sure the columns, labels, and inputs all touch nicely, you have to remove the whitespace. You can either remove it from the code itself, connect elements with comment tags, or set the font-size to 0 on the container. And it wouldn't hurt to throw this in your CSS:
* {
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;}
So you're kinda having the same problem I mentioned belwo... the text can't be what you're basing off of. You have to set an actual width.
http://jsfiddle.net/tueez7bx/6/
This works, but extends past your limits. You should really consider my other options I provide below.
Also, you can't use ids the way you are. That is incorrect. You'll need to change all the #col to .col.
Second Edit
So based on a comment provided below I have come to realize the goal of what you're trying. Sadly because of the flexibility of text and items you would need to have defined widths for each to line up perfectly. If you don't know what the longest string will be then you will have a problem with risking that string breaking below the words or running into the next checkbox.
You can see that here: http://jsfiddle.net/5sz6qdos/22/
However.. if you extend the width of your container and just have a greater width you will be able to avoid this...
You can see what I am imagining you're asking here: http://jsfiddle.net/5sz6qdos/28/
Before Edit
You're doing well overall...
http://jsfiddle.net/5sz6qdos/3/
.col {
width: 160px;
float: left;
margin-right: 10px;
}
label{
display: block;
}
This does define a width and doesn't allow for full flexibility. It does however give you the option of going to responsive layouts with this data.
To get the boxes the way you want them you cannot use pure CSS. You will need a mix of Javascript. Checkboxes themselves can not be edited with CSS directly. You will need to hide the checkbox with display: none and add an image for the boxes themselves. Once you have done this you will need to use jQuery to check and uncheck each box. That is the only way to get the exact look.
SO has a lot of answers out there for the jQuery part.
Thanks to all for the knowledge and push in the right direction.
Here is the working solution I found while setting line widths.
JSFiddle
HTML
<div class="filter">
<h3 style="text-align: center;">Blog Profession Filters</h3>
<ul class="checkbox">
<li>
<input type="checkbox" id="cb1" rel="accounting" value="accounting" />
<label for="cb1">Accounting</label>
</li>
<li>
<input type="checkbox" id="cb2" rel="project-management" value="project-management" />
<label for="cb2">Project Management</label>
</li>
<li>
<input type="checkbox" id="cb3" rel="information technology" value="information-technology" />
<label for="cb3">Information Technology</label>
</li>
<li>
<input type="checkbox" id="cb4" rel="courier" value="courier" />
<label for="cb4">Courier</label>
</li>
<li>
<input type="checkbox" id="cb5" rel="video-games" value="video-games" />
<label for="cb5">Video Games</label>
</li>
<li>
<input type="checkbox" id="cb6" rel="web-development" value="web-development" />
<label for="cb6>">Web Development</label>
</li>
<li>
<input type="checkbox" id="cb7" rel="veterinarian" value="veterinarian" />
<label for="cb6>">Veterinarian</label>
</li>
<li>
<input type="checkbox" id="cb8" rel="web-designer" value="web-designer" />
<label for="cb6>">Web Designer</label>
</li>
<li>
<input type="checkbox" id="cb9" rel="attorney" value="attorney" />
<label for="cb9>">Attorney</label>
</li>
<li>
<input type="checkbox" id="cb10" rel="medical-practitioner" value="medical-practitioner" />
<label for="cb10>">Medical Practitioner</label>
</li>
<li>
<input type="checkbox" id="cb11" rel="telemarketing" value="telemarketing" />
<label for="cb11>">Telemarketing</label>
</li>
<li>
<input type="checkbox" id="cb12" rel="construction" value="construction" />
<label for="cb12>">Construction</label>
</li>
</ul>
</div>
<div id="results">
<ul class="results">
<li class="accounting" style="list-style-type:none"> Accounting
</li>
<li class="project-management" style="list-style-type:none"> Game QA Project Management
</li>
<li class="information-technology" style="list-style-type:none"> Information Technology
</li>
<li class="courier" style="list-style-type:none"> Courier / Parcel Delivery
</li>
<li class="video-games" style="list-style-type:none"> Video Games
</li>
<li class="web-development" style="list-style-type:none"> Web Development
</li>
<li class="veterinarian" style="list-style-type:none"> Veterinarian
</li>
<li class="web-designer" style="list-style-type:none"> Wed Designer
</li>
<li class="attorney" style="list-style-type:none"> Attorney
</li>
<li class="medical-practitioner" style="list-style-type:none"> Medical Practitioner
</li>
<li class="telemarketing" style="list-style-type:none"> Telemarketing
</li>
<li class="construction" style="list-style-type:none"> Construction
</li>
</ul>
</div>
CSS
.filter {
width: 850px;
padding: 25px;
border: 1px solid black;
margin: 25px;
}
ul.checkbox {
margin: 0;
padding: 0;
margin-left: 20px;
list-style: none;
}
ul.checkbox li input {
margin-right: .25em;
}
ul.checkbox li {
border: 1px transparent solid;
display:inline-block;
width:12em;
}
ul.checkbox li label {
margin-left:;
}
ul.checkbox li:hover, ul.checkbox li.focus {
background-color: lightyellow;
border: 1px gray solid;
width: 12em;
}
.results {
width: 850px;
padding: 25px;
border: 1px solid black;
margin: 25px;
}
#one input[type="checkbox"] + #Down{display:none;}
#one input[type="checkbox"]:checked + #Down{display:none;}
<ul>
<li>
<input name="one" type="checkbox" value="" class="inputCheck" id="one" />
<div class="fl-L listItemsPadd">Sample</div>
<label class="fl-L downloadPadd" id="Down">Downloadmain form</label>
<div class="clearboth"></div>
</li>
</ul>
I was trying to use :checked option in CSS. But because the label tag is not right beside the checkbox this is not working.
My main idea is to on check the "Downloadmain form" need to be displayed.
other wise it is not displayed.
Could any one please help. Thanks
You could achieve this by using the CSS ~ general sibling selectors like this:
JSFiddle - DEMO
input[type="checkbox"]#one ~ #Down {
display:none;
}
input[type="checkbox"]#one:checked ~ #Down {
display:block;
}
<ul>
<li>
<input name="one" type="checkbox" value="" class="inputCheck" id="one" />
<div class="fl-L listItemsPadd">Sample</div>
<label class="fl-L downloadPadd" id="Down">Downloadmain form</label>
<div class="clearboth"></div>
</li>
</ul>