Friday, February 15, 2008

SharePoint Web Part Rounded Corners - CSS and no inheritance :)

Yes, just as the title reads:
- no custom webparts
- only css styles and images
- any webpart

Lately I have been searching the web for solutions regarding rounded corner webparts. I want to be able to use this solution on any site - so JPEG images as corners are out of question, since I need the webpart displayed on any background. PNG only work in IE7 and above, so first drawback is at the corner.

If you tried working on this much desired skin, you know that the tag layout of the webpart header is not very complex, so you need to play around with CSS selectors and margins and tricks, but none of these combined really does it.

Well here is my trick: the next image is the left corner of the webpart.




If you set this image as background for a TD, the image will stretch as far as the cell goes, so it won't overlap the right corner of the webpart header. You have to make it wide enough for (almost) any resolution/page width/webpart width to display correctly. One the other hand though, many sites today have a fixed width of the main area content, so there you go - you can anticipate how wide the webpart can get.

You also need the right corner image:


Of course, you can use an image that has rounded corners both at the top and the bottom.

And here are the classes:

.ms-WPHeader
{
 background-color:transparent;

}

tr.ms-WPHeader TD /* right corner */
{
 background-color:transparent;
 background-image:url('wpright2.png');
 background-repeat:no-repeat;
 background-position:right top;
 text-align:left;
 vertical-align:top;
 width:35px;
 height:26px;
 margin-top:-1px;
 overflow:visible;
 margin-right:10px;
}

.ms-standardheader.ms-WPTitle /* left corner */
{
 background-color:transparent;
 background-image:url('wpleftlarge.png');
 background-repeat:no-repeat;
 text-align:left;
 padding-left:25px;
 height:25px;
 }

.ms-standardheader.ms-WPTitle A:link, .ms-standardheader.ms-WPTitle A:visited /* title area */
{
 background-color:transparent;
 background-image:none;
 }

.ms-WPHeader DIV /* right corner action arrow */
{
 background-color:transparent;
 background-image:none;
 
 text-align:left;
 width:14px;
 
 overflow:hidden;
 vertical-align:bottom;
 margin-right:10px;
 margin-top:-1px;
 border:0px;
}

.ms-WPHeader TD /* gets rid of the blue bottom border (present in default style)
      under the web part header*/
{
 border: 0px;
}



The IE result:


The Mozilla result:

In Mozilla there is one issue: the webpart actions arrow on the right doesn't recognize the right margin value, so it's right on the edge.


And there you have it. Unfortunately with the drawback of a wide image load, fortunately a solution.

Posted by Madalina at 12:03:04 | Permanent Link | Comments (11) |
Comments
1 2
1 - Hi thanks for this very nice tip. I was able to achieve the desired rounded corners display. But I'm having an issue where if a user doesn't have enough permissions (e.g. reader -no action arrow), it will not show the right corner. Do you have a workaround for that? (Comment this)

Written by: MIKED at 2008/02/25 - 21:22:51
profile
2 - Yes, there is a work around.

If you add 'margin-right:10px;' to the .ms-standardheader.ms-WPTitle class, the right corner will be round if the action arrow isn't visible. The trick you have to pull is find out when the arrow isn't shown.

To accomplish this, you have to write some javascript that looks in the page to find if the right corner of webparts is there. Here it is:

//find all WPHeaders in IE (DIV)
var arr = document.getElementsByTagName("DIV");
var str = 0;
for (i = 0; i < arr.length; i++) {
if(arr[i].className == 'ms-HoverCellInActive')
str = str + 1;
}

In Mozilla the right corner is not rendered as DIV, so you need to look for the IMG elements with the same className. So you need to look at the browser in javascript code and then write separate codes for IE and other browsers.

Next, you have to build a separate stylesheet for the "no action arrow" scenario and use javascript again to switch between stylesheets based on the value of str.

There it is. They say it's robot labor, but it really isn't. (Comment this)

Written by: Madalina at 2008/02/27 - 12:24:25
3 - thanks for the tutorial on how to get rounded corners for the header, madalina!

can you explain how you'd get a rounded footer/bottom to a web part?
 (Comment this)

Written by: Norm at 2008/03/25 - 00:31:05
profile
4 - Hi Norm,

I don't think rounded corners for the bottom are possible to achieve. On the bottom of the webpart you have a single TD element. So what you can do is maybe round one of the corners, but not both. (Comment this)

Written by: Madalina at 2008/03/25 - 19:27:37
5 - This has been very helpful and we've implemented this in one of our designs but can you tell me why it looks less than beautiful in edit mode?

Thanks,

Marcy (Comment this)

Written by: Anonymous at 2008/03/29 - 03:35:35
profile
6 - It's not that bad... it only shows the left corner rounded, and the right one square :)
 (Comment this)

Written by: Madalina at 2008/03/29 - 13:20:20
7 - This is an awesome post! Thank you so much! ;-) - Celina (Comment this)

Written by: Celina at 2008/04/04 - 05:56:46
8 - Hi. This has been a very helpful post. But after following the css above, i still didnt see my right corner. But it is actually there, its just been covered by the long left image. How do you overcome this ? (Comment this)

Written by: Affendi at 2008/04/24 - 19:15:09
profile
9 - Hi Affendi,

The left image does not overlap the right corner. If you apply the styles to an unbranded (new) site, you will see that the right corner displays correctly. Maybe in the site you are applying the changes to the styles overlap with other css classes that already exist in your skin. You can try to put my clsses at the end of your *.css file, as the last changes made to an object are the ones applied. (Comment this)

Written by: Madalina at 2008/04/25 - 09:19:27
10 - Hi Madalina,

Thanks for posting this. It saved my day!

Cheers
Jag
 (Comment this)

Written by: Jag at 2008/05/07 - 01:49:31
Write a comment






1 2