How do you align left / right a div without using float?

后端 未结 8 748
猫巷女王i
猫巷女王i 2020-12-24 10:04

When doing something like this:

Left Div
Right Div
相关标签:
8条回答
  • 2020-12-24 10:43

    Very useful thing have applied today in my project. One div had to be aligned right, with no floating applied.

    Applying code made my goal achieved:

    .div {
      margin-right: 0px;
      margin-left: auto;
    }
    
    0 讨论(0)
  • 2020-12-24 10:44

    Another solution could be something like following (works depending on your element's display property):

    HTML:

    <div class="left-align">Left</div>
    <div class="right-align">Right</div>
    

    CSS:

    .left-align {
      margin-left: 0;
      margin-right: auto;
    }
    .right-align {
      margin-left: auto;
      margin-right: 0;
    }
    
    0 讨论(0)
  • 2020-12-24 10:53

    It is dirty better use the overflow: hidden; hack:

    <div class="container">
      <div style="float: left;">Left Div</div>
      <div style="float: right;">Right Div</div>
    </div>
    
    .container { overflow: hidden; }
    

    Or if you are going to do some fancy CSS3 drop-shadow stuff and you get in trouble with the above solution:

    http://web.archive.org/web/20120414135722/http://fordinteractive.com/2009/12/goodbye-overflow-clearing-hack

    PS

    If you want to go for clean I would rather worry about that inline javascript rather than the overflow: hidden; hack :)

    0 讨论(0)
  • 2020-12-24 10:54

    No need to add extra elements. While flexbox uses very non-intuitive property names if you know what it can do you'll find yourself using it quite often.

    <div style="display: flex; justify-content: space-between;">
    <span>Item Left</span>
    <span>Item Right</span>
    </div>
    

    Plan on needing this often?

    .align_between {display: flex; justify-content: space-between;}
    

    I see other people using secondary words in the primary position which makes a mess of information hierarchy. If align is the primary task and right, left, and/or between are the secondary the class should be .align_outer, not .outer_align as it will make sense as you vertically scan your code:

    .align_between {}
    .align_left {}
    .align_outer {}
    .align_right {}
    

    Good habits over time will allow you to get to bed sooner than later.

    0 讨论(0)
  • 2020-12-24 10:55

    In you case here, if you want to right-align that green button, just change the one div to have everything right-aligned:

    <div class="action_buttons_header" style="text-align: right;">
    

    The div is already taking up the full width of that section, so just shift the green button the right by right-aligning the text.

    0 讨论(0)
  • 2020-12-24 10:56

    you could use things like display: inline-block but I think you would need to set up another div to move it over, if there is nothing going to the left of the button you could use margins to move it into place.

    Alternatively but not a good solution, you could position tags; put the encompassing div as position: relative and then the div of the button as position: absolute; right: 0, but like I said this is probably not the best solution

    HTML

    <div class="parent">
      <div>Left Div</div>
      <div class="right">Right Div</div>
    </div>
    

    CSS

    .parent {
      position: relative;
    }
    .right {
        position: absolute;
        right: 0;
    }
    
    0 讨论(0)
提交回复
热议问题