so I've been having a huge problem with trying to insert an image slider to my website at the moment, primarily that the nav bar CSS code is conflicting with the code for the slider. I imagine that there is a way to separate different CSS codes but I haven't found it yet.
In keeping the code as simple as possible to break down, here is the CSS I'm using.
<style type="text/css">
* {margin:0;padding:0;}
div {position:relative;height:3.2em;background:#FFFF99;border-top:2px
solid;border-bottom:2px solid;margin:0 0 30px;}
#nav {position:absolute;left:50%;top:0;margin-left:-380px;
height:3.2em;width:760px;list-style:none;}
#nav li {float:left;}
#nav a {display:block;text-align:center;color:#000;height:3.2em;
width:120px;line-height:3.2em;text-decoration:none; margin-left:
-2px;font-weight:bold;border-left:2px solid #000;border-right:2px solid #000;}
#nav a:hover,
#nav a:focus {background:#5E9BD9;color:#fff;}
#nav .active {background-color: #5E9BD9;}
</style>
And here is the HTML
<div>
<ul id="nav">
<li>Home</li>
<li>About us</li>
<li>Services</li>
<li>Contact us</li>
<li>Booking</li>
<li>Register</li>
</ul>
</div>
Please can I have help with splitting large groups of CSS code (I can do with singular lines but not for a large block like I have here)
Any help is appreciated!
Make sure that you give you slider div a unique id e.g
<div id="sliderwrapper">
then code your css in the following way
#sliderwrapper {
background-color: #ddd;
... etc etc
}
#sliderwrapper > div {
//this applies to divs that are DIRECTLY below the slider object
color: brown;
... etc
}
#sliderwrapper div {
//Any div at any level below the slider, not just directly below
}
#sliderwrapper .classnameyoucreated {
//classes that exist below the slider at any level
}
you can also specify custom attributes if your using html5. e.g.
<div id="sliderwrapper"><a class="classnameyoucreated" data-anythingyouwanttowriteherewillwork="value"></div>
#sliderwrapper > a.classnameyoucreated[data-anythingyouwanttowriteherewillwork=value] {
background-color: red;
... etc etc
//This is ultra specific and almost impossible to inadvertently override in another css block - specificity is the key here.
}
Because you've specified #nav in your CSS as long as #sliderwrapper is not a child of that tag you shouldn't have a conflict. If you do have a conflict there are some rules to remember.
The last specifier read by the browser is the priority
The most specifically defined specifier will trump a more ambiguous specifier
you can always override any value by adding !important to the end - but this is lazy and is bad practice when its over used.
You can also consider using .less files to enhance the syntax a bit and organize your code.
Good luck
Sounds like its an issue with specificity, hard to say though without seeing the code for the slider. As long as the nav bar and slider have no classes that overlap, there shouldn't be any issues.
Perhaps adding class="slider-[sometext]" to all slider elements and styling from there would solve your 'conflict' issues?
you have to use different ID-s.
Related
I'm trying to place a link in Wordpress quickly and we have a pretty complex style being applied to all a href links in the section. Here's a small sample of the selector and the styles within (there's about 40 lines of styles which I held back)
div.content-rotator li.featured-content a {
margin: 0px;
border: 1px solid rgb(34,56,19);
}
Is there anyway I can place a link in this li and override the parent style? It has to appear within the li with class featured-content.
I don't want to touch the existing CSS at this stage so I'd prefer to implement inline styles on the a element.
Thanks
EDIT: Just in case it wasn't clear, the CSS above is coming from the style sheet and I'd like to zero it out.
There's > 50 lines of styles in this though, I've only shown two for brevity so inline replacing them all isn't really an option.
Just use inline styles or/and add !important to overriden CSS definition, like:
<div class="content-rotator">
<ul>
<li class="featured-content">
...
</li>
</ul>
</div>
or
div.content-rotator li.featured-content a.other {
margin: 3px !important;
border: none !important;
}
Give the selected link an ID and just add !important to the styles. I don't think there is a better alternative unless you plan to go through the entire stylesheet.
I have a vertical menu like the one you can see HERE. The thing is - I want to have something like header. As you can seen, now my structure is this:
<div id="wrap">
<ul>
<li class="first">Link</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
I could add another <div class="menu-header"> on top of the <ul> but I think that the it would be much easier and appropriate to just change the firs <li> item to looks like a header. However if I just add a class like I'm doing in the example the final output is the same (the styles are not overriden. I can use !important but this is kind of last resort.
Even though it's not a ton of CSS I would like to change only some things and other (like width for example) to be left as they are for all other elements. So how can I do this? Is there more CSS-like approach than just adding !important to each style I want to override?
There are two alternate ways to achieve this.
Alternate 1.
Use the first-child of the class first of the li which is a child of ul
ul li.first:first-child
For Instance,
ul li.first:first-child{
/* Your CSS Values. */
}
Alternate 2.
Use first-child of li which is nested inside its main parent #wrap (which is a unique identifier). Use this alternate only if you do not want to use the class first on li
#wrap ul li:first-child
For Instance,
#wrap ul li:first-child{
/* Your CSS Values. */
}
Hope this helps.
I'd give the CSS selector first-of-type a try.
li:first-of-type {
...
}
http://www.w3schools.com/cssref/sel_first-of-type.asp
Your problem with the example given is called specificity. Take a look here for a working example.
Without using of classes, to style the first-element of a list I suggest you to use the pseudo-class first-child.
#wrap ul li:first-child {
background: none;
color: #000;
}
The navigation I'm referring to looks something like this:
home | about | contact
So what's the best and most flexible HTML/CSS to use for this type of navigation? The best thing I can come up with is to wrap the delimiters in a span so that I can control the spacing around them. For example:
home<span>|</span>about
Is that the best approach?
This all comes down to your target browsers, and if validating as strict HTML4.01 is important to you (ie: a boss/committee thinks it's a "big deal") or not.
Personally, for purposes of nav-menus, I go the route of wrapping everything in an unordered list.
If 4.01-compliance is important, I'll wrap that in a div.nav
If html5 is cool (which it is, with an oldIE JS-shim, as long as there are no committees involved), I'll wrap everything in a <nav id="main-nav"> or similar.
<ul><li>home</li><li>about</li></ul>
Then in CSS:
#main-nav li { display : inline-block; list-style : none; }
From there, you can set your padding on each <li> element to whatever you want.
You can use the :after pseudo-selector to inject "|" or any custom image you want, after each one (and you can use the :last-child:after to make sure that there's no image after the last one, if that's what you want).
You can even play around with the a, turning it into a block-element, and playing with padding to make the entire li block clickable, and not just the text.
See the oldIE-compatibility hack here: how to make clickable links bigger, if necessary.
You could simply add a left border to every element, except the first one:
HTML:
<ul id="nav-list">
<li>Home</li>
<li>Blog</li>
<li>Link</li>
</ul>
With the CSS:
#nav-list li {
display: inline-block;
border-left: 1px solid black;
padding: 4px;
}
#nav-list li:first-child {
border-left: 0;
}
See the above code in action on jsfiddle!
This is rather cross-browser compatible (IE7+) but it can be easily polyfilled with something like Selectivizr for IE6. Thanks to Rob W for suggesting to use border-left and first-child to reach more browsers!
I'm fighting with CSS and can't figure out how to remove bullets. Yeah, I know this sounds easy, but hear me out. I have another external CSS file from our corporate office that has styles that are getting in the way and I can't for the life of me figure out how to override them. I've tried the !important token and it doesn't work either. I'm using chrome and the inspector hasn't yet helped me figure out what's causing it. Anyway, here's my code which works great stand-alone, but once I put the corporate CSS file in there, the stupid bullets come back. Ugh!
<ul style="list-style-type:none;">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
This sounds like more of an issue with CSS specificity. You can't "override" the other styles, per se, you can merely create additional styles which are more specific. Without knowing what the other CSS looks like, there are generally three ways to do this:
Inline styles
Exactly like you have in your example. These are most specific, so they're guaranteed to work, but they're also guaranteed to be a pain in the neck to work with. Generally, if you're using these, something needs to be fixed.
Add an id attribute to the unordered list,
Then use the id as a selector in your CSS. Using an id as a selector is more specific than using a class or an element type. It's a useful tool for cutting through a bunch of styling that you might be inheriting from somewhere else.
<ul id="the-one">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
ul#the-one {
list-style-type: none;
}
Wrap all of your HTML in a div with the id attribute set.
This is what I usually do. It allows me to use that div with it's id in my CSS styles to make sure my styles always take precedence. Plus, it means I only have to choose one meaningful id name, then I can just style the rest of my HTML as I normally would. Here's an example:
<div id="wrapper">
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
<p>Some text goes here</p>
</div>
div#wrapper ul {
list-style-type: none;
}
div#wrapper p {
text-align: center;
}
Using that technique is a pretty good way to make sure that you spend most of your time working on your own styles and not trying to debug somebody else's. Of course, you have to put div#wrapper at the beginning of each of your styles, but that's what SASS is for.
I had the same problem, I was trying to change the CSS for a joomla website, and finally found that the li had a background image that was a bullet... (the template was JAT3). This is the code:
.column ul li {
background: url(../images/bullet.gif) no-repeat 20px 7px;
...
}
Hope it helps someone.
Ensure the rule you're trying to override is on the UL, rather than the LI. I've seen that rule applied to LIs, and overriding the UL as you have above would have no effect.
My situation is similar to the one described by #fankoil: my inherited css had
main-divname ul li{
background-image:url('some-image.png');
}
to get rid of this for a specific ul, I gave the ul an id
<ul id="foo">
...
and in the css, turned off background image for this particular ul
ul#foo li {
background-image: none !important;
}
So to add some clarification to some previous answers:
list-style-type is on ul
background-image in on li
It's better if instead of having the style inline you call it using a class:
<ul class="noBullets">
.noBullets {
list-style-type:none !important;
}
If you can't find the style that's overwriting yours, you can use the !important property. It's better to first inspect your code online using chrome or firefox's Inspect element (or firebug).
EDIT:
Accordnig to your comment, the style comes from div#wrapper ul. Did you try:
div#wrapper ul {
list-style-type:none !important;
}
The Trick is very simple:
HTML get that:
<ul id="the-one">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
Style get that:
ul#the-one {list-style-type: none;}
But, the next two options will blow your mind:
li {width: 190px; margin-left: -40px;} // Width here is 190px for the example.
We limit the width and force the li paragraph to move left!
See a Awesome example here: http://jsfiddle.net/467ovt69/
Good question; it's odd how the bullets show in IE even with the list-style:none;
This is the code that removed the bullets:
/* media query only applies style to IE10 and IE11 */
#media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
/* removes bullets in list items for IE11*/
li {
list-style-position: outside;
overflow: hidden;
}
}
check for the following line of code in your css:
.targeted-class-name>ul>li>a:before {
content: "•";
}
That was the culprit in my case
i think you could solve also your problem by wrapping text in your list-item with span then used something like this:
ul>li:nth-child(odd) > span:before {
display:none;
}
ul>li:nth-child(even) > span:before {
display:none;
}
Odd and even are keywords that can be used to match child elements whose index is odd or even, and display=none will do the trick to by not displaying element before the span element.
I am making a mobile webapp with JQuery Mobile. Now at the bottom I have some kind of a navigation menu. Here is the HTML
<ul data-role="listview">
<li data-icon="arrow-u">Top</li>
<li>Menu</li>
<li>Contacten</li>
<li>Klanten</li>
<li>Planning</li>
</ul>
Now I want the first listItem at the right side. So I made a css class 'top'
.top{
text-align:right;
padding-right:35px;
}
But for some reason it doesn't take this CSS class. Can anybody help ?
Try such:
ul li a.top{
text-align:right;
padding-right:35px;
}
OR such:
ul li:first-of-type a{
text-align:right;
padding-right:35px;
}
You are applying the top class to the a intead of the li.
Update
As your styling gets overridden, you need to increase the CSS-specificity of your selector until it is higher than the specificity of the rule that overrides it. As I don't know much of your DOM, the best I can give you is:
ul li.top{
text-align:right;
padding-right:35px;
}
But that might not be enought. Look through the article on CSS-specificity, there is a part on how to calculate specificity.
I've discovered that in certain cases overrides are a bit tricky. You may have to do something like this. Some browsers mobile & web are not picking up the overrides as I would have expected. End result I have to use important to make sure my style gets applied. Just be careful of how use this and where.
ul li.top{
text-align:right !important;
padding-right:35px !important;
}