Where To Add Custom Code In Divi (CSS, Javascript & PHP) Tutorial by Pee-Aye Creative

Where To Add Custom Code In Divi (CSS, Javascript & PHP)

In this handy guide you will learn exactly where to add custom code like CSS, Javascrip, Jquery, and PHP in your Divi Theme.

CSS Hierachy (Specificity) In Divi

Before we dive in, let’s talk quickly about CSS hierary, otherswise know and specificity. This term can be confusing, and it’s okay if you don’t understand it quite yet.

Basically, if you target something with CSS, and there are two or more conflicting CSS rules somewhere on the website that point to the same element, the CSS in certain locations will override the others. It has more power the higher up in hierarchy it is. The browser would get confused otherwise, and needs to follows some rules to determine which one is most specific and therefore which one wins out.

The level of specificity depends where the code is placed in Divi. It would not be very helpful to show you where to add custom code in Divi without showing you why. Here’s a list of the CSS hierarchy in Divi, with 1 having the most power and 8 the least:

  1. Inline Styles
  2. Advanced Tab
  3. Code Module
  4. Page Settings
  5. Theme Options
  6. Child Theme
  7. Theme Customizer
  8. Divi Parent Theme

If your head is spinning in confusion, don’t worry. It will make more sense as we go and as you gain hands-on experience, and you will soon understand clearly what each of these are and when to use them.

#1. Inline Styles

Code Languages: CSS, HTML

Inline styles are those that are written directly in the html structure of the page or post. For example, in the Text Module, you can change from Visual to Text and see the html of the page. If you have anything styled there, you will see them in the code.

For example, if you highlight a word in Divi and make it bold, it will add <strong> tags around that word. Or if you select a phrase and change the color like this, it will add <span> tags like this: <span style=”color: #e03574;”>change the color like this</span>

Inline styles have the most importance in code. However, even though these are the most powerful, they should be used only for small, simple changes.

#2. Advanced Tab

Code Languages: CSS

Another form of inline styles, although not as obvious as “inline”, is in the Advance tab of any section, row, column, or module. Each have different boxes, and some have quite a lot which are very helpful. For example, a row only has three, but a Blurb Module has one for Title, Image, Content areas as well.

Limitations

These are super handy for quick snippets, but are also limited. I often see questions about these in Facebook groups, with users questioning why their code has an error. Usually this is because users are trying to add the full CSS snippets with the classes instead of just the values. You can only add values like “color: red;” but you couldn’t say “.custom-class { color: red; }. (This makes more sense in the video, so be sure to watch that.)

add custom css to Divi module Custom CSS area

#3. Code Module

Code Languages: CSS, HTML, Javascript

Another option for adding CSS and Javascript is the Divi Code Module. For CSS, make sure to wrap the code in <style></style> tabs, and for Javascript, make sure to wrap the code in <script></script> tags.

The Code Module is perfect for integrating code such as iframes, embed codes, shortcodes, and more. For example, one of our most popular tutorials on how to embed a Google map in Divi uses this. You could also use this to keep code on a specific section or layout that you save to the Divi Library or exprot to another website.

add code to the Code Module in Divi

#4. Page Settings Custom CSS

Code Languages: CSS

Those that follow my videos on YouTube know that one of my favorite places to add temporary custom code is the in the page settings Advanced tab Custom CSS area. The reason I like this so much is because it creates “live view” of the code as you work and edit. As a tip, I recommend editing your headers or other areas of the site using this. Just remember this is temporary, and you should copy and paste this code into your child theme stylesheet or Theme Options custom CSS box when you are done.

add code in Divi to the page settings custom css

#5. Theme Options

Code Languages: CSS, Javascript

The next place to add custom code that affects the entire site at once is the Divi Theme Options. There are several places to add code here at different places.

Custom CSS

You probably have noticed by now that almost every tutorial I write, I mentioned adding the code snippets to the Divi>Theme Options>Custom CSS box. I say this because it is the easiest and most practical place to add custom CSS, especially for beginners who may not have a Divi child theme.

add code to the Divi Theme Options Cusstom CSS box

 Javascript (and Jquery)

There’s also a handy place in the Theme Options for other types of code, and that is in the Integrations tab. Here you can add code that affects the <head> or <body> of the site, similar to a child theme or parent theme. This first box is the perfect place to add all the Javascript and Jquery snippets we give out here on the blog.

NOTE: Be sure to remember to wrap the snippets in <script></script> tags. You can also add any sort of <meta> tags here, like tracking codes or Font Awesome scripts.

add code to the Divi Theme Options Integrations tab

#6. Child Theme

Code Languages: CSS, Javascript, PHP

I know I said to put custom css snippets in the Divi Theme Options, but if you have a child theme installed and active, then I actually recommend putting it there instead. This is the ultimate place to add any time of custom code in Divi.

If you don’t have a Divi child theme yet, you can download ours here.

A child theme inherits the main functionality from the parent theme (Divi), but adds additional customization. It is very important to use a child theme if you do any custom coding rather than modify Divi itself. To learn more about Divi child themes, see our tutorial and video here.

Custom CSS

I recommend moving all of your custom CSS to the Divi child theme style.css file whenever your site is completed and you are ready to go live. Just go to Appearance>Theme Editor and choose your child theme from the dropdown in the top right corner. Then click on the style.css and paste your code at the bottom.

add custom css to a Divi child theme

Custom PHP

Any custom PHP snippets should be placed in the Divi child theme functions.php file. Some of our tutorials have PHP snippets, and this is where you should put it. All you have to do is go to Appearance>Theme Editor and choose your child theme from the dropdown in the top right corner. Then click on the functions.php and paste your code at the bottom.

add custom PHP to a Divi child theme

#7. Theme Customizer

Code Languages: CSS

Even though I never use this and don’t recommend it, I still have to mention it. You can also add custom css to the Theme Customizer. You can access this either from Appearance>Customize in the backed, or from hovering over yoru website name in the top left corner on the frontend.

Since the CSS hierarchy is lower here than in a child theme, I don’t recommend using this. The only thing this is really good for is making live changes to the site where you want to see the change, and then copying and pasting that code to the Theme Options or child theme (similar to page settings CSS).

adding code to the additional CSS area in the Divi Theme customizer
where to add code to the additional CSS area in the Divi Theme customizer

#8. Divi Theme

Code Languages: Pretty Much Anything

Last on the list is Divi. This is the worst place to add any kind of custom CSS. Don’t do it. If you did, it would be erased and overwritten as soon as Divi has an update. As far as hierarchy goes, this is the weakest place to add custom CSS.

customizing the Divi theme with custom code css javascript php
12

Please share this post!

Nelson Lee Miller (aka The Divi Teacher)

Nelson is the owner of Pee-Aye Creative Co in the beautiful state of Pennsylvania. He loves helping small businesses, exploring, building websites with Divi, and teaching others.

Join Our NEW Divi Facebook Group!

The Divi Teacher Logo

Recent Posts

Leave A Response!

Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Asit Aithal
Asit Aithal
1 month ago
Iain Corrigan
Iain Corrigan
1 month ago

When using the integrations tab (#5) for JavaScript or JQuery, make sure Enable Header Code Button is toggled on.

I recently worked on a fresh Divi install and it defaulted to “off”. It took me longer than I’d like to admit to figure out with the JQuery wasn’t doing anything!

Sue
Sue
1 month ago

Again thanks for all the content you share!!!

Shop Our Divi Products!

Child Themes • Plugins • Layouts

Visit The Shop

Divi Tutorials On YouTube

Subscribe to our helpful Divi videos!

Visit Our Channel

0
Would love your thoughts, please comment.x
()
x

Pin It on Pinterest

0

Your Cart