Pure css tree with borders - css

I am trying to create a tree with indentations in pure CSS. I have been trying using something like:
ul.tree ul {
padding-left: 5px;
}
However I would like to have a separation between each item in the list. If I use the code above the separating bar gets indented as well so it's not too good.
Here is my current code (I do the indent directly in js, which I don't like): jsfiddle
Ultimately, I want to create something that basically looks like that:
Any idea how to do this in pure CSS? kudos for the simplest answers.

Simple with Multi-level Depth Support
UPDATED: Tweaked to accommodate hover
No extra HTML needed, no having to limit depth because of css selector chaining, as it supports any number of levels deep without having to adjust your css at all for those levels (no keeping track of "padding" to set on the next level deep).
This works well with only a two minor limitations (which I don't believe will factor into affecting you).
See fiddle demo.
Add a position: relative to your ul.tree, but keep all the child elements the default static position. Then change/add the following css:
ul.tree a {
display: block;
height:30px;
line-height: 30px;
padding-left: 15px;
}
/* this is making our bottom border, but sizing off the .tree ul width */
ul.tree a:before {
content: '';
height: 30px; /* match your <a> height */
position: absolute;
left: 0;
right: 0;
z-index: -1;
border-bottom-width: 1px;
border-bottom-color: lightgray;
border-bottom-style: solid;
}
ul.tree a + ul {
padding-left: 15px; /* this is your spacing for each level */
}
ul.tree a:hover:before {
background-color: #DDDDDD;
}
The limitations are that no child elements can have a position set and we are using a pseudo-element (which means it cannot be used for some other feature, but that is probably not an issue either).

For lists with unknown depths, I've used an absolutely positioned element for separating lines. It adds a little extra markup, but seems to work.
div.separator {
position:absolute;
left:0px;
right:0px;
border-top:1px solid lightgray;
}
<ul class="tree">
<li><a>Item1</a><div class="separator"></div></li>
<li><a>Item2</a><div class="separator"></div>
<ul>
<li><a>Item3</a><div class="separator"></div></li>
<li><a>Item4</a><div class="separator"></div></li>
<li><a>Item5</a><div class="separator"></div>
<ul>
<li><a>Item6</a><div class="separator"></div></li>
</ul>
</li>
</ul>
</li>
</ul>
http://jsfiddle.net/7u87c/20/

This CSS makes the link inside a nested li have a padding-left of 30px, and I add another nested li link have padding-left: 60px.
ul.tree li ul li a {
padding-left: 30px;
}
ul.tree li ul li ul li a {
padding-left: 60px;
}
http://jsfiddle.net/7u87c/5/

No extra markup and use of icon image.
Pretty simple and dynamic based on the content.
Sample HTML:
<ul class="tree">
<li><span>public</span></li>
<li><span>server.js</span></li>
<li>
<span>server</span>
<ul>
<li><span>webfs</span></li>
</ul>
</li>
<li><span>specs</span></li>
<li>
<span>src</span>
<ul>
<li>
<span>core</span>
<ul>
<li><span>CellAddress.js</span></li>
</ul>
</li>
</ul>
</li>
</ul>
CSS:
ul.tree {
border-top: 1px solid grey;
}
ul.tree, ul.tree ul {
padding: 0;
margin: 0;
list-style: none;
}
ul span {
display: block;
padding-left: 25px;
border-bottom: 1px solid #666;
height: 25px;
line-height: 25px;
background: url("http://lorempixel.com/10/8/") no-repeat scroll 5px 8px transparent;
}
ul ul span {
padding-left: 35px;
background-position: 15px 8px;
}
ul ul ul span {
padding-left: 45px;
background-position: 25px 8px;
}
Please see example
Note: You can convert the spans into a tags

Related

How to scale my css/html dropdown menu?

I quite new to programming, as far as html/css is programming. I'm trying to make a website for my own company (in dutch).
I want a nice looking drop-down menu but it won't scale with the page. I have tried using different codes from some sites, but when the page is on half screen it never looks nice and doens't work normally, I was hoping that one of you could help me with this problem, if it is possible at all.
The site i got a part of the code from:
https://medialoot.com/blog/how-to-create-a-responsive-navigation-menu-using-only-css/
My html(5) code:
<nav>
<ul >
<li>Home</li>
<li>
Uitzoeken
<ul
<li>Desktops</li>
<li>Laptops</li>
<li>Producenten</li>
<li>Smartphones</li>
<li>Tablets</li>
<li>Overige</li>
</ul>
</li>
<li>Installeren</li>
<li><a href="Software/Software.html" >Software</a>
<ul>
<li>Basis</li>
<li>Gevorderd
</ul>
<li>Social Media</li>
<li>Problemen</li>
<li>Contact</li>
</ul>
</nav>
My Css code:
nav ul ul {
display : none;
}
nav ul li:hover > ul {
display : block;
}
nav ul {
background: #18c006;
background: linear-gradient(top, #18c0060%, #189c06 100%);
background: -moz-linear-gradient(top, #18c006 0%, #189c06 100%);
background: -webkit-linear-gradient(top, #18c006 0%,#189c06 100%);
box-shadow : 0px 0px 9px rgba(0,0,0,0.15);
padding : 0 20px;
border-radius : 10px;
list-style : none;
position : relative;
display : inline-table;
}
nav ul:after {
content : "";
clear : both;
display : block;
}
nav ul li {
float : left;
}
nav ul li:hover a {
color : #810a11;
}
nav ul li a {
display : block;
padding : 25px 40px;
color : #a60a11;
text-decoration : none;
}
nav ul ul {
background : #18c006;
border-radius : 0px;
padding : 0;
position : absolute;
top : 100%;
}
nav ul ul li {
float : none;
position : relative;
}
nav ul ul li a {
padding : 15px 40px;
color : #a60a11;
}
nav ul ul li a:hover {
background : #18c006;
}
So your new code is :
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
Hope it help.
I want a nice looking drop-down menu but it won't scale with the page.
I have tried using different codes from some sites, but when the page
is on half screen it never looks nice and doesn't work normally, I was
hoping that one of you could help me with this problem, if it is
possible at all.
Your request is vague, so it means we have to guess at your needs. Do you mean, when the browser window is very narrow, you don't like how that particular (from the article) menu transforms? Do you wish to keep the horizontal bar, with a drop-down menu, even in the most narrow view (e.g. 300px wide)?
If that's the case, and your technical skills are low, then continue looking at other articles and demos until you find something you like. However, you will find, most modern menu systems and themes will have some kind of menu system which transforms anything less than say 500-600px wide screens. Search google for "Responsive design".
If you want to tweak the code yourself, technically you should be investigating CSS media queries. You can use them to setup conditional rules for different display sizes.

Working with centering floated li elements with no width in CSS

I'm trying to work with an issue I've had before with CSS with Wordpress and would like to get an extra few sets of eyes on it to see if in fact what I'm doing is the best way for it or is there is a better way.
I'm setting a social media section within my main in the header and footer of my website. My "social media bar" is however not a set size as it is a wordpress website and there may a few more socialmedia buttons added to it. This is the basis of my code:
This is how it appears in my header and footer.
<li id="header-widget-area">
<ul class="icons-medium">
<li class="site-icon"><a target="_blank" href="#">Icon 1</a></li>
<li class="site-icon"><a target="_blank" href="#">Icon 2</a></li>
<li class="site-icon"><a target="_blank" href="#">Icon 3</a></li>
<li class="site-icon"><a target="_blank" href="#">Icon 4</a></li>
</ul>
</li>
#header-widget-area ul {
float: right;
padding: 0;
width: 300px;
border: 1px solid red;
}
#header-widget-area ul.icons-medium li, #footer-widget-area-right ul.icons-medium li {
background: none repeat scroll 0 0 transparent;
float: right;
padding: 0;
width: 60px;
list-style-type: none;
}
My jsfiddle - http://jsfiddle.net/nejsgyp3/
I want to have it floated to the right to align with some boxes and content I have there for both my header and footer (easy peasy!) but then for media queries I'd like to have the element centered and social media icons inside to be centered as well. So I've done this but I still have to keep a width on this or it won't center.
Added slight modifications to CSS for this
#header-widget-area ul {
float: none;
padding: 0;
width: 300px;
border: 1px solid red;
overflow:auto;
margin: 0 auto;
}
#header-widget-area ul.icons-medium li, #footer-widget-area-right ul.icons-medium li {
background: none repeat scroll 0 0 transparent;
float: left;
padding: 0;
width: 60px;
list-style-type: none;
}
My jsfiddle - http://jsfiddle.net/wswvokpu/
So my goal is to allow for more to be added but not have that extra space to the right in my media queries so this is properly centers. Also, is there a way for my "Icon 1" to remain positioned to the right in the media query? Or is the only way to do what I'm trying to do is to always have a set width and then when a new icon is added it would follow suit underneath as long as I keep the height auto? Which would then mean if a new icon was added to the header the height of the would expand thus pushing the box that is below it down?
Thanks in advance!
I think I understand what you are asking. Does this look right to you?
http://jsfiddle.net/nejsgyp3/1/
I have given the list elements a width of 25% each, floated left with centered text. (if you plan on using borders you will also need to set box-sizing:border-box as well - with the various vendor prefixes).
I have also centered the UL which I believe you are trying to do in your media queries.
CSS:
#media only screen and (max-width:600px) {
ul {
list-style:none;
padding:0px;
margin:0px;
}
#header-widget-area ul {
float: none;
padding: 0;
width: 300px;
border: 1px solid red;
margin:0 auto;
list-style:none;
}
#header-widget-area ul:after {
content: " ";
display:block;
height:0px;
clear:both;
float:none;
}
#header-widget-area ul.icons-medium li, #footer-widget-area-right ul.icons-medium li {
background: none repeat scroll 0 0 transparent;
float: left;
padding: 0;
width: 25%;
list-style: none;
text-align:center
}
}

ol numbers underneath li text [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to figure out how to style items in my ol. My goal is for the end result to have the ol horizontal with the numbers under left aligned under the li text.
Example of how end result should look:
Item Item Item Item Item
1 2 3 4 5
Here is one way of realizing this design using pseudo-elements to position a custom counter.
You can start with either an ordered or an unordered list:
<ol class="list">
<li>Apples</li>
<li>Bananas</li>
<li>Cherries</li>
<li>Durian</li>
</ol>
apply the following CSS:
.list {
counter-reset:itemcounter;
margin: 0;
padding: 0;
}
.list li {
border: 1px dotted gray;
margin-right: 30px;
float: left;
list-style: none;
}
.list li:after {
content:counter(itemcounter);
color:#000;
counter-increment: itemcounter;
display: block;
background-color: pink;
padding: 0px;
text-align: left;
padding-right: 5px;
}
Define a custom counter and position it in a pseudo-element li:after.
I floated the list items to get the horizontal menu but you can also use inline-blocks.
See demo at: http://jsfiddle.net/audetwebdesign/DgZvg/
Backwards Compatibility
The limiting design factor here is the custom counter, which is supported from IE8 going forward: http://caniuse.com/#feat=css-counters
Alternative For Older Browsers
For older browsers that don't support counters and generated content, you could try something like the following.
You need to add a wrapper element on your list content:
<ol class="list">
<li><p>Apples</p></li>
<li><p>Bananas</p></li>
<li><p>Cherries</p></li>
<li><p>Durians</p></li>
</ol>
and then apply the following CSS:
.list {
margin: 0;
padding: 0;
}
.list li {
list-style-position: inside;
border: 1px dotted gray;
margin-right: 30px;
padding-left: 5px; /* gives you some control... */
float: left;
padding-top: 20px;
line-height: 20px;
width: 100px;
position: relative;
}
.list li p {
height: 20px;
margin: 0;
position: absolute;
bottom: 20px;
left: 0;
}
In this case, you list-style-position: inside, and then define a two-line text region by specifying a line-height of 20px and padding-top of 20px. You also need to specify a width on the list items.
You then set the height to the inner wrapper and then use absolute positioning to offset the bottom by 20px.
This is much more work, a bit constrained because of the width and height lengths,
but it is a proof-of-concept.
See demo at: http://jsfiddle.net/audetwebdesign/PUH9V/
And here is my way, with CSS 2.x and cross browser :)
HTML
<ol>
<li><span>item</span></li>
<li><span>item</span></li>
<li><span>item</span></li>
<li><span>item</span></li>
<li><span>item</span></li>
</ol>
CSS
ol li {
position:relative;
min-width:100px;
padding:30px 0 0;
float:left;
}
ol li span {
position:absolute;
left:-30px;
top:0px;
}
Working: http://jsfiddle.net/shekhardesigner/fuLjk/
I used a span inside the li to position the text above the element. You may need to adjust the CSS to work better in your actual environment.
http://jsfiddle.net/j5Nbx/2/
HTML
<ol class="example">
<li>
<span>Item</span>
</li>
<li>
<span>Item</span>
</li>
<li>
<span>Item</span>
</li>
<li>
<span>Item</span>
</li>
<li>
<span>Item</span>
</li>
</ol>
CSS
.example li {
float:left;
padding:10px;
}
.example:after{
content:"";
display:block;
clear:both;
}
.example li span {
position:relative;
left:-35px;
top:-20px;
}

CSS Menu Multiple Hover Colo(u)rs

Apologies for what is probably quite a basic question, but I've not found a solution to this online.
I have a simple CSS menu, here's the CSS:
#nav {
width: 100%;
float: left;
margin: 0 0 3em 0;
padding: 0;
list-style: none;
opacity:1;
border-bottom: 1px solid #ccc;
border-top: 1px solid #ccc;
}
#nav li {
float: left;
}
#nav li a {
display: block;
padding: 8px 15px;
text-decoration: none;
font-weight: bold;
color: #069;
border-right: 1px solid #ccc;
}
#nav li a:hover {
color: #c00;
background-color: #fff;
}
/* End navigation bar styling. */
This is from an online tutorial, so not my code.
Here's the HTML:
<!-- language: lang-html -->
<ul id="nav">
<li>About Us</li>
<li>Our Products</li>
<li>FAQs</li>
<li>Contact</li>
<li>Login</li>
</ul>
All I want to do is have different colo(u)rs for each menu item when hovered over.
I assume you need to create a separate id (or class) for each item, but I am unsure of the syntax and no matter what I try it simply won't work.
Many thanks for any assistance.
add a class to the href links and then in your css call the hover state and then style accordingly. Here is an example using your code: http://jsfiddle.net/LGL37/
The HTML:
TEXT
The CSS
.about:hover { background: yellow; }
EDIT: this is a much better solution than the other answer as it is cross browser compatible and if you need to style more in the future you'll have individual classes to target rather than nth which can get confusing.
If you don't use the :nth-child() selector, you can add a unique class to each li in the nav
<li class="about"></li>
and set a hover effect in your stylesheet for that specific class
#nav li.about a:hover { background-color: red; }
You can use :nth-child selector but it won't work in some legacy versions of IE.
JsFiddle
#nav li:nth-child(1) a:hover {
color:green;
}
#nav li:nth-child(2) a:hover {
color:blue;
}
etc.
You could use nth-child:
li:nth-child(2) a:hover{
color: red;
}
http://jsfiddle.net/fAbFg/
This example affects the second item.

How to set <li> Background color?

I believe it's simple, but since I'm new to this I don't have a clue of how to do it. I just want to change the background color of a li tag - just for fashioning, nothing else.
This is my HTML:
<ul id="abas">
<li>PROGRAM</li>
<li>PROC</li>
<li>DDNAME</li>
</ul>
Sorry for being a noob but, this is the css part right?
#abas li a
{
text-decoration:none;
background-color:3B31FF;
color:#FFFFFF;
float:left;
margin-right:20px;
border-top-left-radius:23px;
border-top-right-radius:0px;
-moz-border-radius-topleft:5px;
-moz-border-radius-topright:5px;
-webkit-border-radius-topleft:5px;
-webkit-border-radius-topright:5px;
border-bottom-left-radius:0px;
border-bottom-right-radius:0px;
-moz-border-radius-bottomleft:5px;
-moz-border-radius-bottomright:5px;
-webkit-border-radius-bottomleft:5px;
-webkit-border-radius-bottomright:5px;
padding-top: 10px;
padding-right: 100px;
padding-bottom: 10px;
padding-left: 10px;
}
I noticed that here>>> "background-color:3B31FF;" is where I change the
color of the background, but doing this, changes all the background colors of course
... I only need 1 "li" tab to change and any html tutorial would be nice too.
Css code:
#abas li {
background-color: ... ;
}
fill in color code where dots are, like this:
background-color:#000000; //color black
Single tag:
Css code:
li.selected {
background-color: ... ;
}
Html code:
<ul>
<li></li>
<li class="selected"></li>
<li></li>
</ul>
First any css color code needs to have # followed by a 6 digit value(or 3 if they are repeating i.e #FF33FF as #F3F) and to solve your second part do this
CSS
#abas li {
background-color: #xxxxxx ;
//your other style goes here
}
#abas li.current {
background-color: #xxxxxx ;
//your other style goes here
}
HTML
<ul id="abas">
<li class="current">PROGRAM</li>
<li>PROC</li>
<li>DDNAME</li>
</ul>
To change the background color simply style it:
<li style="background-color:blue;">Program</li>
You will likely also want to set some height and width parameters.
This will make the first item have a red background:
<li style="background: red">PROGRAM</li>
If you want to for example add green to a <li> tag you can do the following:
<li style="background: green;">PROGRAM</li>
But this isn't really best practice because normally you want to keep your HTML and CSS separated. So in CSS you would do it like this:
li { background: green; }
or use hex color codes:
li { background: #00ff00; }
If you only want to change one specific <li> tag you can add a class to it:
<li class="precious">
and then apply a css rule to this class:
.precious { background: #00ff00; }
and only this <li> tag with the .precious class is going to get styled.
Live Example: http://jsfiddle.net/pulleasy/WEdmt/
You can also make your life a whole lot easier with the border-radius element. for what you are doing it would be:
#abas li a {
text-decoration: none;
background-color: 3B31FF;
color: black;
float: left;
margin-right: 20px;
border-radius: 23px 0px 0px 0px;
padding-top: 10px;
padding-right: 100px;
padding-bottom: 10px;
padding-left: 10px;
}
This will give you the same result. Also for example sake, you will need to add a height and a width to get some sort of result. so if that were the case you would need to do this:
#abas li a {
text-decoration: none;
background-color: 3B31FF;
color: black;
float: left;
margin-right: 20px;
border-radius: 23px 0px 0px 0px;
padding-top: 10px;
padding-right: 100px;
padding-bottom: 10px;
padding-left: 10px;
height: 100px;
width: 100px;
}
This will give you the result that I think you were looking for. If you are looking to use pixels instead of percents for a fluid layout, the you will need to use this. (Note this is only for the width, height and positioning).
#abas li a {
text-decoration: none;
background-color: 3B31FF;
color: black;
margin-right: 20px;
border-radius: 23px 0px 0px 0px;
padding-top: 10px;
padding-right: 100px;
padding-bottom: 10px;
padding-left: 10px;
position: absolute;
height: 10%;
width: 10%; /*Replace these percentiles with your width and height*/
}
I will assume that you know how to make the
An alternative to using hex code is using RGB / RGBA:
background-color:rgb(255,0,0);
background-color:rgba(255,0,0,0.5);
This gives you even more control over your color by adding alpha and transparency support, but unfortunately, it's not supported by some browsers (IE, namely, although I don't know about IE 10).

Resources