what are margins and padding and how do they work margin and padding are essential properties in CSS for creating well structured readable web pages margins control the space outside an element helping to separate it from other elements and Define the layout structure while padding controls the space inside an element improving content readability and aesthetic appeal let's take a look at the following illustration in the illustration we have a black dotted border around the content the content in this case is the free code cam logo the dark blue color represents the padding which is used to
apply space inside the element between the content and its border the light blue color outside the black dotted border represents the margin the margin is the space applied to the outside of the HTML element let's take a look at some examples here is an HTML example of three paragraph elements on the page to apply spacing to the top of each paragraph element you can use the margin top property like this in this example we are applying 20 pixels of margin only to the top of each paragraph element we also have a solid black border on
all four sides so you can see the margin better here is what the example looks like rendered on the page the four different margin properties are margin top margin right margin bottom and margin left here is an example of using all four properties in this example we are assigning spacing values on all four sides of the paragraph element a solid black border has also been added so you can see the margins better here's what the example looks like rendered on the page another way to use the margin property is with shorthand notation you can specify
1 2 3 or four values at once let's explore these options together when using a singular value on the margin Shand that exact value will be applied to all four sides of the target element here is an example of using a single value on the margin Shand this code example will apply 10 pixels of margin equally to all four sides of the paragraph element when using two values the first value applies to the top and bottom while the second value applies to the left and right sides of the element here is an example of using
two values for the margin shorthand this sets the top and bottom margins to 10 pixels and 20 pixels for the left and right margins for the paragraph element if three values are provided the first value applies to the top margin the second value applies to the left and right margins and the third value to the bottom margin this sets the margin to 10 pixels for the top 20 pixels for the left and right and 30 pixels for the bottom when using four values this gives you more control as you can independently specify each margin value
for each side of the target element the first value tar targets the top the second value targets the right the third value targets the bottom and the fourth value targets the left here is an example of using the margin Shand with four values this sets the margin to 10 pixels for the top 20 pixels for the right 30 pixels for the bottom and 40 pixels for the left the pading property is used to apply space inside the element between the content and its border like the mar in property with four padding properties are padding top
padding right padding bottom and padding left here is an example on how to set the padding for a paragraph element this sets the padding to 10 pixels for the top 20 pixels for the right 30 pixels for the bottom and 40 pixels for the left here is what the example looks like rendered on the page as you can see the padding is applied to the content which is inside the Border unlike margin which applies to the outside of the Border just like the margin property you can also choose to use the shorthand for the padding
property you can either specify one two three or four values on the padding shorthand property as well here is an example of using the short hand for the paragraph element from earlier in the example using the short hand the code will set the padding to 10 pixels for the top 20 pixels for the right 30 pixels for the bottom and 40 pixels for the left of the paragraph element here is what the example looks like rendered on the page