cabbi
cabbi

Reputation: 403

Bootstrap4 aligned column

My goal is to have a page with some rows. Each row has a label column, a control column and an optional unit of measure column. I would like to have label column be same for each row fitting the larger label. I'm using Bootstrap 4 to ease my few html knowledge. I did some experiments already, maybe I'm on a wrong path using row & col classes.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous">
</script>

<div class="container-fluid">
  <div class="row">
    <div class="col-auto">
      Label
    </div>
    <div class="col-auto">
      <input type="number" class="form-control">
    </div>
    <div class="col-auto">
      min
    </div>
  </div>
  <div class="row">
    <div class="col-auto">
      Longer Label
    </div>
    <div class="col-auto">
      <button type="button" class="btn btn-primary">Primary</button>
    </div>
    <div class="col-auto">
    </div>
  </div>
</div>

At this point my idea would be use jquery to set all first columns to same size, but I'm not sure this is good/best approach.

Upvotes: 0

Views: 26

Answers (1)

AndyJamesN
AndyJamesN

Reputation: 498

Using jquery for layout like this would be incorrect usage. Always use CSS for layouts unless there is a very unique use case.

This is a perfect use case for CSS flexbox. Bootstrap is great but learning CSS flexbox will give you a lot more control of layouts.

Make a flex row and then use a flex:1 and max width on the label.

https://codesandbox.io/s/brave-morning-d3nhz

Upvotes: 2

Related Questions