WPFwiki, wpf, wiki, .net 3.0, windows presentation foundation, FAQ, free resources, solution, development, microsoft Home of the world's largest WPF FAQ
Edit

Category

Layout

Edit

Question

What's the difference between Margin and Padding?

Edit

Answer

In the Windows UI world, Margin is a property of the FrameworkElement class, whereas Padding is a Control and Border class property. So, for all Control- and Border- derived classes, both these properties are present and have the following behavior.

Margin represents the distance between one side of a User Interface (UI) element and the adjacent UI element or the container's limit. For instance, the distance between a TextBox and a Button to its right will be the TextBox's right margin width + the Button's left margin width. Wpfwiki Air Jordan

Padding represents the distance between the side of the control (which can be the margin) and its content. The content depends on the type of the control.

Margin is outside the UI element, while Padding is inside it. In other words, the background of the control will extend to the padding zone but not to the margin. Another way of seeing the difference is if you specify a precise width and height for a control, the margin won't have an impact on the control size: from the container's point of view it is as if the margin is added to the specified size. But the padding will reduce the content's available space: it will be smaller than the specified size.

Negative Margins

Most elements have a default margin from the built-in style. So, a label and a button will be spaced apart slightly. If you want them closer, you can use negative margin values. And the values are real numbers, so Margin="0.5 0 -5.1234 1.1" is fine.

Difference from HTML CSS

In CSS, the four values go in order: Top Right Bottom Left
In XAML, the four values go in order: Left Top Right Bottom

Design your own engagement rings with custom diamond engagement rings. Check out my personal blog

All content is Copyright ©2007 Xceed Software Inc. unless otherwise indicated. See the Terms of Service. Contributors must read and agree to the Contribution Policy. WPFwiki is brought to you by Xceed, makers of the powerful yet free Xceed DataGrid for WPF.