Avoid multiple uls to create a "diagonal" list - html

I am trying to create a slanted/diagonal list, but want to avoid having several nested uls, is there a way to do this, here is the code I have so far:
<ul className={style.steps}>
<li>
<p className={style.step}>Lorem</p>
<ul className={style.steps}>
<li>
<p className={style.step}>Ipsum</p>
<ul className={style.steps}>
<li>
<p className={style.step}>dolor</p>
<ul className={style.steps}>
<li>
<p className={style.step}>sit</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

If you can update how the code outputs, you don't need nth-selectors, especially if the list is dynamic and changes length, you can use CSS vars.
You can set a default value using :root, then you can add the custom property value on each li element --index: number. You can change the CSS var on the element to whatever you need.
:root {
--margin-value: 10px;
}
ul,
li {
margin: 0;
padding: 0;
}
li {
margin-left: calc(var(--index) * var(--margin-value));
}
<ul>
<li style="--index: 1">One</li>
<li style="--index: 2">Two</li>
<li style="--index: 3">Three</li>
<li style="--index: 4">Four</li>
<li style="--index: 5">Five</li>
</ul>

The :nth-child selector would allow you to set different spacing rules on different <li>s within a single list.
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
ul {
list-style: none;
}
ul li:nth-child(1) {
margin-left: 0;
}
ul li:nth-child(2) {
margin-left: 30px;
}
ul li:nth-child(3) {
margin-left: 60px;
}
ul li:nth-child(4) {
margin-left: 90px;
}
<ul>
<li>Lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
</ul>

You did not use the javascript tag, but it could be handy here , so not really an answer but another approach.
Demo below:
var elMargin = 0;
for (let e of document.getElementsByClassName('indent')) {
elMargin = elMargin + 1;
e.style.marginLeft = elMargin + 'em';
}
ul {counter-reset:lis}
li:before{counter-increment:lis;content:counter(lis);
<ul>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
<li class="indent"></li>
</ul>

Related

How do I style this element?

How do I access the style <li> elements so I can set their property to - list-style: none;?
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="prev disabled">? Previous</li>
<li class="active">1</li>
<li>2</li>
<li class="next">Next ? </li>
</ul>
</div>
.dataTables_paginate ul {
list-style: none;
margin: 0;
padding: 0;
}
.dataTables_paginate li {
display: inline-block;
}
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="prev disabled">? Previous</li>
<li class="active">1</li>
<li>2</li>
<li class="next">Next ? </li>
</ul>
</div>
You should use the ul selector to remove the list styling.
ul {
list-style-type: none;
}
To change just one set of ulgive the give your ul a class or ID.

Expanding drop down menu on hover using css

I'm trying to get a drop down menu for my navbar so that when I hover over the tabs it opens a drop down menu with more options. Here is a fiddle of my code. The very final product should look like this, for now I just want to fix the drop down on hover part of it.
Here is a snippet of code im using in css to try and achieve this:
.dropdown {
display: none
}
.navbar-list li:hover .dropdown {
display: relative;
color: white;
text-decoration: none;
}
You are trying wrong approach, please change your css part
.navbar-list li:hover .dropdown {
display: block;
color: white;
text-decoration: none;
}
<ul class="navbar-list">
<li class="navbar-tags">OUR DNA
<ul class="dropdown">
<li>Risk</li>
</ul>
</li>
update code
Your HTML structure is wrong, and you need to use display: block on hover, not display: relative
But re: the HTML, a ul can't be a direct child of a ul. You need to nest the dropdowns in an li. That is not only correct markup, but it allows the hover to persist when you hover over an li that has nested menus. Otherwise, you would need to use li:hover + .dropdown to show the next .dropdown menu, but your :hover will stop once your mouse moves off of the li.
Also, each ul.dropdown that is in a single nested nav element could probably just be li's of a single ul, but what you have isn't incorrect, so I didn't change that.
.dropdown {
display: none
}
.navbar-tags:hover > .dropdown {
display: block;
color: white;
text-decoration: none;
}
.navbar-list a {
color: black;
text-decoration: none;
}
.navbar-tags {
margin: 20px;
}
.navbar-tags, .dropdown {
padding: 0;
list-style-type: none;
}
<ul class="navbar-list">
<li class="navbar-tags">
OUR DNA
<ul class="dropdown">
<li>Risk</li>
</ul>
</li>
<li class="navbar-tags">PROGRAMS
<ul class="dropdown">
<li>Adventure Sport</li>
</ul>
<ul class="dropdown">
<li>Entertainment</li>
</ul>
<ul class="dropdown">
<li>Collegiate</li>
</ul>
<ul class="dropdown">
<li>Individual</li>
</ul>
<ul class="dropdown">
<li>Commercial</li>
</ul>
</li>
<li class="navbar-tags">RESEARCH
<ul class="dropdown">
<li>Corporate Survey</li>
</ul>
<ul class="dropdown">
<li>Individual Survey</li>
</ul>
</li>
<li class="navbar-tags">STORIES</li>
</ul>
1 ) Your HTML structure is wrong.
2) use display: block instead of display: relative.
Change your Code Like THis :
.dropdown {
display: none
}
.navbar-list li:hover > .dropdown {
display: block;
color: white;
text-decoration: none;
}
.navbar-list a {
color: black;
text-decoration: none;
}
.navbar-tags {
padding: 0;
list-style-type: none;
margin: 20px;
}
<ul class="navbar-list">
<li class="navbar-tags">OUR DNA
<ul class="dropdown">
<li>Risk</li>
</ul>
</li>
<li class="navbar-tags">PROGRAMS
<ul class="dropdown">
<li>Professional</li>
</ul>
<ul class="dropdown">
<li>Adventure Sport</li>
</ul>
<ul class="dropdown">
<li>Entertainment</li>
</ul>
<ul class="dropdown">
<li>Collegiate</li>
</ul>
<ul class="dropdown">
<li>Individual</li>
</ul>
<ul class="dropdown">
<li>Commercial</li>
</ul>
</li>
<li class="navbar-tags">RESEARCH
<ul class="dropdown">
<li>Corporate Survey</li>
</ul>
<ul class="dropdown">
<li>Individual Survey</li>
</ul>
</li>
<li class="navbar-tags">STORIES</li>
</ul>

How do I make a dropdown submenu appear directly below its parent <li>?

I'm building a css dropdown menu and have been unable to get the submenus to appear below their respective parent li elements. I've tried a bunch of the solutions suggested in response to similar questions but have been unable to get them to work.
Here's a sample of the menu I'm building:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Menu Test</title>
<link rel="stylesheet" href="menustyle.css" type="text/css">
</head>
<body>
<div id="menudiv">
<ul class="menu">
<li class="menuitem">Aluminum</li>
<ul class="submenu">
<li class="subitem">Round</li>
<li class="subitem">Sheet</li>
<li class="subitem">Plate</li>
</ul>
</li>
<li class="menuitem">Copper</li>
<ul class="submenu">
<li class="subitem">Round</li>
<li class="subitem">Sheet</li>
</ul>
<li class="menuitem">Steel</li>
</ul>
</div>
</body>
</html>
And here's the css:
#menudiv {
text-align:center;
}
ul.menu {
list-style-type:none;
}
li.menuitem {
position:relative;
display:inline-block;
}
ul.submenu {
display:none;
position:absolute;
}
.menuitem:hover+ul.submenu {
display:block;
}
I can move the submenus around by adding things like right:50px; to ul.submenu, but that moves all the submenus to the same location.
What am I missing here? Thanks!!
Here's a Fiddle.
First of all, the following markup structure :
<li class="menuitem">Aluminum</li>
<ul class="submenu">
<li class="subitem">Round</li>
<li class="subitem">Sheet</li>
<li class="subitem">Plate</li>
</ul>
is incorrect. It should be :
<li class="menuitem">Aluminum
<ul class="submenu">
<li class="subitem">Round</li>
<li class="subitem">Sheet</li>
<li class="subitem">Plate</li>
</ul>
</li>
Secondly, you could use a CSS reset for ul,li elements. For the sake of simplicity I've used :
* {
margin: 0;
padding: 0;
}
Now, coming to your question. the following classes needs to be changed :
.menuitem:hover+ul.submenu {
display:block;
}
to
.menuitem:hover > ul.submenu {
display:block;
}
and
ul.submenu {
display:none;
position:absolute;
top:0px;
right:50px;
}
to
ul.submenu {
display:none;
position:absolute;
}
You can then modify the following class (so that the child ul elements "fits-in" to the parent li):
li.menuitem {
position:relative;
display:inline-block;
}
to
li.menuitem {
position:relative;
display:inline-block;
padding: 5px 10px;
margin: 0 10px;
}
In summary, I guess this is what you are looking for :
* {
margin: 0;
padding: 0;
}
#menudiv {
text-align:center;
}
ul.menu {
display: inline-block;
list-style-type:none;
}
li.menuitem {
position:relative;
display:inline-block;
padding: 5px 10px;
margin: 0 10px;
}
ul.submenu {
display:none;
position:absolute;
}
.menuitem:hover > ul.submenu {
display:block;
}
<body>
<div id="menudiv">
<ul class="menu">
<li class="menuitem">Aluminum
<ul class="submenu">
<li class="subitem">Round</li>
<li class="subitem">Sheet</li>
<li class="subitem">Plate</li>
</ul>
</li>
<li class="menuitem">Copper
<ul class="submenu">
<li class="subitem">Round 2</li>
<li class="subitem">Sheet 2</li>
</ul>
<li class="menuitem">Steel</li>
</ul>
</div>
</body>
Hope this helps!!!
Try placing the <ul class="submenu"> inside the <li class="menuitem">. Then set the <li> to position:relative; and set the <ul> to position:absolute;left:0;. This will position the <ul> relative to its parent element, the <li>.
Here's a codepen example: http://codepen.io/anon/pen/WQdMjX
Your markup is incorrect for nesting a sub-list.
You're doing this:
<ul>
<li>text</li><!-- incorrect, don't close li here -->
<ul>
<li>sub</li>
</ul>
</li><!-- correct, though li is already closed -->
<li>text</li><!-- incorrect, don't close li here -->
<ul>
<li>sub</li>
</ul>
<!-- needs closing li here -->
<li>text</li>
</ul>
Instead do this:
<ul>
<li>text
<ul>
<li>sub</li>
</ul>
</li>
</ul>
Then update your CSS selector from .menuitem:hover + ul.submenu to .menuitem:hover > ul.submenu as you're no longer selecting a sibling element (+) but a child element (>).
You'll need to fine tune the positioning of your sub-menus from here but this should get you where you need to be.
Remember, when you are developing menus you need to make sure the link content is inside anchor tags, including the links at the top level navigation that launch the subnav. That way these links are natively focusable. You want to be able to reach these menu elements with a keyboard only since many with arthritis, Parkinson's disease, etc. may be unable to use a mouse (and you won't want to use tabindex to mimic this behaviour since screen-readers will look for anchor tags.)
There was a similar StackOverflow question yesterday: Absolutely positioned child's top edge pinned to the bottom edge of its parent that has unknown height?
You can also Bootstrap Dropdown CSS in a normal case too.

Create a nested ordered list in a tabular style with css

Hi I am looking to display my ordered list this:
so the first node and the first nested node appear on the top line and the remaining nested nodes appear under the 2nd column (under red).
Apples Red
Green
Yellow
Banana Yellow
html:
<ul class="lst" id="list_Apple">
<li>Apple</li>
<ul>
<li id="Apple">Red</li>
<li id="Apple">Green</li>
<li id="Apple">Yellow</li>
</ul>
</ul>
<ul class="lst" id="list_Banana">
<li>Banana</li>
<ul>
<li id="Banana">Yellow</li>
</ul>
</ul>
There is a slight mistake in your html. The <ul> tag should be inside a <li>.
HTML:
<ul class="lst" id="list_Apple">
<li>Apple</li>
<li>
<ul>
<li id="Apple">Red</li>
<li id="Apple">Green</li>
<li id="Apple">Yellow</li>
</ul>
</li>
</ul>
<ul class="lst" id="list_Banana">
<li>Banana</li>
<li>
<ul>
<li id="Banana">Yellow</li>
</ul>
</li>
</ul>
And add this CSS:
.lst {
clear: both;
}
.lst li {
list-style: none;
}
.lst > li {
float: left;
}
Here's a Fiddle: http://jsfiddle.net/rayg8ua9/1/
lithanks... Yes I missed the li tag.
.lst {
clear: both;
padding-top: 10px;
}
.lst li {
list-style: none;
width:100px;
}
.lst > li {
float: left;
}

Vertical Gap at End of List?

I have a vertical UL list on a html page, with a sublist inside of it. At the end of the sublist, it has an unwanted gap, like a linebreak, though I can't seem to find anything in my css or html that would cause it (and my attempts at trying to get it to go away aren't working very well).
Here's what it looks like;
1. Item
2. Item
1. Sub Item
2. Sub Item
3. Item
4. Item
My html code:
<ul class="fic">
<li class="fic"><a href="">item</li>
<li class="fic"><a href="">item
<ul class="fic">
<li class="fic">item</li>
<li class="fic">item</li>
</ul>
</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item</li>
<li class="fic">item
<ul class="fic">
<li class="fic">item</li>
</ul></li>
</ul>
And my css code that I was trying to use;
.fic ul {
padding: 0px;
margin: 0px;
}
And since my template came with menu code for the ul used in the navigation bar, here's that;
#menu ul {
display: block;
width: 778px;
margin: 0em auto;
list-style: none;
padding-left: 2.5em;
}
#menu li {
display: inline;
}
#menu li a {
color: #ffffff;
font-weight: bold;
font-size: 1.2em;
text-decoration: none;
padding: 0.25em 0.75em 0.25em 0.75em;
}
#menu li a:hover {
background: #342117 url('images/hover.gif') top left repeat-x;
color: #fffdc6;
}
html code for navigation menu;
<div id="menu">
<ul>
<li class="first">Home</li>
<li>Fan Art</li>
<li>Fan Fiction</li>
<li>Fan Videos</li>
<li>Other</li>
<li>Forum</li>
</ul>
</div>
Help would be appreciated, thank you.
The menu styles are the culprit, need full code