2

In the Views admin (i.e. admin/structure/views ), is there a section in the settings to insert CSS to style the view? i.e. how do I style a view without editing theme code or other drupal PHP?

I am able to find out which class identifiers that I need to style - see question/answer at: Which html div classes are relevant when styling Views in Drupal 7 (for CSS)?

But I don't know where to put the CSS code to actually style the view.

I assume that it is possible to style a view within the drupal views admin pages, without having to resort to editing/creating themes, PHP and HTML code via FTP (?). Obviously one can do those things too but I'd rather try to find a (non-theme specific) solution within the views admin first.

3 Answers 3

2

Actually, the latest views does allow all kinds of CSSing, both from the easiest, such as adding CSS classes to the view Style Settings section, to embedding custom css and fields wrappers in the fields output when you click on their configuration and edit Style Settings there.

But I think the question here is, after you do all that, where do you put the CSS itself.

And that would be in one of your theme's css files, which are defined in your themename.info file (which is located in your theme's toplevel directory) like:

stylesheets[all][] = myfilename.css

So, you could ADD a css file to your theme with your customized CSS without touching anything else in there.

6
  • Correct me if I'm wrong but (in views) you can only specify additional CSS class names to be added to the output and/or change HTML elements used. You can't add actual CSS rules. Dec 13, 2011 at 1:18
  • 2
    you can rewrite the output of the field to be <div style="all the rules you want"> [fieldname] </div> as a very crude way of doing this...[fieldname] being the token of the field replacement pattern.
    – Jimajamma
    Dec 13, 2011 at 1:43
  • (not that I am suggesting it as a way I'd do it, but it can be done)
    – Jimajamma
    Dec 13, 2011 at 1:44
  • +1 @Jimajamma for this "crude" suggestion. I agree maybe there are more "proper" ways but thanks. I will look at the other options. Dec 13, 2011 at 9:33
  • Accepted answer for the variety of options you gave for doing CSS. I will try your answer first, if no success CSS Injector as mentioned by others credit to them and finally CSS files in a theme - but would try to keep them generic to be able to be shared across themes. Dec 14, 2011 at 12:52
2

While I agree that style rules belong in a CSS file (compare with PHP that gets Eval'd) I can see the requirement, especially if it was a multi domain site that ran different themes. You could write a custom module (basically what CSS Injector does as mentioned in a previous answer) but for a complete code free way you could use Panels which is much more aware of CSS, in that you can add some style rules to specific containers within the interface itself.

1
  • +1 @Malks for the Panels suggestion. I am actually already using those. But I guess that given it is the View itself being styled, it might be preferable (semantically/conceptually) to have that config data near the view itself. Dec 13, 2011 at 9:37
0

Views does not have anything like that because it belongs in a theme. It's considered good practice for module maintainers to keep themeing small and out of the module itself.

The closest you can get to not editing theme CSS or template pages is using the CSS Injector module.

5
  • But what if the styling that I want to do is theme independent - e.g. simply styleing a list generated from a view or adjusting the spacing or font? Surely I would not want to have that in a specific theme? By the way (+1 for your answer (and likely to accept it)) Dec 13, 2011 at 0:02
  • CSS Injector is the only thing I know of that lets you add CSS from the web interface, and there's nothing wrong with that. IMHO you want all lists and fonts and spacing to be uniform across your theme so it should go there anyway (but do what works best for you). Dec 13, 2011 at 1:07
  • you can rewrite the output of the field to be <div style="all the rules you want"> [fieldname] </div> as a very crude way of doing this...[fieldname] being the token of the field replacement pattern.
    – Jimajamma
    Dec 13, 2011 at 1:40
  • I don't agree that styling a view should have to belong to a theme. It can, sure, but reasons why I don't believe this should be an enforced rule: 1) slows down development of a view: flow is disrupted because one would have to go to more than one place to complete the work. The iteration cycle of edit-see result-edit-see-result etc becomes cumbersome. 2) Moreover isn't it beneficial to keep as much as possible within drupal's admin pages rather than having to go to the 'back door' and edit themes and FTP them up 3) CSS styling of views may not be tied to a theme, some things are common. Dec 14, 2011 at 12:46
  • +1 for mentioning the CSS Injector first. I wouldn't agree that CSS has to be strictly within a theme. Themeing is powerful in drupal but is drupal's weakness due to its complexity and the fact that this has to be done at high admin permissions level - i.e. you have to have FTP access to the drupal site to do it. One of the great things about drupal is the precision role assignment within itself. Having to do themeing outside of this breaks the model. CSS in Views is not polluting Views as a module, because this CSS is data, part of the database, not the Views module PHP code. Dec 14, 2011 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.