Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add HTML tag inside CSS Content property

I'm looking for a way to add an HTML tag for CSS content property inside :after and :before.

Unlike this question which only discusses adding symbols like <. What I'm trying to add is something like:

.breadcrumbs li a:before {
    content: '<i class="icon"></i>';
}

Is there any possible way to do that?

like image 982
Muhammad Reda Avatar asked Mar 19 '14 15:03

Muhammad Reda


People also ask

Can I put HTML in content CSS?

Unfortunately, this is not possible. Per the spec: Generated content does not alter the document tree.

How do I embed HTML into CSS?

There are three ways to add CSS to HTML. You can add inline CSS in a style attribute to style a single HTML element on the page. You can embed an internal stylesheet by adding CSS to the head section of your HTML doc. Or you can link to an external stylesheet that will contain all your CSS separate from your HTML.

How do you add a content tag in CSS?

You can't insert tags using content: in CSS. Here is the relevant part of the spec; see the 'content' property in the CSS 2.1 spec.

How do you use tags in CSS?

There are three main methods of accessing or referring to HTML elements in CSS: By referring to the HTML element by its HTML tag name, e.g. p to refer to the paragraph HTML tag – <p> By using an ID, e.g. <p id="red_text">Some text</p> By using a class, e.g. <p class="red_text">Some text</p>


2 Answers

This is impossible. You cannot generate markup from CSS.

It should also be unnecessary - stick your background image / icon font / whatever in the generated pseudo-element you have already.

like image 74
Quentin Avatar answered Nov 16 '22 00:11

Quentin


This is probably what you want,

To put an image to the left of your breadcrumb li links:

.breadcrumbs li a::before {
    content: "";
    display: block;
    background: url('/img/breadcrumb-icon.png') no-repeat;
    width: 20px;
    height: 20px;
    float: left; /*icon on the left*/
    margin: 0 6px 0 0; /*margin on the right so there is a gap between your icon and link*/
}

If you want it on the other side try float:right;margin: 0 0 0 6px; or use ::after should get you part of the way

like image 24
ono2012 Avatar answered Nov 15 '22 23:11

ono2012