Skip to content

Instantly share code, notes, and snippets.

@f8lrebel
Created July 1, 2012 05:59
Show Gist options
  • Save f8lrebel/3027042 to your computer and use it in GitHub Desktop.
Save f8lrebel/3027042 to your computer and use it in GitHub Desktop.
A web page created at CodePen.io
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Pure CSS(3) conditional form field visibiity &middot; CodePen</title>
<style>
.control:checked ~ .conditional,
#immigrant:checked ~ .conditional,
#required-2:checked ~ .conditional
#option-2:checked ~ .conditional {
clip: auto;
height: auto;
margin: 0;
overflow: visible;
position: static;
width: auto;
}
.control:not(:checked) ~ .conditional,
#immigrant:not(:checked) ~ .conditional,
#required-2:not(:checked) ~ .conditional,
#option-2:not(:checked) ~ .conditional {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
</style>
<style>
#codepen-footer, #codepen-footer * {
-webkit-box-sizing: border-box !important;
-moz-box-sizing: border-box !important;
box-sizing: border-box !important;
}
#codepen-footer {
display: block !important;
position: fixed !important;
bottom: 0 !important;
left: 0 !important;
width: 100% !important;
padding: 0 10px !important;
margin: 0 !important;
height: 30px !important;
line-height: 30px !important;
font-size: 12px !important;
color: #eeeeee !important;
background-color: #505050 !important;
text-align: left !important;
background: -webkit-linear-gradient(top, #505050, #383838) !important;
background: -moz-linear-gradient(top, #505050, #383838) !important;
background: -ms-linear-gradient(top, #505050, #383838) !important;
background: -o-linear-gradient(top, #505050, #383838) !important;
border-top: 1px solid black !important;
border-bottom: 1px solid black !important;
border-radius: 0 !important;
border-image: none !important;
box-shadow: inset 0 1px 0 #6e6e6e, 0 2px 2px rgba(0, 0, 0, 0.4) !important;
z-index: 300 !important;
font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, sans-serif !important;
letter-spacing: 0 !important;
word-spacing: 0 !important;
}
#codepen-footer a {
color: #a7a7a7 !important;
text-decoration: none !important;
}
#codepen-footer a:hover {
color: white !important;
}
</style>
<script>
// Kill alerts, confirmations and prompts
window.alert = function(){};
window.confirm = function(){};
window.prompt = function(){};
window.open = function(){};
window.print = function(){};
</script>
</head>
<body>
<h1>Conditionally toggling visibility of form fields with Pure CSS(3)</h1>
<p>I was looking for a pure CSS way to hide/show some form elements based on their value. Turns out it's pretty easy for checkboxes and radios. Basic example:</p>
<form action="">
<fieldset>
<ol>
<li>
<input type="checkbox" id="requirement" class="control">
<label for="requirement">Check me</label>
<fieldset class="conditional">
<ol>
<li>
<label for="option">Related subfield</label>
<input type="text" id="option">
</li>
</ol>
</fieldset>
</li>
<li>
<input type="radio" id="radio" class="control">
<label for="radio">Radio</label>
<fieldset class="conditional">
<ol>
<li>
<label for="option">Related subfield</label>
<input type="text" id="option">
</li>
</ol>
</fieldset>
</li>
</ol>
</fieldset>
</form>
<p>In the above example, I use the class .control to indicate which fields are the control and .conditional to indicate what will be displayed when the control is checked. I use sibling selectors (<code>~</code>) to tie the conditional to the control</p>
<p>You can get creative with IDs to show fields based on certain values only:</p>
<form action="">
<fieldset>
<ol>
<li>
<input type="radio" name="residency" value="Citizen" id="citizen">
<label for="citizen">Citizen</label>
</li>
<li>
<input type="radio" name="residency" value="Immigrant" id="immigrant">
<label for="immigrant">Immigrant</label>
<fieldset class="conditional">
<ol>
<li>
<label for="country_citizenship">Country of Citizenship</label>
<input type="text" id="country_citizenship">
</li>
<li>
<label for="visa_type">Type of Visa</label>
<input type="text" id="visa_type">
</li>
</ol>
</fieldset>
</li>
</ol>
</fieldset>
</form>
<p>You can even nest them.</p>
<form action="">
<fieldset>
<ol>
<li>
<input type="checkbox" id="requirement-2" class="control">
<label for="requirement-2">Check me</label>
<fieldset class="conditional">
<ol>
<li>
<label for="option-2">Related subfield</label>
<input type="checkbox" id="option-2">
<fieldset class="conditional">
<ol>
<li>
<label for="country_citizenship">Country of Citizenship</label>
<input type="text" id="country_citizenship">
</li>
<li>
<label for="visa_type">Type of Visa</label>
<input type="text" id="visa_type">
</li>
</ol>
</fieldset>
</li>
</ol>
</fieldset>
</li>
</ol>
</fieldset>
</form>
<p>I haven't thought of a way to make this work with the <code>.conditional</code> in a different part of the form as it relies on an element being either a sibling or parent to be the control. </p>
<div id="codepen-footer">
<a style="color: #f73535 !important;" href="https://codepen.wufoo.com/forms/m7x3r3/def/field14=" onclick="window.open(this.href, null, 'height=517, width=680, toolbar=0, location=0, status=1, scrollbars=1, resizable=1'); return false">Report Abuse</a>
&nbsp;
<a href="/memco/pen/conditional-form-fields/4">Edit this Pen</a>
</div>
</body>
</html>
<h1>Conditionally toggling visibility of form fields with Pure CSS(3)</h1>
<p>I was looking for a pure CSS way to hide/show some form elements based on their value. Turns out it's pretty easy for checkboxes and radios. Basic example:</p>
<form action="">
<fieldset>
<ol>
<li>
<input type="checkbox" id="requirement" class="control">
<label for="requirement">Check me</label>
<fieldset class="conditional">
<ol>
<li>
<label for="option">Related subfield</label>
<input type="text" id="option">
</li>
</ol>
</fieldset>
</li>
<li>
<input type="radio" id="radio" class="control">
<label for="radio">Radio</label>
<fieldset class="conditional">
<ol>
<li>
<label for="option">Related subfield</label>
<input type="text" id="option">
</li>
</ol>
</fieldset>
</li>
</ol>
</fieldset>
</form>
<p>In the above example, I use the class .control to indicate which fields are the control and .conditional to indicate what will be displayed when the control is checked. I use sibling selectors (<code>~</code>) to tie the conditional to the control</p>
<p>You can get creative with IDs to show fields based on certain values only:</p>
<form action="">
<fieldset>
<ol>
<li>
<input type="radio" name="residency" value="Citizen" id="citizen">
<label for="citizen">Citizen</label>
</li>
<li>
<input type="radio" name="residency" value="Immigrant" id="immigrant">
<label for="immigrant">Immigrant</label>
<fieldset class="conditional">
<ol>
<li>
<label for="country_citizenship">Country of Citizenship</label>
<input type="text" id="country_citizenship">
</li>
<li>
<label for="visa_type">Type of Visa</label>
<input type="text" id="visa_type">
</li>
</ol>
</fieldset>
</li>
</ol>
</fieldset>
</form>
<p>You can even nest them.</p>
<form action="">
<fieldset>
<ol>
<li>
<input type="checkbox" id="requirement-2" class="control">
<label for="requirement-2">Check me</label>
<fieldset class="conditional">
<ol>
<li>
<label for="option-2">Related subfield</label>
<input type="checkbox" id="option-2">
<fieldset class="conditional">
<ol>
<li>
<label for="country_citizenship">Country of Citizenship</label>
<input type="text" id="country_citizenship">
</li>
<li>
<label for="visa_type">Type of Visa</label>
<input type="text" id="visa_type">
</li>
</ol>
</fieldset>
</li>
</ol>
</fieldset>
</li>
</ol>
</fieldset>
</form>
<p>I haven't thought of a way to make this work with the <code>.conditional</code> in a different part of the form as it relies on an element being either a sibling or parent to be the control. </p>
.control:checked ~ .conditional,
#immigrant:checked ~ .conditional,
#required-2:checked ~ .conditional
#option-2:checked ~ .conditional {
clip: auto;
height: auto;
margin: 0;
overflow: visible;
position: static;
width: auto;
}
.control:not(:checked) ~ .conditional,
#immigrant:not(:checked) ~ .conditional,
#required-2:not(:checked) ~ .conditional,
#option-2:not(:checked) ~ .conditional {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment