I am trying to make an input with prefix & suffix elements :
- Alone, the input is rounded
- With a prefix (resp. suffix), the left (resp. right) side is rounded, but not the right (resp. left) side
- With both prefix & suffix, the input isn't rounded at all (the buttons are rounded)
I have managed to handle the prefix element, but I can't seem to find a CSS selector to say "Round the right side of the input if a element follows the input", let it be on the input or on its container.
I would like to do it in HTML/CSS only (no JS). Any ideas ?
PS : I am not asking for a previous sibling selector. I am asking if there is a way to select the input that is followed by a suffix. That includes :
- previous sibling selector (which doesn't exist)
- element being followed by another one (which is the opposite, so probably doesn't exist either)
- parent container having an input as the last child and nothing else
- parent having N children (allowing me to know that if there's 3 children, then the input should be rounded)
:root {
--radius: 5px;
}
div {
height: 36px;
margin: 12px;
}
* {
box-sizing: border-box;
}
input {
height: 100%;
padding: 0;
border-radius: var(--radius);
border: 1px solid #ccc;
padding: 0 6px;
}
button {
height: 100%;
background: teal;
border: 0;
color: white;
}
button[prefix] {
border-radius: var(--radius) 0 0 var(--radius);
}
button[suffix] {
border-radius: 0 var(--radius) var(--radius) 0;
}
div > button[prefix] + input {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
div > button[suffix] ~ input {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
<div>
<input type="text" placeholder="Type here">
</div>
<div>
<button prefix>P</button>
<input type="text" placeholder="Type here">
<button suffix>S</button>
</div>
<div>
<input type="text" placeholder="Type here">
<button suffix>S</button>
</div>
<div>
<button prefix>P</button>
<input type="text" placeholder="Type here">
</div>