how can i change background of a particular block??

how can i change background of a particular block??

by Anushka Modi -
Number of replies: 18

i am trying to use unique instance number ...but its not working..

Help ME!!!

Average of ratings: -
In reply to Anushka Modi

Re: how can i change background of a particular block??

by Patrick Malley -
Why not share with us what you've tried?
In reply to Patrick Malley

Re: how can i change background of a particular block??

by Anushka Modi -

hi Patrick

I am new to moodle and development.i have developed a small application using moodle.On front page there is a block of latest happenings .I want to change its background color,its width and want to make it static.I searched and found there is a unique instance number for every block.

I tried  writing code in style_color  for this block using its unique number..but its not working...

am i able to make myself clear??

Thanks for your response.

In reply to Anushka Modi

Re: how can i change background of a particular block??

by Lalit Bhatt -
Hi,

A block can have multiple instances on the same page. In your case, it would be one, I suppose.

So, lets proceed with that scenario -

There is a div element having class 'content' in every block instance. You need to give background-color style to it.
Go to the PHP file for that block. Say for example, the block is online_users then there would be a file named block_online_users.php in CFG->dirroot/blocks/online_users.
Override the function html_attributes() to include style. For example -
function html_attributes() {
return array (
'id' => 'inst'.$this->instance->id,
'class' => 'block_'. $this->name(),
'style' => 'background-color:#00FF00;' // Green Color
);
}
Now, open up weblib.php and modify the following line contained in its function print_side_block_start() -
echo '<div class="content">';
with
if(!empty($attributes['style'])){
echo '<div class="content" style="'.$attributes['style'].'">';
}
And you are done.

In reply to Lalit Bhatt

Re: how can i change background of a particular block??

by Patrick Malley -
This is:

a.) way more work than necessary for such a simple style change, and

b.) not advised since it requires edits to the core code which will be lost at your next upgrade.
In reply to Patrick Malley

Re: how can i change background of a particular block??

by Lalit Bhatt -
Hi,
You are right that the core code shouldn't be tampered much otherwise the future upgrade would become difficult.
I usually avoid ID selectors in my stylesheet files as there can be problems when working with generic XML documents.
I missed a little code in my solution -
In function print_side_block_start() contained in weblib.php, look for
echo '<div class="content">';
and replace it with
if(!empty($attributes['style'])){
echo '<div class="content" style="'.$attributes['style'].'">';
}else{
echo '<div class="content">';
}
The else block is necessary. I really don't know how I forgot it.
Anyways, thumbs up to you Patrick for your analytical skill.

In reply to Lalit Bhatt

Re: how can i change background of a particular block??

by Anushka Modi -

Hi lalit

Thanks for helping me !!!!!!

Cheerssmilesmile

In reply to Anushka Modi

Re: how can i change background of a particular block??

by Patrick Malley -
Sorry Anushka. I didn't mean to be rude. it just sounded like you were on the right track and I thought it would be ideal if you just copied and pasted the CSS code you've tried into the text editor so that others could have a look.

The reality is that styling a specific block is quite easy since every block has a corresponding ID generated for it's surrounding div element that can be styled indiviually.

For example, if I were to style the Participants block on a demo course on my local install, I would add the following to the bottom of one of my stylesheets:

#inst33.sideblock {
background:#ff0000;
color:#ffffff;
}

Your sideblocks will have different #inst number values, but this should give you the idea. Good luck.
Average of ratings: Useful (1)
In reply to Patrick Malley

Re: how can i change background of a particular block??

by Anushka Modi -

hey Patrick

Thanks a lot for your help.You were not at all rude,seriously.

What steps  i have to take to place the block in center of my page?

Thanks againsmile

In reply to Anushka Modi

Re: how can i change background of a particular block??

by Patrick Malley -
That's currently not possible. You will have to wait until Moodle 2.0 is released sometime early in 2010 for that functionality.
In reply to Patrick Malley

Re: how can i change background of a particular block??

by Anushka Modi -

sorry for bothering you again on the same topic..

i tried this code in one my stylesheet in standard theme

#inst38.sideblock {
border-color: #00FF00;
  background-color: #00FF00;
}

my block instance id is inst38

but its not working

Thanks

In reply to Anushka Modi

Re: how can i change background of a particular block??

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

You need to put the same CSS mark-up in your own theme's stylesheet, whatever file name you have call it. Putting it in the standard stylesheet will not have the same effect.

I have been watching this thread with interest because I wanted to do something similar, and so I tried the code and it works like a treat! I used an image background, rather than a solid color.

You can see the effect HERE

Hope this helps?

Regards

Mary


Average of ratings: Useful (1)
In reply to Anushka Modi

Re: how can i change background of a particular block??

by Patrick Malley -
Mary is right. Style rules appearing lower in a document will have more "power" over rules that appear higher. To be certain it will work, you need to place this at the bottom of the last stylesheet to be listed in config.php of the theme you are using.
In reply to Anushka Modi

Re: how can i change background of a particular block??

by Lalit Bhatt -
Hi,

Patrick is right. I will elaborate his solution.

1. Create a stylesheet file, say styles_miscellaneous.css inside $CFG->dirroot/theme/standard.
2. Include your custom style into that CSS file -
#inst38.sideblock {
border-color: #00FF00;
background-color: #00FF00;
}
3. There is a file named config.php inside $CFG->dirroot/theme/standard. Open it and look for -
$THEME->sheets = array('styles_layout', 'styles_fonts', 'styles_color');
You need to add your CSS filename to this list i.e.,
$THEME->sheets = array('styles_layout', 'styles_fonts', 'styles_color','styles_miscellaneous');
And you are done.
Thanks to Patrick and of course to Mary as well.
By the way, Mary your sample site is really impressive. smile

Average of ratings: Useful (1)
In reply to Lalit Bhatt

Re: how can i change background of a particular block??

by Anushka Modi -

Thanks a lot to for your responses Patrick,Lalit and Mary .. it worked wellsmilesmile

Is there any way to bring this block in middle???

In reply to Anushka Modi

Re: how can i change background of a particular block??

by Frank Ralf -
Hi Anushska,

Moodle 1.9 doesn't provide a mechanism for placing a block in the middle column. You could instead use a Label.

Of course you could use CSS to position your block anywhere you want (see screenshot and http://moodle.org/mod/forum/discuss.php?d=128160 and http://moodle.org/mod/forum/discuss.php?d=128599 ).

hth
Frank


PS:
Better start a new discussion when you have another question. More people will be able to help you.
Attachment Activities_re-positioned.png
Average of ratings: Useful (1)
In reply to Frank Ralf

Re: how can i change background of a particular block??

by Anushka Modi -

hey frank

thanks for replying

I will try this and let you know whether its done or not

I will also keep in mind to start a new thread for a new discussion..

Thanks 

In reply to Anushka Modi

Re: how can i change background of a particular block??

by Lalit Bhatt -
Hi,

I would suggest you to go through this discussion "Patch to allow a center position for Moodle blocks" by Alfredo Artiles.
The URL is http://moodle.org/mod/forum/discuss.php?d=55280.
Hope you would find some useful tips out there. smile

Average of ratings: Useful (1)