Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gap between border and child element with border radius

Tags:

html

css

I'm trying to implement radio buttons which kind of work like segmented controls:

* {
  margin: 0;
  padding: 0;
}

.container {
  background-color: brown;
  width: 80vw;
}

.box {
  display: flex;
  flex-direction: row;
  border: 2rem solid skyblue;
  border-radius: 999px;
}

label {
  flex: 1;
  padding: 2rem;
  border-radius: 999px;
  text-align: center;
}

input {
  display: none;
}

input:checked + label {
  background-color: skyblue;
}
<div class="container">
  <div class="box">
    <input type="radio" id="hello" name="test" checked />
    <label for="hello">Hello</label>
    
    <input type="radio" id="world" name="test" />
    <label for="world">World</label>
  </div>
</div>

However, there's an annoying gap of about 1px between the nested label and the parent div:

image showing gap between nested elements with border radii

This issue is similar to this question, but the workarounds suggested don't really work for my use case since I can't change the background color. I'm also curious if this is a browser bug or some kind of anti-aliasing issue.

like image 487
ParadoxFox Avatar asked Oct 29 '25 10:10

ParadoxFox


2 Answers

just add box shadow to the input:check + label

input:checked + label {
    background-color: skyblue;
    box-shadow: 0px 0px 0 1px skyblue;
}

Link to Jsfiddle

like image 184
Arham Chowdhury Avatar answered Nov 02 '25 08:11

Arham Chowdhury


I don't know the reason why you are getting that 1 px gap but you can add "transform: translateX(-1px);" to shift it left by 1px. This can work as a temp solution.

  label {
    flex: 1;
    padding: 2rem;
    border-radius: 200px;
    text-align: center;
    transform: translateX(-1px);
  }
like image 24
Tanish Surana Avatar answered Nov 02 '25 08:11

Tanish Surana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!