How can I add an image to some text via css?
I've got this:
<span class="create">Create something</span>
and I want to add a 16x16 image to the left of that by using css. Is this possible or should i just manually add this image like so:
<span class="create"><img src="somewhere"/>Create something</span>
I'd rather not have to manually change all of the places which is why I wanted to do it via css.
thanks!
Float property in CSS will float an image to the left or right in the paragraph. The text in the paragraph will wrap around the image. Text-align property in CSS will position an image to the left, center or right of the page.
Use the float CSS Property to Place the Text Next to an Image in HTML. We can use the float CSS property to define how an element can float. An element can float to the right or the left. Some other options are none which means the element will not float and, inherit which, will exhibit its parent's behavior.
In web design terms, this effect is known as floating the image. This is achieved with the CSS property float, which allows text to flow around the left-aligned image to its right side (or around a right-aligned image to its left side).
To align the image to the left use attribute value as “left”. Example : HTML.
.create { background-image: url('somewhere.jpg'); background-repeat: no-repeat; padding-left: 30px; /* width of the image plus a little extra padding */ display: block; /* may not need this, but I've found I do */ }
Play around with padding and possibly margin until you get your desired result. You can also play with the position of the background image (*nod to Tom Wright) with "background-position" or doing a completely definition of "background" (link to w3).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With