Search code examples
csspseudo-elementcss-content

Div with CSS content, after pseudo element is not visible


I have a POC that I have to complete and I am only allowed to use CSS to update the styles of a product. I have replaced one of the images of the product with a logo by using the CSS content attribute.

I have to add a simple string with a phone number to be shown after this logo. I have tried to use the :after pseudo-element to do this. This works only if the content of the div is empty (logo is removed).

.demo {
  content: url('http://placehold.it/350x150')
}

.demo:after {
  content: 'test';
  display: inline-block;
}
<div class="demo"></div>

I have tried changing the display to inline-block and hard coding the heightand width for both rules. Essentially everything I could find. Any help would be greatly appreciated.

JSFiddle here: https://jsfiddle.net/qykbjznm/


Solution

  • The content property replaces all content within the element. By adding content to a non-pseudo selector, that content will replace the ::before and ::after pseudo selector.

    So try doing this using the content property within the ::before and ::after pseudo selectors only.

    .demo:before {
         content: url('http://placehold.it/350x150')
    }
    
    .demo:after {
        content: 'some text';
        display: block;
    }
    <div class="demo"></div>