Sticky rows in tree-like nested list structure - html

I have a long list of items that are nested.
section#container {
height: 250px;
width: 350px;
overflow: auto;
}
li {
position: relative;
background: white;
}
div {
position: sticky;
top: 0;
}
<section id="container">
<ul>
<li>
<div>Aaaaaaaaaaaa</div>
<ul>
<li>
<div>Bbbbbbbbbbbb</div>
</li>
<li>
<div>Cccccccccc</div>
</li>
<li>
<div>Dddddddddddd</div>
<ul>
<li>
<div>Eeeeeeeeee</div>
</li>
<li>
<div>Fffffffff</div>
<ul>
<li>
<div>Ggggg</div>
</li>
<li>
<div>Hhhh</div>
</li>
<li>
<div>Iiii</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
<pre>More<br>content<br>so<br>we<br>can<br>scroll<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.</pre>
</li>
</ul>
</section>
When scrolling, I want to always see the "path that led to the current top row". For example, when scrolling further down, you should see
A # "Path"
B # leading up to
D # G (breadcrumbs)
F #
G
H
...
Or maybe better visualized, see THIS DEMO of marchaos/react-virtualized-sticky-tree. It does exactly what I am looking for. The library however uses JavaScript and just sets the top property according to the nest level. I am looking for a simple CSS-only solution (independent on the amount of recursion), which I think should be fairly basic but I cannot seem to find a solution.
In the snippet above I attempted so with position:sticky on the divs, and it somewhat works, but they are unfortunately all on the same line. Thanks for any hints.

You can simply set z-index to an individual content in order to differentiate it from the other contents which doesn't have sticky position and set the top of an them.
Remember to set top which is different from the one another in order to align them one under another because if you set the same top will cause an overlapping.
the following is an example
section#container {
height: 240px;
width: 350px;
overflow: auto;
}
li {
position: relative;
background: white;
}
div {
position: sticky;
}
.first {
z-index: 99999;
background: red;
top: 0;
}
.second {
z-index: 99999;
background: blue;
top: 15px;
}
.third {
z-index: 99999;
background: green;
top: 30px;
}
<section id="container">
<ul>
<li>
<div class="first">Aaaaaaaaaaaa</div>
<ul>
<li>
<div>Bbbbbbbbbbbb</div>
</li>
<li>
<div>Cccccccccc</div>
</li>
<li>
<div class="second">Dddddddddddd</div>
<ul>
<li>
<div>Eeeeeeeeee</div>
</li>
<li>
<div class="third">Fffffffff</div>
<ul>
<li>
<div>Ggggg</div>
</li>
<li>
<div>Hhhh</div>
</li>
<li>
<div>Iiii</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
<pre>More<br>content<br>so<br>we<br>can<br>scroll<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.</pre>
</li>
</ul>
</section>

Related

Div scrolling, with parent

I have a div. And this div contains a very long list. This list becomes scrollable automatically.
This div is then inside another div, which fills out the entire screen.
Scrolling works fine, but it only works when the mouse is over the inner div, the one that actually contains all the data.
But I want scrolling to work, also when the mouse is over the parent div, the one that fills out the entire screen.
How do I do that?
You can see the sample here:
https://openage.org/fs/jsl_forritun/?page=hlutir
It doesn't scroll when the mouse is over the darker part. /:
you have two divs that contains your content body
id= "nhreyfing"
give him this style padding: 0 20vw; background: black
or what so ever padding you wanna give him
id= "content"
give him your rgb(213, 191, 134) background
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #2B2832;
}
.main {
margin: 0 auto;
padding: 40px;
width: 700px;
height: 100vh;
overflow-y: auto;
background: #D5BF86;
}
.main ul {
margin: 0;
padding: 0;
}
.main ul li {
list-style: none;
}
.main ul li:nth-child(n+2) {
margin: 10px 0 0;
}
.main ul li a {
padding: 10px;
display: flex;
color: #000;
background: #C6B076;
text-decoration: none;
}
<div class="main">
<ul>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
<li>
Link
</li>
</ul>
</div>
Do the following:
remove CSS properties from
BODY element {height:100%}
remove {max-width: 800px} and add CSS to id="initial_screen_base" element:
div#initial_screen_base {
max-width: 100vw;
padding: 0px 20vw;
background-color: rgb(43, 40, 50);
}
if you want it to me maxed always to 800px (for smaller devices it will be full width), make it like this:
div#initial_screen_base {
background-color: rgb(43, 40, 50);
max-width: 100vw;
padding: 0;
padding-right: calc((100% - 800px)/2);
padding-left: calc((100% - 800px)/2);
}
for your id="node":
div#node {
background-color: rgb(213, 191, 134);
}
You should remove you inline CSS and put them in a style TAG inside your HEAD. That way you will fave more clear code for yourself to edit.

How can I keep horizontal element alignment inside an expanding list item?

I have been tasked with styling a website, where I have encountered a hurdle regarding the horizontal alignment of elements inside list items.
I have replicated the structure of the menu in question with this JSFiddle.
I want to know how I can keep the position of the green divs (as shown from the start) when I expand the menu, using the button in the fiddle. I need them to keep horizontal alignment with the first <a> element in their respective <li> element, when the submenus are displayed.
you can do it like this for example:
<html>
<script>
function clickFunction(){
var elements = document.getElementsByClassName("submenu");
for(var i = 0; i < elements.length; i++){
elements[i].classList.toggle("display-sublist");
}
}
</script>
<style>
ul{
list-style-type: none;
margin: 0px;
padding: 0px;
}
ul li{
width: 100%;
background-color: blue;
}
.submenu{
display: none;
}
.display-sublist{
display: block;
}
ul li a{
width: 95%;
background-color: red;
}
.main-test {
display: inline-block;
float: left;
width: 90%;
}
.cancel-test{
display: inline-block;
background-color: green;
float: right;
width: 10%;
}
.expand-button{
clear: both;
display: block;
}
</style>
<body>
<ul>
<li>
<a class="main-test" href="#">Something</a>
<a class="cancel-test">x</div>
<ul class="submenu">
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
</ul>
</li>
<li>
<a class="main-test"href="#">Something</a>
<a class="cancel-test">x</a>
<ul class="submenu">
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
</ul>
</li>
<li>
Something
</li>
<li>
Something
</li>
</ul>
<button onclick="clickFunction()" class="expand-button">Expand</button>
</body>
</html>

How to make child elements bisects the width of their parent element [duplicate]

This question already has an answer here:
How can I have perfectly centered navigation bar with equally wide links?
(1 answer)
Closed 6 years ago.
I wrote a navigation bar like this:
.site-nav ul {
margin: 0;
background-color: #09C;
line-height: 52px;
}
.site-nav li {
display: inline-block;
font-size: 14px;
}
.site-nav a {
display: inline-block;
}
<nav class="site-nav">
<ul>
<li>
首页
</li>
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
</ul>
</nav>
Now I need each <li> tag bisects the width of ul (it's parent). If the count of <li> is known, I can simply give an percentage to <li>,
in this case:
.site-nav li {
width: 25%;
display: inline-block;
font-size: 14px;
}
However, the <li> is dynamically generated ( I use razor), so I don't know how many <li> will generated.
So is there some way to achieve this?
use flexbox, and you can have it dynamic because you are using property flex-grow:1 in child li which will fill the remaining space available
If all items have flex-grow set to 1, the remaining space in the
container will be distributed equally to all children.
Flexbox Tutorial
.site-nav ul {
margin: 0;
background-color: #09C;
line-height: 52px;
display: flex
}
.site-nav li {
flex: 1;
font-size: 14px;
}
<nav class="site-nav">
<ul>
<li>
首页
</li>
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
</ul>
</nav>
You can play with flexbox. Check the snippet below.
.site-nav ul {
display: flex;
margin: 0;
padding: 0;
background-color: #09C;
line-height: 52px;
list-style: none;
}
.site-nav li {
flex-grow: 1;
flex-basis: 0;
font-size: 14px;
border: 1px solid white;
}
.site-nav a {
display: inline-block;
}
<nav class="site-nav">
2 columns:
<ul>
<li>
首页
</li>
<li>
1
</li>
</ul>
4 columns:
<ul>
<li>
首页
</li>
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
</ul>
8 columns:
<ul>
<li>
首页
</li>
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
<li>
首页
</li>
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
</ul>
</nav>

How to change header background image on hover menu

I have been searching for a way to change the background image of my banner when I hover my menu bar.
here you have my html
<div id="container">
<header>
<nav>
<ul>
<li>
home
</li>
<li>
bedrijf
</li>
<li>
diensten
</li>
<li>
foto's
</li>
<li>contacteer ons</li>
</ul>
</nav>
</header>
So what i wanna do is when I hover over one of my buttons the background of header should change to a picture I pick for this page.
I hope this is possible it would be great.
You could do that in jQuery:
$("li > a").hover(function(){
$("header").css("background-image", "img.jpg");
});
This code says if you hover over an element with a-Tag which is in a li-Tag then it changes the background-image of the header to img.jpg.
Now if you want that the background-image changes back if you don't hover then add this code too:
$("li > a").mouseleave(function(){
$("header").css("background-image", "none");
});
You could use pseudo elements (:before) on the LI's when you hover over them.
It's a bit hacky, but does work and without javascript too.
working demo here:
http://jsfiddle.net/ezjjyptk/
Here is a solution I've come up with. Basically, add an empty, absolutely positioned div inside each <li> element, and move it up from behind the header on hover.
JSFiddle
<div id="container">
<header>
<nav>
<ul>
<li>
home
<div style="background:green">
</div>
</li>
<li>
bedrijf
<div style="background:yellow">
</div>
</li>
<li>
diensten
</li>
<li>
foto's
</li>
<li>contacteer ons</li>
</ul>
</nav>
</header>
header {
position: relative;
width: 100%;
background: red;
}
ul li a {
position: relative;
z-index: 3;
}
ul li:hover div {
z-index: 1;
}
ul li div {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
I wouldn't say it's impossible without javascript, but I certainly wouldn't call this code pretty
.fixed {
position: absolute;
top: 50px;
left: 0;
opacity: 0;
transition: opacity .5s ease-in-out;
}
li {
display: inline-block;
}
.show1:hover + #header1 {
opacity: 1;
}
.show2:hover + #header2 {
opacity: 1;
}
.show3:hover + #header3 {
opacity: 1;
}
.show4:hover + #header4 {
opacity: 1;
}
<li>
<a class="show1" href="index.html">Show 1</a>
<img class="fixed" id="header1" src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Color_blocks.svg/800px-Color_blocks.svg.png" />
</li>
<li>
<a class="show2" href="bedrijf.html">Show 2</a>
<img class="fixed" id="header2" src="http://static1.squarespace.com/static/534d1b21e4b077040469bae0/t/5362730de4b04c365eb65a37/1398960956409/The+two+colour+shirt" />
</li>
<li>
<a class="show3" href="diensten.html">Show 3</a>
<img class="fixed" id="header3" src="http://i1.wp.com/makeapowerfulpoint.com/wp-content/uploads/2015/03/Drunk-Tank-Pink.png" />
</li>
<li>
<a class="show4" href="pictures.html">Show 4</a>
<img class="fixed" id="header4" src="http://3.bp.blogspot.com/-YFX5nFtmjiA/T7co_rB3keI/AAAAAAAAI0c/EkNV9tX3xt8/s920/color%2Bwheel%2Bstar%2Bblock%2Blogo%2Bv1.jpg" />
</li>

ul list gets hidden under a div

I've been having this issue in different places where I have a ul list and Im trying to add it on top of a div (image, or a background) but the list does not appear on top. I wonder if it gets sent in the back..i even added z-index
JSFiddle
CSS
.toolbar p{ text-align: right; padding: 10px 180px 0 0; color: #fff; font-size: 26px; z-index: 1; }
.toolbar { width: auto; height: 50px; background-color: #cc1e2c; z-index: 1;}
.social-icons { z-index: 2;}
HTML
<div class="col_full toolbar">
<p>CALL NOW: +1 555.555.1234</p>
<ul class="social-icons">
<li> </li>
<li> </li>
<li> </li>
</ul>
</div>
The reason you aren't seeing anything is because you posted links, without any content. If you added words, the list would appear:
<div class="col_full toolbar">
<p>CALL NOW: +1 555.555.1234</p>
<ul class="social-icons">
<li> Twitter </li>
<li> Instagram</li>
<li> Facebook</li>
</ul>
</div>
On this example I made the height bigger so you could see that the links are in fact on top.
If you want to see the images, you need to use the image tag like so:
<li> <img src="path/to/your/twitter_image.jpg"/> </li>
JSFiddle