<header>
<h2 class="title">element.style.maxWidth</h2>
<p class="description">Змінює максимальну ширину елемента.</p>
</header>
<main>
<div class="result">
<button id="increaseWidthBtn">Збільшити ширину</button>
<button id="decreaseWidthBtn">Зменшити ширину</button>
<div id="textBlock" class="text-block">
<p>Це текстовий блок, ширина якого змінюється за допомогою кнопок. Натисніть кнопки для збільшення або зменшення ширини цього блоку. Цей приклад демонструє роботу з властивістю maxWidth в реальному часі.</p>
</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;
}
.text-block {
max-width: 200px;
margin: 20px auto;
padding: 10px;
background-color: #e0e0e0;
transition: max-width 0.5s ease;
}
button {
margin-top: 10px;
margin-right: 10px;
padding: 10px;
background-color: #3498db;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #2980b9;
}
const increaseWidthBtn = document.getElementById('increaseWidthBtn');
const decreaseWidthBtn = document.getElementById('decreaseWidthBtn');
const textBlock = document.getElementById('textBlock');
increaseWidthBtn.addEventListener('click', () => {
let currentWidth = parseInt(window.getComputedStyle(textBlock).maxWidth, 10);
if (currentWidth < 500) {
textBlock.style.maxWidth = (currentWidth + 50) + 'px';
}
});
decreaseWidthBtn.addEventListener('click', () => {
let currentWidth = parseInt(window.getComputedStyle(textBlock).maxWidth, 10);
if (currentWidth > 100) {
textBlock.style.maxWidth = (currentWidth - 50) + 'px';
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.