This post describes how to customize a standard Web Part in SharePoint by using SharePoint Designer 2010. Depending of the look and feel or factors such as accessibility, performance or cross browser stability it could be a challenge to bend the web part UI exactly in the way you want. The markup of the web parts in SharePoint contains a quite deep nested table structure and the elements do not always have an id or class.

When you need to create an advanced design for the web parts, the alternatives is to manipulate the DOM with jQuery or create custom web part control wrappers with code. Besides of this, CSS 3 comes with some interesting possibilities like box shadow and border radius that lets you create stuff like boxes with curved corners but it’s not an option here because we need to render the master page in IE8 mode to get all functionalities intact in SharePoint 2010.

So what can you do quick and easily then? The web part headers and all the cells in header row can easily be customized with CSS just like the borders around. I have made four examples with different colors you can download and use at the bottom of this page.

Using only CSS and images for rounded corners is the easiest approach where you don’t need plugins like CSS3 Pie or other htc solutions, fallback methods for different browsers or care about slow rendering when rewriting the already complex and nestled DOM. But when you reach the border of what is possible to do with just CSS and images it’s time to bend the DOM with our friend jQuery or fire up Visual Studio for write a control adapter. Take a look at this blog post from ‘All things SharePoint’ that describes how to use jQuery to adding classes and wrappers into the DOM so you can get a wrapper with rounded corners for the web parts, and take a look at Waldek Mastykars post about how to removing the web part tables with code.

Back to the basics, let’s see how you can customize the web part headers the easy way.
The following example, see the image above, applies background images for the corners and for the middle section. The left and right corners have dimensions of 7 x 33 pixels, and the middle image has dimensions of 14 x 33 pixels.

Let’s go

Download the images and put them into Style Library/BlogBranding/Images or change the path in the CSS below if you want to put the images in some other folder.

/* WebPart headers */
/* All tdS in the row */
.ms-WPHeader > TD{
background-image: url('/Style Library/BlogBranding/Images/WP-MidGreen.png');
background-repeat:repeat-x; 
padding-left:1px; padding-right:1px; height:33px;
border-bottom-style:none!important;border-bottom-color:inherit!important; border-bottom-width:0px!important;
}
/* Left cell */
.ms-WPHeader td:first-child {
width:5px;
background-image:url('/Style Library/BlogBranding/Images/WP-LeftGreen.png')!important;
background-repeat:no-repeat;
}
/* Right cell */
.ms-wpTdSpace {
width:7px;
background-image:url('/Style Library/BlogBranding/Images/WP-RightGreen.png')!important;
background-repeat:no-repeat;
background-color:transparent;
}
/* Arrow */
.ms-WPHeaderTdMenu{
background-color:transparent;
border:0px!important;
}
/* Web part title */
.ms-WPTitle {
padding-left:10px;
font-family:Arial, Helvetica, sans-serif;
color:#fff;
font-weight:bold;
margin-bottom:1px;
font-size:14px;
}
/* linked title and visited */
.ms-WPTitle a, .ms-WPTitle a:visited {
color:#fff;
text-decoration:none;
}
/* hover title */
.ms-WPTitle a:hover {
color:#333;
text-decoration:none;
}
/* hover web part menu */
.ms-WPHeaderTdMenu:hover{
border-left:1px solid transparent;
background-image: url('/Style Library/BlogBranding/Images/WP-MidGreen.png');
}

More stuff you can do

If you want to do something special just for one single web part based on some condition or similar? Let’s say you want to have a different look and feel for some web part only if its header text is ‘Links’. I made up an example that makes the header title red and adds a border with a dropshadow filter older IEs and BoxShadow for modern browsers. Other web parts will not apply this look and feel.

Here’s the jQuery

$(document).ready(function() { 
var WPtitle = $('.ms-WPTitle span'); 
for (var i = 0; i <= WPtitle.length; i++) { 
	if ($(WPtitle[i]).text() == 'Links') { 
            $(WPtitle[i]).css({'color': 'red'}); 
           	$(WPtitle[i]).parents().eq(10).addClass("WebPartBorder");
	}
	else if ($(WPtitle[i]).text() == 'Shared Documents') { 
            $(WPtitle[i]).css({'color': 'blue'}); 
	} 
} 
});

Here’s the CSS

/* All TDs in the table row */
.ms-WPHeader TD{
background-color: #f7f7f7;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ebebeb', endColorstr='#e2e2e2');
background: -webkit-gradient(linear, left top, left bottom, from(#e2e2e2), to(#ebebeb));
background: -moz-linear-gradient(top, #e2e2e2, #ebebeb);
border-top:1px #cfcfcf solid; border-bottom:1px #e2e2e2 solid!important;
padding:3px;
}
/* for css / jquery drop shadow */
.WebPartBorder{
border:1px #777 solid!important;
-moz-box-shadow: 5px 5px 5px #ccc;
-webkit-box-shadow: 5px 5px 5px #ccc;
box-shadow: 5px 5px 5px #ccc;/* CSS3 */
background-color:#fff;
filter: progid:DXImageTransform.Microsoft.DropShadow(OffX=5, OffY=5, Color=#e9e9e9);    
}
.WebPartBorder > tbody {
background-color:#f7f7f7!important
}
/* Border to the sides */
.ms-WPHeader td:first-child {
border-left:1px #e2e2e2 solid;
border-right:0px;
}
.ms-wpTdSpace {
border-right:1px #e2e2e2 solid;
}
/* Web part title */
.ms-WPTitle {
color: #333;
font-weight:bold;
}
/* linked title and visited */
.ms-WPTitle a, .ms-WPTitle a:visited {
color: #333!important;
text-decoration:none!important;
}
/* hover title */
.ms-WPTitle a:hover {
color:#0072bc!important;
}
/* hover web part menu */
.ms-WPHeaderTdMenu:hover{
 background-image:none;
border-top:1px solid #ccc;
border-right:1px solid transparent;
border-left:1px solid transparent;
background-color:#f7f7f7;
}
/* for the jQuery */
.LinkTextColor {
color:red;
}

Hopes this gives you some ideas, and please feel free to drop a comment!

/ Christian

Advertisements