InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search
  • Home
  • > Tutorials
  • > CSS cookbook

CSS – use :before to add heading to a div

By admin | Last updated on Mar 25, 2016

Css pseudo element/selector :before can be used to prepend some content before an element using a class or any other selector. This can be pretty handy to add common title to an existing block element having same css selector (e.g. class).

:before syntax

Here is :before syntax using css class selector:

.classname:before {
  content: "content goes here";
  /* other css attributes ... */
}

Example – Use :before to add heading to div

Here is an existing html for a block (class=”myclassnopseudo”) which displays some text.

<style>
.myclassnopseudo {
  background-color: lightgray;
  position: relative;
}
</style>
<div class="myclassnopseudo">
Demo text...
</div>
refresh done
try it online

 
Now we add :before pseudo element to class myclass. Here is the code and how it renders.

<style>
.myclass {
  background-color: lightgray;
  position: relative;
}
.myclass { padding-top: 30px;}
.myclass:before {
  content:"code";
  color: white;
  background: green;
  width: 100%;
  position: absolute;
  top: 0;
}
</style>
<div class="myclass">
Demo text...
</div>
refresh done
try it online

Few points to note

  • Similar to pseudo element :before, we can use :after to add something after the block.
  • We had to use padding-top to create some room for the heading.
  • The div for which we want to use :before pseudo element should be a positioned element (absolute, relative or fixed).

Suggested posts:

  1. Running php eval on code with tags
  2. CSS placeholder style
  3. egrep pipe example on Apache access log
  4. PHP – print array in one line
  5. How to install and run Chrome PageSpeed insights for measuring site performance
  6. CSS :empty – define style for empty element
  7. CSS attribute presence and value selectors
  8. How to install polymer
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CSS, CSS cookbook, CSS Pseudo Elements, HTML, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress