Solution 1 :

No need to do position calculations instead of the browser. This is bad practice. It is better to give the browser the opportunity to calculate everything on its own, it will do it better than you.

#floatingButtons>div {
  background-color: #b6c0c973;
  position: fixed;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  width: min-content;
  gap: 10px;
  justify-content: center;
  align-items: center;
  padding: 20px;
  color: #fff;
  font-size: 20px;
  border-radius: 90px;
}

.lowerButtons {
  flex: 1;
  background-color: #36404a;
  border: 2px solid #FFF;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  height: 40px;
  width: 40px;
  text-align: center;
  border-radius: 40px;
  color: #FFF;
  font-size: 20px;
}
<link href="https://use.fontawesome.com/releases/v6.2.0/css/all.css" rel="stylesheet" />
<script src="https://use.fontawesome.com/releases/v6.2.0/js/all.js"></script>

<div id="floatingButtons" style="display: contents;">
  <div>
    <div id="buttonA" class="lowerButtons">
      <i class="fa fa-pencil"></i>
    </div>
    <div id="buttonB" class="lowerButtons">
      <i class="fa fa-remove"></i>
    </div>
  </div>
</div>

Solution 2 :

There’s no need to specify a width for the parent div you can remove the width and add paddings and make it flex so it will adapt to the children, and no need to make the children position: fixed;, that’s how it should look:

HTML:

<div id="floatingButtons" style="display: contents;">
    <div style="background-color: #b6c0c973; display: flex; align-items: center; gap: 20px; position: fixed; height: 90px; padding: 0 10px; text-align: center; bottom: 10px; color: #fff; font-size: 20px; margin: 0 auto; left: 46%; border-radius: 40px;">
        <div id="buttonA" class="lowerButtons" style="left: 47.5%;">
            <i class="fa fa-pencil" style="margin-top: 9px;"></i>
        </div>
        <div id="buttonB" class="lowerButtons" style="left: 52.5%;">
            <i class="fa fa-remove" style="margin-top: 9px;"></i>
        </div>
    </div>
 </div>

CSS:

.lowerButtons {
    background-color: #36404a;
    border: 2px solid #FFF;
    display: block;
    cursor: pointer;
    height: 40px;
    width: 40px;
    text-align: center;
    border-radius: 35px;
    bottom: 40px;
    color: #FFF;
    font-size: 20px;
    margin: 0 auto;
}

Problem :

I have floating buttons on my interface and need to create a surrounding area for improved user experience purposes: if the mouse click miss the floating button it may select some other object on the background, therefore an undesired result.

What I achieved is shown below: a square grey background covering areas around those buttons. The result is not good because the grey area is not ‘harmonic’ with its contents: is not evenly positioned from its content.

I’m working with width: 10% and left: 46% properties. From what I’ve noticed some cases the width should be different (probably 11%), but not a harmonic result either.

Any ideas on how can I achieve a harmonic result?

.lowerButtons {
  background-color: #36404a;
  border: 2px solid #FFF;
  display: block;
  position: fixed;
  cursor: pointer;
  height: 40px;
  width: 40px;
  text-align: center;
  border-radius: 35px;
  bottom: 40px;
  color: #FFF;
  font-size: 20px;
  margin: 0 auto;
}
<div id="floatingButtons" style="display: contents;">
  <div style="background-color: #b6c0c973; display: block; position: fixed; height: 90px; width: 10%; text-align: center; bottom: 10px; color: #fff; font-size: 20px; margin: 0 auto; left: 46%; border-radius: 40px;">
    <div id="buttonA" class="lowerButtons" style="left: 47.5%;">
      <i class="fa fa-pencil" style="margin-top: 9px;"></i>
    </div>
    <div id="buttonB" class="lowerButtons" style="left: 52.5%;">
      <i class="fa fa-remove" style="margin-top: 9px;"></i>
    </div>
  </div>
</div>

Visual result:

Current settings compare

Comments

Comment posted by rd1218

Yes, you’re right. My code needed improvement. Also, your code also solved some other issues like very wide screens and font awesome vertical position within button.

Comment posted by Rhythm Ruparelia

A couple of corrections can improve the result here. For parent container, removing

Comment posted by rd1218

@RhythmRuparelia Yes, it improved. I applied to my code. Thanks.

Comment posted by rd1218

Thanks but it didn’t actually work: the grey area got similar to an empty area, the buttons stayed outside the grey area.

Comment posted by Mohammed Ahmed

If you take the code I posted as it is it will show look the same as you want, can you please show me what you added?

Comment posted by rd1218

I just made a copy/paste from what you submmited and got that result. For now, please notice that I chosed the other post as a solution. Thanks

By