input and span elements with tooltip within div in same line - html

The span element in div has a tooltip. When I remove the tooltip from the span, see second div (word), the input field moves a bit higher than rest of input fields.
How can I fix this issue? Thank you in advanced.
<div>
<div class="word">
<input id="word-4" name="word-4" type="text" data-index="3" value="the">
<span class="x-tag" value="4"></span>
<span class="tooltip" value="4"><span class="tooltiptext">Words</span>><</span>
</div>
<div class="word">
<input id="word-5" name="word-5" type="text" data-index="4" value="supermarket">
<span class="x-tag" value="5"></span>
<span class="" value=""></span>
</div>
<div class="word">
<input id="word-6" name="word-6" type="text" data-index="5" value="nerby">
<span class="x-tag" value="6"></span>
<span class="tooltip" value="6"><span class="tooltiptext">Words</span>><</span>
</div>
</div>
Here is the full example:
https://jsfiddle.net/myFiddler/5encgkh0/1/

Remove float:left from input, there is no need to use and give padding to parent element
/***************/
/* Tool-Tip */
/***************/
/*https://www.w3schools.com/css/css_tooltip.asp*/
.parent{
padding:25px;
}
.tooltip {
position: relative;
display: inline-block;
/* border-bottom: 1px dotted black; If you want dots under the hoverable text */
}
/* Tooltip text */
.tooltip .tooltiptext {
position: absolute;
z-index: 1;
visibility: hidden;
width: 120px;
margin-left: -60px;
bottom: 100%;
left: 50%;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* Fade in tooltip - takes 1 second to go from 0% to 100% opac: */
opacity: 0;
transition: opacity 2s;
}
/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
.tooltip .tooltiptext:after {
content: " ";
position: absolute;
top: 100%; /* At the bottom of the tooltip */
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: black transparent transparent transparent;
}
/********************/
/*** Word Input ***/
/********************/
.word {
width: 141px;
display: inline-block;
box-sizing: border-box;
position: relative;
margin-bottom: 12px;
}
.word .no-linking {
cursor: auto;
}
.icon-link.checked {
color: rgb(255,120,90);
}
.word > input {
width: 100px !important;
line-height: 24px;
font-size: 14px !important;
}
span.word {
border-radius: 4px;
margin-bottom: 5px;
}
.icon-link {
display: inline-block;
padding-top: 6px;
padding-left: 3px;
cursor: pointer;
}
.x-tag:after {
font-size: 13px;
content: "x";
padding: 1px 2px;
position: absolute;
display: inline-block;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
z-index: 100;
right: 36px;
line-height: 24px;
}
<div class="parent">
<div class="word">
<input id="word-1" name="word-1" type="text" data-index="0" value="I">
<span class="x-tag" value="1"></span>
<span class="tooltip" value="1"><span class="tooltiptext">Words</span>><</span>
</div>
<div class="word">
<input id="word-2" name="word-2" type="text" data-index="1" value="go">
<span class="x-tag" value="2"></span>
<span class="tooltip" value="2"><span class="tooltiptext">Words</span>><</span>
</div>
<div class="word">
<input id="word-3" name="word-3" type="text" data-index="2" value="to">
<span class="x-tag" value="3"></span>
<span class="tooltip" value="3"><span class="tooltiptext">Words</span>><</span>
</div>
<div class="word">
<input id="word-4" name="word-4" type="text" data-index="3" value="the">
<span class="x-tag" value="4"></span>
<span class="tooltip" value="4"><span class="tooltiptext">Words</span>><</span>
</div>
<div class="word">
<input id="word-5" name="word-5" type="text" data-index="4" value="supermarket">
<span class="x-tag" value="5"></span>
<span class="" value=""></span>
</div>
<div class="word">
<input id="word-6" name="word-6" type="text" data-index="5" value="nerby">
<span class="x-tag" value="6"></span>
<span class="tooltip" value="6"><span class="tooltiptext">Words</span>><</span>
</div>
</div>

Related

Focusing with tab key not working in FireFox

I have a problem with focusing link with tab key in FireFox. Focusing works normal in chrome but in FireFox it loops inside one element.
I have created a example with the described behavior(open it in FireFox)
https://jsfiddle.net/e4gcmk6z/2/
.notVisible {
opacity: 0;
position: fixed;
}
.selection-box-v2 .selection-item {
background: #ffffff;
border: solid;
border-width: 1px;
border-color: #9c9c9c;
border-radius: 3px;
padding: 10px;
display: block;
}
label.label-outline {
font-size: 14px !important;
-webkit-transform: translate(0px, 12px);
transform: translate(0px, 12px);
position: absolute;
padding-right: 5px;
padding-left: 5px;
margin-top: 0px;
left: 18px;
line-height: normal !important;
cursor: text;
color: #5f5f5f;
z-index: 1;
}
.selection-box-v2 .co-calendar {
color: #3071a9;
font-size: 20px;
margin-right: 20px;
margin-top: 10px;
position: absolute;
right: 0;
pointer-events: none;
z-index: 1;
}
<div>
Tab key is not focusing link 2 in FireFox
</div>
<div class="links-v2">
<a tabindex="0" href="javascript:void(0);">
link 1
</a>
</div>
<br>
<div class="col-xs-12 selection-box-v2 pad">
<label tabindex="0" role="radio">
<input type="radio" class="shipModeId notVisible" name="shipModeId_1" value="p79652">
<span class="selection-item preferred-arrival-date-v2 inner-addon right-addon calendar-box">
<input class="ship-date" type="hidden" value="">
<span class="padClause" style="display: none">
<p class="checkout-ship-padClause"> </p>
</span>
<span class="ship-type-and-cost" style="margin-top: 10px;">some other elements
<span class="shipping-cost span-inline calendar-arrival"></span>
</span>
</span>
</label>
</div>
<br>
<div class="links-v2">
<a tabindex="0" href="javascript:void(0);">
link 2
</a>
</div>
Any one has and idea what could be wrong?
You can remove the tab index of the two a tags since they are not needed, also there are some input tags that are not visible on the screen, these need to be set to tabIndex as -1 so that these elements are skipped!
.notVisible {
opacity: 0;
position: fixed;
}
.selection-box-v2 .selection-item {
background: #ffffff;
border: solid;
border-width: 1px;
border-color: #9c9c9c;
border-radius: 3px;
padding: 10px;
display: block;
}
label.label-outline {
font-size: 14px !important;
-webkit-transform: translate(0px, 12px);
transform: translate(0px, 12px);
position: absolute;
padding-right: 5px;
padding-left: 5px;
margin-top: 0px;
left: 18px;
line-height: normal !important;
cursor: text;
color: #5f5f5f;
z-index: 1;
}
.selection-box-v2 .co-calendar {
color: #3071a9;
font-size: 20px;
margin-right: 20px;
margin-top: 10px;
position: absolute;
right: 0;
pointer-events: none;
z-index: 1;
}
<div>
Tab is not focusing link 2 in FireFox
</div>
<div class="links-v2">
<a href="javascript:void(0);">
link 1
</a>
</div>
<br>
<div class="col-xs-12 selection-box-v2 pad">
<label tabindex="0" role="radio">
<input type="radio" tabindex="-1" class="shipModeId notVisible" name="shipModeId_1" value="p79652">
<span class="selection-item preferred-arrival-date-v2 inner-addon right-addon calendar-box">
<input class="ship-date" tabindex="-1" type="hidden" value=""/>
<span class="padClause" style="display: none">
<p class="checkout-ship-padClause"> </p>
</span>
<span class="ship-type-and-cost" style="margin-top: 10px;">some other elements
<span class="shipping-cost span-inline calendar-arrival"></span>
</span>
</span>
</label>
</div>
<br>
<div class="links-v2">
<a href="javascript:void(0);">
link 2
</a>
</div>
checked in Firefox 67.0.4 (64-bit)

Radio Buttons Fill

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>

Cannot add element in the same line

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>

CSS error tool tips not working properly

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>

change background when radio button clicked

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