Is it possible to use CSS counter in combination with flexbox order style? I want achieve an effect when counter number is the same like element order.
Counters update when some of elements have "display: none" property so I thought that maybe it works also with flexbox order.
ol {
list-style: none;
display: flex;
counter-reset: section;
}
li {
width: 50px;
height: 50px;
}
li::before {
counter-increment: section;
content: counter(section);
}
<ol>
<li style="background-color: wheat; order: 2"></li> <!-- should display 2 -->
<li style="background-color: olive; order: 3"></li> <!-- should display 3 -->
<li style="background-color: purple; order: 1"></li> <!-- should sisplay 1 -->
</ol>
https://jsfiddle.net/esx9j0hm/9/
After reading this MDN article I figured that you can't show css property values in content, but there's a couple of ways to do this.
1. Use data-attributes
Add data-attributes to li elements and show them in content:
ol {
list-style: none;
display: flex;
}
li {
width: 50px;
height: 50px;
}
li::before {
content: attr(data-order);
}
<ol>
<li style="background-color: wheat; order: 2" data-order="2"></li> <!-- should display 2 -->
<li style="background-color: olive; order: 3" data-order="3"></li> <!-- should display 3 -->
<li style="background-color: lightblue; order: 1" data-order="1"></li> <!-- should sisplay 1 -->
</ol>
2. Use JS
Use js to get style of li element and set it's content accordingly:
let elements = document.querySelectorAll('ol li');
Array.prototype.forEach.call(elements, function(el, i) {
let order = getComputedStyle(el)['order'];
el.textContent = order;
});
ol {
list-style: none;
display: flex;
}
li {
width: 50px;
height: 50px;
}
<ol>
<li style="background-color: wheat; order: 2"></li> <!-- should display 2 -->
<li style="background-color: olive; order: 3"></li> <!-- should display 3 -->
<li style="background-color: lightblue; order: 1"></li> <!-- should sisplay 1 -->
</ol>
I have a CSS multicolumn: it is fine in Firefox (<ul> content is not 'broken') but not on Chrome (some items that should be on first column are splitted into second). Looking around I found this solution, but it doesn't seem to work anywhere I place the code.
Here is a full snippet:
.margin-bottom-0 { margin-bottom: 0; }
.double > ul {
columns: 2;
padding: 0;
}
.double > ul ul {
padding-left: 10px;
list-style: circle;
}
.double > ul ul li {
padding-left: 5px;
}
.double li {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
list-style-position: inside;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-8">
<div class="double">
<ul class="list-unstyled margin-bottom-0">
<li>Area docenti
<ul>
<li>Docenti e classi</li>
</ul>
</li>
<li>Area segreteria
<ul>
<li>Organigramma</li>
<li>D. S. G. A.</li>
<li>U. R. P.</li>
<li>Comunicazioni interne personale ATA</li>
<li>Programma annuale</li>
<li>Modulistica</li>
<li>SIDI - Miur</li>
</ul>
</li>
<li>Area genitori
<ul>
<li>Rappresentanti dei genitori</li>
<li>Ricevimento docenti</li>
<li>Patto di corresponsabilità</li>
<li>Libri di testo</li>
<li>Siti tematici</li>
<li>Iscrizioni On Line</li>
</ul>
</li>
<li>Area alunni
<ul>
<li>Viaggi di istruzione</li>
<li>Materiale didattico</li>
<li>Concorsi</li>
<li>Visite guidate</li>
<li>Attività e progetti</li>
</ul>
</li>
<li>Il Dirigente</li>
<li>Organi collegiali</li>
<li>Piano di lavoro
<ul>
<li>POF</li>
<li>PTOF</li>
<li>PON</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
You see, "Siti tematici" and "Iscrizione On Line" should be on the first column, right after "Libri di testo" (they are part of the same <ul>): instead, they go on the second column.
Looking further I discovered that overflow: hidden; (that I need for ellipsis) is what's causing the correct behavior in Firefox and the wrong one on Chrome: if I remove that line, both Firefox and Chrome show the (wrong) 'broken' <ul>, but at least they are consistent...
Any idea to have the columns broken in the right way mantaining the ellipsis, please?
You should use break-inside:avoid on the li
See more here > break inside
And code snippet below :
.margin-bottom-0 {
margin-bottom: 0;
}
.double >ul {
columns: 2;
padding: 0;
}
.double > ul ul {
padding-left: 10px;
list-style: circle;
}
.double > ul ul li {
padding-left: 5px;
}
.double li {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
list-style-position: inside;
-webkit-column-break-inside: avoid;
/* Chrome, Safari, Opera */
page-break-inside: avoid;
/* Firefox */
break-inside: avoid;
/* IE 10+ */
}
<div class="container">
<div class="row">
<div class="col-xs-8">
<div class="double">
<ul class="list-unstyled margin-bottom-0">
<li>Area docenti
<ul>
<li>Docenti e classi</li>
</ul>
</li>
<li>Area segreteria
<ul>
<li>Organigramma</li>
<li>D. S. G. A.</li>
<li>U. R. P.</li>
<li>Comunicazioni interne personale ATA</li>
<li>Programma annuale</li>
<li>Modulistica</li>
<li>SIDI - Miur</li>
</ul>
</li>
<li>Area genitori
<ul>
<li>Rappresentanti dei genitori</li>
<li>Ricevimento docenti</li>
<li>Patto di corresponsabilità</li>
<li>Libri di testo</li>
<li>Siti tematici</li>
<li>Iscrizioni On Line</li>
</ul>
</li>
<li>Area alunni
<ul>
<li>Viaggi di istruzione</li>
<li>Materiale didattico</li>
<li>Concorsi</li>
<li>Visite guidate</li>
<li>Attività e progetti</li>
</ul>
</li>
<li>Il Dirigente</li>
<li>Organi collegiali</li>
<li>Piano di lavoro
<ul>
<li>POF</li>
<li>PTOF</li>
<li>PON</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
I'm trying to adapt this jsfiddle to work without radio button since I cannot use any <form> related tags, and neither javascript!
I "transformed" the <input type='radio'> into <a> tags, and transform the :checked pseudo class into :target
as you can see in this CodePen.
but it does not work :-(
And also solution I used to show first Tab is not usable
Can suggest what's wrong?
Thanks
Joe
Alright, using the :target pseudo-class we can achieve this.
EDIT: I added a wrapper div so you can use position absolute on the panels. This allows you to have the first panel open and switch between them.
.wrapper {
position: relative;
}
.tab-container {
display: none;
position: absolute;
top: 0;
left: 0;
background: red;
}
.tab-container:first-child { display: block }
:target { display: block }
/* just for demo */
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
display: inline-block;
margin-right: 1rem;
}
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
<div class="wrapper">
<div id="tab-1-container" class="tab-container">
Tab 1 content
</div>
<div id="tab-2-container" class="tab-container">
Tab 2 content
</div>
<div id="tab-3-container" class="tab-container">
Tab 3 content
</div>
</div>
With the following simplified code,
$("[data-position]").each(function() {
$(this).css({
position: 'absolute',
left: $(this).data('position')[0] + '%',
top: $(this).data('position')[1] + '%',
})
});
ul {position: relative;
width: 15em;height: 15em;
background: #0af;
list-style: none}
strong,em {padding: .5em}
strong {background: #000; color: #fff;z-index:50}
em {display: none;background: #fff;z-index:-1}
li:hover em{display: block}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li data-position="[20,20]">
<strong>A</strong>
<em>This is an ay</em>
</li>
<li data-position="[10,10]">
<strong>B</strong>
<em>This is a bee</em>
</li>
<li data-position="[30,30]">
<strong>C</strong>
<em>This is a see</em>
</li>
</ul>
The :hover-displayed element gets hidden by following siblings.
I tried to look at many similar questions and answers on this website but could not come up with an interesting answer. Changing the z-index or opacity levels do not work.
What could bring a hover-displayed element to the very top of z-index, whatever the position of siblings in the dom?
Go through the link to get the solution
em {
background: #fff none repeat scroll 0 0;
display: none;
position: relative;
z-index: 1000;
}
You need to add z-index:1;position:relative in em class.
$("[data-position]").each(function() {
$(this).css({
position: 'absolute',
left: $(this).data('position')[0] + '%',
top: $(this).data('position')[1] + '%',
})
});
ul {position: relative;
width: 15em;height: 15em;
background: #0af;
list-style: none}
strong,em {padding: .5em}
strong {background: #000; color: #fff;z-index:50}
em {display: none;background: #fff;z-index:1;position:relative}
li:hover em{display: block}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li data-position="[20,20]">
<strong>A</strong>
<em>This is an ay</em>
</li>
<li data-position="[10,10]">
<strong>B</strong>
<em>This is a bee</em>
</li>
<li data-position="[30,30]">
<strong>C</strong>
<em>This is a see</em>
</li>
</ul>
Is there any CSS selector to attach some style to the numerical part of an ordered list only?
I have HTML like:
<ol>
<li>a</li>
<li>b</li>
<li>c</li>
</ol>
Which should output:
1.a
2.b
3.c
I need to make 1., 2. and 3. bold, while leaving a, b, and c regular.
I am aware of the <span> workaround...
using The Counter-increment CSS property
ol {
margin:0 0 1.5em;
padding:0;
counter-reset:item;
}
ol>li {
margin:0;
padding:0 0 0 2em;
text-indent:-2em;
list-style-type:none;
counter-increment:item;
}
ol>li:before {
display:inline-block;
width:1.5em;
padding-right:0.5em;
font-weight:bold;
text-align:right;
content:counter(item) ".";
}
<ol>
<li>a</li>
<li>b</li>
<li>c</li>
</ol>
A new ::marker pseudo-element selector has been added to CSS Pseudo-Elements Level 4, which makes styling list item numbers in bold as simple as
ol > li::marker {
font-weight: bold;
}
It is currently supported by Firefox 68+, Chrome/Edge 86+, and Safari 11.1+.
Another easy possibility would be to wrap the list item content into a <p>, style the <li> as bold and the <p> as regular. This would be also preferable from an IA point of view, especially when <li>s can contain sub-lists (to avoid mixing text nodes with block level elements).
Full example for your convenience:
<html>
<head>
<title>Ordered list items with bold numbers</title>
<style>
ol li {
font-weight:bold;
}
li > p {
font-weight:normal;
}
</style>
</head>
<body>
<ol>
<li>
<p>List Item 1</p>
</li>
<li>
<p>Liste Item 2</p>
<ol>
<li>
<p>Sub List Item 1</p>
</li>
<li>
<p>Sub List Item 2</p>
</li>
</ol>
</li>
<li>
<p>List Item 3.</p>
</li>
</ol>
</body>
</html>
If you prefer a more generic approach (that would also cover other scenarios like <li>s with descendants other than <p>, you might want to use li > * instead of li > p:
<html>
<head>
<title>Ordered list items with bold numbers</title>
<style>
ol li {
font-weight:bold;
}
li > * {
font-weight:normal;
}
</style>
</head>
<body>
<ol>
<li>
<p>List Item 1</p>
</li>
<li>
<p>Liste Item 2</p>
<ol>
<li>
<p>Sub List Item 1</p>
</li>
<li>
<p>Sub List Item 2</p>
</li>
</ol>
</li>
<li>
<p>List Item 3.</p>
</li>
<li>
<code>List Item 4.</code>
</li>
</ol>
</body>
</html>
(Check the list item 4 here which is ol/li/code and not ol/li/p/code here.)
Just make sure to use the selector li > * and not li *, if you only want to style block level descendants as regular, but not also inlines like "foo <strong>bold word</strong> foo."
JSFiddle:
ol {
counter-reset: item;
}
ol li { display: block }
ol li:before {
content: counter(item) ". ";
counter-increment: item;
font-weight: bold;
}
I had a similar issue while writing a newsletter. So I had to inline the style this way:
<ol>
<li style="font-weight:bold"><span style="font-weight:normal">something</span></li>
<li style="font-weight:bold"><span style="font-weight:normal">something</span></li>
<li style="font-weight:bold"><span style="font-weight:normal">something</span></li>
</ol>
Answer https://stackoverflow.com/a/21369918/2526049 from dcodesmith has a side effect that turns all types of lists numeric.
<ol type="a"> will show 1. 2. 3. 4. rather than a. b. c. d.
ol {
margin: 0 0 1.5em;
padding: 0;
counter-reset: item;
}
ol > li {
margin: 0;
padding: 0 0 0 2em;
text-indent: -2em;
list-style-type: none;
counter-increment: item;
}
ol > li:before {
display: inline-block;
width: 1em;
padding-right: 0.5em;
font-weight: bold;
text-align: right;
content: counter(item) ".";
}
/* Add support for non-numeric lists */
ol[type="a"] > li:before {
content: counter(item, lower-alpha) ".";
}
ol[type="i"] > li:before {
content: counter(item, lower-roman) ".";
}
The above code adds support for lowercase letters, lowercase roman numerals. At the time of writing browsers do not differentiate between upper and lower case selectors for type so you can only pick uppercase or lowercase for your alternate ol types I guess.
If you are using Bootstrap 4:
<ol class="font-weight-bold">
<li><span class="font-weight-light">Curabitur aliquet quam id dui posuere blandit.</span></li>
<li><span class="font-weight-light">Curabitur aliquet quam id dui posuere blandit.</span></li>
</ol>
This is an update for dcodesmith's answer https://stackoverflow.com/a/21369918/1668200
The proposed solution also works when the text is longer (i.e. the lines need to wrap): Updated Fiddle
When you're using a grid system, you might need to do one of the following (at least this is true for Foundation 6 - couldn't reproduce it in the Fiddle):
Add box-sizing:content-box; to the list or its container
OR change text-indent:-2em; to -1.5em
P.S.: I wanted to add this as an edit to the original answer, but it was rejected.
You also could put <span style="font-weight:normal"> around a,b,c and then bold the ul in the CSS.
Example
ul {
font-weight: bold;
}
<ul><li><span style="font-weight:normal">a</span></li></ul>
Pretty late answer, but hopefully someone will find this useful
I had a situation like this:
List item
a. List item
Where the first item was <strong>, the sub-element was normal weight and the '1.' just wouldn't bold.
My solution was via jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
if ($('ol').has('li').has('strong')) {
$('ol ').css('font-weight', 'bold');
$('ol > li > ol').css('font-weight', 'normal');
}
});
</script>
Hopefully this helps someone!