0

There are 6 buttons, the second one has something inside. But I defined all the buttons a fixed height and the inside content is not using up the room at all. Why it's still not in the expected position?

Please take a look at this simple code:

button {height:60px;width:50px;border:1px solid #000;box-sizing:border-box}
button span {display:block;color:grey; font-size:9px}
<button>1</button>
<button>2<span>2.1</span></button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
AGamePlayer
  • 7,404
  • 19
  • 62
  • 119

4 Answers4

0

This is due to the <br> tag, which indents downwards and therefore offsets your button. If you need to solve this problem, then I made such a solution for you, using position: absolute.

button {
  height:60px;
  width:50px;
  border:1px solid #000;
  box-sizing:border-box
}

button span {
  color:grey; 
  font-size:9px;

  position: absolute;
  transform: translate(-45%, 25%);
}
<button>1</button>
<button>2<br><span>2.1</span></button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
0

The reason is not the br tag (which you have already edited out), it's the fact that button by default (user agent styles) has display: inline-block, which means that your succession of buttons forms a textual line, it sits in line.

If you want to stick with that, you need to make sure all your buttons in that line align vertically, so use vertical-align:

button {
  height: 60px;
  width: 50px;
  border: 1px solid #000;
  box-sizing: border-box;
  vertical-align: middle;
}

button span {
  display: block;
  color: grey;
  font-size: 9px;
}
<button>1</button>
<button>2<span>2.1</span></button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
connexo
  • 53,704
  • 14
  • 91
  • 128
-1

Because you are putting br tag inside the second button which means new line.

So change it to (remove br tag)

<button>2<span>2.1</span></button>

https://jsfiddle.net/z1tao30g

Nefazodone
  • 69
  • 6
-2

here you are:

html:

<button>1<br><span class="visnon">.</span></button>
<button>2<br><span>2.1</span></button>
<button>3<br><span class="visnon">.</span></button>
<button>4<br><span class="visnon">.</span></button>
<button>5<br><span class="visnon">.</span></button>
<button>6<br><span class="visnon">.</span></button>

css:

button {height:60px;width:50px;border:1px solid #000;box-sizing:border-box}
button span {color:grey; font-size:9px}
.visnon{visibility: hidden;}
  • 3
    An explanation of _why_ this answers the question would make your answer a bit more useful. Otherwise, this isn't much more than a code-dump. – Tieson T. Nov 01 '20 at 06:57
  • sorry, i just added
    . to all of them and then i maked them invisible to user dont see that "." , now all of them have one dot under the number and it is invisible
    – MohsenGhetmir Nov 01 '20 at 07:58