I love the "after" (+) selector, it's good to style things after specific elements.
Such as:
.title+div {
background-color: yellow;
}
would make the background color of the div following the element with .title class, yellow.
However, I need a more dynamic way of selecting things.
How would you express this in css?:
"Make the background color of the visibility:visible element, yellow"
Any direction is appreciated.
You can't select non-inline properties in CSS, so something like div[visibility:visible] wouldn't work unless you explicitly stated the property inside the style attribute for the element.
The best way to select an element like this is to select the class with the styles applied to it (if there is one).
.title + .visible {
background-color:yellow;
}
If this isn't the case, you should almost always use JavaScript.
Well as you know there isn't only one type of selector in CSS. In the link below you can find more complex selector such as Attribute Selector:
div[target]{background-color:yellow}
.firstgroup
{
height:50px;
width:50px;
background-color:lightblue;
margin:10px;
}
.secondgroup
{
height:50px;
width:50px;
background-color:lightpink;
margin:10px;
}
<div class="firstgroup" target="a"></div>
<div class="firstgroup"></div>
<div class="secondgroup" target="ab"></div>
<div class="firstgroup"></div>
<div class="secondgroup"></div>
Refrence :Complex Selectors
This is potentially possible, albeit it's very, very fragile and has some requirements.
First, the visibility:visible must appear in the element's inline style attribute, like so:
<span style="visibility:visible">element text</span>
Second you must know the exact string, if a space is added you'll need to either change selector or account for both/all possibilities.
So, with those provisos, you can implement this selection as follows:
[style*="visibility:visible"] {
background-color: yellow;
}
[style*="visibility: visible"] {
border: 1px solid #000;
}
<span style="visibility:visible">element text</span>
<span style="visibility: visible">element text</span>
Now, given those requirements, it's usually far easier to just use JavaScript (or one of its libraries, if you must):
Array.prototype.forEach.call(document.querySelectorAll('span'), function (el, index) {
if (el.offsetHeight) {
el.style.backgroundColor = 'yellow';
el.textContent = el.textContent + ' (' + index + ')';
}
})
span {
visibility: visible;
}
.jsAddedClass {
background-color: yellow;
}
#exception {
display: none;
}
<span>element text</span>
<span>element text</span>
<span id="exception"></span>
References:
CSS:
Substring matching ([attribute*="value"]) attribute selectors.
JavaScript:
Array.prototype.forEach().
HTMLElement.offsetHeight.
Related
I need to remove strings that do not have an html tag.
For example :
<div class="A">
keep this and i want to remove this
</div>
Can I do this using only css ?
Maybe you can use font-size ::
.A {
font-size: 0;
}
.A a {
font-size: 20px;
}
<div class="A">
keep this and i want to remove this
</div>
You could use visibility:
.A {
visibility: hidden;
}
.A a {
visibility: visible;
}
<div class="A">
keep this and i want to remove this
</div>
NOTE - of course this DOES NOT remove the string / element in question from the DOM itself, it merely hides it but achieves the same purpose.
Set the style inside of class "A" to be blank by default. Set up a secondary class to handle ".A a". This will allow you to have two different styles. One for anchored, one for not.
.A { color: rgba(0, 0, 0, 0.5); } //Set this to transparent
.A a { color: #000 }
Something like that.
You can also use display: none with the :not() pseudo-selector
.A :not(a) {
display: none;
}
EDIT: This does not work
Neither does this:
.A {
display:none
}
.A a {
display: inline!important;
}
You can not do this with pure css. If you cannot change the markup, then you will need to use JS to grab the content you want to keep and remove the rest.
If you have any control over the markup you should really consider using different markup. You could have an alternate element that is initially hidden.
<div class="A">
keep this and i want to remove this
</div>
<div class="A hidden">
keep this
</div>
you could also enclose the other content you want to remove in a span tag and give it a class that you can reference later.
<div class="A">
keep this <span class="bad-stuff">and i want to remove this</span>
</div>
Is there any way to change an element's css while focusing or hovering on one of it's children?
Ex: while I move my mouse on A, B's background color changes.
if B is a descendant A, it is possible.
--A
-----B
using #A:hover #B { background-color: blue }
DEMO
in sibling:
---A
---B
It is : #A:hover ~ #B { background-color: blue; }
DEMO
assume B is a descendant of A.
what if I want to change #A background, while I am hovering on B. how could it be?
--A
-----B
Doing this in pure CSS is unfortunately not possible...at this time. However, there is supposedly an upcoming parent selector that would work well in this scenario. Click here for more info.
Work-around
In the meantime, you can also use javascript and/or jquery to accomplish this pretty easily.
DEMO
HTML
<div id="div1">
div 1 area
<p id="paragraph">
This is paragraph
</p>
</div>
CSS
#div1 {
border:1px solid lightgray;
}
p {
margin: 50px;
background-color: lightblue;
padding: 8px;
}
.altbg {
background:grey;
}
jQuery
$(function(){
$('#paragraph').mouseenter(function(){
$(this).parent().addClass('altbg')
}).mouseout(function(){
$(this).parent().removeClass('altbg')
});
});
It is actually possible in css.
Somebody made this fiddle:
http://jsfiddle.net/u7tYE/
#a:hover + #b {
display:block;
background-color:rgb(200,200,150);
}
#b{display:none;background-color:rgb(200,200,150;}
<a id="a">Menu</a>
<div id="b">
<p>Home</p>
<p>About</p>
<p>Login</p>
</div>
It works perfectly.
i have this css:
.test1 {
font-size:10pt;
}
.test2 {
font-size:12pt;
}
and i have this html :
<div class='test1'>name</div>
<div class='test2'>desc_name</div>
<div class='test1'>family</div>
<div class='test2'>desc_family</div>
<div class='test1'>password</div>
<div class='test2'>desc_pass</div>
what i have to do is to select the class test1 but only the tags that contains "family" inside the tag and give a background
i need a solution with css only, i dont need to change inside the html
is it possible ?
Thank You
Impossible with pure CSS. You cannot select on elements content with CSS(3 and higher).
The only you can do is specifying some attribute like:
<div class='test1' data-value="family">family</div>
And then select it with CSS:
div.test1[data-value='family'] { /* ... */ }
If you are using anything below CSS3 -
div.test1:contains("family") {
background: *;
}
The :contains() pseudo-class was deprecated in CSS3 - ergo i wouldn't recommend it, nor am I entirely positive any browser completely supports it.. This answer is entirely subjective.
Edit: apparently the :contains() psuedo class works in jQuery (tested in v 1.9).
You can use jQuery to change your styles. (this solution calls for a CSS only solution, but if this is available, use it!)
$("div.test1:contains('family')").css({ background: "whatever" });
you can use multiple class something like this,
.test1 {
font-size:10pt;
}
.test2 {
font-size:12pt;
}
.family {
/* style */
}
and HTML code,
<div class='test1 family'>family</div>
<div class='test2'>desc_family</div>
<div class='test1 family'>family</div>
<div class='test2'>desc_family</div>
I would suggest this one in orther to prevent override
.test1 {
font-size:10pt;
}
.test2 {
font-size:12pt;
}
.test1.family {
/* style */
}
Suppose I have this HTML:
<div class="SomeClass">test</div>
<div class="SomeClass" id="SomeID">test</div>
<div class="SomeClass">test</div>
with this CSS
.SomeClass{color:blue;}
.SomeClass:hover{color:red}
I want the hover effect not to apply to the SomeID div. I can do this with jQuery but I was wondering if there's an easier way to do it with just CSS.
Thanks for your suggestions.
CSS is parsed in order, meaning that if after you define
.SomeClass:hover { color: red; }
You then define a rule
#SomeId.SomeClass:hover { color: blue; }
That should 'overwrite' the initial color: red;
Just assign another rule to the div with an id of SomeID. This will override the other rule.
.SomeClass{color:blue;}
.SomeClass:hover{color:red}
#SomeID:hover{color:blue}
jsFiddle example
Just overwrite the style:
#SomeID:hover {
color:blue;
}
Alternatively, you could use:
.SomeClass:not(#SomeID):hover {
color:red;
}
Then it is easier to change it, but less browser support.
Let's take a look at link pseudo-class specificity:
Remember: LAHV (:link, :active, :hover, :visited).
First, in order to cascade properly, let's assign the following to .SomeClass:
.SomeClass:link, .SomeClass:active, .SomeClass:visited { color: blue; }
.SomeClass:hover { color: red; }
Next, let's specify #SomeID:
#SomeID:hover { color: blue; }
id always takes precedence over class.
I have looked at several other questions but I can't seem to figure any of them out, so here is my problem: I would like to have a div or a span, when you hover over it an area would appear and would be like a drop down.
Such as I have an div, and I want to hover over it and have it show some info about the item I hovered over
<html>
<head>
<title>Question1</title>
<styles type="css/text">
#cheetah {
background-color: red;
color: yellow;
text-align: center;
}
a {
color: blue;
}
#hidden {
background-color: black;
}
a:hover > #hidden {
background-color: orange;
color: orange;
}
</styles>
</head>
<body>
<div id="cheetah">
<p>Cheetah</p>
</div>
<div id="hidden">
<p>A cheetah is a land mammal that can run up 2 60mph!!!</p>
</div>
</body>
</html>
But this ^ doesn't seem to work, I don't know why... and if there is a way to do that in CSS, I would like to know, but I want any and all suggestions.
You can achieve this in CSS only if the hidden div is a child of the element you use for hovering:
http://jsfiddle.net/LgKkU/
You cannot affect a non-child element using :hover from within CSS2, which is supported by all common browsers.
You can affect a sibling element using CSS2.1 selectors, like so:
a:hover + .sibling { ... }
However, this only works for direct siblings. This means you could have HTML like this:
<p>Cheetah <span class="sibling">Blah Blah Blah</span></p>
Notice that the a and the span are direct siblings.
Here's a fiddle showing the siblings working: http://jsfiddle.net/vUUxp/
However, not all browsers support the CSS2.1 sibling selectors, so you need to decide based on your target audience if you can use this or not.
Edit: Corrected my mistake on the CSS version for the + selector: it's 2.1 that defines it, not CSS3. I also added a link showing browser support. Otherwise, the answer is the same.
Or, if you're open to it, use jQuery.
Something like this would work:
$("#element") // select your element (supports CSS selectors)
.hover(function(){ // trigger the mouseover event
$("#otherElement") // select the element to show (can be anywhere)
.show(); // show the element
}, function(){ // trigger the mouseout event
$("#otherElement") // select the same element
.hide(); // hide it
});
And remember to wrap this in a DOM ready function ($(function(){...}); or $(document).ready(function(){...});).
You can absolutely do this in CSS3 now using the ~ adjacent sibling selector.
triggerSelector:hover ~ targetSelector {
display: block;
}
For example, if you want a tooltip to appear when hovering over an adjacent button:
.button:hover ~ .tooltip {
display: block;
}