<header>
<h2 class="title">element.style.backgroundPosition</h2>
<p class="description">Визначає позицію фону елемента.</p>
</header>
<main>
<div class="result">
<div class="demo-box" id="demoBox"></div>
<div class="controls">
<button onclick="changePosition('left top')">Лівий верх</button>
<button onclick="changePosition('center center')">Центр</button>
<button onclick="changePosition('right bottom')">Правий низ</button>
</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);
text-align: center;
}
.demo-box {
width: 300px;
height: 200px;
margin: 0 auto;
background-image: url('https://via.placeholder.com/300');
background-size: cover;
background-repeat: no-repeat;
transition: background-position 0.5s;
}
.controls {
margin-top: 20px;
}
.controls button {
margin: 5px;
padding: 10px;
font-size: 14px;
cursor: pointer;
}
function changePosition(position) {
const demoBox = document.getElementById('demoBox');
demoBox.style.backgroundPosition = position;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.