The priority of CSS selectors - html

Here is my HTML:
<input type='submit' class='clsname'>
This CSS makes a blue border for that input: Demo
input{
border: 1px solid red;
}
.clsname{
border: 1px solid blue;
}
But this makes a red border for that input: Demo
input[type='submit']{
border: 1px solid red;
}
.clsname{
border: 1px solid blue;
}
I don't know why exactly, But I know it is related to the priority of selectors. I think input[type='submit'] has a bigger priority than classname, but input doesn't have a bigger priority than classname.
Now I have a selector like this: input[type='submit'] and I need to give a bigger priority to class name. How can I do that? I mean something like this:
input[type='submit'] { }
.clsname { /* bigger priority here */ }
Note: I can do that by !important, But I want to know is there any approach else? Because using !important is not recommended.

It's specificity. Read more here on W3.
Use input[type='submit'].clsname, .clsname selector.
Here's the fiddle.

You can overqualify your selector intentionaly to give it higher priority.
input[type="submit"].clsname

Related

Using * with :not in css

Im working on a html linter using css.
Reference: https://bitsofco.de/linting-html-using-css/
I like the idea of highlighting elements that have inline styles like so:
*[style] {
color: red !important;
border: 5px solid red !important;
}
However, I do have certain instances where I have to use inline styles, ie canvas elements.
How do I use the :not selector with the *?
Can I have multiple :nots, ie :not(canvas):not(form), etc
What you have works and excludes the canvas. And yes, you can chain multiple :not()s like that.
* {
border: 1px solid black;
}
*[style]:not(canvas):not(form) {
color: red !important;
border: 5px solid red !important;
}
<canvas style="foo">canvas</canvas>
<form style="foo">form</form>
<div style="foo">div</div>
the :not() rule matches anything not matching the subrule. The subrule is a valid css selector. writing [canvas] will match any element with a canvas attribute, so this isn't what you want.
The correct usage is:
*[style]:not(canvas):not(form)

How to change last table's <tr> border color

I have a basic table with some rows. Each row has a border-bottom of 2px, but I would like the last not to have any bottom at all. However, I do not seem to manage to do this with my current code.
<table id="products">
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr class="productsBottom"></tr>
</table>
This is my CSS:
#products tr {
height: 94px;
border-bottom: 2px solid #e5e5e5;
}
tr.productsBottom {
border-bottom: 0px solid;
}
Any help on this? I don't seem to understand why it works when I add a background-color, but changing the border in any way doesn't seem to do anything?
You need to set border-bottom to 'none', you can also select the last row directly without needing to assign it a class, using the :last-child selector
#products tr:last-child{
border-bottom:none;
}
You may also want to add:
#products{
border-collapse:collapse;
}
The first selector in your css has greater specificity, hence it'll override the second one were you're setting the border to zero.
You can avoid this by using !important keyword or a more specific selector like
#products tr.productsBottom {
border-bottom: 0px solid;
}
This is down to css specificity. The #products tr selector is most specific, so is the one that is applied. To increase the specificity of the later selecter use:
#products tr.productsBottom {
border-bottom: 0px solid;
}

How can I make an <input> hover CSS work only if the <input> does not have focus?

I have an input field CSS like this:
input {
background: white;
border: 1px solid #bbb;
box-shadow: none;
}
input:hover {
background: white;
border: 1px solid #999;
outline: none;
box-shadow: none;
}
input:focus {
outline: none;
box-shadow: none;
border: 1px solid #555;
}
My problem is that when I give the input focus the hover effect takes over if my cursor is also over the <input>.
Is there a way I can make the hover effect work only when the input does not have focus?
With the code you've provided I can't reproduce the issue, which leads me to believe that this is a specificity problem. (If you're not sure what that is, I wrote a blog post about specificity earlier today: What the heck is specificity?)
With the code you've provided, both input:hover and input:focus have a specificity of 011. As long as input:focus is included after input:hover in your stylesheet then the focus style will always override the :hover.
First JSFiddle demo.
If your :hover has an extra selector, its specificity will be higher than the :focus selector. For instance:
input.example:hover { ... }
input:focus { ... }
Second JSFiddle demo.
With an extra class, the :hover selector above now has a specificity of 021 and will override the :focus selector which still has a lower specificity of 011.
Ensure your :focus selector has higher specificity and your :hover will not apply when the element is focussed.

html - how to make <a> clickable in border?

Say I have a
<a class="myclass" href="foo.htm">Click Here</a>
and in css something like this:
.myclass
{
border: 2px solid #000;
padding: 1em;
}
so the <a> looks like a button but it only operates when clicked on the text, not in the whole box. How can I make so that the box also "catches" onClick?
Block will not work well unless you float the element and give it a fixed width. I think "inline-block" would work better.
.myclass{
display: inline-block;
border: 2px solid #000;
padding: 1em;
}
You can see it in action here: http://jsfiddle.net/2tmzL/
Browser support for inline-block is pretty good: http://caniuse.com/inline-block
Wrap the anchor tag around another container element
<a class=".." href=".."><div>Click here</div><a>
< a > is an inline , you have to transform it to a block, try this
.myclass:
{
display:block;
border: 2px solid #000;
padding: 1em;
}
You need to set the css property display: block or inline-block (depending the case...) for your a element.
I seem to be able to click the entire link. Make sure you remove : after .myclass. Also if it's still not working you may like to try adding display:block;
Alternatively in html5 you can wrap the a tag around a block element. This will work in older html though it's not correct.
.myclass
{
border: 2px solid #000;
padding: 1em;
display:block;
}
The issue is that a's are inline elements, and padding doesn't work the way we expect with inline elements. Change the a's to a block level element, and everything should work as you expect (note the removal of the ":" in the CSS declaration, that shouldn't be there):
.myclass {
display: block;
border: 2px solid #000;
padding: 1em;
}

border around text input in a html form

I want a red border around my input text fields in a html form. But using css
input { border: 1px solid #d66 }
also puts a red border around my buttons (which I don't want).
input.button, input.submit or input.text and anything inside { } doesn't do anything.
How do I change the border around a text input only, and how do I change the font in the submit button only? I'm using IE9.
Thanks!
To target textboxes, use
input[type="text"] { border: 1px solid #d66 }
There are a lot more attribute selectors available.
Have a look at http://css-tricks.com/attribute-selectors/
What you need is an attribute selector:
input[type="text"] { border: 1px solid #d66 }
This selects the input of type "text."
You can also use other attribute selectors to select other elements:
input[type="submit"] { border: 1px solid #d66 }
input[type="checkbox"] { border: 1px solid #d66 }
But also be aware that these selectors are not supported by IE7 and lower.