Text-only HTML radio buttons that are keyboard-navigable? - html

This thread (Text only radio buttons) provides a way to make radio buttons that show only text, surrounded by a border that changes color when the button is selected.
The problem is that keyboard navigation does not work in this setup. That's not surprising; it's hiding the radio button and displaying only the labels.
Is there a way to have text-only radio buttons that work with keyboard navigation?
Thank you.

The only solution I can think of without Javascript is to make the button invisible without using display: none, because it doesn't render the button and so you can't navigate it.
This can be done trough opacity. This will make it invisible, but it'll still be there on its spot. In order to make it not interfere with the other elements, you can set it to position: absolute. All troubles gone.
See:
label {
display: inline-block;
width: 100px;
height: 50px;
border: solid 2px red;
}
input[type="radio"] {
opacity: 0;
position: absolute;
}
input[type="radio"]:checked + label {
border: solid 2px green;
}
<input type="radio" id="test" name="jeff">
<label for="test">Pizza</label>
<input type="radio" id="test2" name="jeff">
<label for="test2">Steak</label>
An addition as mentioned by CBroe is to use the .visuallyhidden class instead of just using opacity. The compatibility might be better.
input[type="radio"] {
position: absolute;
overflow: hidden;
clip: rect(0 0 0 0);
height: 1px; width: 1px;
margin: -1px; padding: 0; border: 0;
}

The following W3C page describes a way to make custom radio elements using ARIA:
ARIA radiogroup and radio Example
You should not use any CSS trick with native radio elements as it could led to unexpected behaviour. For instance focusing a native radio element with a screenreader may not always display the focus indicator (outline) around the label, but around the checkbox, which is problematic for people with low vision if that checkbox has been hidden.

Related

Custom input field border behavior when trying to include password eye

I am trying to create a custom input field with a text input and a button with an eye icon to show/hide the password text.
Now, I know that simply create an input field and absolutely position the eye button but the problem with that is that on certain browsers, the Lastpass extension or Safari keychain will insert their respective icons thus overlapping the eye icon which I always want to be visible and clickable. As an example, this is bad:
Therefore, this is what I want to achieve:
I am trying something like this:
<div className="input-wrapper">
<input className="password-input" type="password" />
<button className="password-eye">
</div>
CSS
.input-wrapper {
position: relative;
border: 1px solid #ccc;
}
.password-input {
border: none;
width: calc(100% - 32px);
height: 40px;
}
.password-eye:extend(.icon-password-eye all) {
background: white;
border: none;
height: 40px;
font-size: 24px;
position: absolute;
right: 0px;
top: 0px;
padding-right: 8px;
}
.icon-password-eye::before {
content: '\e618';
}
The problem that I'm facing is that because I want the whole wrapper to behave as if it were an input component, when the user selects the input field, only the actual input part is selected. In addition, if the password validation errors out, only input field is shown to be red while I want the entire portion to be red.
This is what is happening:
Any help would be greatly appreciated.
what you want to do is changing the border CSS of the input-wrapper class.
and not the input
you need to change the code that changing the input css to change the input-wrapper css.
if you need help in that , please give the full code.

Printing a checked checkbox / tick box with HTML and CSS

I have the following problem: I have to use an HTML->PDF conversion service to render a piece of HTML. However, this service is a bit limited in it's functionality, so I need a way to "work around" it.
I'm mainly just printing text, so it's not a big deal, but the only problem is that I have to print some "unticked" and some "ticked" check boxes, my converter is failing at this. In particular I've tried:
Using the unicode ☐ ("☐") and ☑ ("☑") characters, but the converter doesn't render them (probably the font it's using doesn't
have them)
Using the WingDing characters þ and ¨ but again, the wingding font is not recognized
The converter doesn't support images, so can't just use an image
I was thinking, at this point, to "simulate" a checkbox by using spans with borders, something like:
<span style="border: 1px solid black; height: 12px; width: 12px;"></span>
However, I can't make it look correct (no fault of the converter this time, even browsers show the above as just one vertival line.
Can anyone help me "draw" checkboxes using just "basic" html elements? What would be the cleanest way?
PS: checkboxes need to be inline with the text.
You're on the right track.
Using HTML and CSS:
/* The standalone checkbox square*/
.checkbox {
width:20px;
height:20px;
border: 1px solid #000;
display: inline-block;
}
/* This is what simulates a checkmark icon */
.checkbox.checked:after {
content: '';
display: block;
width: 4px;
height: 7px;
/* "Center" the checkmark */
position:relative;
top:4px;
left:7px;
border: solid #000;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}
<div class="checkbox"></div> Unchecked<br><br>
<div class="checkbox checked"></div> Checked
The reason YOUR code didn't work was because you were using a span element, which is an inline element. You can use a span for this, but you'll need to add the style of display: block to the element (making it act as a block element instead of an inline element).
The div tag is a block, so no need for setting it's display style. If you would like the div to display inline, set the display: inline-block
Try this :
<div style="border: 1px solid black;
width: 10px;
height: 10px;
display: inline-block;
margin-right: 4px;">
</div>
https://jsfiddle.net/8rt4dqfc/

Issue with input element hover on Internet Explorer 10+

Let's have a label element with the for attribute specified properly. This label will later have some action bound on click event, so we want to change the label's appearance on hover to indicate the option to click it.
However, if you open IE 10/11 and put your cursor over the label's corresponding input element, the label's :hover pseudoclass will activate!
I tried floatng the label and input and setting position: absolute.
The only solution I could find was to change/remove label's for or input's id so they wouldn't be connected to each other. But this is not a good solution, since it disconnects these two elements semantically and will probably lead to issues with screen readers. Is there any other way to resolve this bug?
JSFiddle: fiddle
JSFidde with broken for-id link: fiddle
You don't have to use "for" at all if you change your HTML structure a little bit. And yes, label and input are still "connected".
<div>
<label>
<span>label</span>
<input id="test" type="text">
</label>
</div>
Here is example: http://jsfiddle.net/Vq8kN/8/
if you want you can get your goal with 2 lines of jquery (I don't know if you can use it)
working demo
$("input").hover(function(){
$("label").removeClass('hovertest');
}).mouseout(function(){
$("label").addClass('hovertest');
});
css
div {
margin: 1em auto;
width: 300px;
padding: 0.2em;
border: 1px dotted black;
}
label {
padding: 0.2em 1em;
}
input {
width: 200px;
}
.hovertest:hover {
background: #ccc;
cursor: pointer;
}
html
<div>
<label class="hovertest" for="test">label</label>
<input id="test" type="text"/>
</div>

Placeholder text in an input field with CSS only (no JavaScript)

Instead of labeling each field in a form, it is sometimes preferable (from a design standpoint) to have placeholder text in each field. For example, instead of having this:
----------------------------------
Full Name: | |
----------------------------------
you have this:
----------------------------------
| Full Name |
----------------------------------
The when you click in the field, the text disappears and you can write whatever you want. If you skip over the field without entering any text, then the placeholder reappears.
I've seen this done many ways, but all methods involve JavaScript. For example, Twitter does a decent job on their signup page but if Javascript is disabled you end up typing your name over the word 'Full name'.
I'm looking for a CSS-only method that would work even with JavaScript disabled. The only potential solution I've come up with is to set the background of the <input> tag to an image of the desired text and then use the input:focus pseudo-class to clear the background image when someone clicks on the text box. This seems to work but it would be nice not to have to use images.
Does anyone know of a good resource on how to do this?
This is the preferred method, and works in all current browsers:
<input type="text" name="" placeholder="Full Name"/>
This version works for IE9 and before:
<input type="text" name="" value="Full Name" onfocus="value=''" onblur="value='Full Name'"/>
You can do this with a <label> placed behind the index using z-index and a transparent background-color on the <input>. Use :focus to change to a white background.
:first-line has some Firefox issues.
Demo: http://jsfiddle.net/ThinkingStiff/bvJ43/
Note: See code-sushi's comment below for blur issues: Placeholder text in an input field with CSS only (no JavaScript)
Output:
HTML:
<label class="input">enter name<input /><label>​
CSS:
.input {
color: gray;
display: block;
font-size: small;
padding-top: 3px;
position: relative;
text-indent: 5px;
}
input {
background-color: transparent;
left: 0;
position: absolute;
top: 0;
z-index: 1;
}
input:focus, input:first-line {
background-color: white;
}
Try this:
HTML
<div>
<input type="text" id="text"></input>
<label for="text">required</label>
</div>
CSS
.text-wrapper {
position: relative;
}
.text-input-label {
position: absolute;
/* left and right properties are based on margin, border, outline and padding of the input text field */
left: 5px;
top: 3px;
color: #D1D1D1;
}
#text:focus + label {
display: none;
}
Working Fiddle
All of the presumably CSS-only answers above have neglected a critical component which is required in order to prevent the label acting as a pseudo-placeholder from "bleeding through" once the user is no longer focused on that particular field.
Hint:
input:valid { background-color:white; }
The pseudo-class :valid obtains whenever a field has any value other than ''. So when your user enters anything in the field of his or her own, the label displayed there will stop being displayed.
Be advised with <input type="email" /> fields, the pseudo-class :valid does and will actually require input of a valid email format (e.g. "xxxx#xxx.com" -- or .net or .org, etc.).
Full instructions how to do this here: http://css-tricks.com/float-labels-css/
Try this: it solves the overflowing placeholder and multi-input cases. The trick is to move the labels behind their inputs and reorder them visually.
You don't need an extra div to achieve what you want.

How do I add a "search" button in a text input field?

How do I create a similar “search” element to the one in this site?
If we view source, he has one textbox followed by a <span> tag.
<input type="text" name="q" id="site-search-input" autocomplete="off" value="Search" class="gray" />
<span id="g-search-button"></span>
Where do I get a similar "magnifying glass" image?
Put the image into the span, for example using background-image, then give it a relative position and move it to the left so it overlaps the right end of the search box, for example:
#g-search-button {
display: inline-block;
width: 16px;
height: 16px;
position: relative;
left: -22px;
top: 3px;
background-color: black; /* Replace with your own image */
}
Working example on JSBin
Your eyes are deceiving you. The button is not within the text box. Using a background image is NOT the way to go, as it wont provide the clickable submit button.
What you need to do is add a wrapper div around the input:text and input:submit.
The wrapper will look like it's a text box, but will actually contain a transparent text box and a submit button. You'll need to specifically remove the styles for the input:text and input:submit elements.
It's very important that you keep the submit button, otherwise hitting enter while searching will not have a default reaction. Additionally placing the submit button after the text field allows people to actually click on the button.
You can make your own magnifying image, they're pretty easy to make in a 20x20px transparent png.
.search {
border: 1px solid #000000;
width: 200px;
}
.search input[type="text"] {
background: none;
border: 0 none;
float: left;
height: 1.5em;
line-height: 1.5em;
margin: 0;
padding: 3px 0;
width: 180px;
}
.search input[type="submit"] {
background: #CCCCCC url(path/to/image.jpg);
border: 0 none;
height: 1.5em;
line-height: 1.5em;
margin: 0;
padding: 3px 0;
text-indent: 100px;
width: 20px;
}
<form ...>
<div class="search">
<input type="text" />
<input type="submit" />
</div>
</form>
If you view the page in Google Chrome, right-click on the search button and select “Inspect element”, you’ll be able to see the CSS used to achieve this effect.
If you’re not familiar with CSS, I thoroughly recommend ‘CSS: The Definitive Guide’.
A site like Iconspedia has a number of free icons that are similar.
Wherever you get the icon be careful to ensure that you have the rights to use it in your application. Many graphics are protected and some have restrictive licenses.
If you use a background image on the field then there's no way to bind to it to get the click action. So the solution is to have a separate search field and image, so you can bind click event in jQuery to the image. Fiddle here:
http://jsfiddle.net/Lzm1k4r8/23/
You can adjust left: position to be left or right side of the search box.
To help with user feedback why not add the pointer icon to your mouse when you're hovering over the magnifying glass? Just att this to your CSS:
.search:hover {
cursor:pointer;
}
I'd like to plug a new jQuery plugin I wrote because I feel it answers to the OP's request.
It's called jQuery.iconfield: https://github.com/yotamofek/jquery.iconfield.
It lets you easily add an icon to the left side of your text field. For using the icon as a button, you can easily bind to the 'iconfield.click' event, which is triggered when the user clicks the icon. It also takes care of changing the cursor when the mouse is hovering over the icon.
For instance:
$('#search-field').iconfield( {
'image-url': 'imgs/search.png', // url of icon
'icon-cursor': 'pointer' // cursor to show when hovering over icon
} );
$('#search-field').on( 'iconfield.click', function( ) {
$('#search-form').submit()
}
I would love to get some feedback on my work.