CSS? Best practice?

CSS? Best practice?

by Sam Thing -
Number of replies: 12

Hello all,

I'm very much at the development stage of overhauling a moodle site that has years of accumulated poor design descisions built in and to ensure continuity of service and content, I'm modding pages and sections of pages bit by bit. As a consequence I'm sticking bits of CSS all over the shop, only moving it onto the theme.css when, or if, it becomes half decent.

I notice that <style type="text/css">foo</style> and <link rel="" etc > seem to work just fine when placed on an html block or in a topic section and not between the <head> tag. In some instances, as this method works, I've just left the CSS where it is and moved onto the next project.

I don't want to compromise stability, performance or anyones' sense of aestethicssurprise so what are the problems or objections to doing this, if any?

Thanks. Sam.

Average of ratings: -
In reply to Sam Thing

Re: CSS? Best practice?

by Danny Wahl -

I don't think anybody will object to you doing that and I don't think it's against spec (for inline style blocks, it's bad for <link >)....  I certainly wouldn't want to have to manage that mess though!

If it were me, I'd be moving the rules to a stylesheet as I came across them.

In reply to Danny Wahl

Re: CSS? Best practice?

by Hubert Chathi -

Actually, it is against spec; <style> and <link> tags are only allowed in the <head>.  Mind you, it's probably more a matter of priorities: if it works well enough for now, and there are a bunch of other bad design decisions that are worse, it may be better to work on those things instead, and fix these issues later.

In reply to Sam Thing

Re: CSS? Best practice?

by Philipp Pavelka -

Wouldn't it be easier to use some browser extension and user stylesheet and when satisfied copying it over to the theme.css?

In reply to Sam Thing

Re: CSS? Best practice?

by Sam Thing -

I am moving rules off to a style sheet as a matter of course now but I'm stuck on tayloring individual page elements that are already defined on layout.css

For example, how do I hide the topic outline on a per page basis? I can stick this inline on a page but as has been pointed out: it's not to spec:

<style type="text/css">h2.outline {display: none;}</style>

As always, all input welcome.

Thanks, Sam.

In reply to Sam Thing

Re: CSS? Best practice?

by Hubert Chathi -

Look at the body tag of each page.  It has a bunch of attributes that tell you what kind of page you are on.  For example, this forum page has: id="page-mod-forum-discuss" class="path-mod path-mod-forum ... pagelayout-incourse course-5 context-114 cmid-55 category-1".  You may be able use some of those to limit styles to certain pages.

In reply to Hubert Chathi

Re: CSS? Best practice?

by Sam Thing -

Ah...this is at the limits of my CSS skills....I'm not sure how to constuct the rules.

So if I want to hide .h2.outline on the following page:

<body id="course-view" class="course course-4975 dir-ltr lang-en_utf8">

how do I put that together?

Thanks Hubert.

In reply to Sam Thing

Re: CSS? Best practice?

by Alex Walker -

You can use the various ID or class names, depending on what you want.

To hide the header on every course:

body#course-view h2.outline {
display: none;
}

To hide it only on this course (ID number 4975):

body#course-view.course-4975 h2.outline {
display: none;
}

To hide h2 class="outline" on every course page (probably including subpages/resources within that course):

body.course h2.outline {
display: none;
}

In reply to Alex Walker

Re: CSS? Best practice?

by Sam Thing -

Oooo... I was so close too. I need to go back to the book obviously.

Thanks Alex, I've learnt something there.

Sam.

In reply to Sam Thing

Re: CSS? Best practice?

by Sam Thing -

 

I've gone and re-read and understood chapter 3; selectors.

I'd got all excited and learnt rounded courners and drop shadows first *chagrin*

In reply to Sam Thing

Re: CSS? Best practice?

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Which version of Moodle are you discussing here as Moodle 1.9 themes and Moodle 2.x.x themes are different, further more, Moodle 1.9.x themes will NOT work in Moodle 2.x.x.

Also which theme or themes are you trying to fix here?

As for messing with what appears to be core files is crazy. Why not just UPgrade and then just fix the theme/s using CSS in the stylesheets attached to the theme?

Better yet...start using a new theme!

Mary

In reply to Mary Evans

Re: CSS? Best practice?

by Sam Thing -

It's 1.9.

I'm moving us up to 2.x as soon as technically feasable. In the meantime I have development issues that need to be addressed. I'm not touching core files and am using the style sheets.

Sam

In reply to Sam Thing

Re: CSS? Best practice?

by Alex Walker -

There's nothing tremendously wrong with having <style> blocks in your page, but given how easy it is to use CSS files in Moodle (and the benefit they bring), it's easiest to use a CSS file.

When you're developing Moodle 2 blocks, you don't even need to do any legwork. All you need is a file called 'styles.css' in your block folder. Moodle automatically picks it up and uses it.

Because of the way Moodle builds, processes and caches CSS, you can do pretty neat things and it's easier to load images without manually specifying which folder you want to load them from.

The attached picture shows an example: a block I'm building right now that contains a few icons and graphics. The CSS to load those icons is in /block/mission_control/styles.css and the images are all in /block/mission_control/pix. 

For example, instead of doing this:

.myResults {
background-image: url("/blocks/mission_control/pix/myresults.png");
}

I can do this:

.myResults {
background-image: url([[pix:block_mission_control|myresults]]);
}

When I build blocks, I build them with the idea that I might release them for other people to use. I put all the CSS for the block in styles.css in the block's folder, and I include all icons/images in the blocks' folder, in a folder called 'pix'. Doing this and using Moodle's CSS shortcodes keeps all the block's stuff neatly bundled together. The first CSS example above would only work if it was used at moodle.somesite.com, not somesite.com/moodle (because it's looking in /blocks instead of /moodle/blocks). The pix" shortcode would still work.

I know I'm rambling a bit here, but if you're refactoring a messy Moodle, refactoring the styles into CSS files will probably help you in the long run.

Attachment mission-control.png