Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

conditional css


Reply

7 Posts   4245 Views

Avatar
irka

17 April 2008 at 4:31pm Community Member, 9 Posts

another newbie question :/ where can I add conditional css ? im working on my first SS website, using higherground theme, and it doesn't seem to be right in IE..

Avatar
Wojtek

17 April 2008 at 6:02pm Community Member, 149 Posts

Hello, irka!
You can add the conditional css in the templates/Page.ss file, just as it's done in the default Blackcandy theme

Avatar
irka

18 April 2008 at 7:49am Community Member, 9 Posts

thank you :) sorted this problem anyway,,and thanks for the theme :)

Avatar
Shane Garelja

22 April 2008 at 2:34pm Community Member, 18 Posts

Another, "better"(?) but slightly more complicated method I've found for doing this is to add your CSS via the Requirements::css static method in your page controller - you can achieve the same results as conditional statements like so:

function init() {         
parent::init();      
Requirements::css( project() . "/css/default.css");      
if($pos = strpos( $_SERVER[ 'HTTP_USER_AGENT' ], 'MSIE' ) ) {      
Requirements::css( project() . "/css/ie.css");      
$version = substr( $_SERVER[ 'HTTP_USER_AGENT' ], $pos + 5, 3 );
if( $version < 7 ) {
Requirements::css( project() . "/css/ie6.css");
}
if( $version < 6 ) {
Requirements::css( project() . "/css/ie5.css");
}
}      
}
}

(You would add this to the Page_Controller class in your mysite/Page.php file)

The advantage here is that you can create page types that extend the Page class and they will automatically pick up the inherited CSS files. You could then then add additional, custom stylesheets for a specific page type if you wanted.

Avatar
Willr

22 April 2008 at 8:38pm Forum Moderator, 5511 Posts

Handy method. You should add this to the CSS page in the documentation - http://doc.silverstripe.com/doku.php?id=css for other peoples reference

Avatar
spenniec

7 May 2008 at 11:48am (Last edited: 7 May 2008 11:56am), Community Member, 37 Posts

You can also place this in the Page_Controller function init() and lose the hard coded entries in Page.ss.

Requirements::insertHeadTags("<!--[if IE 6]><style type='text/css'>@import url(themes/" . SSViewer::current_theme() . "/css/ie6.css);</style><![endif]-->");

Requirements::insertHeadTags("<!--[if IE 7]><style type='text/css'>@import url(themes/" . SSViewer::current_theme() . "/css/ie7.css);</style><![endif]-->");

Avatar
B-Side

29 September 2008 at 6:02pm Community Member, 42 Posts

Hi there - I found I *had* to add the conditional comments into the Page_Controller function in order to achieve the right hierarchy for the conditional stylesheets, using the BlackCandy theme.

When I included the conditional stylesheets in Page.ss they were included *before* the layout.css file, which then over-wrote some of my fixes.

Hope this helps someone!

Y