Lists that contains multiple items are frequently used in websites, and separating them with commas can help enhance readability and user experience. The conventional method of adding commas to lists is to do add them manually. However as you might have already guessed this can be an arduous and time-consuming process, particularly for long lists. Fortunately, the ability to add commas dynamically to lists of items with CSS is an excellent solution.
~ Selector
在CSS中,~选择器用于选择在HTML DOM中位于所需元素之前的所有元素。
语法
el1~el2 {
css declarations
}
In this context, “el1” symbolizes the preceding component of the fellow components, whereas “el2” represents the subsequent fellow components located in a shared parental unit.
For example, a ~ ul, selects every <ul> element preceded by the <a> element.
::before选择器
The ::before selector is used to insert come content before the selected element.
语法
el::before {
css declarations
}
Here, el is the element that the ::before pseudo-element will be applied to. The ::before pseudo-element is preceded by two colons (::) to distinguish it from the :before pseudo-class, which is an older syntax that is still supported for backward compatibility.
例如,p::before会在<p>元素之前添加内容。
方法
The approach to add commas between a list of items dynamically with CSS involves the use of a pseudo-element called ::before that can insert content before the selected element. In this case, we target the li elements within a ul list and add a comma before each one using the ::before selector. This method allows us to avoid manually adding commas to the list and automate the process with CSS. Additionally, we can use the display and flex-wrap properties to arrange the list items and ensure they wrap to a new line if necessary. Finally, we can use JavaScript to add and remove list items dynamically.
Example
的中文翻译为:
示例
以下代码使用CSS在一组项目之间动态添加逗号。文档包括一个标题,带有CSS属性的样式标签,以及一个包含标题、具有类名为“item”的项目列表和两个用于添加和删除项目的按钮的div标签。样式标签包括一个伪元素选择器,在每个列表项之前(除了第一个)添加逗号和空格。脚本标签定义了两个函数;addItem函数添加一个带有文本“Item!”的新列表项,removeItem函数随机选择一个列表项并将其删除。
<!DOCTYPE html>
<html>
<head>
<style>
.items {
display: flex;
list-style: none;
padding: 0;
flex-wrap: wrap
}
.item~.item::before {
content: ", ";
}
</style>
</head>
<body>
<h4>How to Add Commas Between a List of Items Dynamically with CSS?</h4>
<div>
<ul class="items">
<li class="item">Eggs</li>
<li class="item">Bread</li>
</ul>
</div>
<div>
<button onclick="addItem()">Add Item</button>
<button onclick="removeItem()">Remove Item</button>
</div>
<script>
function removeItem(){
let items=document.querySelectorAll('.item');
let idx=Math.floor(Math.random()*items.length);
items[idx].remove();
}
function addItem(){
let itemList=document.querySelector(".items");
let item=document.createElement("li");
item.innerText="Item!";
item.className="item";
itemList.append(item);
}
</script>
</body>
</html>
结论
总结一下,利用CSS在文章串中动态包含逗号的使用是一种聪明的策略,它能够提高网页的可读性和视觉吸引力。通过利用CSS的少用技能,网页设计师可以解决看似琐碎的问题。通过发挥想象力和探索新的可能性的热情,您可以利用CSS的潜力来创建引人注目和艺术性的壮丽网页设计,给观众留下深刻的印象。因此,请毫不犹豫地尝试各种可用工具和技术,并发挥您的网页开发专业知识的全部潜力。
以上就是如何使用 CSS 在项目列表之间动态添加逗号?的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 如何使用 CSS 在项目列表之间动态添加逗号?
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?