I put elements side by side using inline-block
.
I wanted to make the gap between elements 2px, so I set a negative margin for the span
element.
* {
margin: 0;
}
.content {
width: 100%;
min-width: 320px;
}
span {
display: inline-block;
}
span {
display: inline-block;
width: 78px;
padding: 3px 10px;
cursor: pointer;
background: #ddd;
color: #777;
margin-right: -2px;
width: calc(25% - 22px);
}
span:hover {
background: #eee;
}
<div class="content">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
Question 1. It worked with Firefox 64.0. However, when checking with Google Chrome 71.0, the last span
element went around. Browser's default font size is 16px for Firefox and Chrome.
Also, it worked fine in CodePen, but it did not work properly on the stackoverflow code snippet.
Why are these problems occurring? I have read below question, but it does not appear to be about the reason for this problem.
Screenshot in Firefox (CodePen version):
Screenshot with Firefox (stackoverflow's run snippet version):
Screenshot in Google Chrome:
Question 2. I set width: 25%
to make the span
element full width. However, the horizontal width did not become full unless 22 px was subtracted from 25% (in Firefox).
How can I calculate a value of 22px?
I can't understand why (padding-left + padding-right)*4 - negative-margin = 22px
, even though padding-left
and padding-right
are set in each span
element (total 80 px).
I have read below question, but it does not appear to be about the reason for this problem.