margin style property
Specifies or returns a shorthand property for setting the top, right, bottom and left margins, in that order.
The margin property is nearly equivalent to the padding property. Both insert space around an element,
but while padding inserts the space within, margin inserts it around the border of the element.
Syntax:
You can find the related objects in the Supported by objects section below.
This property is read/write.
CSS page for this property: margin |
Possible values:
The type of this property is string.
One of the following values: | ||||||||||||||||
|
Description of values:
Default. The sizes of the 'auto' margins are equal. | |||||||
Takes the value of this property from the computed style of the parent element. | |||||||
The size of the margin in length units. For the supported length units, see the length page. | |||||||
The size is the specified percentage of the width of the parent element. |
Default: this property has no default value.
|
Example HTML code 1:
|
||||
<head> <style> .outer { border: 1px solid #ffffff; background-color: #ffb08a; } .middle { margin: 20px; padding: 20px; border: 10px solid #000000; background-color: #ceb379; } .inner { height: 10px; border: 1px solid #ffffff; background-color: #ffffff; } </style> </head> <body> <table cellpadding="0px" cellspacing="10px"> <tr> <td style="background-color: #ffb08a;"> </td> <td>margin</td> <td style="background-color: #000000;"> </td> <td>border</td> <td style="background-color: #ceb379;"> </td> <td>padding</td> </tr> </table> <div class="outer"> <div class="middle"> <div class="inner"></div> </div> </div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the use of the margin property in JavaScript:
|
||||
<head> <style> .outer { border: 1px solid #ffffff; background-color: #ffb08a; } .middle { margin: 20px; padding: 20px; border: 10px solid #000000; background-color: #ceb379; } .inner { height: 10px; border: 1px solid #ffffff; background-color: #ffffff; } </style> <script type="text/javascript"> function ChangeMargin () { var middleDiv = document.getElementById ("middleDiv"); var marginInput = document.getElementById ("marginInput"); middleDiv.style.margin = marginInput.value + "px"; } </script> </head> <body> <table cellpadding="0px" cellspacing="10px"> <tr> <td style="background-color: #ffb08a;"> </td> <td>margin</td> <td style="background-color: #000000;"> </td> <td>border</td> <td style="background-color: #ceb379;"> </td> <td>padding</td> </tr> </table> <div class="outer"> <div id="middleDiv" class="middle"> <div class="inner"></div> </div> </div> <br /> margin: <input id="marginInput" type="text" value="10" size="2"/> <button onclick="ChangeMargin ();">Change</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CSSStyleDeclaration, htmlElement.currentStyle, htmlElement.runtimeStyle, htmlElement.style
HTML elements:
a, abbr, acronym, address, applet, b, bdo, big, blink, blockquote, body, button, caption, center, cite, code, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frameset, h1, h2, h3, h4, h5, h6, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, label, legend, li, listing, marquee, menu, nobr, object, ol, optgroup, option, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, td, textarea, th, tt, u, ul, var, xmp
Related pages:
External links:
User Contributed Comments