Is it possible to change the appearance of an html link when it's disabled? For example using something like:
a.disabled
{
color:#050;
}
<a class="disabled" disabled="disabled" href="#">Testing</a>
The example above does not seem to work with IE but works for Firefox, on IE it remains gray even when I set the colour in the style. If I remove the disabled="disabled" however it works.
The :disabled pseduo class only works with input fields, like text, radio, checkbox, etc. and applies when you give the element the attribute `disabled="disabled". IE6, however, doesn't recognize the pseudo class, so you'll need to use a class separately to make it work.
<input type="text" value="You can't type here" disabled="disabled" class="disabled" />
can be styled with
input[disabled="disabled"], input.disabled {
/* whatever you want */
}
The pseudo class will apply to modern browsers while the class will cover IE6.
Like Radeksonic said, if you want the disabled CSS to appear on other elements, like anchors, you'll just need to make and use a class. There's no disabled attribute for <a>s
For a link like the one you provided in the comment:
some link
The style would be (just like any other selector based on an attribute):
a[disabled=disabled] {
color: red;
font-weight: bold;
}
If I was in your place, I'd check for cross-browser behavior, though. I haven't seen the disabled attribute used before.
Use
a.disabled
{
color: #CCC;/* Just an example */
}
Just use a dot followed by a class name to indicate that you want to use that class.
It works in all browsers
Of course, just adding a class to style your <a> elements in a particular way isn't going to stop them actually performing their normal action. For that, you'll need javascript. In a basic fashion, you could have:
linky
You could use the attribute selector for full browser support.
This will be sufficient:
a[disabled] {
display:none;
}
ATTRIBUTE SELECTORS
[att]
Match when the element sets the "att" attribute, whatever the value of the attribute.
[att=val]
Match when the element's "att" attribute value is exactly "val".
[att~=val]
Represents an element with the att attribute whose value is a white space-separated list of words, one of which is exactly "val". If "val" contains white space, it will never represent anything (since the words are separated by spaces). If "val" is the empty string, it will never represent anything either.
[att|=val]
Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-" (U+002D). This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML) as described in BCP 47 ([BCP47]) or its successor. For lang (or xml:lang) language subcode matching, please see the :lang pseudo-class.
<a class="disabled">My disabled link</a>
a.disabled {
display:none;
}
There are only 5 (I think) pseudo-class selectors for links: link, visited, hover, and active, and focus.
if you use JQUERY you can add attribute to anchor
.attr("disabled","true")
and remove it
.removeAttr("disabled")
Related
I know that I can target elements which have a specific attribute in CSS, for example:
input[type=text]
{
font-family: Consolas;
}
But is it possible to target elements which have an attribute of any value (except nothing i.e. when the attribute hasn't been added to the element)?
Roughly something like:
a[rel=*]
{
color: red;
}
Which should target the first and third <a> tags in this HTML:
red text
standard text
red text again
I figure it's possible because by default, cursor: pointer seems to be applied to any <a> tag which has a value for its href attribute.
The following will match any anchor tag with a rel attribute defined:
a[rel]
{
color: red;
}
http://www.w3.org/TR/CSS2/selector.html#pattern-matching
Update:
To account for the scenario #vsync mentioned, in the comment section (differentiating between emtpy/non-empty values), you could incorporate the CSS :not pseudo-class:
a[rel]:not([rel=""])
{
color: red;
}
https://developer.mozilla.org/en-US/docs/Web/CSS/:not
Yes in CSS 3 selectors there are several attribute selectors.
E.g.
[att]
Represents an element with the att attribute, whatever the value of the attribute.
[att=val]
Represents an element with the att attribute whose value is exactly "val".
[att~=val]
Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If
"val" contains whitespace, it will never represent anything (since the
words are separated by spaces). Also if "val" is the empty string, it
will never represent anything.
[att^=val]
Represents an element with the att attribute whose value begins with the prefix "val". If "val" is the empty string then the selector
does not represent anything.
[att$=val]
Represents an element with the att attribute whose value ends with the suffix "val". If "val" is the empty string then the selector does
not represent anything.
[att*=val]
Represents an element with the att attribute whose value contains at least one instance of the substring "val". If "val" is the empty
string then the selector does not represent anything.
Should add that if a browser sets an attribute by default you may need to work around. This does not appear to be an issue in "modern" brosers, however, this is an issue I have seen, so be sure to check cross-browser performance.
For instance, I discovered that in IE prior to 9, colSpan is set for all TD's in a Table, so any single cell has the hidden colspan value of 1.
So if you were targetting "any TD with colspan attribute" you apply in your webdoc, even the td's having no colspan attribute set, such as any TD being a single cell, will receive the css styling. IE less than 9 will basically style them all!
Only reason to concern over this is all the remaining XP users out there who cannot upgrade above IE8.
So for Example, I have a group of tables where the content may shift from end to end, leaving anywhere from 1 to 7 cells blank either at the end or the beginning.
I want to apply a color to any blank cells at the end or the beginning utilizing the colspan attribute.
Using the following will not work in IE less than 9
#my td[colspan] {background-color:blue;}
...all TD's will get styled (funny since the conditional attribute styling was supposedly superior in IE, but I digress...).
Using the following works across all browsers when I set the value of colspan to 'single' for any solitary cell/TD I wish to include in the styling scheme, however its a 'hack' and will not properly validate...
#my td[colspan="single"] {background-color:blue;} /* 'single' could be anything */
#my td[colspan="2"] {background-color:blue;}
#my td[colspan="3"] {background-color:blue;}
#my td[colspan="4"] {background-color:blue;}
#my td[colspan="5"] {background-color:blue;}
#my td[colspan="6"] {background-color:blue;}
#my td[colspan="7"] {background-color:blue;}
Alternatively you should be able to more appropriately address the issue using conditional styling using "if lt IE 9" to override. It would be the correct way to do this, just keep in mind you must hide the "properly constructed css" from IElt9 in the process, and I think the only proper way to do that is with selective style sheets.
Most of us already do that anyway, but regardless, you still do well to consider and test if a browser applies an auto-attribute when it sees none, and how it may handle your otherwise corect syntax for styling on attribute values.
(btw, colspan just happens not to be in the css specification yet [as of css3], so this example throws no validation error.)
Consider this css, which make all elements in form and with mask-amount class become bold:
form .mask-amount {
font-weight: bold;
}
Of course the above will not change the font weight of mask-amount elements outside form.
This will not be bold <span class="mask-amount">123,456</span>
<form>
This will be bold <span class="mask-amount">123,456</span>
</form>
Code At: http://jsfiddle.net/a9qt8jw7/
However when I review my css with Sonar, I get this error Name of overqualified element should be removed. The error described at https://github.com/CSSLint/csslint/wiki/Disallow-overqualified-elements In brief
Writing selectors such as li.active are unnecessary unless the element
name causes the class to behave differently. In most cases, it's safe
to remove the element name from the selector, both reducing the size
of the CSS as well as improving the selector performance (doesn't have
to match the element anymore).
I don't want the other elements with mask-amount class be bold and so I cannot simply remove the form from css selector.
I used this kind of selectors a lot. I want to know what is the best way to change my css and fix this error. (I can not change my htmls, this will be a HUGE task!!)
I need to display some text before and after the input tag using CSS content.
Using the following code I am not able to obtain the desired effect on the input tag, but works fine of a tag.
What am I doing wrong? How to fix it?
http://jsfiddle.net/pgdu2tue/1/
<input id="test" type="text" name="nametest">
<br>
<a id="moz" href="http://www.mozilla.org/">Mozilla Home Page</a>
#moz::before {
content:"XXX" ;
}
#test::before {
content:"BEFORE" ;
}
#test::after {
content:"AFTER" ;
}
It doesn't insert content before or after using ::befoer & ::after because there is no content in an input tag. Read a similar post here
This is not due to input tags not having any content per-se.
input elements are a special type called replaced elements, these do not support :psuedo selectors, as they are outside the scope of CSS.
In CSS, a replaced element is an element whose representation is
outside the scope of CSS. These are kind of external objects whose
representation is independent of the CSS. Typical replaced elements
are <img>, <object>, <video> or form elements like <textarea>
and <input>. Some elements, like or <canvas> are replaced
elements only in specific cases. Objects inserted using the CSS
content properties are anonymous replaced elements.
Note that this is even referred to in the spec
This specification does not fully define the interaction of :before
and :after with replaced elements (such as IMG in HTML).
I came across some HTML:
<div id="div-1">
hidfoli
</div>
I want to know what does span inside a tag mean?
span is a tag, not an attribute of any element, so probably that was mistyped and the HTML is invalid.
So either a must be nested inside the span tag, or span must be nested inside the a tag, but as far as attribute goes, there is nothing as such.
You can always validate your markup using W3C Validator.
If you mean that span is NESTED inside the a tag, than I can show you how it can be used.
<span>Hello</span>
Say you have above in your markup, now both the elements are inline so designers often nest the elements in such a way to achieve some typography effect say...
a {
color: red;
}
a span {
color: green;
}
Demo
Or he wants the two words on different lines, so he can do something like
a span {
display: block;
}
Demo 2
So it can be used in various scenarios but as far as your syntax goes, it's completely invalid.
Just some more information over attributes, if you want to define custom attributes, for some or the other reason, you can create them by prefixing their names using data-, and this is valid in HTML5 so the above can be written as hidfoli.
It does not mean anything.
It is ignored, except in the sense that browsers still parse the attribute and store it in the DOM (not directly as a property of the element node, but in the attributes array).
The example contains invalid html, it should be:
<span id="next">hidfoli</span>
In general a span is used to wrap text so that specific styling may be applied. More specifically as stated by the MDN Documentation:
The HTML element is a generic inline container for phrasing
content, which does not inherently represent anything. It can be used
to group elements for styling purposes (using the class or id
attributes), or because they share attribute values, such as lang. It
should be used only when no other semantic element is appropriate.
is very much like a element, but is a block-level
element whereas a is an inline element.
Looks like a mistake to me, it probably meant:
<span id="next">hidfoli</span>
Playing a little bit detective, the author probably put a span with id inside the to be able to access 'hidfoli' string, then realized mid-way that he could put the id right on the and thus ending up with a faulty html code.
Your html code is invalid html...span is html tag which can be placed inside a tag not as attribute of a tag.
correct way
<span>hidfoli</span>
Span is a HTML tag its not an attribute this is incorrect.
may be like this
<span id="next">hidfoli</span>
but will be same as
hidfoli
Using the first one not required just for giving text may be used for styling purposes.
Or if you are going to give attributes to nested objects like
a span $('a span') in Jquery
{
} in css or
I have several blocks that look like this:
<div class='templatechoicedesigncss'>
<img src='/images/templatepics/random(100x140).png' />
<p>
<input type='radio' name='templatechoice' value='random' checked>Random</p>
</div>
Whenever the INPUT field is marked as CHECKED - I need to change CSS to the div with class=templatechoicedesigncss.
But I need to do it through pure CSS only - no javascript, jquery or other triggers.
Is that possible?
Not possible without JavaScript.
By the way, a <p> inside a <span> is bad markup, because span's are inline elements and p's are paragraphs.
Also, put the text "Random" inside a <label>.
No you can not change parent css with pure CSS.
Not possible your way.
Check this http://www.w3.org/TR/selectors/#checked
It says...
Radio and checkbox elements can be toggled by the user. Some menu
items are "checked" when the user selects them. When such elements are
toggled "on" the :checked pseudo-class applies. While the :checked
pseudo-class is dynamic in nature, and can altered by user action,
since it can also be based on the presence of semantic attributes in
the document, it applies to all media. For example, the :checked
pseudo-class initially applies to such elements that have the HTML4
selected and checked attributes as described in Section 17.2.1 of
HTML4, but of course the user can toggle "off" such elements in which
case the :checked pseudo-class would no longer apply.
...exactly, word to word.
which basically means you can change dynamically the properties of the what is checked in CSS3 but not it's parent. but oh, it's not fully supported in browsers. other way is to use JS | jQuery | MooTools | YUI etc
Also, BoltClock pointed out that there is no parent selector in CSS; so not possible via only CSS