Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show pseudo element but not parent element

Hi I have a list item containing text like this:

<li>Search</li>

and I want to display an icon using font awesome

li:before {
  content: "\f002";
}

I don't have the ability to just remove the "Search" text (it is being generated from a Drupal CMS, as is the markup and class names), but I want to hide the Search text, but show the pseudo element (the search icon). How do I do this? Normally what I would do to hide the text is just go:

li {
  text-indent: -1000px;
  overflow: hidden;
}

but that will hide the pseudo element as well

like image 707
mheavers Avatar asked Jul 31 '14 13:07

mheavers


People also ask

Does Z Index work on pseudo elements?

It is important to realize that pseudo-elements are considered descendants of their associated element. You may set a negative z-index for these pseudo-elements, but in order for them to actually appear below their parent element, you must create a new stacking context for the parent.

Can I have multiple before pseudo elements for the same element?

In CSS2. 1, an element can only have at most one of any kind of pseudo-element at any time. (This means an element can have both a :before and an :after pseudo-element — it just cannot have more than one of each kind.)

Is a pseudo element a child?

Technically, the pseudo-element is still a child, so it's still in there doing its thing, but isn't participating in the grid. This isn't unique to CSS Grid either. For instance, you'll find by using flexbox that your pseudo-element becomes a flex item.

Do pseudo elements inherit?

No. The only way that pseudo-elements can inherit values from the parent of their generating element is when the generating element itself is also inheriting from its parent. This is because inheritance occurs from a parent to a child, one level at a time.


Video Answer


2 Answers

A bit late to the party, but you could always change the font-size of the li to 0, and change the font-size of the icon back to the original font-size. Like this:

li {
    font-size: 0;
}

li:after {
    font-size: 1em;
}
like image 129
Tom Avatar answered Nov 15 '22 05:11

Tom


You can stick to the "text-indent" method (or better the "Kellum Method") and use CSS positioning for the pseudo element:

li {
display:block;
position:relative;
text-indent: -100%;
white-space: nowrap;
overflow: hidden;
}
li:after {
content: "visible pseudo-element";
position:absolute;
right:0;
}

http://jsfiddle.net/Fiddel/aopteq8m/

like image 44
Herr_Schwabullek Avatar answered Nov 15 '22 05:11

Herr_Schwabullek