Automatically translated.View original post

css margin -left-right

The margin-left in CSS is the property used to determine the margin to the left of an HTML element. That is, it increases the space between the left edge of that element and the side element or the edge of the container. The value of the margin-left can be a number with a unit of measure (such as pixels, em, rem) or a percentage value, or a special value such as auto.

Example:

Code

< div style = "margin-left: 20px; border: 1x solid black; width: 100px;" >

< p > This message has a margin-left: 20px < / p >

< / div >

In this example, the div box has a left margin of 20 pixels, making the text inside the box 20 pixels away from the left edge of the box.

Additional features:

Negative value:

Negative margin-left can be used, which causes the element to move more to the left than the normal boundary.

Margins: auto:

Applying margin: auto to an element with a defined width will cause the element to be centered horizontally within the container.

Differences from left:

The margin-left is used to determine the distance between the element and the left edge, while the left is used to determine the position of the absolute position.

The margin-right in CSS is the property used to define the right margin of an HTML element. It creates a blank space between that element and the other elements on the right. The value of the margin-right can be a number with a unit of measurement (such as pixels, em, rem) or auto values.

Example of use:

Margin-right: 20px; / / Define the right margin of 20 pixels.

Margin-right: 1.5em; / / Define the right margin 1.5 times the current font size.

Margin-right: auto; / / Let the browser calculate the appropriate right margin.

Difference between margin and padding:

Margin:

Free space on the outside of the element (border).

Paddling:

Free space within an element (between borders and content).

Conclusion:

Margin-right is a CSS feature used to control the outer distance to the right of an HTML element, allowing the placement and spacing between elements on a web page.# Basics made web site

2025/8/16 Edited to

... Read moreในทางปฏิบัติ การใช้ margin-left และ margin-right ใน CSS นั้นมีความสำคัญมากในการออกแบบเลย์เอาต์หน้าเว็บเพื่อเพิ่มประสิทธิภาพในการจัดวางเนื้อหาและการจัดระยะห่างระหว่างองค์ประกอบต่างๆ อย่างเหมาะสม โดยคุณสมบัติ margin-left จะช่วยกำหนดช่องว่างทางด้านซ้ายขององค์ประกอบ และ margin-right ในการกำหนดช่องว่างทางด้านขวา ซึ่งส่งผลโดยตรงต่อการจัดวางองค์ประกอบให้ดูสะอาดตาและเข้าใจง่าย การตั้งค่า margin สามารถกำหนดได้หลายค่า เช่น หน่วยพิกเซล (px) ใช้งานง่ายและแม่นยำ หน่วย em หรือ rem ซึ่งสัมพันธ์กับขนาดตัวอักษร ทำให้เลย์เอาต์มีความยืดหยุ่นเข้ากับขนาดฟอนต์ และค่า auto ใช้สำหรับจัดวางให้องค์ประกอบอยู่กึ่งกลางภายในคอนเทนเนอร์ได้อย่างรวดเร็ว นอกจากนี้ การใช้ค่า margin ที่เป็นค่าลบ (negative margin) ก็เป็นอีกเทคนิคหนึ่งที่ช่วยให้สามารถขยับองค์ประกอบเข้าไปทับซ้อนหรือเข้ามาใกล้กับองค์ประกอบอื่นๆ มากขึ้น ซึ่งมีการนำไปใช้ในกรณีพิเศษ แต่ต้องคำนึงถึงผลกระทบต่อการแสดงผลบนอุปกรณ์ที่แตกต่างกัน ควรเข้าใจความแตกต่างระหว่าง margin และ padding อย่างชัดเจน โดย margin คือพื้นที่ว่างระหว่างองค์ประกอบกับองค์ประกอบอื่นในภายนอก ส่วน padding คือพื้นที่ว่างภายในองค์ประกอบระหว่างขอบและเนื้อหา การใช้ margin-left และ margin-right ที่เหมาะสมจะช่วยให้การจัดวางเว็บไซต์ดียิ่งขึ้น และเพิ่มความสวยงาม สุดท้าย เพื่อความเข้าใจและการประยุกต์ใช้ที่ดี ควรทดลองกำหนดค่า margin-left และ margin-right พร้อมทั้งสังเกตผลลัพธ์บนหน้าจอจริง พร้อมกับตรวจสอบการทำงานที่ถูกต้องในอุปกรณ์และเบราว์เซอร์ต่างๆ เพื่อให้มั่นใจว่าเลย์เอาต์เว็บตอบสนองและแสดงผลอย่างถูกต้องตามต้องการ