Skip to main content
update for joomla 4
Source Link
FFrewin
  • 19.8k
  • 10
  • 48
  • 101

One of my favorite approaches is to create dynamic classes for the body element.

So:

<?php
  $app    = JFactory::getApplication();
  $menu   = $app->getMenu();
  $active = $menu->getActive();
  $class  = $active->alias . " pageid-" . $active->id;
?>
<body class="<?php echo $class; ?>">

Some examples of what the above code will produce is:

Home page:

<body class="home pageid-13">

About us page:

<body class="about-us pageid-15">

Now you can create custom specific styles per page, using the above classes.


Update 2023

Getting the menu in Joomla 4:

use Joomla\CMS\Factory;
$app = Factory::getApplication();
$menu = $app->getMenu();
$activeMenuitem = $menu->getActive();

https://docs.joomla.org/Menu_and_Menuitems_API_Guide

One of my favorite approaches is to create dynamic classes for the body element.

So:

<?php
  $app    = JFactory::getApplication();
  $menu   = $app->getMenu();
  $active = $menu->getActive();
  $class  = $active->alias . " pageid-" . $active->id;
?>
<body class="<?php echo $class; ?>">

Some examples of what the above code will produce is:

Home page:

<body class="home pageid-13">

About us page:

<body class="about-us pageid-15">

Now you can create custom specific styles per page, using the above classes.

One of my favorite approaches is to create dynamic classes for the body element.

So:

<?php
  $app    = JFactory::getApplication();
  $menu   = $app->getMenu();
  $active = $menu->getActive();
  $class  = $active->alias . " pageid-" . $active->id;
?>
<body class="<?php echo $class; ?>">

Some examples of what the above code will produce is:

Home page:

<body class="home pageid-13">

About us page:

<body class="about-us pageid-15">

Now you can create custom specific styles per page, using the above classes.


Update 2023

Getting the menu in Joomla 4:

use Joomla\CMS\Factory;
$app = Factory::getApplication();
$menu = $app->getMenu();
$activeMenuitem = $menu->getActive();

https://docs.joomla.org/Menu_and_Menuitems_API_Guide

Source Link
FFrewin
  • 19.8k
  • 10
  • 48
  • 101

One of my favorite approaches is to create dynamic classes for the body element.

So:

<?php
  $app    = JFactory::getApplication();
  $menu   = $app->getMenu();
  $active = $menu->getActive();
  $class  = $active->alias . " pageid-" . $active->id;
?>
<body class="<?php echo $class; ?>">

Some examples of what the above code will produce is:

Home page:

<body class="home pageid-13">

About us page:

<body class="about-us pageid-15">

Now you can create custom specific styles per page, using the above classes.