let lastSelectButton;
document.querySelectorAll('.primaryButton').forEach(button => {
button.addEventListener('click', event => {
if (lastSelectButton) document.querySelector(`#${lastSelectButton} + div`).classList.toggle('inlb');
lastSelectButton = event.currentTarget.getAttribute('id');
document.querySelector(`#${lastSelectButton} + div`).classList.toggle('inlb');
}, false);
});
button {
text-align: left;
}
.primaryButton {
display: inline-block;
background: #bfbfbf;
border: none;
width: 100%;
padding: 5px;
}
.secondaryButton {
background: #d4d4d4;
border: none;
width: 100%;
text-indent: 1em;
}
.primaryButton:hover {
background: #a1a1a1;
cursor: pointer;
}
.secondaryButton:hover {
background: #bfbfbf;
cursor: pointer;
}
.none {
display: none;
}
.inlb {
display: inline-block !important;
}
<button class="primaryButton" id="p1">A</button>
<div class="none">
<button class="secondaryButton">1</button>
<button class="secondaryButton">2</button>
<button class="secondaryButton">3</button>
</div>
<button class="primaryButton" id="p2">B</button>
<div class="none">
<button class="secondaryButton">1</button>
<button class="secondaryButton">2</button>
</div>
<button class="primaryButton" id="p3">C</button>
<div class="none">
<button class="secondaryButton">1</button>
<button class="secondaryButton">2</button>
</div>
As seen, the secondary buttons do not occupy full width like the primary buttons. Why does this happen? How to correct it?
On the contrary, setting inline-block
s to block
s settles the matter:
let lastSelectButton;
document.querySelectorAll('.primaryButton').forEach(button => {
button.addEventListener('click', event => {
if (lastSelectButton) document.querySelector(`#${lastSelectButton} + div`).classList.toggle('inlb');
lastSelectButton = event.currentTarget.getAttribute('id');
document.querySelector(`#${lastSelectButton} + div`).classList.toggle('inlb');
}, false);
});
button {
text-align: left;
}
.primaryButton {
display: block;
background: #bfbfbf;
border: none;
width: 100%;
padding: 5px;
}
.secondaryButton {
background: #d4d4d4;
border: none;
width: 100%;
text-indent: 1em;
}
.primaryButton:hover {
background: #a1a1a1;
cursor: pointer;
}
.secondaryButton:hover {
background: #bfbfbf;
cursor: pointer;
}
.none {
display: none;
}
.inlb {
display: block !important;
}
<button class="primaryButton" id="p1">A</button>
<div class="none">
<button class="secondaryButton">1</button>
<button class="secondaryButton">2</button>
<button class="secondaryButton">3</button>
</div>
<button class="primaryButton" id="p2">B</button>
<div class="none">
<button class="secondaryButton">1</button>
<button class="secondaryButton">2</button>
</div>
<button class="primaryButton" id="p3">C</button>
<div class="none">
<button class="secondaryButton">1</button>
<button class="secondaryButton">2</button>
</div>
What happens when I do this that solves the problem?
Edit: This question is not a duplicate of this question, the main issue of in my question is buttons taking full width, while the other question asks for a differences between display: inline-block
and display: block
.