Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7921515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:36:32+00:00 2026-06-03T16:36:32+00:00

I use <div> tags to insert icons in my pages: <div class=icon warning></div>There is

  • 0

I use <div> tags to insert icons in my pages:

<div class="icon warning"></div>There is a warning in the page

The problem is that the icons are too close to the text:

enter image description here

Here is the code for the icon:

div.icon{
    display:inline-block;
    width:16px;
    height:16px;
    background-color:transparent;
    background-image:url(/images/icons.png);
    background-repeat:no-repeat;
    vertical-align:text-top;
    background-position:0 0;
}

div.icon.warning{
    background-position:-48px 0;
    cursor:help;
}

I want to place a few pixels distance between the icon and the text only if the icon is being followed by some text. In other words if there is no text after the icon, I don’t want that space. In other words for the following code, I want to have 5px distance between div#icon1 and the text “There is a warning in the page” but I don’t want any distance between div#icon2 and the elements coming after it:

<li><div id="icon1" class="icon warning"></div>There is a warning in the page</li>

<li><div id="icon2" class="icon warning"></div></li>

Please note that the icons will not always appear within <li> elements so your suggested selectors cannot rely on the context that the icons may appear. The only thing certain about the icons is that if they are followed with some text, there must be some space between them and the text.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:36:33+00:00Added an answer on June 3, 2026 at 4:36 pm

    If you are willing to restructure your markup a little (without adding any additional size to it):

    http://jsfiddle.net/8kcQv/

    The key line is:

    .icon:empty{ padding-left: 20px; }
    

    This works in IE 9, Chrome, etc. Other browsers will add extra space between empty elements. Here’s an alternate version which degrades differently (less space between icon and text) when :empty is not supported:

    http://jsfiddle.net/8kcQv/1/

    HTML

    <div class="icon">This message has text.</div>
    <br>
    <br>
    <div class="icon"></div><div class="icon"></div><div class="icon"></div><div class="icon"></div><div class="icon"></div>
    

    CSS

    .icon{
     padding: 4px 4px 4px 32px; /* 32px adds extra space to pad against text */
     height: 24px;
     line-height: 16px;
     font-family: sans-serif;
     font-size: 16px;
     background:url(http://icons.iconarchive.com/icons/pixelmixer/basic/16/warning-icon.png) no-repeat 4px 4px;
     display: inline-block;
    }
    
    /* empty matches elements with no children (including text nodes) */
    .icon:empty{ padding-left: 20px; } 
    

    Alternatively, you might be able to do without :empty altogether if you use a style like:

    .icon{
        padding: 4px 8px 4px 24px;
        ...
    }
    

    This places equal distance on both sides of the icon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen div tags use a clearfix class when it's child divs use the
I need to use jQuery to locate all DIV tags that have no attributes
I use div tags to define areas within my web pages. I set all
I am calling a page that returns data like: <div id=class> <option>Value</option> <option>Value</option> </div>
So I want use multiple div tags that use webkit borders but for some
I'm using the JQuery Thickbox extension. Can I use it on div tags and
I have a div containing a form. I use that div as a dialog
All my div blocks use the same class name and I need to append
i have a div that i use as a container to fit in footer
I want to try to use div tags and css instead of tables in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.