What I am styling here (ul, li or a) - html

I have one CSS file which I found it in one website, but I have a confusion about it. The code is:
ul li a {
background-color: FFFFFF;
border: 1px solid 86B3E6;
color: 2F62AC;
display: block;
font-size: 17px;
font-weight: bold;
margin-bottom: -1px;
padding: 12px 10px;
text-decoration: none;
direction:rtl;
}
So, what I am styling here? as I know, it should be (( a )) tag, so if I add
display:inline-block;
to (( ul )) tag styling which I found here (( UL display: block )) it should work, but unfortunately I failed to make it.
Maybe I will have one more question later, but for timing i want to understand the code and correct my information.
Best regards and thanks in advance,
Gharib
edit:
I want to use both inline-block and block, and here is my full code:
ul.ablock {
display: block;
}
ul.aninline {
display: inline-block;
float: right;
width: 50%;
}
a {
background-color: FFFFFF;
border: 1px solid 86B3E6;
color: 2F62AC;
display: block;
font-size: 17px;
font-weight: bold;
margin-bottom: -1px;
padding: 12px 10px;
text-decoration: none;
direction:rtl;
-webkit-border-top-left-radius: 8px;
-webkit-border-top-right-radius: 8px;
-webkit-border-bottom-left-radius: 8px;
-webkit-border-bottom-right-radius: 8px;
}
a:active, a:hover {
background-color:2F62AC;
color:FFFFFF;
}
and the html is something like:
<ul class="ablock">
<li><div align="center">Find</div></li>
</ul>
<ul class="aninline">
<li><div align="center">Back</div></li>
<li><div align="center">Next</div></li>
</ul>

The above selector will target all a elements which are nested under li which is further nested under ul, that's a general element selector, which will target all the a element which falls in that pattern. It is better to be specific and use a class instead, like
ul.class_name li a {
/* Styles goes here */
}
The above selector will only target a elements which are nested under li which are further nested under an ul element having a class called .class_name
As you commented, it seems like you want to target a ul element, now instead of using something like
ul {
/* Styles goes here */
}
Will apply the styles to all the ul elements, instead, be specific, either assign a class to your ul element and use a selector like
ul.class_name {
/* Styles goes here */
}
Or you can also use a nested selector like
div.wrapper_div ul {
/* Styles goes here */
}
Here, in the above selector we are selecting all the ul which are nested under .wrapper_div.
Just a side note, you seem to be confused so don't wanna confuse you more, so don't read this, you can simply ignore, but if you want to learn, just make sure that, if you are targeting ul, make sure you use > selector which will select direct child, as users tend to nest a ul element under li, say for example dropdown menu, this is common, so it is better to use a selector like
div.class_name > ul { /* Selects first level ul elements */
/* Styles goes here */
}
ul > li > ul { /* Selects nested level ul elements */
/* Styles goes here */
}

You are targeting the <a> element here. The reason for the ul and li is that, you're targeting a specific nesting of a. Namely, you are targeting a <a> that is a descendant of <li> that is in turn, a descendant of a <ul>.
If you want to add dispay: inline-block to all <ul> elements then above the rule for ul li a you want to add:
ul { display: inline-block; }

Related

Reuse CSS style and apply to other HTML element

I would like to reuse given css formatting .parsley-errors-list > li and apply it to different HTML element than it it designed to be used for. I would like to apply it to <span> in order not to create another duplicated style and to be sure that span will always be of the same style as .parsley-errors-list > li.
.parsley-errors-list {
display: none;
margin: 0;
padding: 0;
}
.parsley-errors-list > li {
font-size: 12px;
list-style: none;
color: #f05050;
}
EDIT:
Given CSS sample is part of an updatable theme, so I would like to avoid changing it for the maintainability.
That's very easy to do. Just put a comma on the CSS selector like this:
.parsley-errors-list > li, span {
font-size: 12px;
list-style: none;
color: #f05050;
}

Trouble with CSS styles applying to everything

I want to create a horizontal navigation bar on one of my pages, so I used a list and then edited it in CSS. However, the same page also has other lists, and when I have applied the styling it has worked for the nav bar, but has completely destroyed the other lists! How do I get round this? I've tried ID tags but I don't know if they overrule applying a certain style to all lists? This is my CSS code:
#menubar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #85aff2;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
All lists on the page are 'standard' lists, i.e. they are all bog standard <ul> or <ol> with no id tags - apart from the navigation bar list, which I have called 'menubar'.
For the menubar styles you need to apply the id like #menubar also for its child elements if you only want them to apply inside the menubar
see example:
#menubar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #85aff2;
}
#menubar li {
float: left;
}
#menubar li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
<ul id="menubar">
<li><a>one</a></li>
<li><a>two</a></li>
<li><a>three</a></li>
</ul>
<ul>
<li><a>normal one</a></li>
<li><a>normal two</a></li>
<li><a>normal three</a></li>
</ul>
the problem with your CSS is that you apply styles to all 'li' and 'li a' elements. The best way to get this to work is to be a bit more specific to where you want to apply the CSS.
Try the following (using your code above).
#menubar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #85aff2;
}
#menubar li{
float: left;
}
#menubar li a{
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
If you don't specify an ID or a class the style will affect every matching element.
In your example, you style elements with the id "menubar", and then you style ALL "li" elements and lastly all "li" and "a" elements.
If you wish to apply your style only to items in your navigation menu, you could give them a class like "nav_menu", and write the style like this:
.nav_menu {
float: left;
}
.li_and_a {
display: block;
color:white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
so your list items with the float now need the class "nav_menu" and the list items and the a items need the "li_and_a" class.
Doing this will not impact any other "li" or "a" elements on your page unless they have that specific class.
There are several ways to resolve this, but I think that at this point, the most practical way would be to use the :not() selector with your lists and exclude the #menubar.
For example, if your #menubar is a id for a li, you could add it like this:
li:not(#menubar) {
/* your css */
}
li:not(#menubar) a {
/* your css */
}
EDIT 28/02
My understanding is that you have your horizontal bar with the #navmenu and the rest of your CSS you do not want to take effect in it.
If that is what you want, this solution does work. As it was tested on jsfiddle.
https://jsfiddle.net/a2kj8vds/

Understanding classes in css

I have just started learning css. I have assumed that a class is a way of grouping styling information.
I'm trying to understand CSS pagination. In the example, it is written
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li { display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
}
pagination is a class. what does it mean when they write ul.pagination li and
ul.pagination li a?
ul.pagination li a has a float left; style. what will this achieve?
When you have the following code block in your HTML document:
<ul class="pagination">
<li>
</li>
<li>
</li>
<li>
</li>
</ul>
and you would like the style this list, list items, and the links separately.
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
The CSS block above will affect your list (<ul></ul>).
ul.pagination li {
display: inline;
}
This one will affect each list item (<li></li>) in your list.
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
}
and the last one will affect each link (<a></a>) inside of your list items.
float: left is used to have a horizontal list (for example menu). You can learn more from this link: http://www.w3schools.com/cssref/pr_class_float.asp
Lastly, I also suggest you to read the CSS Selectors in order to understand the logic: http://www.w3schools.com/cssref/css_selectors.asp
I recommend you read an authoritative source on CSS selectors: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors
Note that the term "selector" refers to both a rule's selector (everything before the {) but also to individual components of the selector, so h1.foo p.bar is a selector, as is just h1.foo and p.bar, for example).
In response to your questions directly:
what does it mean when they write ul.pagination li and ul.pagination li a?
When a space character appears between two selectors (e.g. between ul.pagination and li it means "descendant", so any <li> element which appears underneath an <ul class="pagination"> will be matched, no matter how deep it is (as opposed to the > selector, as in ul.pagination > li, which only selects <li> elements that are immediate children of <ul class="pagination">.
CSS rules match and apply to only the deepest element in the rule, so ul li will only apply style rules to the <li>, but ul li a will only apply style rules to <a> elements.
ul.pagination li a has a float left; style. what will this achieve?
It means that every <a> element that is a descendent of a <li> which in-turn is a descendent of a <ul class="pagination"> will have the property float: left applied to it.

Styling specific list within UL

Say this is my setup:
<ul id="filters">
<li>Any distance</li>
<li>10 km</li>
<li>30 km</li>
<li>50 km</li>
</ul>
In my css how can I style my list depending on what UL it is in? I tried this:
#filters ul li {
padding: 10px;
}
But I didn't have any luck, the same with several different variations on this. I know it's a basic question but I'm still learning.
Your selector is incorrect for what you are trying to achieve:
#filters ul li {
padding: 10px;
}
#filters ul li will select li elements that are descendants of a ul that is a descendant of an element with id="filters".
You want:
#filters li {
padding: 10px;
}
You simply want:
#filters li {
padding: 10px;
}
The reason it doesn't work at the moment is because it's saying the ul INSIDE an element with the ID #filters. (e.g. <div id="filters"><ul>...)
Another correct way of styling your list would be ul#filters li. This says that you want a ul element with the ID "filters". This is unnecessary though, since you can only have one element with the ID "filters".
Your selector is wrong.
Below are some alternative selectors that will work just fine:
ul#filters li {
padding: 10px;
}
The above selector will apply to the LI elements of any UL element that has the ID 'filter'. This selector has the highest specificity (in the given context). Here's a good article on specificity http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
#filters li {
padding: 10px;
}
The above selector will apply to the LI elements of an element that has the ID 'filter'.
#filters * {
padding: 10px;
}
The above selector will apply to ANY element of an element that has an ID 'filter'. I don't advise you to use this, but you could because a UL element has to be followed by LI elements according to the W3C.
The #filters is the ul itself, you must select the children like this:
ul#filters li {
padding: 10px;
}
However, because ID is unique, you can ommit the ul:
#filters li {
padding: 10px;
}
Try this instead:
#filters li {
padding: 10px;
}
It would need to be
ul#filters li{some code}
You put the id or class after the name of the element.
or simply
#filters li{your code}

IE7 <li> bullet/number outside of hover

Follow-up from another post here: IE7 li bullet or number shown outside of div
In the previous post, the li element outside the div was fixed, but now I have another IE7 bug with the hover element. Since the hover element can not be set through the , how do I fix this one?
P.S. Obviously I've been having some trouble with the hasLayout bug in IE, so it someone was to give a nice explanation it would be appreciated.
Again everything works in firefox, etc.
The screenshots:
The code:
#create_request ol {
width: 339px;
}
#create_request li {
display: list-item;
line-height: 23px;
background-color: #E3E3E3;
list-style: decimal;
list-style-position: inside;
padding-left: 25px;
padding-top: 5px;
}
#create_request li.alternate {
background-color: white;
}
#create_left li:hover {
width: 356px;
background: url('/images/list_add.png') 100% 100% no-repeat;
background-color: #B0B0B0;
cursor: pointer;
}
Unfortunately, that's not possible without bringing in another element in the <li>. The incorrect list-style-position behaviour occurs in IE6/7 when the <li> element get hasLayout. You want to totally avoid hasLayout on the element. The width is one of the hasLayout triggers.
I suggest to put a <span> in the <li> (yes, sorry if you would cry)
<li><span>Item</span></li>
and change the li:hover style as follows
#create_left li:hover {
background: #B0B0B0;
cursor: pointer;
}
#create_left li:hover span {
display: block;
width: 356px;
background: #B0B0B0 url('/images/list_add.png') 100% 100% no-repeat;
}
This way the span controls the width of the <li> without giving it hasLayout. You only need to remove padding-top: 5px; from the <li>'s CSS and counteract it with line-height, otherwise the <span> will not get the full height.
Make it if necessary an IE6/7 conditional stylesheet.
I believe you need to declare "list-style-position" in the rule for your OL tag:
#create_request ol {
list-style-position: inside;
}