简体   繁体   English

我可以使用 CSS 为任何元素添加项目符号吗?

[英]Can I use CSS to add a bullet point to any element?

Pretty simple question, but I am not sure if it is possible.很简单的问题,但我不确定是否可能。 I want to add an image to act as a bullet in all <h1> elements.我想在所有<h1>元素中添加一个图像作为项目符号。 I know I can achieve this by:我知道我可以通过以下方式实现:

<span class='bullet'></span><h1>My H1 text here</h1>

with css:用CSS:

.bullet{
    background: url('bullet.png') no-repeat;
    display:inline-block;
    vertical-align: middle;
    background-size:100%;
    height:25px;
    width:25px;
    margin-right: 5px;
}

but is there an automatic way to do the same thing?但是有没有一种自动的方法来做同样的事情? I was thinking something like:我在想类似的事情:

h1{
    list-style-image: url('bullet.png');
}

but that only seems to work with <ul> elements.但这似乎只适用于<ul>元素。 I really don't want to have to paste the <span> element everywhere before the <h1> element.我真的不想在<h1>元素之前的任何地方粘贴<span>元素。 Any ideas?有任何想法吗?

While you can use a :before pseudo-selector to add a "-" or "•" character in front of your element, it doesn't really make your element behave like a bullet point.虽然您可以使用:before伪选择器在元素前面添加“-”或“•”字符,但它并不能真正使您的元素表现得像一个要点。 Your element may look like a bullet point, but that's just a dirty hack, really, and should be avoided!你的元素可能看起来像一个要点,但这只是一个肮脏的黑客,真的,应该避免!

To make your element both (1) look like a bullet point and (2) behave like a bullet point, you should set the display of that element to list-item .要使您的元素(1)看起来像一个项目符号点并且(2)表现得像一个项目符号点,您应该将该元素的display设置为list-item Optionally, you can also set list-style-type (default : disc ) and list-style-position (default : outside ) attributes to modify the behavior / look-and-feel of your list item.或者,您还可以设置list-style-type (默认值: disc )和list-style-position (默认值: outside )属性来修改列表项的行为/外观。

If your element spans multiple lines, list-style-position should be the default value outside if you want all of your lines to align to the right of your bullet point.如果您的元素跨越多行,如果您希望所有行都与项目符号点的右侧对齐,则list-style-position应该是outside的默认值。 In that case, however, it is possible you don't see your actual bullet point, as it would be to the left of the content of your element.但是,在这种情况下,您可能看不到实际的项目符号点,因为它位于元素内容的左侧。 If this happens, you can just add a left margin to move the element's content to the right, and your bullet point should show up.如果发生这种情况,您只需添加一个左边距以将元素的内容向右移动,您的项目符号点应该会显示出来。


EXAMPLE 1例 1

 h1 { display: list-item; /* This has to be "list-item" */ margin-left : 1em; /* If you use default list-style-position 'outside', you may need this */ }
 <h1> Your H1 text should go here. If it consists of multiple lines, the left alignment of all lines is the same. </h1> <h1> Here's another item. </h1>


EXAMPLE 2例 2

 h2 { display: list-item; /* This has to be "list-item" */ list-style-type: square; /* See https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type */ list-style-position: inside; /* See https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-position */ }
 <h2> Your H2 text should go here. </h2> <h2> Note that, if you have multiple lines, only the first line is aligned to the right of the bullet point when using list-style-position 'inside'. Subsequent lines are left aligned with the left of the bullet point. </h2>

You could do something like this:你可以这样做:

h1:before {
    content:"• ";
}

See Fiddle :见小提琴:

http://jsfiddle.net/6kt8jhfo/6/ http://jsfiddle.net/6kt8jhfo/6/

You can use pseudo-selector :before to add anything what you want before your tag.您可以使用伪选择器:before在标签之前添加您想要的任何内容。

 h1:before { content: "- " }
 <h1>My H1 text here</h1>

Give a class name to the paragraph or any element and apply the below code (I have given class name as bullet ):为段落或任何元素指定类名并应用以下代码(我已将类名指定为bullet ):

.bullet::before {
content: '';
position: absolute;
bottom: 7px;
left: -10px;
width: 3px;
height: 3px;
background-color: #000;
border-radius: 50%;
}

Something like this should work像这样的东西应该工作

h1, h2, h3 {
  background: url("the image link goes here") 0 center no-repeat;
  padding-left: 15px; /* change this to fit your needs */
}

If you want to adjust dot size, color and position you can do this:如果你想调整的大小、颜色和位置,你可以这样做:

 .bullet:before {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 5px;
    display: inline-block;
    background-color: #29cf00;
    vertical-align: middle;
}

list-style-type is reserved for ul only. list-style-type仅为ul保留。 You can use <h1 class="bullet"> with pseudo-element :before .您可以将<h1 class="bullet">与伪元素:before一起使用。

The very simple way to create a bullet using the before css is to utilize the font family ... this way there is no need to include any graphics and etc.使用之前的 css 创建项目符号的非常简单的方法是使用字体系列......这样就不需要包含任何图形等。

here is the class code:这是课程代码:

.SomeClass:before {
  font-family: "Webdings";
   content: "=  ";

{ {

Nope, list-style and list-style-image are only for ul and ol tags you'll have to get back to your first method or make something with js不, list-stylelist-style-image仅适用于ulol标签,你必须回到你的第一个方法或用 js 做点什么

http://www.w3schools.com/css/css_list.asp http://www.w3schools.com/cssref/pr_list-style-type.asp http://www.w3schools.com/css/css_list.asp http://www.w3schools.com/cssref/pr_list-style-type.asp

Just use <p>&bull; </p>只需使用<p>&bull; </p> <p>&bull; </p> to create a dot in front of your word <p>&bull; </p>在你的单词前面创建一个点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM