I have a button on my webpage. I want to use CSS pseudo-classes to make changes to a different part of the document when the cursor hovers over the button. This is what I've tried:
<div id="content">
<p id="foo">blah blah blah</p>
<p id="blah">blah blah blah</p>
</div>
<div id="navigation-panel">
Hover over a button!
<div class="buttons">
<div id="button1">button1</div>
<div id="button2">button2</div>
<div id="bar">bar</div>
</div>
</div>
And the corresponding style sheet:
#bar:hover #foo {
color: green;
}
But of course this doesn't work, because in CSS you can only use a selector of the format A B to select a B descendant of element A. With the new CSS3 spec, it's possible to select a sibling element of A using some new pseudo-classes.
But is there a way to select one (or more) elements B that are more like distant cousins to A?
I was thinking of a couple solutions. One: use Javascript. This doesn't sound too appealing to me because I'm hoping to make my page entirely functional without JS, in case there is somebody browsing my page who has JS disabled in their browser. Also I'm hoping to keep things simple. The second solution: put the foo div inside the bar div and then use position: absolute to move foo where I want it to end up. This is a messy solution, for reasons that should be obvious.
Yeah, but not with CSS. Use JavaScript. Here's a jQuery example.
$('#bar').hover(
function(e) {
$foo = $('#foo');
$foo.data('prevColor', $foo.css('color'));
$foo.css('color', 'green');
},
function(e) {
$foo = $('#foo');
$foo.css('color', $foo.data('prevColor'));
}
);
You could put it inside the container and then absolutely position it outside of the container's DOM flow to give the same effect. It's not totally ideal but it's possible:
<div id="content">
<p id="blah">blah blah blah</p>
</div>
<div id="navigation-panel">
Hover over a button!
<div class="buttons">
<div id="button1">button1</div>
<div id="button2">button2</div>
<div class="foo" id="bar">bar<p id="foo">blah blah blah</p></div>
</div>
</div>
#content {
height:80px;
}
#foo {
position:absolute;
top:40px;
}
#bar:hover #foo {
color: green;
}
http://jsfiddle.net/Uyypc/
THE “PARENT” SELECTOR
There is currently no way to select the parent of an element in CSS.
If there was a way to do it, it would be in the CSS selectors specs, either CSS 2 or 3
CSS 3 Selectors Spec &
CSS 2 Selectors Spec
In the meantime you'll have to resort to JavaScript if you need to select a parent element.
The CSS Selectors 4 Spec provides a syntax for defining the "subject" of a selector by using a ! sign. As of 2012, this is not available in any browser.
Using CSS4 selectors, the original question could be solved with this:
li! > a.active { /* styles to apply to the li tag */ }
MooTools has supported CSS level 4 selectors for a couple of years now - and ironically, the Slick selector engine in MooTools actually was able to process these selectors before the spec draft was even published -->How do I specify a different subject for the selector?
Related
I know it might sound awkward and kind of rookie, but still.
So the problem is a have a site on CMS with no acces to html source code (i know you could that, by searching for that specific .php file but it is a bit too much), and have to change some of it's elements style.
This is how that div i must modify looks like:
<div class="tg-feature-product-widget element-item uxdesign tg-column-3 portfolio "
data-category="portfolio " style="position: relative; left: 359.95px; top: px;left: 500px;"><figure><img>
blah blah blah
There is that annoying style i can't acces within F12 moreover the class can't be accessed with
.tg-feature-product-widget element-item uxdesign tg-column-3 portfolio{
in CSS (i do not exclude that i might not know how to access such complicated stuff).
So how do i change it's style?
You can access it like so: .tg-feature-product-widget.element-item.uxdesign.tg-column-3.portfolio
Thats how the CSS selectors work
<div class="element top"> //first div
</div>
<div class="element"> //second div
<div class="top"> //third div
</div>
</div>
.element.top{
//Selects the first div aka every div with the class element AND top
}
.element{
//Selects the first and second div aka every div with the class element
}
.element .top{
//Selects the third div nested in the second div aka every div with the
class top thats nested in a div with the class element
}
This is a simple question. However, I couldn't find an answer after 10 minutes search. I would like to explain my question with examples, so you can understand what I am exactly talking about.
Let's say there is a div tag with an id and it has also some text inside:
<div id="text">Hello World</div>
and I also have css rule which will turn the text into red.
.makeRed{
color: #FF0000;
}
The question is I want to make the text red in my div tag. I can simply do it like this:
<div id="text" class="makeRed">Hello World</div>
Instead of doing it, is there another way to make that text turn to red? Because if I keep adding makeRed rule to my every div that I need, it will turn my html into garbage. So I wonder if there is any way to do it clearly. I would like to use that way for "clearfix" method for some of my divs.
Whenever I need clearfix, I do like this and this is bad:
<div class="clearfix">
<div id="text">Hello World</div>
</div>
The question is: which text do you want to make red, and why?
If you want the text of all your divs red, you can just write
div{ color: red; }
If it's just for, say, an error message, I would add the class 'error' rather than 'red'. That way, you can make the HTML more semantic. You still have to add a class, but it has more meaning:
.message.error { color: red; }
You can add the ID of your div to your css like so:
.makeRed, #text{
color: #FF0000;
}
You can separate targets by commas to include multiple different elements in the style. This will maintain the styles applied to .makeRed and apply to your #text div.
I have css
div{margin-left:15px;}
div:hover{color:red;}
and I have multiple divs
<div>
<div>123</div>
<div>456</div>
<div>
<div>789</div>
<div>
<div>10 11 12</div>
</div>
</div>
</div>
I need that only one div became hilighted when it's under mouse pointer.
Now, when mouse is over any of these, they all become red.
Is it possible with pure CSS code?
I'm not sure why you have so many divs, if you want them indented like a list you should use ul and li but to solve your issue you could wrap all of them in a container and target the div hover within that container:
.all div:hover{color:red;}
JSFIDDLE
EDIT
Just add class names to them and target the class then
JS
Given the details the OP provided in the comments of the question I would suggest following solution:
I would add a span wrapper around every text. So the abstract function that generates the HTML would become this:
function(){
$a.="<div><span>$inc</span>"; // span tag added
if(1){
$a.=function();
}
$a.='</div>';
return $a;
}
Now the HTML output should look somewhat like this:
<div>
<div><span>123</span></div>
<div><span>456</span></div>
<div>
<div><span>789</span></div>
<div>
<div><span>10 11 12</span></div>
</div>
</div>
</div>
Which gives the possibility to style it with this CSS:
div span:hover {
color:red;
}
This sound silly, but I want to apply a CSS to a content inside and element tag.
Example:
<div class="wrap">
Tag 1
,
Tag 2
,
Tag 3
</div>
Is there any possibility that I'll be able to remove or hide the comma , every between the tag using CSS? I have no idea how to tweak a generated output for tags that contains comma.. so I was thinking if this would be possible using CSS?
This might work, allthough css might not be the best way. How did they get there in the first place... Css is for style (hence StyleSheet), not for content.
.wrap{
visibility:collapse;
}
.wrap a{
visibility:visible;
}
And a jsFiddle demo
CSS3 selectors are fun, but can be difficult to understand what is happening, and the support for older browsers is minimal.
Hide text node in element, but not children
You can use this css for hiding your comma.
.wrap{
visibility:collapse;
}
.wrap a{
visibility:visible
}
I have the following HTML markup:
<h1>
<div class="sponsor">
<span>Hello</span>
</div>
World
</h1>
When I use the CSS selector h1 I get Hello World.
I can't unfortunately change the markup and I have to use only CSS selectors because I work with the system that aggregates RSS feeds.
Is there any CSS selector which I can take only the text node? Specifically the World in this example?
The current state of CSS can't do this, check this link: W3C
The problem here is that the content you write to the screen doesn't show up in the DOM :P.
Also ::outside doesn't seem to work yet (at least for me in Safari 6.0.3) or it simply doesn't generate the desired result yet.
Check my fiddle and then check the DOM source: JSfiddle
Finally there are attribute selectors a { content: attr(href);}, making CSS able to read DOM-node attributes. There doesn't seem to be a innerHTML equivalent of this yet. It would be great tho if that was possible, whereas you might be able to manipulate the inner markup of a tag.
Bit of a workaround:
h1 {
color: red;
}
h1 * {
color: lime;
}
<h1>
<div class="sponsor">
<span>Hello</span>
</div>
World
</h1>
This is almost the opposite of a question I asked last week: Is it possible to select the very first element within a container that's otherwise pure text without using classes or identifiers in pure CSS?
The short answer is no. "World" in this example isn't an element of its own - therefore there isn't a way to select it.
What you would have to do here is style the h1 then override that styling with div.sponsor. For instance, if you wanted "World" here to have a black background with white text you woud use something similar to:
h1 {
background:black;
color:white;
}
h1 div.sponsor {
background:white;
color:black;
}
Unfortunately, however, this wouldn't work if you were only wanting the word "World" styled and your markup had more than just that within <div>Hello</div> World Foo, for instance.
I don't believe it would be possible with pure CSS to style just "World" in this situation.
I also met same problem, where I can't touch the markup and have no control with js.
I needed to hide a text nodes in a div element, but the element to remain visible.
So here is my solution:
markup:
<div id="settings_signout_and_help">
<a id="ctl00_btnHelpDocs" class="ico icoHelp" href="http://" Help Guide</a>
Signed in as: <a id="ctl00_lUsr" href="Profile.aspx">some</a>
Home
Sign out
</div>
css:
#settings_signout_and_help {
font-size: 1px !important;
}
#settings_signout_and_help a {
font-size: 13px !important;
}
Hope this helps guys!
I had a similar problem where I had to remove the "World" text from html generated by a C# function.
I set the font-size to 0 on the 'h1' element and then applied my css to div class. Basically hiding the extra text, but keeping content in the div.
I don't know how to do it with just CSS, but...
Using JQuery, you could select all the elements inside except the stuff inside its child element
$("h1:not(h1 > div)").css()
and put whatever CSS effect you want inside there.