Append horizontal rule to each <li> - html

For my <ul> list, I would like to add a <hr> after each element of a list. The Result should render like:
<ul class="mylist">
<li>
moooo!
<hr width="40%">
</li>
<li>
maaaaa!
<hr width="40%">
</li>
...
</ul>
It is bad style adding <hr> to each <li> so I would like to refractor this using css only. I cannot use:
.mylist > li: after{
content: "<hr>"
}
as content would escape the characters.
I also do not want to use jQuery:
$('.mylist').find('li').append('<hr width="40%">');
So the question is, how could I append <hr width="40%"> to each <li> of a certain list using css3 ?

jQuery Solution
Just realized that you wanted to nest the hr element inside the li before you close it, yes it's perfectly valid, so simply use append(), and note, you cannot do this using CSS only, as you cannot modify DOM using CSS, you need to use jQuery or JS
jQuery("ul li").append("<hr />");
Demo
CSS Solution
If you don't need an extra element, or you don't want a jQuery solution(As you want)
Using hr element as a direct child to ul element is not a valid markup, instead, you can use a border-bottom for each li which will behave same as hr does, still if you want an explicit way to do so, say for controlling the width of the separator without changing the width of li than you can do it like this
Demo
ul li:after {
content: "";
display: block;
height: 1px;
width: 40%;
margin: 10px;
background: #f00;
}
Here, am just creating a virtual block level element, which doesn't actually exists in the DOM, but it will just do the thing which you need. You can just design the element, the same way you style a normal div. You can also use border on this but to keep the thin line horizontally centered, I've assigned height: 1px; and than am using margin to space up.

I think it's better to use CSS for this. for example you can stop using <hr> tag, instead do something like:
<ul class="mylist">
<li>
moooo!
</li>
<li>
maaaaa!
</li>
...
</ul>
and then with CSS:
.mylist li { border-bottom: 1px solid black; }
There are other options too, for example if you want to show the horizontal line only for some list items, you can give them a class and add a CSS rule only for that class. like this:
<ul class="mylist">
<li class="hr">
moooo!
</li>
<li>
maaaaa!
</li>
...
</ul>
and CSS:
.mylist li.hr { border-bottom: 1px solid black; }

You can use like this:
<ul>
<li></li>
</ul>
<hr/>
Thats simple. If you have nested ul and li then you use li instead of <hr/> or simply <hr/> inside a <li></li> tag. See below. Its purely your choice.
<ul>
<li>
<ul><li></li></ul>
</li>
<li style="height:1px;border:solid 1px #666"> </li> // or you can also use
<li><hr/></li>
<li>
<ul>
<li></li>
</ul>
</li>
</ul>

Tags in content are not allowed and even if it would be very misleading (css { content: "text"}, How do i add tags?)
If you think is wrong to add <hr> in HTML than it is wrong adding with css (if it would be possible) or js. IMHO a first You should try to use border of <li> if result won't be as expected add that <hr>

Insert A Class That Creates A bottom-border: For Each <li>
<!--########## STYLE EACH li USING CLASS ##########-->
<style>
.hr {
width:40%;
border-bottom:1px solid rgba(0,0,0,.7);
}
</style>
<!--########### PAGE CONTENT ############-->
<ul class="mylist">
<li class="hr">
-CONTENT-
</li>
<li class="hr">
-CONTENT-
</li>
...

Try this CSS:
li:after {
content: " ";
display: block;
height: 2px;
width: 100%;
}

Related

CSS to target li inside ul inside li

I've looked through several DBs but I haven't found the answer that matches or works for my scenarios, so I'm turning to the experts, or at least the more experienced. I'm fairly new to HTML and CSS. I'm trying to figure out how to target an li inside of a ul, that's inside of an li, under a ul. Sort of like a drop down menu, where the main header has a submenu with more options. I don't want to add more classes or id's. I've tried the following versions to apply some basic CSS to it, but I can't seem to get it to target it:
#header-nav ul li
li ul li a
li#header-nav ul li
Even descendants doesn't seem to work (or it may be that I'm not doing it correctly?)
HTML:
<ul id="header-nav">
<li>
Home
</li>
<li>
Test Submenu
<ul>
<li> > Test 1 </li>
<li> > Test 2 </li>
</ul>
</li>
</ul>
I am attempting to target the Test 1 and Test 2 lines. Is there a way I can do this? I'm tapped on formats to make it work. I just want to change the font size of those two lines. I know it would be easier to add the classes or IDs but I am trying to avoid them where possible as I'm trying to understand the whole child, descendants, targeting thing better.
like that
#header-nav li > ul > li a {
background-color: red;
font-size: 80%;
}
<ul id="header-nav">
<li>
Home
</li>
<li>
Test Submenu
<ul>
<li> > Test 1 </li>
<li> > Test 2 </li>
</ul>
</li>
<li>
Test Submenu
<ul>
<li> > Test 1 </li>
<li> > Test 2 </li>
</ul>
</li>
<li>
Test Submenu
<ul>
<li> > Test 1 </li>
<li> > Test 2 </li>
</ul>
</li>
</ul>
Problem
"...I don't want to add more classes or id's.... I am attempting to target the Test 1 and Test 2 lines. Is there a way I can do this?"
Answer
Yes. But we must know some rules. CSS is declarative and its basic foundation are rules.
Casscading
The flow of CSS is a cascade of rulesets that take higher priority than the rulestes that have preceded them. From top, (less priority but wider influence due to inheritance,) to the bottom (higher priority but less influence due to how inheritance works in the same cascading direction (parent to child.))
The closer a ruleset is to the element it represents, the higher a ruleset chances of overriding the styles of the rulesets that have preceded it.
Styles
External Stylesheets: Normal priority, most maintainable, greatest scope -- unlimited amount of pages.
Page Location: Top of <head> tag.
Example: <link href="https://style.com/path/to/style.min.css" rel="stylesheet">
Inline Style Block: Higher priority, maintainable, limited scope -- a single page.
Page Location: Bottom of </head> tag.
Example: <style> selector { propertyName: propertyValue } ...</style>
Inline Style Attribute: Highest priority, least maintainable, least scope -- limited to a single tag.
Page Location: In a tag.
Example: <div style="propertyName: propertyValue"></div>
Specificity
The rules of Specificity are the only means of avoiding the cascading rule. This is the reason when we add a ruleset with !important after the Bootstrap CSS file and still have no success in overriding any style. Here's a CSS ruleset:
selector {propertyName: propertyValue}
⎱ ⎰
Declaration Block
Each CSS Selector has a measurable quality called Specificity. It is the measure of how specific a selector's declaration is as opposed to other selectors that share one or more properties and are used by a common element or group of elements. From that conflict, it is resolved by allowing the ruleset with the selector of the greatest specificity to override the styles of the other rulesets with its own styles. Should conflicting rulesets have selectors of equal specicity, then the rules of cascading apply (which ruleset is furthest from the top).
Specificity of a selector is measured by 4 separate numbers. From left (greatest) to right (least):
Being an inline style attribute. A single point in this category overrides all other categories that follow it. The only thing that can override it is !important unless of course it has an !important as well. If that's the case, then we can use the Grand Equalizer: JavaScript.
#ID. Having an #ID in a selector overrides everything except !important and inline style attributes.
.CLASS. Having .Class(es), [Attributes], and :Pseudo-class(es) in a selector overrides <Element Tags> and ::Pseudo-elements.
<Element Tags> and ::Pseudo-elements, very general thus the least in specificity.
Go to this page for an Online Specificity Calculator
If there's no dynamic elements added in the path then this'll work:
#header-nav li:last-of-type ul li a {
font-size: 48px;
}
There's 3 identical fragments of the layout -- each using a different relative unit of measurement (rem, em, %) at the equivalent distance of 48px (3 times the default of 16px = 1rem = 1em = 100%. Each fragment also shows how specificity and !important are used to make styles from frameworks like Bootstrap CSS so invincible.
Demo
html {
font: 400 16px/1.45 Consolas;
}
body {
font-size: 1rem;
}
b {
font-size: 1.5rem;
color: tomato;
}
i {
font-size: 1.25rem;
color: #A3CF65;
}
/* A */
/* 0,1,1,4 ⭐ */
#header-navA li:last-of-type ul li a {
font-size: 3rem !important;
}
/* 0,1,0,4 */
#header-navA li ul li a {
font-size: 2rem !important;
}
/* B */
/* 0,2,1,4 ⭐ */
#header-navB#header-navB li:last-of-type ul li a {
font-size: 3em;
}
/* 0,1,1,4 */
#header-navB li:last-of-type ul li a {
font-size: 2em;
}
/* C */
/* 🟊 */
#header-navC li:last-of-type ul li a {
font-size: 300% !important;
}
<!DOCTYPE html>
<html>
<head>
<meta chrset='utf-8'>
</head>
<body>
<ul id="header-navA">
<li>
<b>A</b>
</li>
<li>
<i>!important & Specificity Test</i>
<ul>
<li> <b>3rem</b> > Test A1 </li>
<li> <b>3rem</b> > Test A2 </li>
</ul>
</li>
</ul>
<hr>
<ul id="header-navB">
<li>
<b>B</b>
</li>
<li>
<i>Specificity Test</i>
<ul>
<li> <b>3em</b> > Test B1 </li>
<li> <b>3em</b> > Test B2 </li>
</ul>
</li>
</ul>
<hr>
<ul id="header-navC">
<li>
<b>C</b>
</li>
<li>
<i>!important & Inline Test</i>
<ul>
<li> <b>300%</b> > <a href="#" style='font-size:200%;'> Test C1 </a> </li>
<li> <b>200%</b> > <a href="#" style='font-size:200% !important'> Test C2 🟊</a> </li>
</ul>
</li>
</ul>

jQuery mobile button icon position too far left

I am using JQM 1.4.2 and trying to get the button icons closer to the Text when using the class .ui-btn-icon-left.
I took a screen shot and modified the the list icon the way I want it to look in the middle button. I just cant seem to get the icon off the left side at all. using padding just changed the top and bottom.
What class do I need to use to accomplish this?
<div data-role="navbar" >
<ul id="navfooter">
<li>Tour</li>
<li><a class="ui-nodisc-icon ui-icon-bars ui-btn-icon-left svbtn" href="#listViewPage" data-transition="slide" >List</a></li>
<li><a class="ui-nodisc-icon ui-icon-location ui-btn-icon-left svbtn" href="#map-page" data-transition="slide" >Map</a></li>
</ul>
</div>
Here is the image of what it now looks like after adding the css
.ui-icon-home::after {
position: relative;
left:10px;
}
You can set the left position of the icons within the button:
.ui-navbar a:after {
left: 20% !important;
}
However, this will allow the icon and text to overlap as the buttons get smaller.
A better way to achieve this might be to put the icons inline with the text. You put a span in front of the text with CSS to place the icon. To control the distance between the icon and the text, tweak the margin-right attribute:
<div data-role="navbar">
<ul>
<li><span class="ui-icon-alert ui-btn-icon-notext inlineIcon"></span>Summary
</li>
<li><span class="ui-icon-star ui-btn-icon-notext inlineIcon"></span>Favs
</li>
<li><span class="ui-icon-gear ui-btn-icon-notext inlineIcon"></span>Setup
</li>
</ul>
</div>
.inlineIcon {
display: inline-block;
position: relative;
vertical-align: middle;
margin-right: 6px;
}
If you want icons in black with no disc behind them add the ui-alt-icon class to the span and change the CSS to get rid of the disc:
<div data-role="navbar">
<ul>
<li><span class="ui-alt-icon ui-icon-alert ui-btn-icon-notext inlineIconNoDisk"></span>Summary
</li>
<li><span class="ui-alt-icon ui-icon-star ui-btn-icon-notext inlineIconNoDisk"></span>Favs
</li>
<li><span class="ui-alt-icon ui-icon-gear ui-btn-icon-notext inlineIconNoDisk"></span>Setup
</li>
</ul>
</div>
.inlineIconNoDisk {
display: inline-block;
position: relative;
vertical-align: middle;
margin-right: 6px;
}
.inlineIconNoDisk:after {
background-color: transparent !important;
}
Here is a DEMO showing all 3 options
If it's just the home icon you're looking to move, applying the following to that icon should fix it but it's hard to say what the best solution is without seeing the menu in action.
.ui-icon-home::after {
left: (amount to shift left)px;
}
Note that ::after applies styling to just the icon, whereas ".ui-icon-home" applies to the entire anchor element. Again, if you could link to a pastebin or similar, it would be easier to give you a better answer.

Modifying list title in HTML

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li>Interests
<ol>
<li style="color: orange">Basketball</li>
<li>Coding</li>
<li>Weight Lifting</li>
</ol>
</li>
<li>Jobs
<ul>
<li>Tutor</li>
<li>Salon Associate</li>
</ul>
</li>
</body>
</html>
How can I modify "Interests" (e.g. change its font color) without also modifying the ordered list it is a title of?
No classes or wrapping required here:
Demo: http://jsfiddle.net/abhitalks/jgbX7/
li { /* reset all list items */
color: black;
}
:not(li) > ul > li { /* all "li" under "ul" which are not under "li" */
color: red;
}
But, of course if you have multiple such lists, then providing a class to the top ul would help.
You can wrap Interests in a <span> tag and stylize it.
The part which changes :
<li><span style="color: red">Interests</span>
The full code :
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li><span style="color: red">Interests</span>
<ol>
<li style="color: orange">Basketball</li>
<li>Coding</li>
<li>Weight Lifting</li>
</ol>
</li>
<li>Jobs
<ul>
<li>Tutor</li>
<li>Salon Associate</li>
</ul>
</li>
</body>
</html>
fast and easy way is to put a <span> around it then you can apply any style
If you select the li containing the text and the other list with CSS and apply styles like color, this will apply to child elements too if not excluded explicitly.
Just wrap the text in another element:
<li><span>Interests</span>
<ol>
It’s valid and light-weight, and you can apply styles directly.
Also, your ul should be closed, and the title element must not be empty.
you can use inline css
suppose
<li><span style="color:blue;">Interests</span>
</li>
You should be able to give it a class, i.e.
<li class="bob">
Then:
li.bob
{
color: #C1C1C1;
}
li
{
color: black;
}
and then refer to that in your style section
Actual color style, just for reference - but you get the idea.

Multiple CSS declaration for same tag

I have a css style defined in a .js file for <li> tag in a page. I am trying to modify the html page without changing the existing css and js files and layout of the page. I want to insert the following code:
<div style="float: left; width: 18%; bottom: 6.4%; border-right-style: groove; border-right-width:2px; border-top-style: groove; border-top-width:2px; border-bottom-style: groove; border-bottom-width: 2px; padding-right: 0%; padding-top: 0%; margin-right: 0%; margin-top: 0%; position: fixed;">
<ul class="MenuBarVertical">
<li>Text 1</li>
<li>Text 2</li>
</ul>
</div>
But every <li> tag in the html page automatically picks the style from the existing css file. I don't want this for some <li> tags. I want a different css for <li> tags. Is it possible that I enclose those <li> tags under a <div> tag and add a class to the div tag and define a css specific for it which can override existing css for every <li> tag? I can override existing css by defining style in the <li> tag like <li style="...">. It works fine, but I have to add style in every <li> tag. I want to know if I add id or class to the parent tag and write css for it somewhere in the page itself so as to override the existing style from .css file. What will be the code? How to declare such css in the section? or should I declare in body?
I hope I am able to explain my problem. I want to know the possible ways to override css style for a tag.
To override the old rules, you need to define elements as specific as possible, like this:
div ul.MenuBarVertical li {...}
Even better if you can put an id to the div or ul elements (if you have a single such element on the page).
If nothing else works, you can use the !important parameter, but it's strongly not recommended.
There are several possible solutions:
1) use classes to define the properties you want. e.g.
<ul class="MenuBarVertical">
<li class="other">Text 1</li>
<li class="other">Text 2</li>
</ul>
and then declare the styles you want to have on those lis. Declare it accordingly in your css file or to insert the ruleblock for this class with javascript, you can create a styleblock on the fly and add it to the head or use an existing one and append this rule.
2) Include an inline style block:
<ul class="MenuBarVertical">
<style scoped>
.MenuBarVertical li{
/*declarations here*/
}
</style>
<li>Text 1</li>
<li>Text 2</li>
</ul>
The scoped attribute is rather new and not recognized by browsers other than firefox and chrome, that's why I added the .MenuBarVertical part to further specify the lis you want to style (increase specificity) and it will still work. Once scoped is recognized in all browser you could drop this and just write li{...}.
Please don't:
a) use inline styles - bad, always try to avoid this!
b) use !important - even worse, dont do this either!

Exclude CSS property for only one element

I have a page with a default CSS file. In this page I have:
<ul>
<li>A1</li>
<li>A2</li>
</ul>
<br>
<ul>
<li>B1</li>
<li>B2</li>
</ul>
<br>
<ul>
<li>C1</li>
<li>C2</li>
</ul>
<br>
I can view the default CSS but I cannot amend
ul {
paddind-left:15px;
}
what I want to do is to exclude only B1 and B2 from the default css. A and C should still have the default property but B1 and B2 should have PADDING-LEFT:0PX;.
I have used (cssreset-min.css) but all the css was eliminated. Any help?
Give the parent ul a new class:
<ul>
<li>A1</li>
<li>A2</li>
</ul>
<ul class="newClass">
<li>B1</li>
<li>B2</li>
</ul>
<ul>
<li>C1</li>
<li>C2</li>
</ul>
Then do:
ul.newClass {
paddind-left:0px;
}
This will work in all browsers. If you're not concerned about that, use #Andy answer.
if you want to apply for this 3 named list..simply use div with different id for B1,B2....but if you want to apply for an huge list it would be difficult
If I understand right, ul li:nth-child(3), ul li:nth-child(4) { padding-left: 0; } should work
The nth-child selector targets specific children, in this case the 3rd and 4th
Edit: After seeing your edit, the new code that you will need to do is: (I will use #container as the name for your containing div, whatever that is)
#container ul:nth-child(2) li { padding-left: 0; }
You can try something like this :
http://jsfiddle.net/4X62Y/
Here's another solution without changing the HTML:
ul:not(:nth-child(3)) {
padding-left:15px;
}
Example
This will probably not work in all browsers, you'll need to change the HTML for that and use the answer provided by #Alex Thomas .