Wisnu
Wisnu

Reputation: 83

Bootstrap 5 modal footer align left and right

Can somebody help me with Bootstrap 5 modal footer?
I want to make a modal footer have a text (span or label) in the left and button in the right

I've found this solution and try it (changed everyting from "left" to "start") but not working.
I tried using d-flex justify-content-start and also tried using float-start but still not working.

Upvotes: 8

Views: 11212

Answers (4)

Carol Skelly
Carol Skelly

Reputation: 362780

Simply use the justify-content-between class on the modal-footer...

<!-- Modal -->
<div class="modal fade" id="inputInv" tabindex="-1" aria-labelledby="InputInventory" aria-hidden="true" data-bs-backdrop="static">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="InputInventory">INPUT DATA</h5>
            </div>
            <div class="modal-body">
                <!-- CONTENT HERE -->
            </div>
            <div class="modal-footer justify-content-between">
                <div>
                    <span>Example vertically aligned text</span>
                </div>
                <div class="actions">
                    <button id="btnInClose" type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button id="btnInSave" type="button" class="btn btn-sm btn-primary">Save</button> 
                </div>
            </div>
        </div>
    </div>
</div>

Updated Codeply

Upvotes: 15

Ayush
Ayush

Reputation: 700

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />


<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#inputInv">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="inputInv" tabindex="-1" aria-labelledby="InputInventory" aria-hidden="true" data-bs-backdrop="static">
  <div class="modal-dialog modal-xl">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="InputInventory">INPUT DATA</h5>
      </div>
      <div class="modal-body">
        <!-- CONTENT HERE -->
      </div>
      <div class="modal-footer">
        <div class="flex-grow-1 bd-highlight">Example vertically aligned text</div>
        <div class="bd-highlight"><button id="btnInClose" type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button></div>
        <div class="bd-highlight"><button id="btnInSave" type="button" class="btn btn-sm btn-primary">Save</button></div>
      </div>
    </div>
  </div>
</div>

I think This would fix your problem
Just added the buttons and the text into the div elements and added class bd-highlight to the three div elements
Also added class flex-grow-1 to the text div element

Upvotes: 1

Raeesh Alam
Raeesh Alam

Reputation: 3490

If you are using Bootstrap-5 then don't need to write CSS for text or element alignment so you need to use pre-defined classes from BS-5.

You just need to add me-auto class in <span> or <label> then text will render from left side.

Helpful like:
https://getbootstrap.com/docs/5.1/utilities/flex/
https://css-tricks.com/how-auto-margins-work-in-flexbox/

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary m-5" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <span class="me-auto">Example vertically aligned text</span>
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save</button>
      </div>
    </div>
  </div>
</div>

Upvotes: 4

Jello
Jello

Reputation: 414

You need to modify some structure to achieve it.

CSS:

.modal-footer {
    justify-content: space-between;
}

HTML:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#inputInv">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="inputInv" tabindex="-1" aria-labelledby="InputInventory" aria-hidden="true" data-bs-backdrop="static">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="InputInventory">INPUT DATA</h5>
            </div>
            <div class="modal-body">
                <!-- CONTENT HERE -->
            </div>
            <div class="modal-footer">
                <div>
                    <span>Example vertically aligned text</span>
                </div>
                <div class="actions">
                    <button id="btnInClose" type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button id="btnInSave" type="button" class="btn btn-sm btn-primary">Save</button> 
                </div>
            </div>
        </div>
    </div>
</div>

Check this for the example.

Upvotes: 1

Related Questions