0

I have a div with 3 buttons, with some CSS applyed. (JSFiddle).

I want to delete the blank space between the div border and the buttons. I am new to HTML and CSS, so I guess it has an easy fix.

.navBar {
  display: block;
  border: 5px solid black;
  width: 521.5px;
  position: sticky;
  margin-left: auto;
  margin-right: auto;
  top: 2px;
}

.button {
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  cursor: pointer;
  background-color: #555555;
  transition-duration: 0.4s;
}
<div class="navBar">
    <button type="button" class="button">text1</button>
    <button type="button" class="button" >text2</button>
    <button type="button" class="button">text3</button>
</div>
Quentin Veron
  • 3,079
  • 1
  • 14
  • 32
Guy Adler
  • 15
  • 1
  • 11

2 Answers2

2

In many cases you don't have control over the white-spacing in your rendered HTML, e.g. if your CMS generates the HTML.

To fix the white-space-problem that arises with layouts based on display: inline-block;, the standard method is to set font-size: 0; on the parent element, then re-set the font-size for the children as per your requirements.

.navBar {
  display: block;
  border: 5px solid black;
  width: 521.5px;
  position: sticky;
  margin-left: auto;
  margin-right: auto;
  top: 2px;
  font-size: 0;
}

.button {
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  cursor: pointer;
  background-color: #555555;
  transition-duration: 0.4s;
  font-size: 16px;
}
<div class="navBar">
  <button type="button" class="button">text1</button>
  <button type="button" class="button">text2</button>
  <button type="button" class="button">text3</button>
</div>
connexo
  • 53,704
  • 14
  • 91
  • 128
0

If you are looking to fit the buttons in the wrapper. Then this might help you.

.navBar {
    display: block;
    border: 5px solid black;
    width: 521.5px;
    position: sticky;
    margin-left: auto;
    margin-right: auto;
    top: 2px;
    display: flex;
}

.button {
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer;
    background-color: #555555;
    transition-duration: 0.4s;
    margin: 0 0.5%;
    width: 33%;
}

.button:hover {
    background-color: white;
    color: black;
}
<div class="navBar">
    <button type="button" class="button">text1</button>
    <button type="button" class="button" >text2</button>
    <button type="button" class="button">text3</button>
</div>
Abin Thaha
  • 4,493
  • 3
  • 13
  • 41