April 6, 201511 yr Hello, I have this code: http://jsfiddle.net/e7qnhxce I have a problem: when I mouse over the div button,I whant that the background-color of #main change color. Sorry for my bad english. Regards, Tayken.
April 6, 201511 yr No you can not target parent elements with CSS. But you can change color when the user hovers over the parent element itself. #main { background-color: red; width: 200px; height: 300px; margin: 0 auto; padding: 10px; text-align: center; } #main:hover{ background-color: blue; }
April 7, 201511 yr As the div elements are nested the parent div is still being hovered even when the cursor is over the child div. This means that the parent div background colour can be changed purely through CSS. Use the following CSS body { padding: 10px; } #main { background-color: red; width: 200px; height: 300px; margin: 0 auto; padding: 10px; text-align: center; } #main:hover { background-color:blue; } .button { background-color:red; padding: 5px; border: 1px solid #000; color: #fff; margin: auto; cursor: pointer; } http://jsfiddle.net/Lmgsjc1f/ Another way of doing it but a little less practical would be to use just the button div and apply a 10px border to the top and sides and a 250px border to the bottom and change the border colour on hover body { padding: 10px; } .button { background-color:light-grey; width:200px; height:40px; border-color: red; border-radius: 3px; border-style: solid; border-width: 10px 10px 250px; color: black; margin: 0 auto; padding: 10px; text-align: center; cursor: pointer; line-height:40px; } .button:hover { background-color:black; border-color:blue; color:white; } <div class="button">Change Color</div> http://jsfiddle.net/cowuuhqt/
April 8, 201511 yr ^ I suppose the parent div is there for a reason Unfortunately (as I mentioned before) there is no parent selector in CSS I've reduced the amount of JavaScript from my first fiddle by using a parentNode selector and anonymous functions. So it might be easier to use. http://jsfiddle.net/Nillervision/e7qnhxce/5/ You will still need to add an ID for the button (fastest way to select elements in js) paste the js in script tags just before your closing body tag <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Page Title</title> <link rel="stylesheet" href="yourstylesheet.css" media="all" /> </head> <body> <div id="main"> <div id="button" class="button" role="button">Change Color</div> </div> <script> var button = document.getElementById('button'); button.addEventListener('mouseover', function(){ this.parentNode.style.backgroundColor= 'blue'; }, false); button.addEventListener('mouseout', function(){ this.parentNode.style.backgroundColor= 'red'; }, false); </script> </body> </html> Edited April 8, 201511 yr by Nillervision
April 8, 201511 yr An alternative is to use toggleClass() to switch the background color using jQuery: (function($) { var main = $('#main'), button = main.find('.button'); button.hover(function() { main.toggleClass("main--hover"); }); })(jQuery); http://jsfiddle.net/e7qnhxce/6/
April 8, 201511 yr I remember reading an article somewhere where apparently a CSS parent selector had been considered in the original spec for CSS3 but it was dropped on performance concerns. It would be useful though and any performance hit may be offset in the reduction of scripting.
April 8, 201511 yr I remember reading an article somewhere where apparently a CSS parent selector had been considered in the original spec for CSS3 but it was dropped on performance concerns. It would be useful though and any performance hit may be offset in the reduction of scripting. You can do it in Sass. .foo { .bar & { // styles here only applied if .foo has a parent or ancestor with the class of .bar } } Compiles to .bar .foo { } Edited April 8, 201511 yr by rbrtsmith
April 8, 201511 yr Quite, however that is a fixed relationship between the elements having those class names. The parent selector would apply less tangibly or definitely. The problem then is that while it might be good on static pages, pages that contain dynamic content might be completely re-rendered as the rendering engine would need to check back up the cascade to see if any dependencies have changed. I can't find the artivcle I read but there is one here from Remy Sharp https://remysharp.com/2010/10/11/css-parent-selector Good post from Tab Atkins in the comments as well.
April 8, 201511 yr An alternative is to use toggleClass() to switch the background color using jQuery: (function($) { var main = $('#main'), button = main.find('.button'); button.hover(function() { main.toggleClass("main--hover"); }); })(jQuery); http://jsfiddle.net/e7qnhxce/6/ Actually I believe Lindseys alternative performs a bit better than mine. Switching between predefined class names is better than hardcoding style properties in js. For educational reasons I've added a native JS version of Lyndseys solution: http://jsfiddle.net/Nillervision/e7qnhxce/9/ var button = document.getElementById('button'); button.addEventListener('mouseover', function(){ this.parentNode.className = 'mainOver'; }, false); button.addEventListener('mouseout', function(){ this.parentNode.className = 'main'; }, false); Edited April 8, 201511 yr by Nillervision
April 8, 201511 yr Quite, however that is a fixed relationship between the elements having those class names. The parent selector would apply less tangibly or definitely. The problem then is that while it might be good on static pages, pages that contain dynamic content might be completely re-rendered as the rendering engine would need to check back up the cascade to see if any dependencies have changed. I can't find the artivcle I read but there is one here from Remy Sharp https://remysharp.com/2010/10/11/css-parent-selector Good post from Tab Atkins in the comments as well. I'm pretty sure that when you inject HTML that any predefined classnames & associated styles in the CSS will take effect, even HTML that is injected after the page has rendered. Can just write the templates to match the CSS, any dynamic content should just be JSON text injected into the template. Unless there is something that I'm not quite understanding..? Edited April 8, 201511 yr by rbrtsmith
April 8, 201511 yr function hoverFunc(){ var $mainElement = $('#main'); if ($mainElement.hasClass('hovered')){ $mainElement.removeClass('hovered'); }else { $mainElement.addClass('hovered'); } } $('.button').on('hover', hoverFunc); if you want it to change colour by click then you just change the event within the .on() part. Doing it this way you can do other cool things as well as just having the background change colour because your css is controlling the animations by using transitions. Edited April 8, 201511 yr by Matthew Claffey
April 8, 201511 yr The point is though that the elements wouldn't need class names. In which case unwary usage could cause serious performance issues. It might prove a nightmare for mobile users if pages kept having to completely re-render. Sometimes designers have to be protected from themselves. On another forum I frequent which is mainly amateur and semi-pro it is surprising the number there who use incredibly inefficient techniques with a massive overhead in resources but think it will be fine because it loads instantly for them in local testing or because they already have it cached in their browser. Never occurs for a moment that some poor sod is going to be looking at a blank screen watching the wait cursor spinning and spinning and spinning and......
April 8, 201511 yr If you just want to change the background colour when the button is hovered, you can do that in CSS, but you would have to amend what you have slightly. This is what I've come up with. It's super hacky though (mainly because of the margin), so I'm not sure I'd recommend actually using it, but you get the idea.
Create an account or sign in to comment