Align two divs horizontally side by side center to the page using bootstrap css

前端 未结 9 2260
春和景丽
春和景丽 2020-12-12 16:07

Please refer below code what i tried

First Div
相关标签:
9条回答
  • 2020-12-12 16:22

    Alternative which I did programming Angular:

        <div class="form-row">
            <div class="form-group col-md-7">
                 Left
            </div>
            <div class="form-group col-md-5">
                 Right
            </div>
        </div>
    
    0 讨论(0)
  • 2020-12-12 16:23
    <div class="container">
        <div class="row">
           <div class="col-xs-6 col-sm-6 col-md-6">
              First Div
           </div>
           <div class="col-xs-6 col-sm-6 col-md-6">
              Second Div
           </div>
       </div>
    

    This does the trick.

    0 讨论(0)
  • 2020-12-12 16:29

    Alternate Bootstrap 4 solution (this way you can use divs which are smaller than col-6):

    <div class="container">
      <div class="row justify-content-center">
        <div class="col-4">
          One of two columns
        </div>
        <div class="col-4">
          One of two columns
        </div>
      </div>
    </div>
    

    More

    0 讨论(0)
  • 2020-12-12 16:35

    The response provided by Ranveer (second answer above) absolutely does NOT work.

    He says to use col-xx-offset-#, but that is not how offsets are used.

    If you wasted your time trying to use col-xx-offset-#, as I did based on his answer, the solution is to use offset-xx-#.

    0 讨论(0)
  • 2020-12-12 16:36

    I recommend css grid over bootstrap if what you really want, is to have more structured data, e.g. a side by side table with multiple rows, because you don't have to add class name for every child:

    // css-grid: https://www.w3schools.com/css/tryit.asp?filename=trycss_grid
    // https://css-tricks.com/snippets/css/complete-guide-grid/
    .grid-container {
      display: grid;
      grid-template-columns: auto auto; // 20vw 40vw for me because I have dt and dd
      padding: 10px;
      text-align: left;
      justify-content: center;
      align-items: center;
    }
    
    .grid-container > div {
      padding: 20px;
    }
    
    
    <div class="grid-container">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>5</div>
      <div>6</div>
      <div>7</div>
      <div>8</div>
    </div>
    

    css-grid

    0 讨论(0)
  • 2020-12-12 16:38

    Use the bootstrap classes col-xx-# and col-xx-offset-#

    So what is happening here is your screen is getting divided into 12 columns. In col-xx-#, # is the number of columns you cover and offset is the number of columns you leave.

    For xx, in a general website, md is preferred and if you want your layout to look the same in a mobile device, xs is preferred.

    With what I can make of your requirement,

    <div class="row">
      <div class="col-md-4">First Div</div>
      <div class="col-md-8">Second DIV </div>
    </div>
    

    Should do the trick.

    0 讨论(0)
提交回复
热议问题