4

I am using calc() to measure the available width of the input after subtracting the width of the sibling elements.

<div class="containero">
  <button class="noey">No</button>
  <input  class="inpoot" />
  <button class="yeso">Yes</button>
</div>

And I have the following CSS.

.containero {
    width: 100%;
    background-color: yellow;
    display: inline-block;
    box-sizing:border-box;
}

.noey, .yeso {
  border: 1px solid red;
  width: 30px;
  height: 30px;
  text-align: center;
  vertical-align: middle; 
  display:inline-block;
  color: red;
  padding:0px;
  box-sizing:border-box;

}


.inpoot {
  height: 31px;
  margin: 0 5px;
  display:inline-block;
  width: calc(100% - 70px);
  box-sizing:border-box;
}

Now as per my calculations, the element .inpoot should fit in nice and cosy if I set its width to calc(100% - 70px); taking into account the widht of 30px for both the siblings, and then its own margin of 10px. But to my utter dismay, life isn't easy its not working as expected. The last sibling, .yeso is being pushed to next line.

DEMO 1

BUT!!! if I set the .inpoot width to calc(100% - 82px) voila!! it works, but I am flabbergasted where on GOD's beautiful earth, is that additional width coming from?

DEMO 2

1 Answer 1

2

You need to know that between inline and inline-block elements, the spaces matters. So if you have a whitespace between two inline elements, it takes account in the total calculation. To avoid this there are a lot of tricks, but the simplest is the following:

 .containero {
       font-size: 0;
 }

Add this property in your CSS and it works. Working example:

.containero {
    font-size: 0;
    width: 100%;
    background-color: yellow;
    display: inline-block;
    box-sizing:border-box;
}

.noey, .yeso {
  border: 1px solid red;
  width: 30px;
  height: 30px;
  text-align: center;
  vertical-align: middle; 
  display:inline-block;
  color: red;
  padding:0px;
  box-sizing:border-box;

}


.inpoot {
  height: 31px;
  margin: 0 5px;
  display:inline-block;
  width: calc(100% - 70px);
  box-sizing:border-box;
}
<div class="containero">
  <button class="noey">No</button>
  <input  class="inpoot" />
  <button class="yeso">Yes</button>
</div>

And please, don't use codepen.io since we need an account to modify the code and share it. It's better jsfiddle.net and the best choice is the integrated stacksnippets (like mine)

Sign up to request clarification or add additional context in comments.

3 Comments

@MohdAbdulMujib there's not a bug, is a feature. Spaces between inline elements are physically there. So it takes its space. It's not a bug, it's wide refferenced along internet. Search about inline-block and spaces. Good luck!
You can check this answer as correct. Thank you! :)
:D yeah I was waiting for the 5-mins timeout, then got caught up in something. Nonetheless thanks for reminding, here you go, you deserve it. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.