This question already has answers here:
How do I prevent CSS inheritance?
(13 answers)
Closed 6 years ago.
Below are the sample code block i use.
I have two set of css, and want to apply onto two UL component.
however, the result come out, the inner "UL" will hold some of the css which defined for its parent.
and even some of the css defined in "b" will be override by "a"... nightmare...
how can i stop the inheritance???
<ul class="moduleMenu-ul">
/* for loop begin */
<li class="moduleMenu-li">
<a></a>
</li>
/* for loop end */
<li class="moduleMenu-li">
<a>On Over the div below will be show</a>
<div id="extraModuleMenuOptions">
<ul class="flow-ul">
/*for loop begin*/
<li class="flow-li">
<a class="flow-a"></a>
</li>
/*for loop end*/
</ul>
</div>
</li>
</ul
CSS:
.moduleMenu-ul {
width: 100%;
height: 43px;
background: #FFF url("../images/module-menu-bg.gif") top left repeat-x;
font-weight: bold;
list-style-type: none;
margin: 0;
padding: 0;
}
.moduleMenu-ul .moduleMenu-li {
display: block;
float: left;
margin: 0 0 0 5px;
}
.moduleMenu-ul .moduleMenu-li a {
height: 43px;
color: #777;
text-decoration: none;
display: block;
float: left;
line-height: 200%;
padding: 8px 15px 0;
text-transform:capitalize;
}
.moduleMenu-ul .moduleMenu-li a:hover {
color: #333;
}
.moduleMenu-ul .moduleMenu-li a.current{
color: #FFF;
background: #FFF url("../images/module-menu-current-bg.gif") top left repeat-x;
padding: 5px 15px 0;
}
#extraModuleMenuOptions {
z-index:99999;
visibility:hidden;
position:absolute;
color:#FFFFFF;
background-color:#236FBD;
}
#extraModuleMenuOptions .flow-ul {
text-align:left;
}
#extraModuleMenuOptions .flow-ul .flow-li {
display:block;
}
#extraModuleMenuOptions .flow-ul .flow-li .flow-a {
color:#FFFFFF;
}
lets say you have this:
<ul>
<li></li>
<li>
<ul>
<li></li>
<li></li>
</ul>
</li>
<li></li>
<ul>
Now if you DONT need IE6 compatibility (reference at Quirksmode) you can have the following css
ul li { background:#fff; }
ul>li { background:#f0f; }
The > is a direct children operator, so in this case only the first level of lis will be purple.
Hope this helps
If the inner object is inheriting properties you don't want, you can always set them to what you do want (ie - the properties are cascading, and so you can overwrite them at the lower level).
e.g.
.li-a {
font-weight: bold;
color: red;
}
.li-b {
color: blue;
}
In this case, "li-b" will still be bold even though you don't want it to be. To make it not bold you can do:
.li-b {
font-weight: normal;
color: blue;
}
While this isn't currently available, this fascinating article discusses the use of the Shadow DOM, which is a technique used by browsers to limit how far cascading style sheets cascade, so to speak. He doesn't provide any APIs, as it seems that there are no current libraries able to provide access to this part of the DOM, but it's worth a look. There are links to mailing lists at the bottom of the article if this intrigues you.
Non-inherited elements must have default styles set.
If parent class set color:white and font-weight:bold style then no inherited child must set 'color:black' and font-weight: normal in their class. If style is not set, elements get their style from their parents.
The short story is that it's not possible to do what you want here. There's no CSS rule which is to "ignore some other rule". The only way around it is to write a more-specific CSS rule for the inner elements which reverts it to how it was before, which is a pain in the butt.
Take the example below:
<div class="red"> <!-- ignore the semantics, it's an example, yo! -->
<p class="blue">
Blue text blue text!
<span class="notBlue">this shouldn't be blue</span>
</p>
</div>
<div class="green">
<p class="blue">
Blue text!
<span class="notBlue">blah</span>
</p>
</div>
There's no way to make the .notBlue class revert to the parent styling. The best you can do is this:
.red, .red .notBlue {
color: red;
}
.green, .green .notBlue {
color: green;
}
Using the wildcard * selector in CSS to override inheritance for all attributes of an element (by setting these back to their initial state).
An example of its use:
li * {
display: initial;
}
Override the values present in the outer UL with values in inner UL.
you can load the new content in an iframe to avoid css inheritance.
Related
I've created a tab menu using Divs and CSS, and I'm wondering how I can make the selected tab change color? I assumed it would be by making .tab's background color different, but for some reason it only works for half of the tab... See here: http://www.sunporchhomes.com/features-3
Does anyone know why that is? See code below.
Features.html
<div class="tabcontent">
<div class="left_lane float">
<div class="tab blu" id="t1">INTERIORS</div><br>
<div class="tab" id="t2">BATHROOMS</div><br>
<div class="tab" id="t3">KITCHEN</div><br>
<div class="tab" id="t4">EXTRAS</div><br>
</div>
.CSS
.tabcontent {
position: absolute;
padding-top:35px;
padding-left:485px;
width: 600px; !important /*HOLD TABS AND CONTENT*/
z-index:205;
height:300px;
}
.left_lane {
border-right: 2px #ffffff solid;
padding-right: 0px;
width:100px;
font-family: Trebuchet MS;
color:#ffffff;
background-color:#6cc7df;
height:176px;
font-size:14px;
}
.right {
background-color:#0ba8cb;
width:410;
height:500;
}
.float {
float: left;
padding-left:0px;
padding-top:10px;
}
.tab {
height:20px;
padding-left:8px;
border-bottom: 2px #FFF solid;
}
.tab #active {
}
.cnt {
display: none;
font-family: Trebuchet MS;
font-size:14px;
color:#ffffff;
padding-left: 10px;
padding-right:10px;
}
#c1 {
display: block;
}
.clear {
clear: left;
}
.blu {
color: #ffffff;
background-color:#0ba8cb;
}
The height of the tabs divs are only 20px, which is what gets filled with that darker blue. The lighter blue appears to be coming from another element rendering behind that winds up showing when the front div changes color, probably b/c of padding or margin settings.
For example, the padding of the container element is affecting the first tab, as it's being pushed down by the padding-top of the parent div.
I suggest using Chrome devtools or FireFox/IE devtools to hover over your html elements and look at what styles are affecting them, and what the actual geometries of their boxes are. Makes debugging this stuff much easier.
You can play around with CSS's:
padding-top
margin-top
for the tab and left_lane classes.
I got some almost-satisfactory results, but I am not a fan of hard-coding pixel dimensions. So I'll leave it up to you.
Add an "active" class to the "li" as given bellow. Also add an simple css. The css code also added bellow.
HTML code
<ul>
<li>HOME</li>
<li class="active">FEATURES</li>
<li>FLOORPLAN </li>
<li>NEIGHBOURHOOD</li>
<li>WARRANTY</li>
<li>ROWHOME FAQ </li>
<li>ABOUT US</li>
CSS code,
ul li.active { bacground: #ff0000; }
I am trying to create a button for my link which has the name on the button
and allows the user to click on it and go to the link.
Also I'm not sure why but my link "click-able range" seems to be extended.
Here is the Code:
<body>
<div id="container">
<div id="link">My Favorite Website</div>
</div>
</body>
Here is the CSS:
#container {
width:960px;
margin-left: auto;
margin-right: auto;
padding: 30px 0px;
}
a {
padding: 7px 100px;
border-radius: 10px;
background-size: 80px 60px;
background-color: green;
text-decoration: none;
}
#link {
padding: 7px;
font-size: 15px;
font-weight: bold;
font-style: italic;
}
Thanks!
Your link is inline element so you need to make it block or inline-block to add your styles so:
CSS
a {
display:inline-block;
}
Having a block element within an inline one is causing your problems.
By default, anchors are displayed inline. You need to display it a little differently, as inline-block:
a {
padding: 7px 100px;
border-radius: 10px;
background-size: 80px 60px;
background-color: green;
text-decoration: none;
display:inline-block;
}
JSFiddle
Remove div tag into a tag..
Demo
<div id="container">
My Favorite Website
</div>
just add this to #link in css
appearance:button;
-moz-appearance:button;
-webkit-appearance:button;
is an inline element. To make it behave like a block level element, you need to define its display property in CSS.
a {display:block;} or a {display:inline-block;}
and your link "click-able range" seems to be extended, because you are using a , which is a block level element, inside your tag.
Block level elements take the entire width of its container.
You need to redefine its bevavior.
link{display:inline-block;} or #link{display:inline;}
I have an unordered list of elements, each list element has a frame, background, and an image within them. When you hover over the list element, the entire background changes to orange, and the text changes to white. But when I link the images within the li to other pages, everything works, except the text stopped changing to white when I hover. The background still changes to orange either way though, and the text color works perfectly as soon as I take out the <a href ""> parts.
HTML
<ol id="selectable">
<li class="ui-state-list">
<b>Title</b>
Text here should change to white when hovering over entire li element.
</br>
<h5 class="discProd">More text</h5>
<a href="www.youtube.com">
<img class="iconAlign" src="images/icons/videoIcon.png">
</a>
</li>
</ol>
CSS
h6 {
font-size: 12px;
color: #2D2D2D;
font-weight: 100;
line-height: 1.5em;
}
.discProd {
font-size: 10px;
color: red;
}
#selectable {
list-style-type: none;
width: 900px;
}
#selectable li:hover {
background: orange;
color: white;
}
.ui-state-list {
border: 1px solid #d3d3d3;
background: #e6e6e6 url(../images/Products/productBG5.png) 50% 50% repeat-x;
/*searched bg gloss wave */
font-weight: normal;
color: #555555;
}
.iconAlign {
float: right;
margin-left: 34px;
margin-right: 8px;
margin-top: -.2em;
}
With this in your CSS
#selectable li:hover span {
background: orange;
color: white;
}
and an added span for the changed text it should all work fine ;)
<ol id="selectable">
<li class="ui-state-list"><b>Title</b>
<span>Text here should change to white when hovering over entire li element. </span>
</br>
<h5 class="discProd">More text</h5>
<a href="www.youtube.com">
<img class="iconAlign" src="images/icons/videoIcon.png">
</a>
</li>
</ol>
First, the code you posted actually does exactly what you want it to do.
But there is a problem.
You're turning the <img> element into a block by floating it (default is inline for images). Putting a block element inside of an inline element (the <a>), is a recipe for disaster (aka rendering outside the bounds of the parent elements as you can see in this fiddle).
To avoid this, remove the float: right on .iconAlign and instead, you can make the <a> element a block and float it. You'll need to change the order of your HTML to have your <a><img></a> setup come at the beginning of your <li> (assuming you're planning to have the element show up on the top right corner as I'm presuming you are).
You also are using an ORDERED list with <ol>. Un-ordered lists are <ul> as the letters suggest. It's also <br /> (XHTML proper) for a line-break, not </br> (although either will result in the same thing in new browsers). You don't actually need that </br> at all since all heading elements (<h1>, <h2>...<h6>) default to block elements that have a line-break before and after to start with.
Here's a little demo
...of what I've covered. I use an icon from my own server to demonstrate the icon placement.
PS - same protocol goes for <li> - not a block element until display: block or float: left/right is applied. More information on your goal with this would be very helpful and I could point you in a more obvious direction (such as using a position: relative/absolute setup with offsets instead of float all together).
PPS - in my demo, Firefox shows my icon smaller than it does in Chrome, just so you are aware that they have different ways of rendering a .ico file.
HTML:
<ul id="selectable">
<li class="ui-state-list">
<a href="www.youtube.com">
<img class="iconAlign" src="http://1054.fleeceitout.com/sites/all/themes/jack/favicon.ico" />
</a>
<b>Title</b>
Text here should change to white when hovering over entire li element.
<h5 class="discProd">More text</h5>
</li>
</ul>
CSS:
h5 {
font-size: 12px;
color: #2D2D2D;
font-weight: 100;
line-height: 1.5em;
}
.discProd {
font-size: 10px;
color: red;
}
#selectable {
list-style-type: none;
width: 900px;
}
#selectable li:hover {
background: orange;
color: white;
}
.ui-state-list {
border: 1px solid #d3d3d3;
background: #e6e6e6 url(../images/Products/productBG5.png) 50% 50% repeat-x;
/*searched bg gloss wave */
font-weight: normal;
color: #555555;
}
a {
float: right;
margin-left: 34px;
margin-right: 8px;
margin-top: -.2em;
}
.iconAlign {
/* no positioning done here since it's an inline element. position using the block element it's inside of - the <a> */
}
What is so annoying about CSS when you add style in the css class, it may apply other element/class by itself.
What the best way to prevent that?
For example:
HTML:
<div class='main-content'>
<p> Hello World </p>
<span> Test One </span>
<div class='column'>
<span> Test Two</span>
</div>
</div>
CSS:
.main-content span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
.column span {
font-size:20px;
text-transform:none;
display:inline-block;
}
I do not want "Test Two" <span> to have a background color.
Test: http://jsfiddle.net/szm9c/1/
Use a selector that actually selects the elements you want. In this case >, the child selector, will suffice.
.main-content > span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
http://jsfiddle.net/mattball/mQFz2/
Use .main-content > span, that selects only directly descendent elements.
This has nothing to do with inheritance.
To use CSS properly, assign properties to elements using selectors that match only the elements that you wish to affect. (The example given is far too artificial for a useful analysis and for constructive suggestions on a better approach.)
You can use this
.main-content > span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
If you use like .main-content > span that style will only affect to the immediate child spans of .main-content class
Just use all: initial at your root element.
.selector
{
all: initial;
}
I have a div that is a link to another page. When someone hovers over the div(ie, link) I want the whole div's background color to go blue. I would like to do this all in CSS because javascript may not work with everyone.
My Problem: My code below attempts to do this, the link works fine BUT when I hover over the div the background doesn't change color. What do you think I am doing wrong & how do you think I can fix this to make the div change background color on hover?
I have a feeling that I should place the link(a element) inside the div(instead of outside) but I can never get the a to stretch to the full space of the div that way.
<html>
<head>
<style type="text/css">
<!--
body { background-color: RGB(218,238,248); }
#rentalAnnc { margin-top: 5%; border-color: #99CCFF; padding-left: 10px; padding-right: 10px;
border-width:thin; border-style:solid; border-right-width:thick;
border-bottom-width:thick; background-color: #FFFFFF; width: 300px; }
/* Using pure CSS I am trying to make the background color of the div renatalAnnc have a blue background when we hover over it*/
.sidebarLink { color: #000000; text-decoration: none; }
.sidebarLink a:hover { background-color: blue; }
/* The following on works in Firefox not IE! :( #rentalAnnc:hover { background-color: blue; } */
-->
</style>
</head>
<body>
<a class="sidebarLink" href="facilitiesForHire.html">
<div id="rentalAnnc">
<p>We have a range of Education Facilities available for lease & hire</p>
</div>
</a>
</body>
</html>
:hover support is not great for non-anchor elements in older browsers and IE, so you can attach the hover psuedo class to the <a> instead and use a simple descendant selector:
a:hover #rentalAnnc { background-color: blue; }
You should put the <a> inside the <div>. If you want it to stretch across the full space, add display: block to its style.
<div id="rentalAnnc">
<a class="sidebarLink" href="facilitiesForHire.html">
<p>We have a range of Education Facilities available for lease and hire</p>
</a>
</div>
a.sidebarLink { color: #000000; text-decoration: none; display: block; }
a.sidebarLink:hover { background-color: blue; }
Add <!DOCTYPE html> to top of your page to make it a HTML5 document and use the outcommented #rentalAnnc:hover { background-color: blue; } rule. Having a <div> inside <a> is invalid in HTML3/4, but apparently valid in HTML5 (disclaimer: HTML5 standard is still not definitive). After adding the proper doctype and the outcommented rule, your current problem (and many other (future?) layout-related issues) should be solved in MSIE.
Don't forget to fix the other http://validator.w3.org errors after adding the doctype, such as a missing title and so on. Browser behaviour is undetermined on invalid HTML.
A bit late I'm sure but I've been looking at this recently and I think the better solution is:
<style type="text/css">
body { background-color: RGB(218,238,248); }
#rentalAnnc { margin-top: 5%; border-color: #99CCFF; padding-left: 10px; padding-right: 10px;
border-width:thin; border-style:solid; border-right-width:thick;
border-bottom-width:thick; width: 300px; }
a.sidebarLink div { color: #000000; text-decoration: none; background-color: #FFFFFF;}
a.sidebarLink:hover div { background-color: blue; }
</style>
<a class="sidebarLink" href="facilitiesForHire.html">
<div id="rentalAnnc">
<p>We have a range of Education Facilities available for lease & hire</p>
</div>
</a>
Note: the rentalAnnc div does not have a background-color in it's style. This is in the link style only.
This way, the link covers the entire div exactly, not just a part of it. Also, any background-image applied to the div (eg with transparent areas for the background color to show through) will still be displayed!