Align two inline-blocks left and right on same line

后端 未结 9 740
滥情空心
滥情空心 2020-11-27 11:50

How can I align two inline-blocks so that one is left and the other is right on the same line? Why is this so hard? Is there something like LaTeX\'s \\hfill that can consume

相关标签:
9条回答
  • 2020-11-27 12:02

    If you don't want to use floats, you're going to have to wrap your nav:

    <header>
    <h1>Title</h1>
    <div id="navWrap">
    <nav>
        <a>A Link</a>
        <a>Another Link</a>
        <a>A Third Link</a>
    </nav>
    </div>
    </header>
    

    ...and add some more specific css:

    header {
    //text-align: center; // will set in js when the nav overflows (i think)
    width:960px;/*Change as needed*/
    height:75px;/*Change as needed*/
    }
    
    h1 {
    display: inline-block;
    margin-top: 0.321em;
    }
    
    #navWrap{
    position:absolute;
    top:50px; /*Change as needed*/
    right:0;
    }
    
    nav {
    display: inline-block;
    vertical-align: baseline;
    }
    

    You may need to do a little more, but that's a start.

    0 讨论(0)
  • 2020-11-27 12:06

    I think one possible solution to this is to use display: table:

    .header {
      display: table;
      width: 100%;
      box-sizing: border-box;
    }
    
    .header > * {
      display: table-cell;
    }
    
    .header > *:last-child {
      text-align: right;  
    }
    
    h1 {
      font-size: 32px;
    }
    
    nav {
      vertical-align: baseline;
    }
    

    JSFiddle: http://jsfiddle.net/yxxrnn7j/1/

    0 讨论(0)
  • 2020-11-27 12:12

    give it float: right and the h1 float:left and put an element with clear:both after them.

    0 讨论(0)
  • 2020-11-27 12:15

    For both elements use

    display: inline-block;
    

    the for the 'nav' element use

    float: right;
    
    0 讨论(0)
  • 2020-11-27 12:23

    Taking advantage of @skip405's answer, I've made a Sass mixin for it:

    @mixin inline-block-lr($container,$left,$right){
        #{$container}{        
            text-align: justify; 
    
            &:after{
                content: '';
                display: inline-block;
                width: 100%;
                height: 0;
                font-size:0;
                line-height:0;
            }
        }
    
        #{$left} {
            display: inline-block;
            vertical-align: middle; 
        }
    
        #{$right} {
            display: inline-block;
            vertical-align: middle; 
        }
    }
    

    It accepts 3 parameters. The container, the left and the right element. For example, to fit the question, you could use it like this:

    @include inline-block-lr('header', 'h1', 'nav');
    
    0 讨论(0)
  • 2020-11-27 12:24

    New ways to align items right:

    Grid:

    .header {
            display:grid;
            grid-template-columns: 1fr auto;
        }
    

    Demo

    Bootstrap 4. Align right:

    <div class="row">
          <div class="col">left</div>
          <div class="col">
              <div class="float-right">element needs to be right aligned</div>
          </div>
    </div>
    

    Demo

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