I have wordpress sidebar with:
<h3 class="widget-title">TITLE OF SIDEBAR</h3>
and I need show small icon before “TITLE OF SIDEBAR. Can I do with CSS?
Or I must manually add image into code? like:
<h3 class="widget-title"><img src="">TITLE OF SIDEBAR</h3>
Pseudo elements will do what you want. Using the
:before
pseudo element, your CSS would look like this:This will place an image before the text content of the
<h3>
, however this won’t change the DOM at all which is important to note.A good explanation of how pseudo elements work can be found here, on CSS Tricks.
If your image is 10px wide, you could try this:
Keep your h3 tag without including img tag, and do the following:
You can find more information at https://developer.mozilla.org/en-US/docs/Web/CSS/content.
Sample http://jsfiddle.net/KCXVM/
Yes, you can do it in CSS.
Simply use the
:before
pseudo-selector, like this:Or, of course, use
h3:before { ... }
for it to apply to allh3
elements.Here’s a working example for you
Browser compatibility: This works in all common browsers, except IE7 or earlier.
Why not simply apply the image as a background?
So, at first, I thought a
<span>
thing would work.Then, I tried this, and it worked seamlessly:
You can add icon before each
h3
heading in CSS by following these ways below (via OIW Blog):– Use Glyphicons of Bootstrap
If you are using Bootstrap then you can use Glyphicons to add icons to the desired title or text.
Bootstrap contains a diverse set of icons, to pick up a suitable icon you can take a look at here: https://getbootstrap.com/docs/3.3/components/. Once choosing a desired icon, adding it to theme is a piece of cake. You just need to add the card after the location that you want your icon to be displayed
Notice that the icon I added is âokâ so its class shall be âglyphicon-okâ. Each icon (in the list I mentioned above) is compatible to a different class.
– Use icons of existing Cheatsheet of the currently used Font or third party
If your website donât use Bootstrap or the current set of icons of Bootstrap doesnât meet your need (despite containing a lot) (Glyphicons of bootstrap has displaying errors on IE10 of Window Phone OS). After that you can check what font of the website you are using is and find out if it has an icons Cheatsheet library or not. For example: Elusiveicons, Fontisto, Material Design⦠are some of the fonts that have icons Cheatsheet which are for immediate use.
If your currently used font of the website has Icons Cheatsheet then you can have a set of icons of the third party. Here I would like to introduce âFont Awesome Iconsâ. This is a good-looking and popular set of icons.
To use this set of cons, you need to add this code to the head section in your website:
â After adding CSS, you can use this code to put in the HTML which shows icons (you can apply this method to the part you use Cheatsheet of the font as mentioned above. Some fonts have unique way of using)
â If you donât want the code in the HTML, you can just use CSS. With CSS you need to find the Class or ID of the part that displays icon and after that use the below CSS code to display it. Here I display the EDIT icon of the third party âFont Awesome Iconsâ before (::before) the title, along with 2 properties of padding-right and font-style (you can also display it after the title by using after property):
Notice: the code of content is hexadecimal code. You can find and replace it with the code of the currently used icon. With âFont Awesome Iconsâ you can find it here: https://fontawesome.com/cheatsheet