Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I move the button on the right part instead of main with negative right Y?

I want something like that:

enter image description here

With button on the right part not simply at the right of input.
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Where the button is on the right side. Actually, I’m using ugly width: 138%, which seems to don’t work the same between browser, computers etc. Actually, the button goes to bottom instead of going at good place.

I can’t put it on the right side box directly as it’s never at the good Y position. And my way to put on the right make the button no longer working.

I’m using bootstrap 5 and I prefer to don’t use JS (as in the mre, I didn’t put any js)

.full-width {
   width: 100%;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-aFq/bzH65dt+w6FI2ooMVUpc+21e0SRygnTpmBvdBgSdnuTN7QbdgL+OapgHtvPp" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/js/bootstrap.bundle.min.js" integrity="sha384-qKXV1j0HvMUeCBQ+QVp7JcfGl760yU08IQ+GpUo5hlbpg51QRiuqHAJz8+BrxE/N" crossorigin="anonymous"></script>

<div class="row">
  <div class="col-3">
    Left part
  </div>
  <div class="col-6">
    <label>Input 1</label>
    <input type="text" class="form-control full-width">
    <label>Input 2</label>
    <input type="text" class="form-control full-width">
    <label>Input 3</label>
    <input type="text" class="form-control full-width">
    <div class="row" style="width: 138%;">
        <div class="col-9">
            <label for="sim">Special input</label>
            <input class="full-width form-control" type="text" name="sim">
        </div>
        <div class="col-3">
            <button type="button" class="btn btn-danger my-4">One button</button>
        </div>
    </div>
  </div>
  <div class="col-3">
    Right part
  </div>
</div>

How can I fix it?

>Solution :

Disclaimer:

This solution is in no way universal and will really depend on what you’re actually planning to do with the page. However, I will try to explain why your solution didn’t work and the important aspects you should consider.

Since you want your button to be to the right of the entire form, you should keep it out of it. aka, it should be part of the Right part div. This will make sure you never need to do things like 106% width, and will remove your need for forcing overflows.

Once you’ve gotten to that part, it really becomes a matter of preference and situation:

.full-width {
  width: 100%;
}

.right-part {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-aFq/bzH65dt+w6FI2ooMVUpc+21e0SRygnTpmBvdBgSdnuTN7QbdgL+OapgHtvPp" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/js/bootstrap.bundle.min.js" integrity="sha384-qKXV1j0HvMUeCBQ+QVp7JcfGl760yU08IQ+GpUo5hlbpg51QRiuqHAJz8+BrxE/N" crossorigin="anonymous"></script>

<div class="row">
  <div class="col-3">
    Left part
  </div>
  <div class="col-6">
    <label>Input 1</label>
    <input type="text" class="form-control full-width">
    <label>Input 2</label>
    <input type="text" class="form-control full-width">
    <label>Input 3</label>
    <input type="text" class="form-control full-width">
    <label for="sim">Special input</label>
    <input class="full-width form-control" type="text" name="sim">
  </div>
  <div class="col-3 right-part">
    <span>
            Right part
        </span>
    <div>
      <button type="button" class="oneButton btn btn-danger">One button</button>
    </div>
  </div>
</div>

Here, I used justify-content: space-between; because it was a convenient way to reach the desired result, but this solution fails if you have any other elements after the button inside the same parent div.

So, this solution will work if:

  1. Your parent div shares the same height as it’s neighboring columns.
  2. You have more than 1 element in the parent div (otherwise use justify-content: end;)
  3. Your button is the last item in that div.

Otherwise, you’ll have to explore other options, but ultimately you should avoid using % and px values when using bootstrap’s grid system and css flexbox is possible.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading