Как поместить button в input
Необходимо создать счетчик товаров для добавления в корзину, сделал так
up.onclick = () => {
numericUpDown.value = (isNaN(numericUpDown.value)) ? 1 : +numericUpDown.value + 1;
};
down.onclick = () => {
numericUpDown.value = (numericUpDown.value) > 0 ? +numericUpDown.value - 1 : 0;
}
.test {
color: #484E56;
border: none;
font-size: 18px;
text-align: center;
width: 129px;
height: 48px;
background: rgba(193, 217, 94, 0.22);
border-radius: 50px;
}
.test:focus {
outline: 0;
outline-offset: 0;
}
.minus {
background: url("img/minus.png");
width: 40px;
height: 40px;
border: none;
outline: none;
}
.plus {
background: url("img/plus.png");
width: 40px;
height: 40px;
border: none;
outline: none;
}
<div class="counter">
<input id="numericUpDown" type="number" value="1" class="test" onkeypress="return false" />
<button id="down" class="minus"></button>
<button id="up" class="plus"></button>
</div>
Проблема в том, что не могу "засунуть" кнопки плюс и минус в input. Новичок в верстке.
Сейчас это выглядит как-то так
Ответы (2 шт):
Автор решения: Вячеслав Лядов
→ Ссылка
Есть вариант позиционировать отрицательными значениями относительно родительского элемента.
.range {
width: 80px;
height: 30px;
}
.number {
text-align: center;
width: 80px;
height: 28px;
border-radius: 7px;
}
.up,
.down {
border-radius: 5px;
position: relative;
}
.up {
left: 4px;
top: -28px;
}
.down {
top: -28px;
right: -36px;
}
<div class="range">
<input type="number" class="number">
<button class="up">+</button>
<button class="down">-</button>
</div>
Выходит такое:
Автор решения: CbIPoK2513
→ Ссылка
Вот такой вариант
up.onclick = () => {
numericUpDown.value = (isNaN(numericUpDown.value)) ? 1 : +numericUpDown.value + 1;
};
down.onclick = () => {
numericUpDown.value = (numericUpDown.value) > 0 ? +numericUpDown.value - 1 : 0;
}
.counter {
display: inline-flex;
flex-direction: row;
align-items: center;
max-width: 100%;
position: relative;
}
.counter input,
.counter button {
border: none;
outline: none;
box-sizing: border-box;
}
.counter .test {
display: block;
width: 100%;
height: 40px;
border-radius: 40px;
text-align: center;
padding: 0 45px;
background: #f2f6de;
color: #333;
font-size: 15px;
}
.counter button {
display: block;
width: 40px;
height: 40px;
border-radius: 40px;
background: #dce7a4;
color: #1e8448;
font-weight: bold;
font-size: 20px;
line-height: 0;
position: absolute;
}
.counter .minus {
left: 0;
}
.counter .plus {
right: 0;
}
/* Код чтобы убрать стрелки у стандартного input number */
.counter > input[type="number"]::-webkit-outer-spin-button,
.counter > input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none !important;
margin: 0;
}
.counter > input[type="number"] {
-moz-appearance: textfield;
}
<div class="counter" style="width: 150px">
<input id="numericUpDown" type="number" value="1" class="test" onkeypress="return false" />
<button id="down" class="minus">-</button>
<button id="up" class="plus">+</button>
</div>

