Search code examples
htmlcssbootstrap-4bootstrap-accordion

Bootstrap 4 accordion header float left and float right divs make both same hight and align vertically


I created a Bootstrap 4 accordion. I have two divs in the button header. One float left the other float right. The one on the right is a picture and the one on the left is a title and a subtitle.

All works great except I want the div on the left to be the same height as the div on the right and to also have the 2 titles be vertically aligned.

When I mess around with css to try and accomplish the above, I keep breaking something else in the accordion. I am at the end of my rope. I have created a fiddle and hope that somebody is able to help.

I created a fiddle here: >> Fiddle <<

<div class="accordion" id="page-accordion">
    <div class="card">
        <div class="card-header" id="headingOne">
            <h5 class="mb-0">
                <button class="btn btn-link collapsed" style="width:100%;" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
                    <div class="left">
                        <h5 class="card-title">OCTOBER 2018</h5>
                        <h6 class="card-subtitle mb-2">TODAY IS THE DAY</h6>
                    </div>
                    <div class="right">
                        <img src="pic.png" width="209px" height="119">
                    </div>
                    <div class="clear"></div>
                </button>
            </h5>
        </div>
        <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#page-accordion">
            <div class="card-body">
                <p class="card-text">DETAILS COMING SOON</p>
                <a href="#" class="btn btn-danger">Click here for details</a>
            </div>
        </div>
    </div>
</div>

Solution

  • Fixed JSFiddle Here

    You can use display: flex to make each item in a container have the same height. I also had to make one small change to your container h5. Display flex is a great alternative to using float (which can be very tricky to work with).

        <!doctype html>
    <html lang="en">
    <head>
        <!-- before all other stylesheets -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
    </head>
    
    <body>
    
    <!-- ACCORDION -->
    <div class="accordion" id="page-accordion">
        <div class="card">
            <div class="card-header" id="headingOne">
                <h5 class="mb-0">
                    <button class="btn btn-link collapsed" style="width:100%;" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
                            <h5 class="card-title">OCTOBER 2018</h5>
                            <h6 class="card-subtitle mb-2">TODAY IS THE DAY</h6>
                    </button>
                </h5>
          <div>
            <img src="pic.png" width="209px" height="119">
          </div>
            </div>
    
            <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#page-accordion">
                <div class="card-body">
                    <p class="card-text">DETAILS COMING SOON</p>
                    <a href="#" class="btn btn-danger">Click here for details</a>
                </div>
            </div>
        </div>
    </div>
    
    </body>
    </html>