Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I know I can achieve this by:

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

with 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. I really don't want to have to paste the <span> element everywhere before the <h1> element. Any ideas?

like image 222
Sablefoste Avatar asked Aug 16 '15 20:08

Sablefoste


People also ask

How do I add bullet points in CSS?

To set the bullet appearance you can use CSS' list-style or list-style-type . list-style-type is the exact property we are changing here, but you can also use list-style to change multiple properties, which we will come to shortly. You don't have to remember to set the bullet appearance each time.

What CSS property could you use to use square bullets instead of round ones?

The style attribute specifies an inline style for an element. The attribute is used with the HTML <ul> tag, with the CSS property list-style-type to add square bullets to an unordered list.

How do I align text with bullets in CSS?

You need to bring the bullet points inside the content flow by using list-style-position:inside; , and then center align the text.

How do I change the shape of a bullet point in CSS?

Changing Bullet Point Shape button at the bottom of the Classic Builder. On line 3, you can change the bullet point shape by replacing square with another value, such as disc or circle. For more about bullet point shapes, check out W3 School's CSS list-style-type Property. Save and Publish, or Republish the page.


2 Answers

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. 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, list-style-type and list-style-position attributes of that element.


EXAMPLE CODE

h1 {     display: list-item;          /* This has to be "list-item"                                               */     list-style-type: disc;       /* 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 */ }
<h1>My H1 text here</h1>

THE FIDDLE

http://jsfiddle.net/L15a53cb/

like image 99
John Slegers Avatar answered Sep 19 '22 16:09

John Slegers


You could do something like this:

h1:before {     content:"• "; } 

See Fiddle :

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

like image 29
sinisake Avatar answered Sep 21 '22 16:09

sinisake