如何在CSS中控制列表样式类型光盘的大小?
首先是我的 HTML:
<ul class="moreLinks" >
<div>More from Travelandleisure.com</div>
<li><a rel="nofollow" href="">xyz1</a></li>
<li><a rel="nofollow" href="">xyz1</a></li>
<li><a rel="nofollow" href="">xyz1</a></li>
</ul>
我知道我可以在非常小的 li 上应用字体大小,以便光盘对我来说看起来正确,然后将 css 应用到 li 内的“a”。但我不知道为什么这在我正在工作的网站上不起作用。我无法直接控制html。
我看到当我这样做时:
.farParentDiv ul li {
list-style: disc inside none;
}
TO this:
.farParentDiv ul li {
list-style-type: disc;
font-size:10px;
}
现在将字体大小应用于“a”后,它可以在 Firebug 中工作。但来自我的 css 文件。我尝试了很多但很累。我可以在 css 文件中覆盖上面的内容,但不能像在 firebug 中那样直接更改它。请写出可能是什么问题?
我曾经在内部链接之前放置点 (.),然后在其上应用 css 来控制光盘大小,但正如我所说,我无法控制 HTML。
My HTML first:
<ul class="moreLinks" >
<div>More from Travelandleisure.com</div>
<li><a rel="nofollow" href="">xyz1</a></li>
<li><a rel="nofollow" href="">xyz1</a></li>
<li><a rel="nofollow" href="">xyz1</a></li>
</ul>
I know that I can apply font-size on li very small so that disc look correct to me and then apply css to "a" inside li. But I do not know why this is not working on the site I am working. I cannot control the html directly.
I saw that when I make this:
.farParentDiv ul li {
list-style: disc inside none;
}
TO this:
.farParentDiv ul li {
list-style-type: disc;
font-size:10px;
}
and now after applying font-size to "a", it works in Firebug. but from my css file. I tried a lot but tired. I can overwrite the above this in css file but cannot change it directly as I did in firebug. Please write what can be the problem?
I used to put dots (.) just before link inside and then apply css on that to control the disc size, but as I said, I cannot control the HTML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
由于我不知道如何使用 CSS 仅控制列表标记大小,而且还没有人提供此功能,因此您可以使用
:before
内容来生成项目符号:演示:http://jsfiddle.net/4wDL5/
这些标记仅限于出现在这个特定 CSS 的“内部”,尽管您可以更改它。这绝对不是最好的选择(浏览器必须支持生成的内容,因此不能使用 IE6 或 7),但它可能是最简单的 - 而且您可以选择任何您想要的标记字符。
如果您选择图像路线,请参阅
list-样式图像
。Since I don't know how to control only the list marker size with CSS and no one's offered this yet, you can use
:before
content to generate the bullets:Demo: http://jsfiddle.net/4wDL5/
The markers are limited to appearing "inside" with this particular CSS, although you could change it. It's definitely not the best option (browser must support generated content, so no IE6 or 7), but it might be the easiest - plus you can choose any character you want for the marker.
If you go the image route, see
list-style-image
.在现代浏览器中,您可以使用 ::marker CSS像这样的伪元素:
浏览器支持请参考:我可以使用::marker伪元素
In modern browsers you can use the ::marker CSS pseudo-element like this:
For browser support, please refer to: Can I Use ::marker pseudo-element
我一直很幸运地使用背景图像,而不是相信所有浏览器都以完全相同的方式解释项目符号。这也可以让你严格控制子弹的大小。
另外,您可能希望将
DIV
移到UL
之外。这是您现在拥有的无效标记。如果必须将列表头放在列表中,则可以使用列表头LH
。I have always had good luck with using background images instead of trusting all browsers to interpret the bullet in exactly the same way. This would also give you tight control over the size of the bullet.
Also, you may want to move your
DIV
outside of theUL
. It's invalid markup as you have it now. You can use a list headerLH
if you must have it inside the list.如果您选择使用内联 SVG 来渲染项目符号,则可以使用 width 和 height 属性来控制其大小:
单击此处查看 viewBox 的简单说明。
If you choose to use inline SVG to render your bullets, you can use width and height properties to control their size:
Click here for an easy explanation of the viewBox.
尝试对 li 使用不同的字体大小,
这使我免于使用图像
try to use diffrent font-size for li and a
this saved me from using images
我正在建立Kolja的答案,来解释viewBox如何工作
viewBox是一个坐标系。
语法:
viewBox="posX posY width height"
viewBox="0 0 4 4"
将创建此坐标系统:黄色区域为可见区域。
因此,如果您想将某些内容居中,那么您需要使用
viewBox='-2 -2 4 4'
我知道这看起来很荒谬,我也不明白他们为什么这样设计 方式...
但还有一种更简单的方法,您可以使用 Circles
cx
和cy
属性。I am buliding up on Kolja's answer, to explain how viewBox works
The viewBox is a coordinate system.
Syntax:
viewBox="posX posY width height"
viewBox="0 0 4 4"
will create this coordinate system:The yellow area is the visible area.
So if you like to center something in it, then you need to use
viewBox='-2 -2 4 4'
I know it looks ridiculous and I also don't understand why they designed it this way...
But there is a much easier method, you can just use the circles
cx
andcy
attributes.除了使用自定义图像作为项目符号之外,您还可以以一种方式设置 ul 或 li 元素的样式,然后以不同的方式设置内容的样式,如此处所示。
好处是一方面缺乏图像,另一方面增加了控制。缺点是它往往涉及非语义标记,除非在已经需要锚点的情况下。
Apart from using custom images for bullets, you can also style the ul or li elements one way and then style the contents differently, as seen here.
The benefit is the lack of images for one thing, and also the added control. The disadvantage is that it tends to involve non-semantic markup, except in this case where the anchors are required already.
可以使用
text-indent
代替position:absolute
来解决“内部”问题:http://jsfiddle.net/poselab/zEMLG/
Instead of using
position: absolute
,text-indent
can be used to solve the "inside" problem:http://jsfiddle.net/poselab/zEMLG/
当我将
list-style-type: Disc;
添加到ul
中,然后在 Google Chrome 版本 92.0.4515.131(官方版本)(arm64) 中,我看到这个。也许有些浏览器不支持这个伪元素。我通过这个CSS修改了
li
盘大小:但是请考虑这个新功能的支持范围:
When I add
list-style-type: disc;
toul
then in Google Chrome Version 92.0.4515.131 (Official Build) (arm64), I see this. maybe some browsers haven't been supported this pseudo-element.I modified the
li
disc size by this CSS:But please consider the support range of this new feature:
我认为通过使用内容“.”;如果太大,点就会变得太方,因此我相信这是一个更好的解决方案,在这里您可以决定“圆盘”的大小而不影响字体大小。
您可以通过编辑边框 px 的大小来编辑磁盘的大小。您可以根据您给它的剩余边距来调整与文本的距离。以及通过编辑页边距顶部来调整 y 位置。
I think by using the content"."; The dot becomes too squared if made too big, thus I believe that this is a better solution, here you can decide the size of the "disc" without affecting the font size.
You edit the size of the disk by editing the size of the border px. and you can adjust the distance to the text by how much - margin left you give it. As well as adjust the y position by editing the margin top.
对我来说最简单的方法是使用 Unicode 字符,然后使用 span 标签对其进行样式设置。为了示例,我将使用内联 css,但您可以按照自己喜欢的方式进行操作。
例如。当我想使用方形项目符号并控制其颜色和大小时:
您可以在这里找到很多 unicode 项目符号:
http:// unicode-table.com/
我这样做是因为链接图像的项目符号对我来说似乎有点不必要。
Easiest way for me is to use Unicode Characters and then style them with span tags. I'm going to use inline css for the sake of the example, but you can do it whatever way you prefer.
eg. When I want to use a square bullet and control its color and size:
You can find lots of unicode bullets here:
http://unicode-table.com/
I do this because linking images for bullets seems a little unnecessary to me.
我很惊讶没有人提到 list-style-image 属性
I'm surprised that no one has mentioned the list-style-image property
您还可以使用 2D 变换。下面的代码片段对其进行了说明,其中列表缩放了 25%。
参考:
You can also use 2D transform. It is illustrated in the snippet below with a list being scaled by 25%.
Reference:
在这里,我附上了我的解决方案,该解决方案在响应式中也能完美工作,您可以非常轻松地控制子弹的大小。
谢谢
Here I am attaching my solution that works perfectly in responsive too and you can control the size of the bullet very easily.
Thanks