I have a radio button which i have given some style.
when i click on it then the div background color should change.
.no_of_speakers_radio_button {
margin: 0 auto;
display: flex;
padding: 35px 0px;
text-align: center;
flex-direction: column;
border:thin red solid;
width:100px;
}
.no_of_speakers_radio_button label {
overflow: hidden;
}
.no_of_speakers_radio_button label span {
text-align: center;
font-size: 15px;
margin: auto;
display: block;
}
.no_of_speakers_radio_button input {
position: absolute;
}
.no_of_speakers_radio_button input:checked + span {
background-color: #ebeff1;
text-align: center;
}
.no_of_speakers_radio_button input:not(:checked + span) {
background: transparent;
}
.no_of_speakers_radio_button:hover{
background: #ebeff1;
}
<div class="no_of_speakers_radio_button">
<label class="four_speakers">
<input type="radio" name="choose_speaker" class="click">
<span class="font_size_20">4</span>
<span class="font_size_17">Speakers</span>
</label>
</div>
<div class="no_of_speakers_radio_button">
<label class="eight_speakers">
<input type="radio" name="choose_speaker" class="click">
<span class="font_size_20">8</span>
<span class="font_size_17">Speakers</span>
</label>
</div>
What i want is, when clicking on radio button then whole div background should change rather than changing only span background.
Thank you.
With pure css, you can't select a parent element from within a child
element
Approach #01 (Without HTML Modifications):
Without modifying current HTML structure, you can use :before or :after pseudo element to create a fake active state.
You will need to add following CSS:
.no_of_speakers_radio_button {
position: relative;
}
.no_of_speakers_radio_button input:checked + span:before {
background-color: #ebeff1;
position: absolute;
z-index: -1;
content: '';
bottom: 0;
right: 0;
left: 0;
top: 0;
}
.no_of_speakers_radio_button {
margin: 0 auto;
display: flex;
padding: 35px 0px;
text-align: center;
flex-direction: column;
border:thin red solid;
position: relative;
width:100px;
}
.no_of_speakers_radio_button label {
overflow: hidden;
}
.no_of_speakers_radio_button label span {
text-align: center;
font-size: 15px;
margin: auto;
display: block;
}
.no_of_speakers_radio_button input {
position: absolute;
}
.no_of_speakers_radio_button input:checked + span {
background-color: #ebeff1;
text-align: center;
}
.no_of_speakers_radio_button input:checked + span:before {
background-color: #ebeff1;
position: absolute;
z-index: -1;
content: '';
bottom: 0;
right: 0;
left: 0;
top: 0;
}
.no_of_speakers_radio_button input:not(:checked + span) {
background: transparent;
}
.no_of_speakers_radio_button:hover{
background: #ebeff1;
}
<div class="no_of_speakers_radio_button">
<label class="four_speakers">
<input type="radio" name="choose_speaker" class="click">
<span class="font_size_20">4</span>
<span class="font_size_17">Speakers</span>
</label>
</div>
<div class="no_of_speakers_radio_button">
<label class="eight_speakers">
<input type="radio" name="choose_speaker" class="click">
<span class="font_size_20">8</span>
<span class="font_size_17">Speakers</span>
</label>
</div>
Alternate Approach (With HTML Modification):
You can modify your HTML to the code below. The trick is to place original input out of the view and place a fake input[type="radio"] button in place.
HTML:
<div class="no_of_speakers_radio_button">
<input type="radio" id="radio1" name="choose_speaker" class="click">
<label class="four_speakers" for="radio1">
<div>
<span class="font_size_20">4</span>
<span class="fake-radio"></span>
</div>
<span class="font_size_17">Speakers</span>
</label>
</div>
CSS:
.no_of_speakers_radio_button input {
position: absolute;
opacity: 0;
left: -9999px;
top: -9999px;
}
.no_of_speakers_radio_button label .fake-radio {
position: relative;
border: 1px solid black;
border-radius: 100%;
height: 12px;
width: 12px;
}
.no_of_speakers_radio_button label .fake-radio:before {
background-color: #222;
border-radius: 100%;
position: absolute;
opacity: 0;
z-index: 10;
content: '';
bottom: 2px;
right: 2px;
left: 2px;
top: 2px;
}
.no_of_speakers_radio_button input:checked + label .fake-radio:before {
opacity: 1;
}
.no_of_speakers_radio_button {
margin: 0 auto;
border:thin red solid;
position: relative;
overflow: hidden;
width:100px;
}
.no_of_speakers_radio_button label {
padding: 35px 0px;
text-align: center;
flex-direction: column;
position: relative;
cursor: pointer;
display: flex;
height: 100%;
}
.no_of_speakers_radio_button div span {
text-align: center;
font-size: 15px;
margin: auto;
display: inline-block;
vertical-align: middle;
}
.no_of_speakers_radio_button input {
position: absolute;
opacity: 0;
left: -9999px;
top: -9999px;
}
.no_of_speakers_radio_button label .fake-radio {
position: relative;
border: 1px solid black;
border-radius: 100%;
height: 12px;
width: 12px;
}
.no_of_speakers_radio_button label .fake-radio:before {
background-color: #222;
border-radius: 100%;
position: absolute;
opacity: 0;
z-index: 10;
content: '';
bottom: 2px;
right: 2px;
left: 2px;
top: 2px;
}
.no_of_speakers_radio_button input:checked + label {
background-color: #ebeff1;
}
.no_of_speakers_radio_button input:checked + label .fake-radio:before {
opacity: 1;
}
.no_of_speakers_radio_button input:not(:checked + span) {
background: transparent;
}
.no_of_speakers_radio_button label:hover {
background: #ebeff1;
}
<div class="no_of_speakers_radio_button">
<input type="radio" id="radio1" name="choose_speaker" class="click">
<label class="four_speakers" for="radio1">
<div>
<span class="font_size_20">4</span>
<span class="fake-radio"></span>
</div>
<span class="font_size_17">Speakers</span>
</label>
</div>
<div class="no_of_speakers_radio_button">
<input type="radio" id="radio2" name="choose_speaker" class="click">
<label class="eight_speakers" for="radio2">
<div>
<span class="font_size_20">8</span>
<span class="fake-radio"></span>
</div>
<span class="font_size_17">Speakers</span>
</label>
</div>
<div class="no_of_speakers_radio_button">
<input type="radio" id="radio3" name="choose_speaker" class="click">
<label class="eight_speakers" for="radio3">
<div>
<span class="font_size_20">8</span>
<span class="fake-radio"></span>
</div>
<span class="font_size_17">Speakers</span>
</label>
</div>
<div class="no_of_speakers_radio_button">
<input type="radio" id="radio4" name="choose_speaker" class="click">
<label class="eight_speakers" for="radio4">
<div>
<span class="font_size_20">8</span>
<span class="fake-radio"></span>
</div>
<span class="font_size_17">Speakers</span>
</label>
</div>
<div class="no_of_speakers_radio_button">
<input type="radio" id="radio5" name="choose_speaker" class="click">
<label class="eight_speakers" for="radio5">
<div>
<span class="font_size_20">8</span>
<span class="fake-radio"></span>
</div>
<span class="font_size_17">Speakers</span>
</label>
</div>
You can use JQuery for this:
$(document).ready(function() {
$('.no_of_speakers_radio_button input[type="radio"]').click(function() {
$(this).parent().parent().addClass("selected");
});
});
CSS:
.no_of_speakers_radio_button.selected {
background: #ebeff1;
}
Using css there is no click trigger. However you can use javascript for click function to toggle background color
var radio1 = document.getElementsByClassName("click")[0];
var radio2 = document.getElementsByClassName("click")[1];
radio1.addEventListener("click",function(){
radio1.parentNode.parentNode.style.backgroundColor="red";
radio2.parentNode.parentNode.style.backgroundColor="white";
})
radio2.addEventListener("click",function(){
radio2.parentNode.parentNode.style.backgroundColor="red";
radio1.parentNode.parentNode.style.backgroundColor="white";
})
.no_of_speakers_radio_button {
margin: 0 auto;
display: flex;
padding: 35px 0px;
text-align: center;
flex-direction: column;
border:thin red solid;
width:100px;
}
.no_of_speakers_radio_button label {
overflow: hidden;
}
.no_of_speakers_radio_button label span {
text-align: center;
font-size: 15px;
margin: auto;
display: block;
}
.no_of_speakers_radio_button input {
position: absolute;
}
.no_of_speakers_radio_button input:checked + span {
text-align: center;
}
.no_of_speakers_radio_button input:not(:checked + span) {
background: transparent;
}
.no_of_speakers_radio_button:hover{
background: #ebeff1;
}
<div class="no_of_speakers_radio_button" class="four_speakers">
<label >
<input type="radio" name="choose_speaker" class="click" >
<span class="font_size_20">4</span>
<span class="font_size_17">Speakers</span>
</label>
</div>
<div class="no_of_speakers_radio_button">
<label class="eight_speakers">
<input type="radio" name="choose_speaker" class="click">
<span class="font_size_20">8</span>
<span class="font_size_17">Speakers</span>
</label>
</div>
You cannot traverse a parent in css i.e. there is no parent-selector.
However you can modify your html structure and style it in such a way as to achieve what you want here. I have modified your code a little bit here:
.no_of_speakers_radio_button {
margin: 0 auto;
display: flex;
height: 100px;
padding: 0;
text-align: center;
flex-direction: column;
border: thin red solid;
width: 100px;
}
.no_of_speakers_radio_button label {
overflow: hidden;
height: 100%;
display: block;
}
.no_of_speakers_radio_button label span {
text-align: center;
font-size: 15px;
margin: auto;
display: block;
}
.no_of_speakers_radio_button input {
position: absolute;
margin-top: 40px;
}
.no_of_speakers_radio_button input:checked + span {
background-color: #ebeff1;
text-align: center;
}
.no_of_speakers_radio_button input:not(:checked + span) {
background: transparent;
}
.no_of_speakers_radio_button:hover {
background: #ebeff1;
}
.font_size_20 {
height: 60px;
padding-top: 40px;
}
<div class="no_of_speakers_radio_button">
<label class="four_speakers">
<input type="radio" name="choose_speaker" class="click">
<span class="font_size_20">4 <span>Speakers</span> </span>
</label>
</div>
<div class="no_of_speakers_radio_button">
<label class="eight_speakers">
<input type="radio" name="choose_speaker" class="click">
<span class="font_size_20">8 <span>Speakers</span></span>
</label>
</div>
give a radio button a class name and in that class just put change background
Related
How can I make these radio buttons responsive? I'm not sure what I'm doing wrong. I'd like to be able to click the grid item each option is in and have it behave like a radio button, but I'm not sure what I'm missing. Any recommendations for streamlining this code would also be extremely helpful. Thanks!
body {
font-family: Arial, Helvetica;
font-size: 3vh;
background-color: #b0b0b0;
}
.wrapper {
width: 50%;
margin-left: 25%;
margin-right: 25%;
display: grid;
grid-template-columns: 100px 100px 100px;
grid-gap: 20px;
grid-auto-rows: 100px;
justify-content: center;
background-color: ;
}
.item {
border-radius: 50px;
color: white;
padding: 10px;
display: flex;
align-items: center;
justify-content: center;
}
.item input {
opacity: 0;
position: absolute;
/*this makes it not interfere with the text location*/
}
.item:nth-child(odd) {
background: gray;
}
.item:nth-child(odd):hover {
background: limegreen;
cursor: pointer;
}
.item:nth-child(odd):label {}
.wrapper h2 {
grid-row: 1;
grid-column: 1/-1;
}
<div id="trialDiv" style="font-size: 13pt;">
<div id="TrialQuestions">
<div id="Questions" class="wrapper">
<h2 style="text-align:center"><i>What emotion was the face expressing? </i></h2>
<div id="gap" class="item"></div>
<div id="HappyButton" class="item">
<label>
<p><input class="responseButton" id="emotion_1" name="emotion" onclick = "GrabEmotionClickTime()" type="radio" value="1" /> Happiness</p>
</label>
</div>
<div id="gap" class="item"></div>
<div id="AngryButton" class="item">
<label>
<p><input class="responseButton" id="emotion_2" name="emotion" onclick = "GrabEmotionClickTime()"type="radio" value="2" /> Anger</p>
</label>
</div>
<div id="gap" class="item"></div>
<div id="FearButton" class="item">
<label>
<p><input class="responseButton" id="emotion_4" name="emotion" onclick = "GrabEmotionClickTime()"type="radio" value="3" /> Fear</p>
</label>
</div>
<div id="gap" class="item"></div>
<div id="NeutralButton" class="item">
<label>
<p><input class="responseButton" id="emotion_4" name="emotion" onclick = "GrabEmotionClickTime()"type="radio" value="4" /> Neutral</p>
</label>
</div>
</div>
</div>
</div>
Without doing all the work for you, your arrangement of elements is incorrect. Here is a simple example:
https://codepen.io/seanstopnik/pen/8a2ca693e9fe1f1334b921a6d75dbe99
/* For demo only */
body {
font-family: sans-serif;
padding: 40px;
}
/* Emotion button */
.emotion-button {
position: relative;
margin: 20px; /* For demo only */
}
.emotion-button__input {
position: absolute;
left: -9999px;
}
.emotion-button__label {
display: inline-block;
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
border-radius: 50%;
background-color: #ccc;
cursor: pointer;
transition: all 0.15s ease-in-out;
}
.emotion-button__input:checked ~ .emotion-button__label {
color: #fff;
background-color: green;
}
<div class="emotion-button">
<input id="r1" class="emotion-button__input" type="radio" name="emotion"/>
<label for="r1" class="emotion-button__label">Happiness</label>
</div>
<div class="emotion-button">
<input id="r2" class="emotion-button__input" type="radio" name="emotion"/>
<label for="r2" class="emotion-button__label">Anger</label>
</div>
Same idea as #SeanStopnik, did all the work for you:
body {
font-family: Arial, Helvetica;
font-size: 13pt;
background-color: #b0b0b0;
}
#Questions h2 {
font-style: italic;
}
.wrapper {
width: 50%;
margin-left: 25%;
margin-right: 25%;
display: grid;
grid-template-columns: 100px 100px 100px;
grid-gap: 20px;
grid-auto-rows: 100px;
justify-content: center;
background-color: ;
}
.item label {
display: block;
border-radius: 50%;
color: white;
padding: 10px;
display: flex;
align-items: center;
justify-content: center;
height: 80px;
background: gray;
}
.item label:hover {
background: lime;
}
.item input {
display: none;
}
.item input:checked + label {
background: blue;
}
.wrapper h2 {
grid-row: 1;
grid-column: 1/-1;
}
<div id="trialDiv">
<div id="TrialQuestions">
<div id="Questions" class="wrapper">
<h2 style="text-align:center">What emotion was the face expressing?</h2>
<div class="gap"></div>
<div id="HappyButton" class="item">
<input class="responseButton" id="emotion_1" name="emotion" type="radio" value="1" />
<label for="emotion_1">Happiness</label>
</div>
<div class="gap"></div>
<div id="AngryButton" class="item">
<input class="responseButton" id="emotion_2" name="emotion" type="radio" value="2" />
<label for="emotion_2">Anger</label>
</div>
<div class="gap"></div>
<div id="FearButton" class="item">
<input class="responseButton" id="emotion_3" name="emotion" type="radio" value="3" />
<label for="emotion_3">Fear</label>
</div>
<div class="gap"></div>
<div id="NeutralButton" class="item">
<input class="responseButton" id="emotion_4" name="emotion" type="radio" value="4" />
<label for="emotion_4">Neutral</label>
</div>
</div>
</div>
</div>
Explanation:
radio inputs are made invisible
they are placed right before their labels
labels are associated with them using for="id" attribute, so that clicking the label will check the radio button
I'm using the CSS pseudoclass :checked to target checked radio for styling
the CSS operator + allows to target the label right after the checked radio
Also:
you shouldn't have javascript event handlers in your HTML. Better use addEventListener from javascript for that. Your onclick made the click fail.
you shouldn't repeat ids in html
you shouldn't style your fonts inline
you shouldn't use <i> to make your titles italic. You should use CSS for that
Use font-style: italic; for the h2 element, nowdays <i> is used to insert icons
You can the id value only once in your HTML! Can't give more than one element the same id.
By clicking on the label we check the input, so we hide the input and style the label : width + height = 100%
We give all the inputs the same name to prevent checking more than one input[radio].
AddEventListener for all the inputs to toggle the class active on each one item (depends on which is the checked one).
Print to the console the id of the input that is checked.
var inputs = document.querySelectorAll('.item label');
for (let i = 0; i < inputs.length; i++){
inputs[i].addEventListener('click', (e) => {
checkEmotion(inputs[i], e);
});
}
function checkEmotion(input, e){
for(let i = 0; i < inputs.length; i++){
inputs[i].parentElement.classList.remove('active');
}
e.target.parentElement.classList.add('active');
console.log(input.nextElementSibling.getAttribute("id"));
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica;
background-color: #b0b0b0;
}
h2 {
margin: 20px 0;
font-style: italic;
}
.wrapper {
display: flex;
justify-content: center;
}
.item {
border-radius: 50px;
color: white;
display: flex;
align-items: center;
justify-content: center;
width: 90px;
height: 90px;
margin: 0 10px;
background: gray;
position: relative;
overflow: hidden;
transition: all 0.2s;
}
.item.active {
background: #121212;
}
.item input {
opacity: 0;
display: absolute;
height: 0;
width: 0;
}
.item label {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.item:hover {
background: limegreen;
}
<h2 style="text-align:center">What emotion was the face expressing?</h2>
<div class="wrapper">
<div class="item">
<label for="emotion-happy">Happy</label>
<input type="radio" id="emotion-happy" name="emotion">
</div>
<div class="item">
<label for="emotion-sad">Sad</label>
<input type="radio" id="emotion-sad" name="emotion">
</div>
<div class="item">
<label for="emotion-angry">Angry</label>
<input type="radio" id="emotion-angry" name="emotion">
</div>
<div class="item">
<label for="emotion-confused">Confused</label>
<input type="radio" id="emotion-confused" name="emotion">
</div>
</div>
I'm Having trouble with my radio buttons, I don't want them to entirely fill the circle. Any word of advice. Here is link to what is happening on Codepen.
https://codepen.io/winterlion/pen/LYYJwZP
.item .text {
position: absolute;
display: inline-block;
cursor: pointer;
}
.item .text:before {
content: '';
display: inline-block;
vertical-align: text-bottom;
width: 18px;
height: 18px;
margin-right: 5px;
border-radius: 50%;
border: 2px solid #235b96;
outline: none;
box-shadow: 0 0 5px 0px gray inset;
}
.item input[type="radio"] {
display: none;
}
.item input[type="radio"]:checked+label .text:before {
content: '';
color: #235b96;
background-color: #479623;
}
<div class="item">
<input type="radio" id="r1" name="group1" value="trial1" />
<label for="r1" class="wrapper">
<span class="background"></span>
<h1 class="rp-text">Radio Buttons</h1>
<hr class="split-hr">
<br>
<span class="text"></span>
</label>
</div>
<div class="item">
<input type="radio" id="r2" name="group1" value="trial2" />
<label for="r2" class="wrapper">
<span class="background"></span>
<h1 class="rp-text">Buttons</h1>
<span class="text"></span>
</label>
</div>
If I understood your question right, you just want to move styles related to a green dot to an ::after pseudo element that must be a bit smaller than ::before.
.item .text {
position: absolute;
display: inline-block;
cursor: pointer;
}
.item .text::before,
.item .text::after {
content: '';
display: inline-block;
margin-right: 5px;
border-radius: 50%;
}
.item .text::before {
width: 18px;
height: 18px;
border: 2px solid #235b96;
box-shadow: 0 0 5px 0px gray inset;
}
.item input[type="radio"]:checked + label .text::after {
width: 12px;
height: 12px;
position: absolute;
left: 5px;
top: 5px;
}
.item input[type="radio"] {
display: none;
}
.item input[type="radio"]:checked + label .text:after {
color: #235b96;
background-color: #479623;
}
<div class="item">
<input type="radio" id="r1" name="group1" value="trial1" />
<label for="r1" class="wrapper">
<span class="background"></span>
<h1 class="rp-text">Radio Buttons</h1>
<hr class="split-hr">
<br>
<span class="text"></span>
</label>
</div>
<div class="item">
<input type="radio" id="r2" name="group1" value="trial2" />
<label for="r2" class="wrapper">
<span class="background"></span>
<h1 class="rp-text">Buttons</h1>
<span class="text"></span>
</label>
</div>
I'm trying to make a favourite checkbox on the right side of the div, this is the html structure:
.star {
visibility: hidden;
font-size: 30px;
cursor: pointer;
color: orange;
}
.star:before {
content: "\2605";
position: absolute;
visibility: visible;
}
.star:checked:before {
content: "\2606";
position: absolute;
}
.group {
background-color: #20262e;
}
<div class="group text-truncate">
<label class="font-weight-bold">
<span class="align-middle text-truncate" style="color:white">This is a long text</span>
<span class="align-middle" style="color: orange;">(3 items)</span>
</label>
<input type="checkbox" style="float:right;" class="group-checkbox star">
</div>
if you look at the JSFIDDLE, you will see that the checkbox is greather than the container, how can I manage this?
There are a few things I would change:
use flex instead of float
use a label for your star instead of the input itself
remove the absolute positioning - it's not needed and can just complicate things
don't mix inline styles with css - it's best to use all css (I haven't fixed this though)
/* seperate the checkbox and star styles */
.group-checkbox {
display:none;
}
.group-checkbox:checked + .star:before { /* using the adjacent sibling selector to selec the star after a checked input */
content: "\2606";
}
.star {
font-size: 30px;
cursor: pointer;
color: orange;
}
.star:before {
content: "\2605";
}
.group {
background-color: #20262e;
line-height:30px; /* may want to match the size of the star font */
display:flex; /* make the container flex */
width:100%;
}
.group .font-weight-bold {
flex-grow:1; /* make this label grow to fill the space the star doesn't take */
}
<div class="group text-truncate">
<label class="font-weight-bold">
<span class="align-middle text-truncate" style="color:white">This is a long text</span>
<span class="align-middle" style="color: orange;">(3 items)</span>
</label>
<input type="checkbox" class="group-checkbox" id="checkbox"><!-- give this an id -->
<label class="star" for="checkbox"><!--use a label and point it at the id of the chechbox so that you can click on it to change the status--></label>
</div>
My solution it's to suggest you to use flexbox it's very easy to understand, your problem is come from the font-size that you assign on your icon. You need to reduce font-size and on the parent make the CSS I make for you :)
.group {
display: flex;
flex-flow: row wrap;
align-items: center;
justify-content: space-between;
}
.star {
position: relative;
visibility: hidden;
font-size: 20px;
cursor: pointer;
color: orange;
}
.star:before {
content: "\2605";
position: absolute;
top: 50%;
transform: translateY(-50%);
visibility: visible;
}
.star:checked:before {
content: "\2606";
position: absolute;
}
.group{
background-color: #20262e;
}
<div class="group text-truncate">
<label class="font-weight-bold">
<span class="align-middle text-truncate" style="color:white">This is a long text</span>
<span class="align-middle" style="color: orange;">(3 items)</span>
</label>
<input type="checkbox" style="float:right;" class="group-checkbox star">
</div>
Please add position: relative; to .group and adjust the star position accordingly. check below:
.star {
visibility: hidden;
font-size: 20px;
cursor: pointer;
color: orange;
}
.star:before {
top: -3px;
right: 2px;
content: "\2605";
position: absolute;
visibility: visible;
}
.star:checked:before {
content: "\2606";
position: absolute;
}
.group {
position: relative;
background-color: #20262e;
}
<div class="group text-truncate">
<label class="font-weight-bold">
<span class="align-middle text-truncate" style="color:white">This is a long text</span>
<span class="align-middle" style="color: orange;">(3 items)</span>
</label>
<input type="checkbox" style="float:right;" class="group-checkbox star">
</div>
in your .star:before and .star:after, you need to set the position to relative
Now it let's them to be in the same position as the input that has star class itself!
Now you can align the input to be in the right place.
In that case, you can add these to your .star styles:
.star {
position: relative;
bottom: 12px;
right: 15px;
}
And it will be what you're looking for
This is the snippet:
.star {
visibility: hidden;
font-size: 30px;
cursor: pointer;
color: orange;
position: relative;
bottom: 12px;
right: 12px;
}
.star:before {
content: "\2605";
position: relative;
visibility: visible;
}
.star:checked:before {
content: "\2606";
position: relative;
}
.group {
background-color: #20262e;
height: 25px;
}
<div class="group text-truncate">
<label class="font-weight-bold">
<span class="align-middle text-truncate" style="color:white">This is a long text</span>
<span class="align-middle" style="color: orange;">(3 items)</span>
</label>
<input type="checkbox" style="float:right;" class="group-checkbox star">
</div>
I think you can just add a top attribute and clean it up with positioning and padding like below.
.star {
visibility: hidden;
font-size: 30px;
cursor: pointer;
color: orange;
}
.star:before {
content: "\2605";
position: absolute;
visibility: visible;
top: 0;
right: 10px;
}
.star:checked:before {
content: "\2606";
position: absolute;
}
.group{
background-color: #20262e;
padding: 5px;
}
<div class="group text-truncate">
<label class="font-weight-bold">
<span class="align-middle text-truncate" style="color:white">This is a long text</span>
<span class="align-middle" style="color: orange;">(3 items)</span>
</label>
<input type="checkbox" style="float:right;" class="group-checkbox star">
</div>
Few minor updates to the .star and .star:before classes will fix the issue, please have a look at the below-working snippet
.star {
visibility: hidden;
font-size: 30px;
cursor: pointer;
color: orange;
position: relative;
margin: 0;
padding: 0;
}
.star:before {
content: "\2605";
position: absolute;
visibility: visible;
top: 0;
right: 0;
line-height: 1;
font-size: 20px;
}
.star:checked:before {
content: "\2606";
position: absolute;
}
.group {
background-color: #20262e;
}
<div class="group text-truncate">
<input type="checkbox" style="float:right;" class="group-checkbox star">
<label class="font-weight-bold">
<span class="align-middle text-truncate" style="color:white">This is a long text</span>
<span class="align-middle" style="color: orange;">(3 items)</span>
</label>
</div>
I got my general CSS code for the tool tips from here, but tried tailoring it to my needs:
/* The tooltip stuff */
.tooltip
{
top: -5px;
z-index: 1;
display: inline-block;
width: 200px;
background-color: #e55;
padding: 5px 0;
position: absolute;
color: #fff;
border-radius: 6px;
margin-left: 6px;
}
.tooltip::after
{
content: "";
position: absolute;
top: 50%;
right: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent #e55 transparent transparent;
}
For some reason, however, when I test this with a dummy tool tip, it not only is missing its arrow, but it isn't even on the row that needs it! Even worse, the tool tips, for some reason, sit on top each other!
The HTML code to the main div is as follows:
<div class="main pageCenter">
<form id="newUserRegistration">
<span class="row">
<label for="firstName" class="half">First name</label>
<input type="text" id="firstName" class="half">
<span class="tooltip">text</span>
</input>
</span>
<span class="rowTight">
<label for="lastName" class="half">Last name</label>
<input type="text" id="lastName" class="half">
<span class="hidden tooltip"></span>
</input>
</span>
<span class="rowTight">
<label for="empIDNumber" class="half">Employee ID number</label>
<input type="text" class="number" class="half">
<span class="tooltip">other text</span>
</input>
</span>
<span class="rowTight">
<label for="dept" class="half">Department</label>
<select id="dept" class="half">
<!-- To be populated with data from a Mustache template-->
{{#departments}}
<option id="{{departmentHTMLID}}">{{departmentName}}</option>
{{/departments}}
</select>
<span class="hidden tooltip"></span>
</span>
<span class="rowTight">
<label for="manager" class="half">Manager name</label>
<input type="text" id="manager" class="half">
<span class="hidden tooltip"></span>
</input>
</span>
<span class="rowTight">
<label for="username" class="half">Username</label>
<input type="text" id="username" class="half">
<span class="hidden tooltip"></span>
</input>
</span>
<span class="rowTight">
<label for="password" class="half">Password</label>
<input type="text" id="password" class="half">
<span class="hidden tooltip"></span>
</input>
</span>
<span class="rowTight">
<label for="confirmPassword" class="half">Confirm password</label>
<input type="text" id="confirmPassword" class="half">
<span class="hidden tooltip"></span>
</input>
</span>
<span class="rowTight">
<label for="email" class="half" title="A confirmation email will be sent to this e-mail address.">E-mail address</label>
<input type="email" class="half" title="A confirmation email will be sent to this e-mail address.">
<span class="hidden tooltip"></span>
</input>
</span>
<span class="right row buttonRow">
<input type="reset" class="right" value="Clear"/>
<input type="submit" class="right" value="Submit" />
</span>
</form>
</div>
and the rest of my CSS code is as follows:
/* Fractional-width classes */
.fiveSixths { width: 83.3333333333333333%; }
.fourFifths { width: 80%; }
.threeFourths { width: 75%; }
.twoThirds { width: 66.6666666666666667%; }
.threeFifths { width: 60%; }
.half { width: 50%; }
.twoFifths { width: 40%; }
.third { width: 33.3333333333333333%; }
.fourth { width: 25%;}
.fifth { width: 20%; }
.sixth { width: 16.6666666666666667%; }
.main
{
border: 2px solid orange;
border-radius: 5px;
box-shadow: 0px 0px 100px orange;
}
.main > *
{
padding: 10px;
}
.pageCenter
{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
input[type="text"],input[type="password"], input[type="email"]
{
border-left-style: none !important;
border-right-style: none !important;
border-top-style: none !important;
border-bottom-color: #888 !important;
}
input[type="submit"],input[type="reset"],button
{
height: 25px;
width: 100px;
border-width: 1px;
border-radius: 3px;
}
input[type="submit"]
{
background-color: orange;
border-color: orange;
}
input[type="submit"]:hover
{
background-color: #fb0;
}
.hidden
{
display: none;
visibility: hidden;
}
.row:not(.buttonRow),.rowTight:not(.buttonRow)
{
display: block;
width: 100%;
}
.row
{
margin-top: 10px;
margin-bottom: 10px;
}
.rowTight
{
margin-top: 5px;
margin-bottom: 5px;
}
.right
{
float: right;
}
.number
{
text-align: right;
}
.main
{
min-width: 450px;
width: 50%;
}
#formContainer
{
padding: 20px;
}
#newUserRegistration > *:not(input[type="submit"])
{
width: 100%;
overflow: hidden;
}
#newUserRegistration > * > *
{
float: left;
}
#newUserRegistration > * > *:not(:first-child):not([type="submit"]):not([type="reset"]):not(button)
{
margin-left: -4px;
}
#newUserRegistration span
{
overflow: auto;
}
I've been trying to play with this for a while, but couldn't get the tool tips to position properly, nor for them to not be on top each other.
Although position: relative was declared on your containing elements, the overflow: auto property declared in addition prevented any overflow of the element's contents being visible, and so the tooltip could not be properly observed because of this.
To address this:
Remove the float: left rules declared on nested elements by the
following rule: #newUserRegistration > * > * - the global
selectors (*) for this rule causes havoc in this case, and should
be avoided, for better practice use more specific selectors and
only style what you are required to.
Only float your form fields right, with more specific selectors,
e.g:
#newUserRegistration select, #newUserRegistration input {
float: right;
}
You are no longer required to clear floats using overflow: auto on
the containing parent element, allowing your absolutely positioned
tooltips to remain visible. In addition, a left positioning property has been added to your tooltips.
Updated JSFiddle
Code Snippet Demonstration:
/* The tooltip stuff */
.tooltip
{
top: -5px;
left: 100%; /* Added - position absolute tooltip relative to parent row */
z-index: 1;
display: inline-block;
width: 200px;
background-color: #e55;
padding: 5px 0;
position: absolute;
color: #fff;
border-radius: 6px;
margin-left: 6px;
}
.tooltip::after
{
content: "";
position: absolute;
top: 50%;
right: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent #e55 transparent transparent;
}
/* Fractional-width classes */
.fiveSixths { width: 83.3333333333333333%; }
.fourFifths { width: 80%; }
.threeFourths { width: 75%; }
.twoThirds { width: 66.6666666666666667%; }
.threeFifths { width: 60%; }
.half { width: 50%; }
.twoFifths { width: 40%; }
.third { width: 33.3333333333333333%; }
.fourth { width: 25%;}
.fifth { width: 20%; }
.sixth { width: 16.6666666666666667%; }
.main
{
border: 2px solid orange;
border-radius: 5px;
box-shadow: 0px 0px 100px orange;
}
.main > *
{
padding: 10px;
}
.pageCenter
{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
input[type="text"],input[type="password"], input[type="email"]
{
border-left-style: none !important;
border-right-style: none !important;
border-top-style: none !important;
border-bottom-color: #888 !important;
}
input[type="submit"],input[type="reset"],button
{
height: 25px;
width: 100px;
border-width: 1px;
border-radius: 3px;
}
input[type="submit"]
{
background-color: orange;
border-color: orange;
}
input[type="submit"]:hover
{
background-color: #fb0;
}
.hidden
{
display: none;
visibility: hidden;
}
.row:not(.buttonRow),.rowTight:not(.buttonRow)
{
display: block;
position: relative;
width: 100%;
}
.row
{
margin-top: 10px;
margin-bottom: 10px;
}
.rowTight
{
margin-top: 5px;
margin-bottom: 5px;
}
.right
{
float: right;
}
.number
{
text-align: right;
}
.main
{
min-width: 450px;
width: 50%;
}
#formContainer
{
padding: 20px;
}
#newUserRegistration > *:not(input[type="submit"])
{
width: 100%;
overflow: hidden;
}
#newUserRegistration > * > * /* this is causing mayhem, selectors should be more specific */
{
float: left;
}
/* ...like this */
#newUserRegistration select, #newUserRegistration input {
float: right;
}
#newUserRegistration label {
float: none;
}
#newUserRegistration > * > *:not(:first-child):not([type="submit"]):not([type="reset"]):not(button)
{
margin-left: -4px;
}
/* not necessary, since you no longer need to clear nested elements that are floated
#newUserRegistration span
{
overflow: auto;
} */
<div class="main pageCenter">
<form id="newUserRegistration">
<span class="row">
<label for="firstName" class="half">First name</label>
<input type="text" id="firstName" class="half" />
<span class="tooltip">text</span>
</span>
<span class="rowTight">
<label for="lastName" class="half">Last name</label>
<input type="text" id="lastName" class="half" />
<span class="hidden tooltip"></span>
</span>
<span class="rowTight">
<label for="empIDNumber" class="half">Employee ID number</label>
<input type="text" class="number half" />
<span class="tooltip">other text</span>
</span>
<span class="rowTight">
<label for="dept" class="half">Department</label>
<select id="dept" class="half">
<!-- To be populated with data from a Mustache template-->
{{#departments}}
<option id="{{departmentHTMLID}}">{{departmentName}}</option>
{{/departments}}
</select>
<span class="hidden tooltip"></span>
</span>
<span class="rowTight">
<label for="manager" class="half">Manager name</label>
<input type="text" id="manager" class="half" />
<span class="hidden tooltip"></span>
</span>
<span class="rowTight">
<label for="username" class="half">Username</label>
<input type="text" id="username" class="half" />
<span class="hidden tooltip"></span>
</span>
<span class="rowTight">
<label for="password" class="half">Password</label>
<input type="text" id="password" class="half" />
<span class="hidden tooltip"></span>
</span>
<span class="rowTight">
<label for="confirmPassword" class="half">Confirm password</label>
<input type="text" id="confirmPassword" class="half" />
<span class="hidden tooltip"></span>
</span>
<span class="rowTight">
<label for="email" class="half" title="A confirmation email will be sent to this e-mail address.">E-mail address</label>
<input type="email" class="half" title="A confirmation email will be sent to this e-mail address." />
<span class="hidden tooltip"></span>
</span>
<span class="right row buttonRow">
<input type="submit" class="right" value="Submit" />
<input type="reset" class="right" value="Clear"/>
</span>
</form>
</div>
I am setting up toogle button by absolutely positioning labels on top of checkboxes. I need them to horizontally aligned but they are just layering on top of each other.
<ul style="display: block; float: left;">
<li style="display: inline-block;">
<div class="btn_wrapper">
<input type="checkbox" value="1" id="newest" name=""/>
<label class="btn btn-sm btn-default" for="newest">Newest</label>
</div>
</li>
<li style="display: inline-block;">
<div class="btn_wrapper">
<input type="checkbox" value="1" id="popular" name=""/>
<label class="btn btn-sm btn-default" for="popular">Popular</label>
</div>
</li>
<li style="display: inline-block;">
<div class="btn_wrapper">
<input type="checkbox" value="1" id="price" name=""/>
<label class="btn btn-sm btn-default" for="price">Price</label>
</div>
</li>
</ul>
ul {
display: block;
li {
display: inline-block
.btn_wrapper {
position: relative;
clear: both;
label {
display: block;
cursor: pointer;
position: absolute;
top: 5px;
left: 5px;
z-index: 1;
background-color: #CCC;
}
input[type=checkbox], input[type=radio] {
position: absolute;
top: 5px;
left: 5px;
}
input[type=checkbox]:checked + label {
color: $shop_color_text_strong;
}
}
}
}
This is what it looks like:
This is what I ended up going with. Basically adding display: none to my inputs and removing the absolute position from the label it worked out fine.
ul {
display: block;
li.filter_btn {
margin-left: -2px;
}
li {
display: inline-block;
width: auto;
height: 30px;
label {
display: block;
cursor: pointer;
top: 5px;
left: 5px;
z-index: 1;
width: auto;
background-color: #CCC;
}
input[type=checkbox], input[type=radio] {
display: none;
}
input[type=checkbox]:checked + label {
color: $shop_color_text_strong;
}
}
}