This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 4 years ago.
I'm trying to skin a checkbox as a button with just HTML and CSS, similar to the BS_PUSHLIKE style in Windows. I've got a pretty good look so far, but I can't figure out how to remove some seemingly arbitrary padding/margin around the elements. This has horizontal padding between the "buttons." If I set the display of the .switch elements to table-cell, I can remove the horizontal padding, but then get a tonne of vertical padding.
:root {
--button-color: #ccc;
}
.switch {
display: inline-block;
background: var(--button-color);
vertical-align: top;
-webkit-user-select: none; /* Safari 3.1+ */
-moz-user-select: none; /* Firefox 2+ */
-ms-user-select: none; /* IE 10+ */
user-select: none; /* Standard syntax */
}
.switch input {
display: none;
}
.button {
font-family: sans-serif;
border: 2px outset var(--button-color);
display: table-cell;
vertical-align: middle;
text-align: center;
padding: 5px 10px;
}
input:checked + .button {
padding: 7px 8px 3px 12px;
border-style: inset;
background-color: rgba(255, 255, 255, 0.5);
}
<label class="switch">
<input type="checkbox">
<div class="button">Checkbox</div>
</label><br />
<label class="switch">
<input type="checkbox">
<div class="button">As</div>
</label>
<label class="switch">
<input type="checkbox">
<div class="button">Button</div>
</label><br /><br />
<label class="switch">
<input type="radio" name="example">
<div class="button">Radio</div>
</label>
<label class="switch">
<input type="radio" name="example">
<div class="button">As</div>
</label>
<label class="switch">
<input type="radio" name="example">
<div class="button">Button</div>
</label>
Those are the auto-converted "space" character. In HTML, any kind of white space character (space, new line, tab, etc) between 2 inline-block elements will be converted into 1 space character. In this case in the HTML there was a line break so the whitespace is inserted.
To remove those whitespaces, you can choose one of these several methods.
Related
This question already has answers here:
How to remove jQuery Mobile styling?
(5 answers)
Closed 2 years ago.
I want some customized radio buttons in Jquery mobile page. So I used CSS to customize the radio buttons. But unfortunately, the used CSS is overided by the jquery default css.
My HTML code snippet is as follows.
<div class="answers">
<label class="container-quiz">Doe
<input type="radio" checked="checked" value="doe" name="answer2">
<span class="checkmark"></span>
</label>
<label class="container-quiz">Kit
<input type="radio" value="kit" name="answer2">
<span class="checkmark"></span>
</label>
<label class="container-quiz">Buck
<input type="radio" value="buck" name="answer2">
<span class="checkmark"></span>
</label>
<label class="container-quiz">Hare
<input type="radio" value="hare" name="answer2">
<span class="checkmark"></span>
</label>
</div>
My CSS is as follows
.container-quiz {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default radio button */
.container-quiz input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: white;
border-radius: 50%;
border: 2px solid gray;
}
/* On mouse-over, add a grey background color */
.container-quiz:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the radio button is checked, add a blue background */
.container-quiz input:checked ~ .checkmark {
background-color: white;
border: 2px solid gray;
}
/* 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-quiz input:checked ~ .checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container-quiz .checkmark:after {
top: 3px;
left: 3px;
width: 19px;
height: 19px;
border-radius: 50%;
background: #f4364c;
}
When I remove the jquery js scripts from the <head> I get the result which I want and it is like below.
But when I have jquery I don't get the result I want.
Your style is completely diffrent from that of JQM and You already have a complete CSS for Your radio buttons.
So the fastest way is to tell JQM to avoid to enhance that elements. Add data-role="none" to Your inputs.
<div class="answers">
<label class="container-quiz">Doe
<input data-role="none" type="radio" checked="checked" value="doe" name="answer2">
<span class="checkmark"></span>
</label>
<label class="container-quiz">Kit
<input data-role="none" type="radio" value="kit" name="answer2">
<span class="checkmark"></span>
</label>
<label class="container-quiz">Buck
<input data-role="none" type="radio" value="buck" name="answer2">
<span class="checkmark"></span>
</label>
<label class="container-quiz">Hare
<input data-role="none" type="radio" value="hare" name="answer2">
<span class="checkmark"></span>
</label>
</div>
Otherwise, You need to override the JQM .ui-radio-off, .ui-radio-on, .ui-radio-off:after, .ui-radio-on:after styles (the standard radio buttons are already hidden by JQM).
Use !important property on properties which are being overriden
Context: I am attempting to use the radio hack to toggle what text is viewed within the .tabinfo div, but my radios and the text whose display attribute I want to change are located in different divs.
Problem: Is it possible to use pure CSS selectors to select the #text element by clicking on a nested radio?
Reference Code: I am using the bootstrap layout and have created the following HTML code:
<div class="col-xs-2">
<input id="tab1" type="radio" name="tabs">
<label for="tab1">Foo</label>
</div>
<div class="col-xs-2">
<input id="tab2" type="radio" name="tabs">
<label for="tab2">Bar</label>
</div>
<div class="col-xs-2">
<input id="tab3" type="radio" name="tabs" checked>
<label for="tab3">Foo Bar</label>
</div>
<div class="col-xs-12">
<div class="tabinfo">
<div id="text1">
</div>
<div id="text2">
</div>
<div id="text3">
</div>
</div>
</div>
And the following CSS:
label {
border: solid;
border-top-right-radius: 10px;
border-top-left-radius: 10px;
border-bottom: none;
border-color: rgb(211,211,205);
border-width: 2px;
color: rgb(12,174,175);
background-color: rgb(247,247,247);
}
input:checked + label {
background-color: #fff;
color: rgb(94,94,94);
}
label:hover {
cursor: pointer;
}
.tabinfo {
border: solid;
border-color: rgb(211,211,205);
border-width: 2px;
border-top-right-radius: 10px;
}
#tab1:checked ~ .col-xs-12 .tabinfo #text1,
#tab2:checked ~ .col-xs-12 .tabinfo #text2,
#tab3:checked ~ .col-xs-12 .tabinfo #text3 {
display: block!important;
}
As you probably already guessed, the above does not work since the #texts and the #tabs are located in different divs. Is there any workaround or any solution without breaking the Bootstrap layout?
A brittle solution can be used, but this involves moving the <input> elements away from the <label> elements, and you specify one requirement of any HTML changes is that any change
…does not break the [Bootstrap] layout.
I don't think my changes break that layout, but I'm not entirely sure, so you will need to evaluate this yourself.
That preamble aside, however, I've modified your HTML to the following:
<input id="tab1" type="radio" name="tabs" />
<input id="tab2" type="radio" name="tabs" />
<input id="tab3" type="radio" name="tabs" />
<div class="col-xs-2">
<label for="tab1">Foo</label>
</div>
<div class="col-xs-2">
<label for="tab2">Bar</label>
</div>
<div class="col-xs-2">
<label for="tab3">Foo Bar</label>
</div>
<div class="col-xs-12">
<div class="tabinfo">
<div id="text1">
</div>
<div id="text2">
</div>
<div id="text3">
</div>
</div>
</div>
This approach allows us to take advantage of the <label> element's ability to check/uncheck its associated <input> element regardless of where in the document it may be located (so long as the for attribute identifies the id of that associated <input>); placing the <input> elements ahead of the content allows us to use sibling combinators to find the elements containing the relevant content to style.
On the assumption that you wish to retain the visual effect of the <input> being checked, or otherwise, we've also used CSS generated content to emulate a checked or unchecked radio; this could use some fine tuning, though:
/* Here we hide all <div> elements within the .tabinfo
element, and also all <input> elements whose 'name'
attribute is equal to 'tabs' and whose 'type' is
equal to 'radio': */
.tabinfo div,
input[name=tabs][type=radio] {
display: none;
}
/* This styles the generated content of the ::before
pseudo-element to show the attribute-value of the
element's 'id' attribute; purely for the purposes
of this demo: */
div[id^=text]::before {
content: attr(id);
}
/* Styling the generated content, the ::before pseudo-
element, of the <label> elements, in order to
emulate the moved radio <input>: */
label::before {
/* An empty string, content is required in order for
the pseudo-element to be visible on the page: */
content: '';
/* To allow the pseudo-element to have specified
width and height values: */
display: inline-block;
height: 1em;
width: 1em;
/* To include the border, and any padding, widths
in the calculations for the element's size: */
box-sizing: border-box;
background-color: #eee;
border: 1px solid #999;
/* In order for the pseudo-radio to have a round
shape/border: */
border-radius: 50%;
margin-right: 0.2em;
}
/* This selector styles the <label> element whose 'for'
attribute is equal to 'tab1', which is a child of
the div.col-xs-2 element which itself is a general
sibling of the #tab1 element when that element is
checked; this is the 'checked' style of the pseudo-
'radio' generated content: */
#tab1:checked~div.col-xs-2>label[for=tab1]::before {
background-color: #666;
box-shadow: inset 0 0 0 3px #fff;
}
/* This selects the element with an id of 'text1',
inside of a <div> with the class of 'col-xs-12',
which is a general sibling of the '#tab1' element
when that element is checked: */
#tab1:checked~div.col-xs-12 #text1 {
/* Here we make the content of that element visible: */
display: block;
}
#tab2:checked~div.col-xs-2>label[for=tab2]::before {
background-color: #666;
box-shadow: inset 0 0 0 3px #fff;
}
#tab2:checked~div.col-xs-12 #text2 {
display: block;
}
#tab3:checked~div.col-xs-2>label[for=tab3]::before {
background-color: #666;
box-shadow: inset 0 0 0 3px #fff;
}
#tab3:checked~div.col-xs-12 #text3 {
display: block;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<input id="tab1" type="radio" name="tabs" />
<input id="tab2" type="radio" name="tabs" />
<input id="tab3" type="radio" name="tabs" />
<div class="col-xs-2">
<label for="tab1">Foo</label>
</div>
<div class="col-xs-2">
<label for="tab2">Bar</label>
</div>
<div class="col-xs-2">
<label for="tab3">Foo Bar</label>
</div>
<div class="col-xs-12">
<div class="tabinfo">
<div id="text1">
</div>
<div id="text2">
</div>
<div id="text3">
</div>
</div>
</div>
JS Fiddle demo.
As you can see from the rules formed to show the elements related to the checked <input> elements those rules require some precision and repetition, since CSS has no concept of this, so, given a data-affectedby attribute whose value might be set to the id of the related <input>, there's no way we can have a rule along the lines of:
input[id^=tab]:checked ~ .col-xs-12 [data-affectedby=this.id]
This will be very difficult (perhaps impossible) when working with divs on different levels.
If you flatten the HTML structure a little you might be able to achieve something close to what you are looking for. Note though, it means getting rid of most of the Bootstrap helper layout divs.
Example HTML:
<input id="tab1" type="radio" name="tabs">
<label for="tab1">Foo</label>
<input id="tab2" type="radio" name="tabs">
<label for="tab2">Bar</label>
<input id="tab3" type="radio" name="tabs" checked>
<label for="tab3">Foo Bar</label>
<div id="text1" class="tabinfo">text1</div>
<div id="text2" class="tabinfo">text2</div>
<div id="text3" class="tabinfo">text3</div>
Example CSS:
label {
border: solid;
border-top-right-radius: 10px;
border-top-left-radius: 10px;
border-bottom: none;
border-color: rgb(211,211,205);
border-width: 2px;
color: rgb(12,174,175);
background-color: rgb(247,247,247);
}
input:checked + label {
background-color: #fff;
color: rgb(94,94,94);
}
label:hover {
cursor: pointer;
}
.tabinfo {
display:none;
}
#tab1:checked ~ #text1{
display:block;
}
#tab2:checked ~ #text2{
display:block;
}
#tab3:checked ~ #text3{
display:block;
}
See the example I made here: https://plnkr.co/edit/DyID8me4bM7VPCI9l6Cg?p=preview
I am trying to customize Radio buttons on bootstrap but it is not working, When i try outside bootstrap environment in plain HTML it works, Below is code for HTML followed by CSS I am using
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
My Current AMP Policies
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
Financial Planning and Retirement Advice
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios3" value="option3">
LifeCover
</label>
</div>
THE CSS
.newradio input[type=radio] {display: none;}
.newradio label {
display: inline-block;
cursor: pointer;
position: relative;
padding-left: 25px;
margin-right: 15px;
font-size: 13px;
}
.newradio label:before {
content: "";
display: inline-block;
width: 14px;
height: 14px;
border-radius: 7px;
margin-right: 10px;
position: absolute;
left: 0;
bottom: 3px;
background-color: #fff;
border:solid 1px #bcb882;
}
.newradio input[type=radio]:checked + .newradio label:before {
content: "";
background: url(../../Content/images/radio-check.png);
display: inline-block;
}
The reason for .newradio is I am using that div as parent class of above given HTML, I am getting proper display of my Radios but Checked status (the image radio-check) is not showing, I tried using color change instead of background image but that doesn't work either.
Does anyone has experience in customizing radio button in bootstrap? please guide
Manoj Soni
You need to find the bootstrap css class and overwrite it. Or use ID instead of class in the css. When that also don't work try !important.
I am trying to customize the look of my checkboxes using font-awesome and to have all the text of the labels correctly indented. I have customized the look of the checkboxes which makes the usual approaches to indent the text not working as I am hiding the actual checkbox (see the CSS below).
Currently, I obtain the following (left) while I would like the one on the right:
I used the following code (see the JSFiddle):
CSS
Inspired by this simple CSS checkboxes, I use the following to format my checkboxes with font-awesome:
input[type=checkbox] {
display:none;
}
input[type=checkbox] + label:before {
font-family: FontAwesome;
display: inline-block;
content: "\f096";
letter-spacing: 10px;
cursor: pointer;
}
input[type=checkbox]:checked + label:before {
content: "\f046";
}
input[type=checkbox]:checked + label:before {
letter-spacing: 8px;
}
HTML
<input type="checkbox" id="box1" checked="">
<label for="box1">Item 1: some long text...</label>
<br>
<input type="checkbox" id="box2" checked="">
<label for="box2">Item 2: some long text...</label>
<br>
<input type="checkbox" id="box3">
<label for="box3">Item 3: some long text...</label>
I have tried to modify the margin-left and text-indent attributes of the label and label:before selectors but without any success.
Any idea how I could have the correct indent while using the nice font-awesome icons?
Thank you very much for your help!
Add this style (tested both on Chrome and Firefox)
label {
display: block;
padding-left: 1.5em;
text-indent: -.7em;
}
http://jsfiddle.net/tkt4zsmc/2/
Final result:
After trying fcalderan's suggestion and not being able to get the values for padding-left and text-indent right for different browsers, I switched to a flex box. It is pretty green nowadays.
If you put the input/label pairs in divs as it is recommended by Mozilla, you can style them this way.
fieldset {
width: 13ch;
}
fieldset > div {
display: flex;
}
fieldset > div > * {
align-self: baseline;
}
fieldset > div > input[type=checkbox] {
margin: 0 0.5ch 0 0;
width: 1em;
height: 1em;
}
<fieldset>
<legend>Sichtbarkeit</legend>
<div>
<input id="c1" checked="" type="checkbox">
<label for="c1">Minuten</label>
</div>
<div>
<input id="c2" checked="" type="checkbox">
<label for="c2">Nur Minuten, die Vielfache von 5 sind</label>
</div>
<div>
<input id="c3" checked="" type="checkbox">
<label for="c3">Stunden</label>
</div>
<div>
<input id="c4" checked="" type="checkbox">
<label for="c4">Nur 12 Stunden</label>
</div>
</fieldset>
Based on the answer by Fabrizio Calderan, I used the following modifications to the CSS:
label{
display: inline-block;
margin-left: 20px;
}
label:before{
margin-left: -23px;
}
The advantage is that it does not modify the spacing between the items. You can see the final results in JSFiddle.
Basicly it's a survey with answers from 1 to 10. When someone clicks on a number I want the background of my label to change color. I've used radiobuttons because they can only choose 1 answer.
<label class="lfirst">
<input type="radio" name="answer1" value="1">
1
</label>
<label>
<input type="radio" name="answer1" value="2">
2
</label>
<style>
input[type="radio"]{
display: none;
}
.answers label {
display: inline-block;
font-weight: bold;
margin: 0 3px 0 0;
padding: 4px 12px;
cursor: pointer;
background: orange;
}
.answers label + input[type="radio"]:checked{
background: red;
}
</style>
you can get the width and height of radio button, wrap it in a div with that width and height, and apply the background color to it. This way when you hide the radio button the div will still be there with same width and height and background color of your choice.