Tips/Tricks: How to center Silverlight control on a webpage?

[Post written as of Silverlight 2 beta 1]


How can I center my Silverlight UserControl on the center of the web page (horizontally and vertically). To reproduce, create a new default Silverlight application in VS2008. I want that default xaml to appear in the center of the page, instead of the top left.


My answer :

Centering something horizontally is so easy but vertically (especially for cross-browser). However, I found the CSS trick that works in any browser from this link. (Thanks to the original developer for this CSS trick.)


Let’s say your project name is “SL2Center” .

The following code is the default code from SL2CenterTestPage.aspx

<%@ Page Language="VB" AutoEventWireup="true" %>

<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" style="height:100%;">
<head runat="server">
<title>Test Page For SL2Center</title>
<body style="height:100%;margin:0;">
<form id="form1" runat="server" style="height:100%;">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div  style="height:100%;">
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SL2Center.xap" Version="2.0" Width="100%" Height="100%" />

You change the width and height properties of Usercontrol to 300. And also, you add one TextBlock in that control.

<UserControl x:Class="SL2Center.Page"
Width="300" Height="300">
<Grid x:Name="LayoutRoot" Background="Red">
<TextBlock Text="Silverlight Content" Foreground="Yellow" FontSize="30" FontWeight="Bold" VerticalAlignment="Center"  HorizontalAlignment="Center"/>

The first thing that you need to do is that you have put the following CSS in <head> of SL2CenterTestPage.aspx.

<style type="text/css">

position: absolute;
left: 50%;
top: 50%;
width: 300px;
height: 300px;
margin-left: -150px; /* half of width */
margin-top: -150px;  /* half of height */
background-color: #6699CC;


I assume that the height and width properties of your xaml is 300. So, I set 300 to width and height in CSS.

Secondly, you have to set the class of DIV to the CSS class that we have created.

<div class="sl">
<asp:Silverlight ID="Xaml1" runat="server"
Source="~/ClientBin/SL2Center.xap" Version="2.0" Width="100%"
Height="100%" />

That’s all. You can run the application. You will see that the SL content is showing at the middle of web page. Cool, isn’t it? but one thing you should know is that if you want to change either width or height of XAML, you have to change the CSS again. I didn’t spend that much time for that but I hope you will get some ideas how you can customize it. Let me know if you have any problem or better solution.

You can download the sourcecode here.

25 Replies to “Tips/Tricks: How to center Silverlight control on a webpage?”

  1. sorry. what you posted does work in firefox. the problem is with absolute positioning in firefox when you don’t specify a height and width. i want this since specifying the width and height pops scrollbars on some resolutions.

  2. Hi,

    I played with a few different ways to center Silverlight content and find that making the control fill the entire browser window (100% height and width) then using a grid control like the following works quite well:

  3. Woops, the code was stripped out of my comment. It’s just a (scale 9) grid with 3 columns and 3 rows. The center column’s width is set to the width of your content and the center row’s height to the height of your content. The content obviously then goes in the center cell. All other widths and heights are set to *.

  4. Thanks! This was an irritation as I couldn’t get my Silverlight control centered properly. This did the trick. Thanks for taking the time to post the advice.

  5. I use silverlight so I don’t have to deal with CSS issues. It seems that it would be so much easier to make the control take up the whole browser window then center whatever you need inside of that. Let Silverlight do the centering, that way you know it’ll work across browsers.

    I realize you were answering a question on a forum, but it wouldn’t hurt to ask the person if they had considered doing it the easy way.

  6. Ebenso vielfältig sind die Teile der Ernährung und doch helfen ein paar kleine Richtlinien, um bereits
    das gute Gesamtbild zu erkennen.

  7. First off I want to say terrific blog! I had a quick question which I’d like to ask if you
    do not mind. I was interested to know how you centerr
    yourself and clear your thboughts before writing. I have had trouble
    clearing my mind in getting my ideas out there. I do take pleasure in writing but it just seems
    like the first 10 to 15 minutes are lost just trying to
    figure out how to begin. Any suggestions or tips? Appreciate it!

Leave a Reply