<header>
<h2 class="title">element.style.margin</h2>
<p class="description">Змінює відступи елемента за допомогою JavaScript</p>
</header>
<main>
<div class="result">
<!-- Контейнер з інпутами для введення відступів -->
<div class="controls">
<label for="marginTop">Верхній відступ (px):</label>
<input type="number" id="marginTop" value="0">
<label for="marginRight">Правий відступ (px):</label>
<input type="number" id="marginRight" value="0">
<label for="marginBottom">Нижній відступ (px):</label>
<input type="number" id="marginBottom" value="0">
<label for="marginLeft">Лівий відступ (px):</label>
<input type="number" id="marginLeft" value="0">
<button id="applyMargins">Застосувати відступи</button>
</div>
<!-- Елемент, до якого будуть застосовуватись відступи -->
<div class="box" id="box">Тестовий елемент</div>
</div>
</main>
body {
font-size: 16px;
line-height: 1.5;
font-family: monospace;
}
header {
background-color: #f1f1f1;
margin-bottom: 25px;
padding: 15px;
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}
header h2.title {
padding-bottom: 15px;
border-bottom: 1px solid #999;
}
header p.description {
font-style: italic;
color: #222;
}
.result {
background-color: #f8f8f8;
padding: 15px;
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}
.controls {
margin-bottom: 20px;
}
.controls label {
display: block;
margin: 5px 0 2px;
}
.controls input {
width: 100%;
padding: 5px;
margin-bottom: 10px;
}
.controls button {
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
.controls button:hover {
background-color: #45a049;
}
.box {
width: 200px;
height: 100px;
background-color: #b0c4de;
text-align: center;
line-height: 100px;
}
document.getElementById('applyMargins').addEventListener('click', function() {
// Отримання значень відступів з інпутів
const marginTop = document.getElementById('marginTop').value + 'px';
const marginRight = document.getElementById('marginRight').value + 'px';
const marginBottom = document.getElementById('marginBottom').value + 'px';
const marginLeft = document.getElementById('marginLeft').value + 'px';
// Застосування відступів до елемента
const box = document.getElementById('box');
box.style.marginTop = marginTop;
box.style.marginRight = marginRight;
box.style.marginBottom = marginBottom;
box.style.marginLeft = marginLeft;
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.