Affect div when radiobutton is checked with css - css

I'm trying to create a css-only select thingy.
I've got a container with three radiobuttons. The active radiobutton must be placed in the middle (vertically) of the container. The radiobuttons have to move as a whole, meaning that if the top radiobutton is selected the other two have to be spaced just beneath it; and if the middle radiobutton is selected, the other radiobuttons have to be spaced just above and just below the selected radiobutton.
It's hard to explain, but hear is what I've got so far.
http://jsfiddle.net/PaulvdDool/ZwdUL/1/
In this example the blue button is in the middle of the container. When I select the green button, all three buttons must move down 125px (the height of one button). But I can't make it work.
I can't seem to affect the other radiobuttons when one radio button is checked. I've tried to put an extra container around the buttons and change the margin-top, but I could not affect the container.
<div id="extracontainer">
<form>
<radiobutton 1 + label>
<radiobutton 2 + label>
<radiobutton 3 + label>
</form>
</div>
I've also tried to put an extra div above the buttons and change the height, but I couldn't affect this div too.
<div id="spacer"></div>
<form radiobuttons>
I'm guessing I'm using the wrong selector, doing something else wrong or am trying to do something impossible with just css.
Any CSS solutions?

What you really need here is jQuery or some other JavaScript library (or if you're a native-js freak, a pure JavaScript). You need to capture events, and then to change the CSS properties of corresponding elements you want to update.
It is simply not possible to do this via CSS at this point.
I hope this helps.

This can be done. It is all a matter of specificity as well as document structure.
I had to add a class to each label: cPantsx where x is the same number as the pants radio.
Then I then had to rearrange the html as follows:
<div id="pantscontainer">
<input type="radio" name="pants" id="pants1" value="pants1" />
<input type="radio" name="pants" id="pants2" value="pants1" />
<input type="radio" name="pants" id="pants3" value="pants1" />
<label for="pants1" class='cPants1'><span></span></label>
<label for="pants2" class='cPants2'><span></span></label>
<label for="pants3" class='cPants3'><span></span></label>
</div>​
everything had to become position:relative; so that I could adjust the placement within the container.
After that the magic really happens here:
#pantscontainer > input[id="pants1"]:checked ~ .cPants1 span{
top:125px;
}
#pantscontainer > input[id="pants1"]:checked ~ .cPants2 span{
top:-125px;
}
#pantscontainer > input[id="pants2"]:checked ~ .cPants2 span{
top:0px;
}
#pantscontainer > input[id="pants2"]:checked ~ .cPants1 span{
top:0px;
}
#pantscontainer > input[id="pants3"]:checked ~ .cPants3 span{
top:-125px;
}
#pantscontainer > input[id="pants3"]:checked ~ .cPants1 span{
top:0px;
}
#pantscontainer > input[id="pants3"]:checked ~ .cPants2 span{
top:125px;
}
Basically by adjusting the placement of the elements on the page, I was able to very specifically target the elements in question and make sure that they always moved out of each others way.
Here is a fork of your fiddle
I should mention that this is by no means an easy solution. It is more like a house of cards. If you had to add more elements you would need to re-write the whole thing. It will scale, but it would take a lot of effort to do so.

Related

Make icon appear after input

i have this search bar in my application which i want to modify:
Right now the "X"-Icon is visible from the beginning even tho it does nothing before an input was done, so i want to make it appear AFTER the user starts entering text.
The icon is a SVG i added and styled seperatly.
I don't realy know how i can do this, i thought its easy and i can just use something like "::after" but it seems that this it not possible with input fields.
Ps.: im an absolute beginner in CSS so please have mercy.
Best way to achieve your requirement would be to have different classes which shows/hides the icon by checking when input is not empty in JS.
If you want to achieve without using JS you can target the adjacent button element when the input is focussed and add ::before pseudo element and style it.
input:focus+button:before {
content: "X";
position: absolute;
left: 0;
color: red;
}
It's not possible with CSS. You would have to use Javascript.
Javascript
// set the id of the x button to x-button
// set the id of the input field to input
var x_button = document.getElementById("x-button");
var input = document.getElementById("search-input");
input.oninput = function(){
if(this.value) x_button.classList.add("visible");
else x_button.classList.remove("visible");
}
CSS
.x-button { display:none;}
.visible {display:block;}
it is possible if you wanna do it using only css.
#Search{
font-size:22px;
color:green;
background-image:url('images/search.jpg');
background-repeat:no-repeat;
background-position:center;outline:0;
}
#Search::-webkit-search-cancel-button{
position:relative;
right:20px;
}
<input id="Search" name="Search" type="search" placeholder="Search" />

Getting a Custom Checkbox to Display Correctly

I am planning a page with about 20 or more checkboxes and I want to use a particular style of custom checkbox to match other things in other places on the website. Essentially, the aim is for an unchecked box to look like this
and the checked version will be coloured. At the moment I can colour the icon and the text but I'm drawing a blank with the border.
The HTML and PHP code is this:
<?php foreach ($amenity_array as $key => $value) {?>
<label class="amenity">
<input name="<?php echo $key ?>" type="checkbox" value="<?php echo $key ?>"><i class="icon-<?php echo $key ?>"></i><span><?php echo $value ?></span>
</label>
<?php }?>
The CSS is this:
.amenity{position:relative;border:1px solid #ccc;color:#ccc;padding:.25em;width:14.5em;margin:0 .5em .5em 0;float:left;white-space:nowrap;overflow:hidden; cursor:pointer}
.amenity input{z-index:-1;opacity:0;width:0}
.amenity i{color:#ccc}
/* Hover and focus states */
.amenity:hover input ~ i{color:#3498db}
/* Checked state */
.amenity input:checked ~ i{color:#5470a3}
.amenity input:checked ~ span{color:#007aba}
Obviously I need a third line under the checked state with
.amenity {border:1px solid #007aba}
but how do I invoke it only when the input is checked?
Considering basic documentation on html element :
The tag defines a label for an element.
The element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the element, it toggles the control.
The element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the element, it toggles the control.
The for attribute of the tag should be equal to the id attribute of the related element to bind them together.
EDIT :
The idea of your question is : "Can I apply a CSS property depending on the child state ?".
The answer is NO. It is impossible, and it has been answered many times on Stack Overflow already.
Thus, there are two mains solutions :
1) Use Javascript !
I too prefer making beautiful CSS solutions all the time, but in your case, javascript solves the problem in a second with any framework.
For instance in AngularJS but don't import this MVC for that, go for native javascript or the framework you are using:
ng-style="{'border' : disabled ? '1px solid red' : '1px solid blue'}"
Pros : Very fast, very simple, don't change your html structure at all.
Cons: Some people may feel dishonoured not succeeding in doing a pure CSS solution.
2) A CSS trick.
I just did one, you can see it on this fiddle. The idea was to create a sibling .absolute positioned div with the dimensions of the .amenity div, remove the border of the .amenity div and then apply it to absolute div.
As it is a sibling, we don't have the parent problem anymore and the trick is done.
Pros : Fast and understandable too, pure CSS solution.
Cons : You can use width 100%, but the height need to be hardcoded somehow. If you use SASS, it can be okay, but if the height of your icon or font change with the screen dimension, you'll need to use media queries or just put every height in em.
Don't forget to remove some pixels to height and width to keep some place for the border pixels !
Absolute div class :
.amenity .borderer {width : 100%; position: absolute; top: 0; left : 0; height : calc(37px - 4px); width : calc(100% - 2px); border:1px solid #ccc;}
Border change :
.amenity input:checked ~ div{border:1px solid #007aba}
New HTML :
<div class="amenity">
<label>
<input name="disabled" type="checkbox" value="disabled"><i
class="icon">D </i><span>Disabled Access</span>
<div class="borderer">
</div>
</label>
</div>

Is there a way to set a CSS style to a label of a specific input type?

I have the following style set for labels of a particular form:
#incidentForm label {
font-weight:bold;
width:40%;
vertical-align:top;
}
Which is exactly what I want for my full page form. But for the modal that allows one to update a single field from a report where someone would view the data results, I would like the label for the TEXTAREA ONLY to NOT be limited to the "width:40%". All other input types should keep the "width:40%".
I've been trying to wrap my brain around how to either do a :NOT exception to the existing label style, or somehow set a separate style based on the class of the modal. I.E.:
.updateModal label(that somehow identifies only textareas) {
width:100%;
}
Here is an example of the structure of the update modal itself:
<div id="Return" class="updateModal">
<div id="incidentForm">
<div class="[name of this incident form type]">
<form class="AjaxForm" action="https://blahblahblah" method="post">
<fieldset id="fs1">
<legend>Edit report field</legend>
<div class="field">
<label for="31">This is the label for this text area field:</label>
<textarea id="31" name="31"></textarea>
</div>
</fieldset>
<input value="Update record" type="submit">
</form>
</div>
</div>
</div>
Please note that the id for the textarea and, thus the label for it, are generated dynamically based on the id field of the database the form information is pulled from. All of the field information is stored in a db so I would not be able to generate a specific style definition based on the id of the specific textarea itself. Or at least not that I can think of.
Does anyone have any suggestions?
Thank you very much!
The selector should be like
label[for=xxx]
{
/* ...definitions here... */
}
For multiple, you can make your selector simpler and generalize for modern browsers:
label[for^="3"] {
color: red; //It will apply to all label that starts with "3"
}
Or Simply you can write:
label[for="31"],label[for="32"],label[for="and so on.."] {
color: red;
}
Or For General Label Simply write
label {
color: red; //It will affect to all the label in the page
}
With CSS, the subject of the selector is the last compound selector of the entire selector (https://www.w3.org/TR/selectors4/#subject). In CSS4, there is a new subject selector that will allow this. It looks like: label! + textarea. The ! means that the label selector is the subject of this selector.
Unfortunately, this is not yet implemented in any browsers (http://css4-selectors.com/selector/css4/subject-of-selector-with-child-combinator/). Given that, we only have the ability to look for descendants, children, and younger siblings.
If you have some ability to control your HTML, this gives us a possibility: if we flip the DOM order of the form element and label, then the label becomes the younger sibling of the textarea. This gives us the option of using the adjacent + selector. The visual ordering can be altered by using a reverse-column flexbox. Consider:
<div class="field">
<textarea id="f1">My textarea</textarea>
<label for="f1">My Label</label>
</div>
<div class="field">
<input type="text">
<label for="f1">My Label</label>
</div>
.field {
display: flex;
flex-direction: column-reverse;
margin: 1em;
}
textarea + label {
background: #faa;
}
Demo: https://codepen.io/honzie/pen/weMRam
To summarize: (1) Is it possible in CSS2.1/3? Not with the current HTML. (2) Is it possible with CSS4 (assuming browsers implement it and the spec doesn't change)? Yes, in the future.
Although I don't have any code to work with, if provide some I'll try to include it, it seems to me you should be able to add a CSS class to any label that is being used for a textarea. If backend code is using database info to decide on the element to use for a form field then you can use that logic to add the class, otherwise add it yourself when you write the HTML.
You can use the following to select and attribute of an element:
input[type="text"] {
background-color: yellow;
}

how to make elements Aligned inside div

I have a div with few elements , my label and textbox inside a div are not well aligned , You can see the screenshot ..
Any idea to align these legal name and business name and textbox , so all the textboxes should start from the same point
thanks
Set width for your labels and inputs:
.the-label {
width: 160px;
}
.the-input {
width: 220px;
}
Here's one way you can do it:
http://jsfiddle.net/FmKfP/
You could align them using an invisible table.
<table border="0">
<tr><td>Legal Name:</td><td><input type="text"></td></tr>
<tr><td>Business Name:</td><td><input type="text"></td></tr>
</table>
You'll have to ask yourself which is worse: using a table or using enough mark-up that it looks like a table but with spans/divs instead of trs/tds. I think tables are fine in this instance (if you've worked with relational databases, it's not uncommon to have lots of tables with only 2 columns), others will assure you that it is evil.
You can use a width on your label elements as already suggested or you can use slightly less evil CSS tables.
div.pseudo-row { display: table-row }
/* input might need to go in a container and have the container set to table-cell instead */
div.pseudo-row label, div.pseudo-row input { display: table-cell }
<div class="pseudo-row">
<label for="first-item">First item</label>
<input type="text" id="first-item" />
</div>
<div class="pseudo-row">
<label for="second-item">Second item</label>
<input type="text" id="second-item" />
</div>
http://www.vanseodesign.com/css/tables/
For the record, I would just use tables in this instance and save the CSS table display properties for another day.

CSS Two-state button

I need a button-like control that has a checked property, so that when clicked it stays pressed. Something like the "Purge responses" button in the example image below.
How can I do this in CSS? I'm a CSS newbie. Can someone provide an example or point to one that is similar to this?
PS: I know that I need to use Javascript to update a boolean variable that holds the state of the button, and dynamically apply a style to the button. My problem is more like how to create a button that contains a checkbox , as I have only one image for background.
http://i.stack.imgur.com/vBV6F.png
As for CSS you can do the following:
<style type='text/css'>
/* this is the style of an unchecked "button" */
.input-check{
display:inline-block;
height:20px;
padding:5px 8px;
background:green;
width:70px;
color:white
}
/* This is the style for a checked "button" */
.input-check.checked{
background:red;
color:black;
font-weight:bold
}
/* Hide the checkbox */
.input-check input{
display:none
}
</style>
Next is the HTML. To reduce JavaScript coding, it's best to nest a checkbox inside a label. This will make it automatically handle the checking/unchecking of the checkbox when you click on the label.
<label class="input-check"><input onchange="change_state(this)" type="checkbox" value="something" name="test"/> click me </label>
Finally the JavaScript:
<script type="text/javascript">
/* If you have more experience in JavaScript, I recommend not binding the change event this way, I didn't bother much about this part, since I guess it isn't part of the question */
function change_state(obj){
if (obj.checked){
//if checkbox is being checked, add a "checked" class
obj.parentNode.classList.add("checked");
}
else{
//else remove it
obj.parentNode.classList.remove("checked");
}
}
</script>
This is a jsFiddle for you to test.
Why don't you just style a checkbox to look like a button?
Then you can use the :checked CSS psudeo selector to style it the way you want without adding classes through javascript.
Here's an elaborate example in CodePen: http://codepen.io/arjabbar/pen/csafj
See the section here titled checkbox button. If I'm understanding your question correctly, that seems to do what you're after, maybe with a little modification.
No CSS needed, if I understand what you want correctly
<button><input type="checkbox" /> Purge</button>
Then you'll likely need javascript to check and uncheck the box when the button is clicked, but the above is the basic idea.
Here's with a bit of quick js
<html>
<head>
<script type="text/javascript">
function check() {
var c = document.getElementById('check') ;
c.checked = (c.checked) ? false : true ;
}
</script>
</head>
<body>
<button onclick="check()"><input type="checkbox" id="check" /> Purge</button>
</body>
</html>

Resources