Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent wrapping of content around :before selector content

Tags:

css

I know I can just use an image for this but who in their right mind really wants to?

I'm trying to find a neat way of changing the colour of the list-item prefix in an unordered list.

I can do this quite easy using the :before selector. (Yeah I know about ie7, lucky me it doesn't matter).

e.g.

.ul1 li
{
    list-style-type:none;
}

.ul1 li:before, .ol1 li:before
{
    content:"\25CF"; /*escaped unicode coloured circle*/
    color:#F68A39;
    font-weight:bold;
    font-size:18px;
    text-align:right;
    padding-right:6px;
    width:10px;
}

The problem I have is that the content in my list-item will now wrap around the :before content. Is there a way of preventing this?

Here's some markup to to start with.. Cheers!

<ul class="ul1">
    <li>
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text 
        ever since the 1500s, when an unknown printer took a galley of 
        type and scrambled it to make a type specimen book. It has survived
        not only 
    </li>
    <li>
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text 
        ever since the 1500s, when an unknown printer took a galley of 
        type and scrambled it to make a type specimen book. It has survived
        not only 
    </li>
    <li>
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text 
        ever since the 1500s, when an unknown printer took a galley of 
        type and scrambled it to make a type specimen book. It has survived
        not only 
    </li>
    <li>
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text 
        ever since the 1500s, when an unknown printer took a galley of 
        type and scrambled it to make a type specimen book. It has survived
        not only 
    </li>
</ul>
like image 439
James South Avatar asked Nov 15 '11 17:11

James South


People also ask

How do you prevent elements from wrapping?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).

How do you stop wrapping in CSS?

To prevent the text from wrapping, you can use the CSS white-space property with the “nowrap” or “pre” value.

What is :: before in CSS?

::before (:before) In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

How do you select before an element in CSS?

The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.


1 Answers

You can do it by positioning the :before content absolutely to the li element, and then applying a margin or padding to the li element. Fine-tune the bullet positioning with, top,left,right, and/or bottom to finish up.

Here's an example:

.ul1 li
{
    list-style-type:none;
    padding-left: 16px;
    position: relative;
}

.ul1 li:before, .ol1 li:before
{
    content:"\25CF"; /*escaped unicode coloured circle*/
    color:#F68A39;
    font-weight:bold;
    font-size:18px;
    text-align:right;
    padding-right:6px;
    width:10px;
    position: absolute;
    left: 0;
    top: -0.2em;
}
<ul class="ul1">
    <li>
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text 
        ever since the 1500s, when an unknown printer took a galley of 
        type and scrambled it to make a type specimen book. It has survived
        not only 
    </li>
    <li>
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text 
        ever since the 1500s, when an unknown printer took a galley of 
        type and scrambled it to make a type specimen book. It has survived
        not only 
    </li>
    <li>
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text 
        ever since the 1500s, when an unknown printer took a galley of 
        type and scrambled it to make a type specimen book. It has survived
        not only 
    </li>
    <li>
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text 
        ever since the 1500s, when an unknown printer took a galley of 
        type and scrambled it to make a type specimen book. It has survived
        not only 
    </li>
</ul>
like image 68
Brent Avatar answered Nov 15 '22 10:11

Brent