how to create a circle around ordered <li> elements [duplicate] - css

This question already has answers here:
Setting the width of inline elements
(7 answers)
What is the default display property of the :before and :after pseudo-elements?
(1 answer)
Closed 3 years ago.
Alright, this is really a simple question that I can't solve. I have an html document that looks like this:
<ol>
<li> item 1 </li>
<li> item 2 </li>
<li> item 3 </li>
</ol>
and the accompanying css looks like this:
ol {
list-style: none;
counter-reset: li-counter;
}
ol li {
counter-increment: li-counter;
}
ol li::before {
content: counter(li-counter);
color: #59cbbe;
font-weight: bold;
width: 20px;
height: 20px;
border: 2px solid black;
border-radius: 50px;
padding: 4px;
margin: 10px;
}
and here is the codepen
I have tried following and modifying this example, but wasn't able to get it. So I tried this one as well. The main issue that I am having, when you look at the code pen is that the circles are not circles, they are always oblong and oval weather I use 30px or 50% is there something that I am missing. Sorry if this is a really simple answer, but, I am not that great at css.

Well, you were on the right track. You just need to add display: inline-block to the pseudo element so the width:20px; height:20px will have effect. And so the pseudo-element will be a square + rounded border = circle.
inline-block :
Displays an element as an inline-level block container. The element
itself is formatted as an inline element, but you can apply height and
width values
As you can read up here default display of pseudo-elements the :before pseudo-element has display:inline ( just like a span ) which does not accept width and height
from w3schools :
Displays an element as an inline element (like <span>). Any height and
width properties will have no effect
ol {
list-style: none;
counter-reset: li-counter;
}
ol li {
counter-increment: li-counter;
}
ol li::before {
content: counter(li-counter);
color: #59cbbe;
font-weight: bold;
width: 20px;
height: 20px;
border: 2px solid black;
border-radius: 50px;
display: inline-block;
text-align: center;
padding: 4px;
margin: 10px;
}
<ol>
<li> item 1 </li>
<li> item 2 </li>
<li> item 3 </li>
</ol>

Related

How can I set a different value to an element style property when the element is inside a ol or ul tags?

I'm not sure the title is phrased clearly so I'll try explaining here.
In the .Note class, margin-left set to 20px.
I want that when I apply this class inside <ol> or <ul>, margin-left will be set to 0px - only inside these tags not anywhere else.
Is that possible?
This is the CSS and html sample:
.Note
{
width: 98%;
height: 70px;
margin-left: 20px;
font: 16px "Segoe UI";
background-color: #F5F5F5;
background-position: 10px 10px;
padding-left: 80px;
padding-top: 5px;
padding-bottom: 5px;
padding-right: 5px;
border: 1px solid;
}
div.Note
{
background-image: url("../Images/note.png");
background-repeat: no-repeat;
}
<ol>
<li>
<p>[Replace with your text].</p>
<div class="Note">This is my note.</div>
</li>
<li>
<p>[Replace with your text].</p>
</li>
</ol>
You can see that the div is indented 20px inside <li>. I want it to be 0px, while keeping .Note {margin-left: 20px;}.
ul .Note, ol .Note {
/* Selects all elements with the Note class within an <ol> or an <ul> */
}
ul > .Note, ol > .Note {
/* Selects all elements with the Note class IMMEDIATELY within an <ol> or an <ul> */
}
Does this solve your problem? because if it does, you should really check out basic css selectors before you try something serious with css.

How do I vertically align my list items with the bullets?

So I have an unordered list with custom bullet images. They are triangles pointing to the right at the list. I would like the point to be aligned with the vertical center of the first line of text in the list item. How can I achieve this?
This is what I am currently viewing:
<ul>
<li>Photography for events and portraits</li>
<li>Image editing and restoration</li>
<li>Video and audio production</li>
</ul>
main ul {
list-style-image: url(../img/bullet.png);
margin-top: 25px;
}
main ul li {
line-height: 35px;
}
The line-height doesn't seem to do anything.
you can use pseudo-element before \ after instead, take a look at this example below:
main ul {
margin-top: 25px;
}
main ul li {
list-style: none;
}
main ul li:before {
content: url("http://www.milksmarter.co.nz/images/basement_platform/grey_triangle_bullet_point_large.png");
position: relative;
top: 10px;
left: -10px
}
<main>
<ul>
<li>Photography for events and portraits</li>
<li>Image editing and restoration</li>
<li>Video and audio production</li>
</ul>
</main>
It's really hard to actually provide you with finalized code without access to your image, but try merging the following code with your own. The first Padding value (currently 3px) should be the item you need to update.
li {
background: url(images/bullet.gif) no-repeat left top;
padding: 3px 0px 3px 10px;
/* reset styles (optional): */
list-style: none;
margin: 0;
}
src: Adjust list style image position?

Pure css tree with borders

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

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 Centering Child Regardless of Padding/Margin

Basically, I have this website:
http://www.ug.it.usyd.edu.au/~sgre9702/week3/dropDownMenu/semantics.html
I want to centre the drop-down list items on the nav-bar, I know I can centre it with:
left:-11px;
However, I don't want to use a value I have calculated. Instead I would like it to automatically centre, taking the margin/padding values into consideration. I don't know if this is possible after googling around a bit.
My related HTML code is:
<nav>
<ul id="nav">
<li>Tours
<ul>
<li>New South Wales
<li>Australian Capital Territory
<li>Queensland
<li>Western Australia
<li>Northen Territory
<li>Tasmania
<li>South Australia
<li>Victoria
</ul>
<li>Attractions
<li>Food
<li>Resources
<li>About
<li>Contact
<ul>
<li>Online
<li>Phone
<li>Facimile
</ul>
</ul>
</nav>
The related CSS:
/* general nav list */
nav ul li {
background-color: #EEEEEE;
border-color: #000000;
border-style: solid;
border-width: 1px;
display: inline-block;
margin: 0px 5px;
padding: 5px;
position: relative;
text-align: center;
width: 120px;
}
/* nav sub list */
nav ul li ul {
display: none;
}
/* nav sub list shown */
nav ul li:hover ul {
display: block;
width: 142px;
position: absolute;
list-style-type: none;
}
/* nav sub list shown - list item */
nav ul li:hover ul li {
display:block;
background-color: #AACCFF;
border: solid 1px #000000;
position: relative;
/*left:-11px;*/
}
Remove the padding from the ul>li elements and apply to the anchors themselves (they will need display:block). Remove the margins from the sub-li elements
Then give the child ul width: auto. The submenu block will still be offset 1px to the right, though, as it will takes its left edge from where its parent's left border ends. You can get around that by either replacing the borders without outlines (which don't effect the widths of their host elements), putting borders on the child anchor/li elements or finally trying a left:-1px value on the child UL.

Resources