Why is my button going to the next line in my layout?

Viewed 27

Trying to have it to where these elements appear on the same exact line. So the header of "Header" will be on the left and the button to submit will be on the right.

<div>
  <div width="float: left; width: 50%">
    <h4> Header </h4>
  </div>
  
  <div style="float: right; width: 50%">
    <button> Export to Excel </button>
  </div>
</div>

enter image description here

I have used similar setup in the past and it worked. Any ideas?

2 Answers

To align two items next to eachother you need to make sure you assign them both a 50% width and you can get them both on the same line using align: left. This method works, but I suggest looking into flexbox for positioning, this makes it much easier.

.wrapper {
  width: 50%;
  float: left;
}
<div>
    <div class="wrapper">
      <h4>
        Header
      </h4>
    </div>
    <div class="wrapper">
        <button
        cButton
        (click)="exportExcel(1)"
        >
        Export to Excel
        </button>
    </div>
  </div>

Flexbox:

.parent {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
}

.wrapper {
  flex-direction: column;
  flex: 1;
}

.wrapper button {
  width: 125px;
}
<div class="parent">
    <div class="wrapper">
      <h4>
        Header
      </h4>
    </div>
    <div class="wrapper">
        <button
        cButton
        (click)="exportExcel(1)"
        >
        Export to Excel
        </button>
    </div>
  </div>

If you are going to use float, the right column needs to be first

<div>
    <div style="float: right; width: 50%">
        <button
        cButton
        (click)="exportExcel(1)"
        >
        Export to Excel
        </button>
    </div>
    <div width="float: left; width: 50%;">
      <h4>
        Header
      </h4>
    </div>
  </div>

Using Flex

.row {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
}

.column {
  display: flex;
  flex-direction: column;
  flex-basis: 100%;
  flex: 1;
}
<div class='row'>
  <div class='column'>
    <h4>
      Header
    </h4>
  </div>
  <div class='column'>

    <button cButton (click)="exportExcel(1)">
        Export to Excel
        </button>

  </div>
</div>

Related