I have several input elements for dates and texts.
If their width is bigger than 94px the "X" ::-ms-clear is displayed but some of my inputs are smaller.
Is there a way to tell IE to display the "X" even if the input elements are only 50-60px wide? The solution should work for IE9 and higher.
Your best option here is to disable the custom clear button, and provide your own. You can disable the clear button in Internet Explorer and Microsoft Edge like this:
::-ms-clear { display: none }
This will not, however, disable the custom clear button in other browsers, such as Google Chrome. In Chrome, you'll need to target a different pseudo-element:
::-webkit-search-cancel-button {
-webkit-appearance: none;
}
The above pattern is understood by both Chrome and Microsoft Edge. For Internet Explorer, you will need to continue using the earlier ::-ms-clear element as well.
With these elements hidden, we can now provide our own:
<form>
<input type="search">
<button class="clear">✕</button>
</form>
We can then use Event Delegation to intercept click events on .clear at the parent <form>:
var form = document.querySelector( "form" );
form.addEventListener( "click", function ( event ) {
event.preventDefault();
if ( event.target.className === "clear" ) {
event.target.previousElementSibling.value = "";
}
});
You can see the end-result online at https://jsfiddle.net/jonathansampson/ety8bx93/.
Related
It's a basic question really, but I can't find an answer. My CSS looks like this;
a:focus, button:focus{outline:3px solid #000;}
What I want is for the focus to only apply when the user tabs to those elements using the keyboard.
This is exactly how it works in Chrome, but in FF and IE the outline shows when the user clicks on the elements. Which isn't what I want.
Is Chrome in fact wrong? I'm trying to keep it simple and avoid Javascript is possible. How can I get the outline to show only when the user tabs around the page?
Browsers are IE8+, FF and Chrome only.
Thanks for any help!
Ok I know I said no JS if possible, but I don't wanna to spend any more time on this ... so for anyone interested this is what I did
$("a, button, select, input[type=submit], input[type=text]").keyup(function(){
$(this).addClass("focusOutline");
}).blur(function(){
$(this).removeClass("focusOutline");
});
Which does the job just fine. HTH someone sometime
Use :focus-visible. It's currently a W3C proposal for styling keyboard-only focus using CSS. Until major browsers support it, you can use this robust polyfill.
/* Remove outline for non-keyboard :focus */
*:focus:not(.focus-visible) {
outline: none;
}
/* Optional: Customize .focus-visible */
.focus-visible {
outline-color: lightgreen;
}
I also wrote a more detailed post just in case you need more info.
I did this a different way elsewhere, and thought I'd add it in here as an alternative solution for anyone who stumbles across it:
/**
* If user hits tab key then we add a class to <html> that lets us use
* additional styling hints to show focus etc.
*/
function detectTabKey() {
$(document).keydown(function(e) {
if (e.keyCode === 9) {
$('html').addClass('is-tab-user');
}
});
}
I am trying to find out whether Safari is in fullscreen mode using Javascript. Chrome and Mozilla both use vendor-prefixed versions of document.fullscreenElement:
isFullscreen = function() {
return document.fullscreenElement
|| document.webkitFullscreenElement
|| document.mozFullScreenElement;
}
However, this doesn't work on Safari 5.1.7 -- there is no "webkitFullscreenElement" or similar property on document.
Does anyone know if there's a way to tell whether Safari is in fullscreen mode?
It's pretty hard to find this one, but the property you are looking for is:
document.webkitCurrentFullScreenElement
As you can see it doesn't follow standard, not even close, at all.
Also worth to mention as it is related: the property to check if fullscreen mode is supported is also hard to find (I still haven't...) so to check for support I do:
if (typeof document.webkitCurrentFullScreenElement !== 'undefined') ...
as the property will be null (or the fullscreen element).
Since there was no response to this, here is the hack I ended up using, which should be pretty broadly-applicable to anyone. Presumably this shouldn't be necessary in the near future once Safari adds the document.fullscreenElement specified in the W3 standard.
In my case, the situation was that I had a fullscreen button, and I wanted the fullscreen button to become a "shrink" button when the site was in fullscreen (like YouTube videos). As part of this, I had already implemented the :fullscreen css selectors to turn my button into a "shrink" button whenever the site was fullscreen.
I realized that Safari respected the :-webkit-full-screen css pseudoclass. Therefore, it was quick work to check that my button's image was the "shrink" image (AFAIK you can't test for pseudo-classes directly), and, if it was, we must be fullscreen.
So:
CSS:
.fullscreen-button
background-image: fullscreen.png
/* these match if .fullscreen-button is inside a fullscreen body element
(comma-separated selectors don't seem to work here?) */
body:fullscreen .fullscreen-button
background-image: fullscreen-exit.png
body:-webkit-full-screen .fullscreen-button
background-image: fullscreen-exit.png
body:-moz-full-screen .fullscreen-button
background-image: fullscreen-exit.png
JS:
isFullscreen = function() {
// this doesn't yet exist in Safari
if (document.fullscreenElement
|| document.webkitFullscreenElement
|| document.mozFullScreenElement) {
return true;
}
// annoying hack to check Safari
return ~$(".fullscreen-button").css("background-image").indexOf("exit")
}
This hack should be useable by anyone. Even if you're not doing something like changing the background image of a button in fullscreen mode, you should always be able to make some trivial little css rule change to one element -- especially if it's something that won't actually be visible, like the background-color of an element that's invisible, or the font-size of an element that doesn't have any text -- and test for that in JavaScript.
Update 2022. I found that this works for Safari - 'webkitIsFullScreen'.
So the following snippet seems to cover most browsers:
// Check Fullscreen
function check_fullscreen() {
if (document.fullscreenElement || document.webkitIsFullScreen || document.mozFullScreen) {
// document is fullscreen
}
else {
// document is not fullscreen
}
}
I noticed that if you wrap a radio button or checkbox in a label, the whole thing becomes clickable, even without a for/id pair (in fact, it seems to ignore this because I screwed it up!)
Example:
<label><input type="checkbox"> some text</label>
Then "some text" becomes clickable to check the box. I tested it in FF, Chrome and Opera, and IE8, does anyone know if it works in older browsers, like IE6?
Yes, that's the intended behaviour.
http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.9
To associate a label with another control implicitly, the control element must be within the contents of the LABEL element. In this case, the LABEL may only contain one control element. The label itself may be positioned before or after the associated control.
It looks like this doesn't actually work in IE6 (haven't tried other versions). If you already have something like jQuery loading on your page, then you could come up with a workable solution fairly easily:
if ($.browser.msie) {
$('label:has(:input):not([for])').each(function() {
var $t = $(this)
, $in = $t.find(':input')
;
if (!$in.attr('id')) {
// use this, or make a proper GUID...
$in.attr('id', 'input_' + (Math.random() * 1000000));
}
$t.attr('for', $in.attr('id'));
});
}
Wrapping label elements are broken in Internet Explorer up until version 7. Link (Google Cache to bypass the registration annoyance).
Is it possible to override the styling that is applied to a hyperlink if it has the disabled="disabled" attribute?
It's currently greyed out. Not bothered about making it an active link, just want to change the font, color, etc.
UPDATE : Must work in IE6, IE7 & FF
UPDATE :
It's worse than I though the html is <A id="someId" disabled>About Your Group</A>
UPDATE :
I'm going to really have to see what is adding this 'disabled' to the links.. I think it's a jquery plugin.. (ui.tabs, jquery ui.tabs)
The disabled property can't be used on a elements. it only applies to input, select and button elements.
Sure; Internet Explorer puts a bevel-effect on links with this property set. FireFox, on the other hand, ignores this property completely.
Note: Links will still function. Their default behavior is NOT prevented--they just look disabled. They do not behave like a disabled text input.
You are better off using a class to signal if a link is disabled. This will work cross-browser as well...:
The CSS
.disabled { color: #ccc; }
The HTML
...
And to complete the disabled effect; using jQuery, you can select all links with the class "disabled" and prevent their default behavior, like so:
$(function ()
{
$("a.disabled").click(function ()
{
// return false to disable the link (preventDefault = true)
return false;
});
});
I've noticed that ASP.Net puts disabled="disabled" on <a> tags when setting the Enable property to false on an <asp:HyperLink>.
This causes css-rules for that element to be ignored in IE (even for a[disabled="disabled]!), which is extremely annoying. Other browsers don't care, since they ignore that property.
My solution was to simply set the NavigationUrl property to null in the code-behind for the elements I wanted to disable.
The advantage of doing this server side instead of with JavaScript is that it will work even if users have JavaScript turned off.
I don't know to what extent the disabled attribute is supported for hyperlinks. Make sure you test thoroughly.
I see two ways of targeting this in CSS:
CSS 2.1
You can try the CSS 2.1 attribute selector
a[disabled=disabled] { color: blue }
I think this is most likely to work with a non-form element. Doesn't work in IE <= 6. Quirksmode compatibility table.
CSS 3
In CSS 3, it's possible to use the :disabled pseudo-class (source)
input:disabled { background-color: yellow; }
doesn't work in any IE including 8. Works in Firefox, Chrome and Opera. Quirksmode compatibility table
I've never seen disabled used on a normal hyperlink so you will have to try whether that works. Per the specification, the disabled pseudo-class is for disabled form elements only.
Whe you're using ASP.NET, and you disable a LinkButton on server side, the html generated is an <a> tag with disabled="disabled" non-standard attribute. However, there's no href attribute generated, so that the link will not behave like a link in any of the browsers.
The problem is that IE adds the typical "bevel effect" to the disabled link, and the other browsers render it as "regular text".
You can solve the problem in non-IE browsers styling like this:
a:not([href]) /* this is for ASP.NET disabled links */
{
opacity: .5; /* all but IE before 9 */
}
The problem is that IE (at least up to IE 8) keeps doing the "bevel" effect on the disabled link. To make IE behave like the other browsers you need to change the CSS style, adding this non-standard filter attirbute (only works for IE):
filter: alpha(opacity=50);
And you also need to use some javascript, i.e. jQuery, to remove the offending disabled attribute. I.e.
$('#controlId').attr('disabled','')
If your case is even more strange, and you have disabled and href, you should remove also the href so that the style can be applied and the link doesn't work.
I don't think there is a 'disabled' attribute for hyperlink (anyway it doesn't respect w3c recommandations) but you can try to add class for styling these elements like :
<a class="inactive" ...>...</a>
And for the css :
a.inactive {
color:#000
}
I'm trying to change the color of input controls when they are disabled using the following css.
input[disabled='disabled']{
color: #666;
}
This works in most browsers, but not IE. I'm able to change any of the other style properties such as background-color, border-color, etc... just not color. Can anyone explain this?
Unfortunately if you use the disabled attribute, no matter what you try IE will just default the color of the text to Grey, with a weird white shadow...thing... yet all other styles will still work. :-/
I had the same problem for <select> elements in IE10 and found a solution that works for select elements only:
http://jsbin.com/ujapog/7/
There is a Microsoft pseudo-element that allows the text color to be modified:
select[disabled='disabled']::-ms-value {
color: #000;
}
The rule must be on it's own, because otherwise other browsers will ignore the whole rule due to syntax error. See http://msdn.microsoft.com/en-us/library/ie/hh869604(v=vs.85).aspx for other Internet Explorer only pseudo elements.
Edit: I think the rule should probably be select[disabled]::-ms-value but I don't have older IE versions in front of me to try it - re-edit this paragraph or add comment if that is an improvement.
There is no way to override styles for disable="disable" attribute. Here is my work around to fix this problem, note I am only selecting submit buttons in my case:
if ($.browser.msie) {
$("input[type='submit'][disabled='disabled']").each(function() {
$(this).removeAttr("disabled");
$(this).attr("onclick", "javascript:return false;");
});
}
example available: http://jsfiddle.net/0dr3jyLp/
I had the same problem with textarea "disabled" changing font color to gray.
I did a workaround by using "readonly" attribute instead of "disabled" attribute to textarea
with below css
textarea[readonly] {
border:none; //for optional look
background-color:#000000; //Desired Background color
color:#ffffff;// Desired text color
}
It worked for me like a charm!!, so I suggest to try this first before any other solution as it is easy to replace "disabled" with "readonly" without changing any other parts of code.
I Know it's been a while since the creation of this topic, but i created this workaround, and well... It worked for me! ( Using IE 9 )
The only consequence is that you can't select the value of the input.
Using Javascript:
if (input.addEventListener)
input.addEventListener('focus', function(){input.blur()}, true)
if (input.attachEvent)
input.attachEvent("onfocus", function(){input.blur()})
I just made the whole background a light gray color, I think it more easily/quickly convey's that the box is disabled.
input[disabled]{
background: #D4D4D4;
}
The way I solved the problem of "disabling" a control in IE w/o the ugly gray with a input control of type = checkbox was to leave it enabled and use a little javascript in the onclick event to prevent changes:
onclick='this.checked == true ? this.checked = false : this.checked = true;'
As mentioned by Wayne, and three years later still no luck w/ IE9, but...
You could try lowering the opacity using CSS, it makes it more readable and helps with the whole disabled state.
It is the solution that I found for this problem:
//if IE
inputElement.writeAttribute("unselectable", "on");
//Other browsers
inputElement.writeAttribute("disabled", "disabled");
By using this trick, you can add style sheet to your input element that works in IE and other browsers on your not-editable input box.
I mixed user1733926's and Hamid's solutions and I found an effective code for IE8, would be nice to know if it works in ie 9/10 as well (?).
<script type="text/javascript">
if ($.browser.msie) {
$("*[disabled='disabled']").each(function() {
$(this).removeAttr("disabled");
$(this).attr("unselectable", "on");
});
}
</script>
After reading this post I decided to create a input that acts similarly to a disabled input box but was "readonly".
So I've made it so it wasn't able to be selected or tabbed to, or have a mouse cursor that gave the user the idea they can change or select the value.
Tested on IE8/9, Mozzila 18, Chrome 29
<input name="UserName" class="accountInputDisabled" id="UserName" type="text" readOnly="readonly" value="" unselectable="on" tabindex="-1" onselectstart="return false;" ondragstart="return false;" onmousedown='return false;'/>
input.accountInputDisabled {
border: 1px solid #BABABA !important;
background-color: #E5E5E5 !important;
color: #000000;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-moz-user-input: disabled;
-ms-user-select: none;
cursor:not-allowed;
}
input:focus {
outline: none;
}
No need to overrride CSS use class based approach and play with events works perfectly
You can do one thing:
<button class="disabled" onmousedown="return checkDisable();">
function checkDisable() {
if ($(this).hasClass('disabled')) { return false; }
}
http://navneetnagpal.wordpress.com/2013/09/26/ie-button-text-shadow-issue-in-case-of-disabled/
Remove disabled attribute and use readonly attribute. Write required CSS for achieving the required result. This works in IE8 and IE9.
for e.g., for dark grey,
input[readonly]{
color: #333333;
}
Please check this CSS code.
input[type='button']:disabled, button:disabled
{
color:#933;
text-decoration:underline;
}
or check this URL.
http://jsfiddle.net/kheema/uK8cL/13/
The problem is solved in IE11.
If the problem still persists in IE11, check for the rendering engine IE is using.
I came across this piece of code at stackoverflow which helped me take off disable css class using javascript.
$("#textboxtest").prop("disabled", false).removeClass("k-state-disabled");
Original thread can be found at Applying k-state-disabled class to text inputs - Kendo UI
Thought I should share!
Use this css, works for IE11:
input[disabled="disabled"], select[disabled="disabled"], textarea[disabled="disabled"] {
opacity:0.99 !important;
background:black;
text-shadow:inherit;
background-color:white;
color:black
}