Thuộc tính HTML DOM style marginRight trả về và sửa đổi lề phải của một phần tử trong tài liệu HTML.
Cú pháp
Sau đây là cú pháp -
-
Trả lại lợi nhuận bên phải
object.style.marginRight
-
Sửa đổi marginRight
object.style.marginRight = “value”
Ví dụ
Hãy để chúng tôi xem một ví dụ về thuộc tính style marginRight -
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
background: lightblue;
height: 100vh;
}
p {
border: 2px solid #fff;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>DOM Style marginRight Property Example</h1>
<p>Hi! I'm awesome paragraph with some random text. Hi! I'm awesome paragraph with some random text. Hi! I'm awesome paragraph with some random text. Hi! I'm awesome paragraph with some random text.
</p>
<button onclick="add()" class="btn">Add margin</button>
<script>
function add() {
document.querySelector('p').style.marginRight = "50px";
}
</script>
</body>
</html> Đầu ra
Điều này sẽ tạo ra kết quả sau -
Nhấp vào “ Thêm ký quỹ ”Để thêm lề phải vào phần tử đoạn văn.