Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after pseudo element not appearing in code

I'm trying to use the after pseudo element to add some effects to a site.

<div class="product-show style-show">
  <ul>
    <li>
      ....
      <div class="...">
        <div class="readMore less">...</div>
        <a href="3" class="readMoreLink" onclick="return false;">Read More</a>
      </div>
      ....
    </li>
  </ul>
</div>

And stylesheets:

.product-show .readMore.less {
   max-height: 200px;
   height: 100%;
   overflow: hidden;
}

.product-show .readMore.less:after {
   background: rgba(255, 255, 255, 0);
   display: block;
   position: absolute;
   bottom: 0;
   left: 0;
   width: 100%;
   height: 30px;
 }

I see the styling for .product-show .readMore.less being applied, but I don't see a ::after notation in the HTML blocks when I'm examining the site from Chrome (latest version)/MacOS. I read that there are sometimes issues with older browsers, but I assumed that I should be able to see at least the ::after pseudo element notation if I was defining the style correctly. What am I doing wrong?

like image 764
Ji Mun Avatar asked Apr 11 '15 00:04

Ji Mun


People also ask

Why is after element not showing?

Basically replaced elements will have all its contents replaced - so when rendered by the browser, we cannot see any :after or :before pseudo elements. Therefore the styling will not apply. So if you are using :after for the above HTML tags, then most likely it will not work.

What is after pseudo element?

::after. In CSS, ::after creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

Can I use a before or after pseudo element on an input field?

The input element has no content in the CSS view, and so has no :before or :after pseudo content. This is true of many other void or replaced elements.

Why is Z Index not working?

You set z-index on a static element By default, every element has a position of static. z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.


2 Answers

It's because the pseudo-element isn't generated if the content value is omitted (since the initial/default value is none).

Specify a content value in order to generate the pseudo-element. A value of '' is sufficient.

.product-show .readMore.less:after {
    content: '';
    background: rgba(255, 255, 255, 0);
    display: block;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 30px;
}
like image 110
Josh Crozier Avatar answered Oct 17 '22 07:10

Josh Crozier


As per the MDN:

The content CSS property is used with the ::before and ::after pseudo-elements to generate content in an element.

This means that if you don't include the content property, the :after or :before element will not be generated.

Add this line inside your pseudo (:before or :after) selector:

content: "";  // Leave this empty

And see how that affects the result.

Just as a note, you could also add text into the content property if you want to use the :before or :after to display text. In many cases though, you would find that you are simply leaving it empty.

like image 9
Lucas Avatar answered Oct 17 '22 07:10

Lucas