I'm trying to understand how wildcard selector in CSS does work?
Consider the following HTML markup:
<div id="child">
</div>
And corresponding CSS:
div[id="child"] {border-color: green; }
#child{
border: 20px solid;
background: aqua;
height: 50px;
margin: 10px;
}
I think, that style of div.child will be:
border: 20px solid;
background: aqua;
height: 50px;
margin: 10px;
border-color:green;
I.e. border-color:green just add to a stylesheet of div.child. But if we write
div[id="child"] {border-color: green!important; }
#child{
border: 20px solid;
background: aqua;
height: 50px;
margin: 10px;
}
It works like
#child{
border-color: green;
border: 20px solid;
background: aqua;
height: 50px;
margin: 10px;
}
Question: Why we must using div[id="child"] {border-color: green!important; } rather than div[id="child"] { border-color: green } for applying green color for border?
It's an issue of specificity. Example demonstrating this.
Refer to the following documentation:
6.4.3 Calculating a selector's specificity
count the number of ID attributes in the selector (= b)
count the number of other attributes and pseudo-classes in the selector (= c)
count the number of element names and pseudo-elements in the selector (= d)
Therefore #child has a specificity of 100. And div[id="child"] would have a specificity of 11.
Usage of !important would effectively override the border applied by #child.
Alternatively, you could use the following, and avoid using !important.
jsFiddle example
div#child[id="child"] {
border-color: green;
}
This doesn't explain the difference between using !important and not using it, but rather how to apply the green border without using two rules.
If you look at the documentation for border:, you will find this:
Formal syntax: <br-width> || <br-style> || <color>
<color>:
A <color> denoting the color of the border. If not set, its default value is the value of the element's color property (the text color, not the background color). See border-color.
The default color is #000 (black).
So, by writing
border: 20px solid;
you are basically specifying:
border-width: 20px
border-style: solid;
border-color: #000;
And if you put border-color: green before that rule, it will be overwritten. So you could either write:
border: 20px solid;
border-color: green;
or
border: 20px solid green;
Using two rules just to apply the border color is unnecessary.
It has to do with the specificity of your selector. When there is a conflict between multiple selectors which are trying to apply mutually exclusice styles, specificity is the measure that determines which styles win out.
So your first selector div[id="child"] is an attribute/class selector, which has lower specificity than the Id selector from your second block: #child
When you apply !important to a style, it is applied irrespective of specificity. It should also be used sparingly for that reason.
Related
I have different css classes based on different actions. Everything is working good, but when I apply activeBackground class based on condition its making div background-color to green but border-left-color is not coming green its still using .arrow-div class. How can I resolve this issue and apply .activebackground class when needed?
HTML
<div class="text-arrow" ng-class="{'activeBackground': applyActiveFile, 'completeBackground':applyComplete}">File Selection
<span class="arrow-div"></span>
</div>
CSS
.text-arrow {
background-color:#BABABA;
color:#fff;
display:inline-block;
padding-left:45px;
}
.arrow-div {
border-style: dashed;
border-color: transparent;
border-width: 0.15em;
display: -moz-inline-box;
display: inline-block; /* Use font-size to control the size of the arrow. */
font-size: 100px;
height: 0;
line-height: 0;
position: relative;
vertical-align: middle;
width: 0;
background-color:#fff; /* change background color acc to bg color */
border-left-width: 0.2em;
border-left-style: solid;
border-left-color: #BABABA;
left:0.25em;
}
.activeBackground{
background-color: green;
border-left-color: green !important;
}
It appears to me that you're applying .arrow-div and .activeBackground to different elements, and the way your code is written, .activeBackground can't override .arrow-div because it's being applied to a different element (the parent). To affect the child element (the span containing the arrow) you need to set up a css rule that directly targets any child .arrow-div of .activeBackground.
My solution was to simply modify your css like so, providing a way to change the arrow div:
.activeBackground{
background-color: green;
}
.activeBackground .arrow-div{
border-left-color: green;
}
Here's a fiddle of it in action:
https://jsfiddle.net/cupno5g9/
I am trying to apply a css style to the first children of an element. So say I have a div, with two divs, which are the children, and within each child is their own child, which are the grandchildren.
This JSFiddle, I hope is what I've done: http://jsfiddle.net/o8xhba9u/
#parent {
border: 1px solid;
padding: 10px;
}
#child-one {
text-indent: 5px;
padding: 10px;
}
#child-two {
text-indent: 5px;
padding: 10px;
}
#parent * {
border-top: 1px solid red;
}
My goal is to only have the children (child-one and child-two) to only be the ones with the red border-top. The paragraph elements (grandchildren) shouldn't have the red outline. I am trying to accomplish this dynamically, as if I were to have different elements, and add new ones later and have the effect applied without having to edit the css. How can I accomplish that?
You are looking for the direct child combinator, >.
Example Here
#parent > * {
border-top: 1px solid red;
}
So I have a field that is supposed to have a black outline. Like this
Where the 237 is. But here's what I have
.r{
height: 40px;
font-size: 30px;
width: 100px;
font-family: 'proxima_novalight';
outline: none;
background: none;
outline: 3px solid black;
}
For some reason when I select the field it gets smaller. And on initial load, there's kind of like an outline around it. A grayish one. You could call it a shadow Here's a demo. Ideas?
Use border instead of outline to remove the "shadow":
.r{
height: 40px;
font-size: 30px;
width: 100px;
font-family: 'proxima_novalight';
outline: none;
background: none;
border: 3px solid black;
}
JSBin: http://jsbin.com/cuwurowu/2/edit
The “shadow” is the default border of the input element. To remove it, add
.r { border: none }
(but note that this affects the totals dimensions of the element, which may matter in pixel-exact layout).
The shrinking effect in Chrome (does not seem to happen in Firefox or IE) is apparently caused by a browser default style sheet that sets outline-offset: -2px on the element when it is focused. The outline-offset sets the distance between an outline and the outer edfes of the element, so a negative value shrinks the outline. To fix this, add
.r { outline-offset: 0 }
I have this style:
input[type=text], textarea {
width: 98%;
border: 1px #999 solid;
}
It applies the width to all the input that are text but how can I exclude the ones that have the attribute size? I tried input[type=text],input[size!=2], textarea but it doesnt work.
Thanks!!
If you ask for a CSS3 solution you may use :not pseudoclass
input[type=text]:not([size]), textarea {
width: 98%;
border: 1px #999 solid;
}
Codepen Example: http://codepen.io/anon/pen/KFped/
if you want instead to support older browsers (like IE8) just apply a style for
input[type=text], textarea {
width: 98%;
border: 1px #999 solid;
}
and thus revert the style for
input[type=text][size] {
/* revert properties */
}
Essentially i have a pricing table with the class of .priceblock, and i have a border-bottom on my <li> tags, i simply want it to change color when i hover on the priceblock. The code to me seems correct but nothing changes.
Heres the initial li tag:
ul.pricingtable .priceblock .contents li {
font-family: 'OpenSans';
font-size: 13px;
width: 81.904762%;
height: 35px;
margin:0 auto;
padding: 10px 0;
border-bottom: 1px solid rgba(221,221,221,1);
}
And here hover state css code, this hover class works for he coloring of texts, but i can't change the border color.
.priceblock:hover .contents li {
border-color: rgba(255,117,109,1);
}
Any ideas?
I think you might need to change the hover state from.
.priceblock:hover .contents li {
border-color: rgba(255,117,109,1);
}
To:
.contents li:hover {
border-bottom: 1px solid rgba(255,117,109,1);
}
HTML may be able to read it better.
The css attributes need to be equals.
for example:
If in the first style block you write "ul.pricingtable" then you need to do that in the second block two.
And in the content of block, they need to be same.
for example:
border-bottom: 1px solid rgba(221,221,221,1);
and
border-bottom: 1px solid rgba(255,117,109,1);
You cann'ot use once with "border-bottom" and then with "border-color" only...