By khelll on
hi
due to my being new to drupal
so i still can't demonstrate the domine of theme devloping
so my question is if there is a way(programing i mean) to give a different unique color to each block (background) on the page, iam working on spreadfox theme and in fact they use background image to specifiy the color of the block....
thanks in advance.....
Comments
Different colors for different blocks
Hi - this should definitively be possible. Have a look at the block.tpl.php of - say - the bluemarine theme and include this in your own theme as well:
You see that each block's div will get not only a unique class but also a unique id. If you have a look at the source of your drupal site in your web browser, you can look up what these classes and ids will finally be called and you'll see that they are all addressable individually so different colors etc. should be no problem through the CSS file!
Just a little more advice
panatlantica,
Thank you for submitting this - I'm learning more about CSS everyday --- but this is still a little over my head
This is from my source --- what would I need to do next (based on this info)
<div class="block block-menu" id="block-menu-62">thank you very much, I've wanted to do this for sometime now and it wasn't until your post that see this IS possible
I think I have this part. and it would go in my page.tpl.php file????
if this is correct? ... what would go into my style.css file??
thanks
Some more help ;-)
Hi jwells,
Okay, let me explain what this line does and how you can address it from your CSS file:
<div class="block block-menu" id="block-menu-62">So, what do we have here: that is a DIV, a certain, rectangular area on your web page. It has been assigned various classes and one ID. Now, in CSS there are three ways to address things (I am simplifying just to keep things easy):
Okay, so far for the basics, but propably you know all of this already. Let's analyse the above DIV:
So we open a DIV that has classes and one ID assigned. You see, not only can classes be used on several items on the same time in your document, elementes can also inherit several classes at the same time. So this div got the class "block" AND the class "block-menu" assigned. Additionally it carries the ID "block-menu-62". This is the DIV's very unique ID.
Now it depends what you would like to do with the DIV. The entire reason for having classes and IDs is that you won't need to repeat things over and over again. Let's see; here's the DIV again:
<div class="block block-menu" id="block-menu-62">Now in your CSS file you can reference to that DIV in three ways: you can call it by using
.block {CSS deffinitions come here;}(now, .block can occure again later on the site, handy if all your blocks have a blue background like you see on Drupal.org), or by.block-menu {CSS deffinitions come here;}- this is a bit more specific then just .block, as it says that the styling should only be applied to any menu in blocks. OR, you can call JUST THIS ONE DIV and none else by its ID#block-menu-62 {CSS deffinitions come here;}.Okay, so far for how to call your block from your CSS file and style it to your likes.
now for the
Placint this in your page.tpl.php will result in errors. You might want to have a look at the PHPtemplate section of the Handbook on Drupal.org, but this is it in short:
A PHPtemplate theme works like this:
You have the page.tpl.php that will define the HTML markup for the entire page. (If you need additional functions in your templates, you need to add a template.php file to the directory of your theme, this will first be processed and its functions will be made available to the template then in a second step. But for now let's just keep this info for you to remember once it occurs, you won't need it in everyday page theming).
So the first thing that is going to be analyzed is the page.tpl.php file. Its content will render your web site's markup. The PHP tags in it add things like header-stuff sloagans and: sidebars!!! This is done by e.g. for a left sidebar (and you might already know this):
What does this do: first it checks if there really is a left sidebar
if($sidebar_left) { ...- if it finds a sidebar, it will continue to call itprint $sidebar_left(finally you'll find a<?php } ?>which just closes the initial if-query - you remember?Okay. What happens WHEN the sidebar gets called?! Additionally to what goes on inside Drupal at this stage is that the Template Engine will look if it finds a block.tpl.php file! And inside this block.tpl.php file you'll find
As there come more blocks on the site, this block.tpl.php file gets called again and again until all boxes with side content have been drawn. This is why it goes in the extra file block.tpl.php!
You remember calling
<?php print $content; ?>in your page.tpl.php?! What happens here is that anything in the node.tpl.php will be included here!I guess you now get the overall idea of how PHPtemplate works and where things belong to and why - and how you can call them again in your CSS file!
Let's recab:
Okay: the initial DIV cann be addressed in your CSS like this (you don't necessarily need ALL of the definitions, this is just to show you all the possiblities:
Now, you can get more specific in your CSS file if you want a different title just in a block by
.block .title {definition: value;}. However, you have a competitor ;-) This competitor I am talking about is a SECOND CSS file that might need to be overwritten by your style.css! This is drupal.css, Drupal's internal style sheet.CSS means "Cascading Style Sheets". What does that mean: it means, there are things that "cascade" downwards. Basically it means that lower hirachy elements will inherit higher hirachy element's definitions - even if they are in other CSS files...
You might want to find out more on the Cascade and which element has a higher value and can overwrite other elements by Googleing on the subject. For now, I just want to give you this on your way: you can override the hirachy system! If by any matter your definitions do not show up, try adding a "!important" behind the rule. E.G.:
So you got that and you are sure the element has a class="red" assigned, but still, the element is not red? Okay, just try altering it to
and check the result. You might find your element bright red by now!
If not, just let me know! ;-)
Steve
wow! that is possibly
the most detailed (and useful) reply I have ever had the pleasure to read here on drupal.org!
Congrats panatlantica!
I feel certain that jwells (who asked for advice) will be well and truly happy with your efforts.
Maybe this answer should be elevated to a handbook page? Certainly it would be useful to many future users I think.
wellsy
orchidsonline.com.au
2nd the request
to add to the handbook- lucid discussions of the CSS are in short supply.
Thanks!
-Peter
---
Work: BioRAFT
Read Eric Meyer
Actually, lucid discussions of CSS are plentiful. If interactive tutorials are your thing, I find that w3schools.com has a fairly large set, and they're easy to follow. If you don't like their style, Google will turn up many other well-done alternatives.
But CSS is very rich, and at times complicated. If you're going to do this seriously at all, you'll really need something heftier than a few online handbook pages. I'm fond of Eric Meyer's books. He writes very clearly, both on the technical side (a number of O'Reilly books, including Cascading Style Sheets:The Definitive Guide) and the design side (Eric Meyer on CSS: Mastering the Language of Web Design is a good book for how to use CSS, once you know the basic language syntax and features.)
When it comes to the external technologies used by Drupal (CSS, PHP, SQL, HTML, etc.), the handbook can't be expected to be as complete as the sources dedicated to those technologies.
Gary Feldman
Astonishing!
panatlantica,
Thank you so very much for such a detailed and elegant explanation. Not only did you answer questions about bits of code, that I’ve been wondering about --- but you even answered questions that I don’t even have the knowledge to ask about yet.
I greatly appreciate the time you took to explain this in detail. It’s always more useful to know how to plant a garden, then knowing only how to consume one. Your explanation provides me with this knowledge. And I finally understand the difference between a “#” and “.”
Thank you again and I’m sure there will be many people to learn from this explanation.
Best regards,
Jeff
I hope you don't mind, but I've asked sepeck to copy this into the handbook for others to learn from. I have a lot of Forum posts to help answer - to balance the karma on this one :-)
leading?
Steve,
With your help, I was able to change 6 different custom menu blocks. I was able to set, background, padding, margin and border to the look I was hoping for. It was so very easy -- once I knew what I was doing.
There is one setting however I can't seem to grasp or locate -- if it is even available.
I might use the wrong words to describe it however.
In a menu block, there are menu items attached to links. If the menu link description wraps into a two line description there is the spacing between the sentences. Leading?
There there is also the spacing between actual menu items or bullets shall we say.
It is this spcaing between the menu bullets I would like to change. For a little better defination between menu bullets.
If I change line-height, everything changes, is there a different way around this. I use to edit my actual bullet icons in a graphic program - by adding a little transparent space to the top of the bullet icon. But I'm thinking there might be a better way.
And was thinking you just miight know.
Thanks again
Jeff
Possibly you want something
Possibly you want something like:
.menu li {
line-height:1em;
margin-top:0.5em;
}
thus for all items within a menu, the follow-on menu labels are packed tight, while the gaps between them are spaced out.
tweak as needed.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
active link color
panatlantica
Are you still out there. I have one I'm stuck on.
I want to change the color of my active links in a single block. Its pretty easy to change a active link color in a theme. Put in this case, I only want to change the color in one block.
Changing the font color in one block was easy, but darn if I can figure out how to change the active or hoover prams in that block.
thanks
Still there...
Hi ! I'm still there. Don't know if you settled the question before this one, so I'll quickly just explain how to proceed with the active link color thingy:
Example HTML:
In your CSS, you have set up all sorts of colors for text and links, but you want the links to differ right in the block-block-1 Block. Add to your style.css
You see, you call an "a" or "a:active" that belongs to the inside of a certain ID! You should use the IDs and not the class of a block, because IDs normally value higher in the CSS cascade. Also, when styling links, remember "to LoVe HAte your links"! This is a good way of remembering in which order to write your link definitions for them to work consitantly:
L = Link -- that is "a" or "a:link" first
o
V = Visited -- that is a a:visited second
H = Hover -- that is a:hover third
A = Active -- that is a:active last
t
e
or
a:link { }
a:visited { }
a:hover { }
a:active { }
If you put them in the right order, things will work out fine, if you disrespect that order, some browsers might ignore the rules you gave!
There is another one going:
Las
Vegas
Hells
Angels
Fight
F is for the :focus pseudoclass, but to be better off, forget about :focus right again ;-)
Thanks, this is going to
Thanks, this is going to come in real handy.
I haven't master the other lesson, but with your info as a crib sheet - I can make changes as required.
Also, now that I know how to locate a blocks name. I've been able to make other changes.
Thanks again
Great!
Great! Looking forward to seeing your page some day!
Steve.
kewl
Steven,
I just wanted to say thanks again, for really explaining this in detail.
Today, I decided to try and use the Nifty Drupal. I figured it based on BlueMarine, so it should be rather solid.
Thing is this Theme uses Php to set the colors. At first it Yikes!, I know nothing about Php and arrarys.
But I sat down and had a good look, simple! .. and understanding the basics for locating a block name and how to address a block remained the same - just a few small changes on the way you set and call the color.
thanks again,
Jeff
Kudos Steve, splendid tutorial
Your patience and detail at answering this question was truly wonderful. You're among that rare handful of terrific Drupalers who knows how to do things, wants to help, and has a a gift for hand-holding that is rarely seen in this -- or any -- software community. The best thing about your writing is that you don't assume the user knows much of anything, yet proceed to educate them without condescension of any kind.
But it left me--and probably a few readers--wanting just a bit more. You've explained how to tweak the CSS classes, but what if the user wants to reshape or replace the form elements INSIDE the block? (I've wasted hours and hours trying to find a quick and dirty illustration of doing this, and I have seen dozens of posts proving that many others have as well).
The Form_alter method could be used to modify the block's inner source HTML (the value of the $content variable), but that requires a lot of confusing detail about the form API and tree. Most casual users just assume they can do what they might do in many systems with Smarty, or some other popular template system; just overwrite the given block with their own HTML source (and form elements).
So if you would now extend this great thread with a BASIC example of how the non-programmer might add a NEW HTML SOURCE ELEMENT to their BLOCK, it would make this among the more valuable lessons for the typical themer without programming skills.
My idea of a good example would be something I just wrestled with. Your illustration could address a few key sub-issues:
1) User wishes to add and "Advanced Search" link into their search box (block).
Such a link would come after the standard block content, and would not require any adjustment to the default content. For that illustration, you might next address this:
2) User wishes to insert an image after the text input, and before the submit button in the standard drupal search box. The user might replace the standard form elements entirely, but what if they want drupal to continue to render the form elements normally, but merely tweak what comes between them. (If this is NOT a good idea, or worth trouble, that too might be discussed, because the new user doesn't really have the knowledge or Drupal insight to know).
3) To do 1 and 2, users needs to know just how to identify ONLY the block (or blocks) they willl be acting on, thus, they need to put an IF SEARCH BLOCK statement in their block.tpl.php template.
I could be wrong, but I'd bet such an illustration woudl be one of the most useful tutorials on this site, and you might take both your original post, and the followups, redact the comments, and post it has a HOW-TOO.
Again, wonderful post. Thanks.
Theming forms
1) Simply wrapping the form or adding to it is fairly easy.
Suppose you want to theme the search block you first have to find its form_id. For now, it's sufficient to know that it is search_block_form. To theme this form you need to provide a theme function override in template.php;
In template.php
Note how the theme override relates to the form_id (phptemplate_form_id).
In search-block-form.tpl.php:
The key here is form_render, a truly marvellous function that takes a form element as its argument. It then proceeds to render the element and all it's children, marking them as rendered, so it won't render the same thing twice. When you give it the entire form as argument it simply renders the html output for the entire form.
As you can render parts of the form 2) is also not to difficult. There is an easier route however, we can cheat our way out by putting the image in the #prefix property of the submit element. I leave figuring out the override to you as you can find the different form_ids in http://drupal.org/node/45295#comment-136588.
In your .tpl.php file
--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.
Thanks Heine. Another great reply
Thanks Heine!
This thread is rockin. Someone should bubble it up to the How-tos in a few places. I figured much of this out already (with help of other posts), but there were still some new things to learn. I figured prefix and suffix had to be a set. Never dawned on me I could just use it as an 'insert here'.
Heine
Questions :
return _phptemplate_callback('search-block-form', array('form' => $form));What does that do ? What can I use for second argument, generally ? An array of... what ?
The array that I pass as 2nd argument, is that what becomes available to... in... where ? Only the... what file ?
The function form_render(), is this a function that works in both Drupal 4 and 5 ?
I still am not sure how this works, that function. What do you mean by "so it won't render the same thing twice"...?
And what's a prefix, finally ?
Caroline
Theming forms
This instructs the phptemplate theme engine to look for the file search-block-form.tpl.php, load and run it and make the variables of the second argument available to it. It basically connects
theme('seach_block_form' ...)to the file seach-block-form.tpl.php.The second argument to _phptemplate_callback defines what variables are available in the .tpl.php file.
array('form' => $form)makes the parameter$formtophptemplate_search_block_formavailable to search-block-form.tpl.php as the variable$form.The form would be accessible to seach-block-form.tpl.php as
$myformif the following second parameter would have been used:Summary:
_phptemplate_callback('my_tpl_php_file', array('variable_name' => 'variable_content', 'variable2_name' => 'variable2_content'));To use form_render (4.7.x) or drupal_render (5.x) effectively you need to learn about Forms API.
#prefixis just a property that is output before a form element.Both form_render and drupal_render take a structured array as argument. These functions enable you to render parts of the array out of order while flagging those parts as being rendered. This enables you to render parts of the form out of order, without them being rendered twice:
--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue
Also, check out Fancy
The Fancy theme uses a different color for each block - studying the main style sheet and blocks.css sheet (defines colors for each block) will teach you a lot.
I was thinking that
Thanks Pak - I saw that theme was thinking it would probably be a good exmaple to see how things are done
thank you
I need help :)
Sorry guys, I have tried following this but keep either missing something or doing something wrong.
I want to change the background color on just one of my blocks for now.
My theme does not have a block.tpl.php file (is that normal?)
I have tried adding in a default (& customised) block.tpl.php but get errors at the top of each page.
The element I want to change the background for is:
>body >table #content > tbody > tr > td #sidebar-left.Sidebar > DIV #block-og-0 .block block-og
My css file has everthing up to the DIV, and when I have tried adding an entry for DIV #block-og-0 but with no luck.
I would really really really appreciate it if someone could tell me what I need to put in my 'new' block.tpl.php and also what to add to my CSS file.
Thanks in advance. Scott
You shouldn't need to create
You shouldn't need to create a block.tpl.php file if one doesn't exist. Just add
The key is understanding that ID's, which are indicated with the # sign, must be unique to a page. Thus the only situation in which it's useful to add more qualifiers to an #id specificier is when multiple pages are sharing the same style sheet, using the same #id in different ways, and you want different results depending upon how the #id is being used. I don't think that's the case for you here, so all you need is the #id.
Also, the sequence you quoted (>body >table ...) isn't in the correct syntax to cut and paste as a .css specifier. For example, it says
DIV #block-og-0 .block block-og, which corresponds to "<div id="block-og-0" class="block block-og"&rt;", but in the .css file it would be div.block or div.block-og or div#block-og-0, etc. In other words, there should not be a space between the element name (div) and the class or id (.block or #block-og-0).Gary Feldman
Thank you
thanks Gary, that was much easier than what I was trying! I understand your bit on the id's now too.
I really appreciate your help.
Scott.
great explanation
Yes I just have to chime in and say congrats for a great contribution by Steve. simple, in depth and very helpful to many people not very familiar with css and using it in Drupal.
what if you want them CONFIGURABLE
Dunno what happened to my previous post regarding this, it never showed up.
My client is a system administrator who likes to automate everything. I have configured awesome styles for .block-menu and .block-block such that they have fancy borders etc. Not good enough.
OK, so I can override the borders for any specific block-block-x or block-menu-x in the style sheet and make them different. Still not good enough. Ideally wants to be able to create an arbitrary block or block menu any time and be able to specify from within the admin panel which border/color treatment it gets, without editing the stylesheet.
I don't know the Drupal API but I am actually a programmer (Skinning Drupal or any other CMS is not what I normally do.) I've looked at the API and there are lots of hooks. and I don't know exactly where to get started. I'm using a Theme I made based on BlueMarine so it is a PHPTemplate theme. If someone knows how and where I can hook into drupal to make it apply the appropriate classes to the appropriate objects when it GENERATES it's code, then I could write generic border/color classes and have drupal apply them at will.
i too want to comment on
i too want to comment on what a great post that was pana. you've taught me so much. thank you.
mark
what monotreme said
i'm really interested in making blocks configurable, too. so the content managers/authors of the site can make a block and then specify it as 'attention-block', for example. it would then get a different class that could be styled with css. should this be done somehow with taxonomy?
Wow, what an amazing post!
Wow, what an amazing post! Kudos to all who have made this thread so useful!
Thank you for taking the
Thank you for taking the time to answer this question so thoroughly. Amazing post!
Good Post!
but nothing seems to work for me. :(
I am trying to change the font colour of all the items in a certain menu block. I was just using red for the moment like the example so it was obvious when it started working. I am assuming that because all of the menu items are actually links I am assuming I should be changing the link behavior.
My Page source for the menu in question:
So I have completed the following in my themes style.css
/* Changing Fonts for Main Menu */
#block-menu-65 a:link {
color: red !important;
}
#block-menu-65 a:visited {
color: red !important;
}
#block-menu-65 a:hover {
color: red !important;
}
#block-menu-65 a:active {
color: red !important;
}
beware of !important
i just wanted to make some comments about using !important in css.
the following style definition:
#extras #block-menu-65 .content .menu li a { color:yellow; }
will override this one:
#block-menu-65 .content .menu li a { color:green; }
which, in turn will override this:
.content .menu li a { color:blue; }
however, this one will override all the previous ones:
a { color:red !important; }
so, instead of using this:
#block-menu-65 a:link { color: red !important; }
try to add more selectors (#extras, for example), to give your style more 'weight'. that means your css can still be overwritten somewhere else if necessary.
if the #block-menu-65 a:link { color: red !important; } style isn't turning your menu items red, i would guess that you already have a { color:someothercolor !important; } defined somewhere else in your stylesheet.
or try removing the psuedo-class :link. i seem to recall having problems with that in the past. so just use #block-menu-65 a { color: red; }
yeah, still does not work
yeah, still does not work for me though, I have played around with it a lot. :( I dont understand to why it is not working.