Is there any way to specify a CSS class for a Content Query web part? I tried setting its CssClass property but it didn't do anything.


You can also adapt the XSL which drives the CQWP to meet you own needs. Usually people do this the create new display options, but you might also do it to apply your own styles. Look in the Style Library for the file.

Another option would be to use script, but it's probably overkill. It's much better to just create overrides for the styles the CQWP uses in your own CSS file.


You might have to get specific when setting it.

classname {}

will not overwrite (from what I remember)

body div div#classname {}

From what I remember SharePoint likes to be specific. Use the Developers Tools in IE/Firefox/Chrome to find out what styles are being applied. For example Chrome will tell you exactly what style is being used ( like div div span div classname {} ). Once you know what is exactly being used you can reset that.

  1. Copy the out-of-the-box header.xsl file and rename it.
  2. Open that file up and put a wrapper div around the default template (or another template if it suits you) that has a unique CSS class.
  3. Then place a CQWP on a page, export it, and open the .webpart file in notepad++.
  4. Then you'll change the closed header style tag to point to your new header.xsl file.
  5. Import that modified .webpart file to the web part gallery and apply it to a page.
  6. Now you can use your custom wrapper tag in you style sheet to select various instances of a cqwp.

A work-around is to set the __WebPartId attribute of the web part in SharePoint designer. I have done this for an XSLT View Web Part:

<WebPartPages:XsltListViewWebPart __WebPartId="{5BD5DD01-20DD-48E6-AD74-B709773CAAB1}" ...

This is rendered as follows:

<div WebPartID="5bd5dd01-20dd-48e6-ad74-b709773caab1" ...

You can than apply styling to it with a CSS attribute selector:

[WebPartID="5bd5dd01-20dd-48e6-ad74-b709773caab1"] { ... }

It is not a great way because the attribute selectors are rather slow but it works...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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