HTML | DOM Style marginRight Property Last Updated : 03 Jun, 2022 Comments Improve Suggest changes Like Article Like Report The Style marginRight property in HTML DOM is used to set or return the right margin of an element. Syntax: It returns the marginRight property.object.style.marginRightIt is used to set the marginRight property.object.style.marginRight = "length|percentage|auto|initial| inherit" Return Values: It returns a string value, which representing the right margin of an element. Property Values: length: It is used to specify the margin in fixed length units. The default value is 0. Example: html <!DOCTYPE html> <html> <head> <title> DOM Style marginRight Property </title> <style> .container { display: flex; flex-direction: row; padding: 10px 1px; } .div1, .div2 { padding: 5px; border: 2px solid; } </style> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>DOM Style marginRight Property</b> <div class="container"> <div class="div1">Block One</div> <div class="div2">Block Two</div> </div> <button onclick="setMargin()"> Change marginRight </button> <!-- Script to set marginRight to a fixed value --> <script> function setMargin() { elem = document.querySelector('.div1'); elem.style.marginRight = '50px'; } </script> </body> </html> Output:Before clicking the button: After clicking the button: percentage: It is used to specify the amount of margin as a percentage relative to the width of the containing element. Example: html <!DOCTYPE html> <html> <head> <title> DOM Style marginRight Property </title> <style> .container { display: flex; flex-direction: row; padding: 10px 1px; } .div1, .div2 { padding: 5px; border: 2px solid; } </style> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>DOM Style marginRight Property</b> <div class="container"> <div class="div1">Block One</div> <div class="div2">Block Two</div> </div> <button onclick="setMargin()"> Change marginRight </button> <!-- Script to set marginRight to a fixed value --> <script> function setMargin() { elem = document.querySelector('.div1'); elem.style.marginRight = '20%'; } </script> </body> </html> Output:Before clicking the button: After clicking the button: auto: If the value is set to 'auto', then the browser automatically calculates a suitable value for the margin size. Example: html <!DOCTYPE html> <html> <head> <title> DOM Style marginRight Property </title> <style> .container { display: flex; flex-direction: row; padding: 10px 1px; } .div1, .div2 { margin-right: 50px; padding: 5px; border: 2px solid; } </style> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>DOM Style marginRight Property</b> <div class="container"> <div class="div1">Block One</div> <div class="div2">Block Two</div> </div> <button onclick="setMargin()"> Change marginRight </button> <!-- Script to set marginRight to auto --> <script> function setMargin() { elem = document.querySelector('.div1'); elem.style.marginRight = 'auto'; } </script> </body> </html> Output:Before clicking the button: After clicking the button: initial: It is used to set the property to its default value. Example: html <!DOCTYPE html> <html> <head> <title> DOM Style marginRight Property </title> <style> .container { display: flex; flex-direction: row; padding: 10px 1px; } .div1, .div2 { margin-right: 20px; padding: 5px; border: 2px solid; } </style> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>DOM Style marginRight Property</b> <div class="container"> <div class="div1">Block One</div> <div class="div2">Block Two</div> </div> <button onclick="setMargin()"> Change marginRight </button> <!-- Script to set marginRight to initial --> <script> function setMargin() { elem = document.querySelector('.div1'); elem.style.marginRight = 'initial'; } </script> </body> </html> Output:Before clicking the button: After clicking the button: inherit: It is used to inherit the value from its parent element. Example: html <!DOCTYPE html> <html> <head> <title> DOM Style marginRight Property </title> <style> .container { margin-right: 50px; display: flex; flex-direction: row; } .div1, .div2 { padding: 5px; border: 2px solid; } </style> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>DOM Style marginRight Property</b> <div class="container"> <div class="div1">Block One</div> <div class="div2">Block Two</div> </div> <button onclick="setMargin()"> Change marginRight </button> <!-- Script to set marginRight to inherit --> <script> function setMargin() { elem = document.querySelector('.div1'); elem.style.marginRight = 'inherit'; } </script> </body> </html> Output:Before clicking the button: After clicking the button: Supported Browsers: Google Chrome 1 and aboveEdge 12 and aboveInternet Explorer 3 and aboveFirefox 1 and aboveOpera 3.5 and aboveSafari 1 and above Create Quiz Comment S sayantanm19 Follow 0 Improve S sayantanm19 Follow 0 Improve Article Tags : Web Technologies HTML HTML-DOM Explore HTML BasicsHTML Introduction4 min readHTML Editors4 min readHTML Basics7 min readStructure & ElementsHTML Elements4 min readHTML Attributes7 min readHTML Headings3 min readHTML Paragraphs3 min readHTML Text Formatting4 min readHTML Block and Inline Elements3 min readHTML Charsets4 min readListsHTML Lists3 min readHTML Ordered Lists5 min readHTML Unordered Lists4 min readHTML Description Lists3 min readVisuals & MediaHTML Colors11 min readHTML Links Hyperlinks2 min readHTML Images7 min readHTML Favicon4 min readHTML Video4 min readLayouts & DesignsHTML Tables9 min readHTML Iframes4 min readHTML Layout4 min readHTML File Paths3 min readProjects & Advanced TopicsHTML Forms5 min readHTML5 Semantics5 min readHTML URL Encoding4 min readHTML Responsive Web Design11 min readTop 10 Projects For Beginners To Practice HTML and CSS Skills8 min readTutorial ReferencesHTML Tags - A to Z List5 min readHTML Attributes Complete Reference8 min readHTML Global Attributes5 min readHTML5 Complete Reference8 min readHTML5 MathML Complete Reference3 min readHTML DOM Complete Reference15+ min readHTML DOM Audio/Video Complete Reference2 min readSVG Element Complete Reference5 min readSVG Attribute Complete Reference8 min readSVG Property Complete Reference7 min readHTML Canvas Complete Reference4 min read Like