I would like to add custom CSS styling to some of my pages to give them a unique style, and override the default template styling.
How can I achieve this in Joomla?
There are several ways to add custom CSS code to a specific page. Here's a couple of alternatives:
Alt. 1 - Page class
Use page classes. In the menu item editor, under the "Page display" tab, there's a field called "Page class". This will add a class to your <body>
tag (or a <div class="YOURCLASS">...</div>
around your content), depending on how your template is configured.
Then simply create a new rule in your template's CSS file, using the class you specified.
E.g.
Add mycustomclass
to the "Page class" field in your menu item, and put this in your CSS file:
.mycustomclass p {font-color:red;} // Make text red on this particular page
Alt. 2 - Custom CSS modules
Several modules are available for this purpose. One example is Custom CSS, a module that allows you to add any CSS code and publish it to the pages you want to style.
A similar option is Custom HTML Advanced, a module that lets you add HTML, JavaScript and CSS to your pages. The code can be added to the <head>
tag automatically.
Alt 3. - load additional CSS stylesheet
Another alternative is to check the current menu item ID in your template's index.php
file, and load another CSS stylesheet if needed:
<?php
$currentMenuId = JSite::getMenu()->getActive()->id ;
if ($currentMenuId == "14") {
echo '<link rel="stylesheet" type="text/css" href="mystyle.css">';
}
?>
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.
use Joomla\CMS\Factory;
$app = Factory::getApplication();
$menu = $app->getMenu();
$activeMenuitem = $menu->getActive();
This is another method of doing the same thing.
Goes above the doctype:
<?php
$app = JFactory::getApplication();
$menu = $app->getMenu()->getActive();
$pageclass = '';
if (is_object($menu))
$pageclass = $menu->params->get('pageclass_sfx');
?>
Goes in your index where your body class is:
<body id="<?php echo $pageclass ? htmlspecialchars($pageclass) : 'default'; ?>">
Now, anything you add into the menu item's page class will appear in the body id. Any menu item without a class will automatically be body id="default".
If you only want to change the content area, use "Page Class" in "Page Display" Tab inside your menu item.
If you want to change the style for the entire menu item page, try this:
As an example in the default Protostar template you can see in the <body>
tag a class like:
<body class="... itemid-101 ...">
This corresponds to the menu item with the Id 101. From here you can add a class in the file user.css for the new CSS class .item101 {}
, this works for the entire <body>
tag.
The code used here, in the index.php file:
<?php echo $itemid ? ' itemid-' . $itemid : ''; ?>
By active menu item, find here more examples:
https://docs.joomla.org/Using_the_Page_Class_Suffix_in_Template_Code
Via extension:
http://extensions.joomla.org/extensions/extension/style-a-design/templating/css2switch-basic
Regards.