1

I'm trying to add text in this structure with pure css:

<div><i></i>world!</div>

so it would look like

<div><i></i>Hello, world!</div>

on the page. The jsFiddle example contains this css style:

div{
    border:1px solid red;
    margin-top:20px;
    margin-left:20px;

}
i {
    display: inline-block;
    width: 48px;
    height: 48px;
    background-image: url("http://png-.findicons.com/files/icons/1688/web_blog/48/pencil_small.png");
    border:1px solid green;
}

My bad solution is to add:

i:after {
content: "Hello, ";
}

but it doesn't put the text "Hello, " where I actually want and the style from i applied. So, is there any way to add Hello, right in front of another text with the same style?

Please only css solution. Thank you.

P.S. And here is my bad solution @ jsFiddle

It should be like this: enter image description here

4
  • why do you want to add with CSS in the first place ?
    – Ajey
    Commented Apr 1, 2014 at 16:17
  • CSS was invented to style the document. Not to edit the document. huh! Commented Apr 1, 2014 at 16:18
  • Adding content with CSS isn't accessible by screen readers. Commented Apr 1, 2014 at 16:18
  • That's not my project and such customization is needed. CSS is the best way in my case (if solution exists). Commented Apr 1, 2014 at 16:20

1 Answer 1

1

http://jsfiddle.net/RbLRA/2/

Is this how you want it? Your first fiddle doesn't help a lot - is the text meant to overlap the pencil image? Anyway, you can style the pseudo-element itself to have the green border, and remove the browser default styling from the tags which is usually italic, using:

font-style: normal;

It's not considered semantic to use the tag either in this case, just a thought...

3
  • Thank you for your answer. However, I've added the image as it has to look. Sorry, I has to add it when I opened the question. Thank you again. Commented Apr 1, 2014 at 16:35
  • 1
    I don't understand why you want to do this, to be honest - why aren't you just typing 'Hello' before 'World' in the HTML? Also, you have an <i> which only has a background image in it. If you want the 'Hello' part to be dynamic, you'll have to use Javascript anyway, and then you're much better off inserting the extra word with JS as well.
    – b4rbika
    Commented Apr 1, 2014 at 16:40
  • That's because the customization of css is easier in my case. Commented Apr 5, 2014 at 15:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.