Align two inline-blocks left and right on same line

后端 未结 9 741
滥情空心
滥情空心 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:26

    If you're already using JavaScript to center stuff when the screen is too small (as per your comment for your header), why not just undo floats/margins with JavaScript while you're at it and use floats and margins normally.

    You could even use CSS media queries to reduce the amount JavaScript you're using.

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

    Edit: 3 years has passed since I answered this question and I guess a more modern solution is needed, although the current one does the thing :)

    1.Flexbox

    It's by far the shortest and most flexible. Apply display: flex; to the parent container and adjust the placement of its children by justify-content: space-between; like this:

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

    Can be seen online here - http://jsfiddle.net/skip405/NfeVh/1073/

    Note however that flexbox support is IE10 and newer. If you need to support IE 9 or older, use the following solution:

    2.You can use the text-align: justify technique here.

    .header {
        background: #ccc; 
        text-align: justify; 
    
        /* ie 7*/  
        *width: 100%;  
        *-ms-text-justify: distribute-all-lines;
        *text-justify: distribute-all-lines;
    }
     .header:after{
        content: '';
        display: inline-block;
        width: 100%;
        height: 0;
        font-size:0;
        line-height:0;
    }
    
    h1 {
        display: inline-block;
        margin-top: 0.321em; 
    
        /* ie 7*/ 
        *display: inline;
        *zoom: 1;
        *text-align: left; 
    }
    
    .nav {
        display: inline-block;
        vertical-align: baseline; 
    
        /* ie 7*/
        *display: inline;
        *zoom:1;
        *text-align: right;
    }
    

    The working example can be seen here: http://jsfiddle.net/skip405/NfeVh/4/. This code works from IE7 and above

    If inline-block elements in HTML are not separated with space, this solution won't work - see example http://jsfiddle.net/NfeVh/1408/ . This might be a case when you insert content with Javascript.

    If we don't care about IE7 simply omit the star-hack properties. The working example using your markup is here - http://jsfiddle.net/skip405/NfeVh/5/. I just added the header:after part and justified the content.

    In order to solve the issue of the extra space that is inserted with the after pseudo-element one can do a trick of setting the font-size to 0 for the parent element and resetting it back to say 14px for the child elements. The working example of this trick can be seen here: http://jsfiddle.net/skip405/NfeVh/326/

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

    Displaying left middle and right of there parents. If you have more then 3 elements then use nth-child() for them.

    HTML sample:

    <body>
        <ul class="nav-tabs">
            <li><a  id="btn-tab-business" class="btn-tab nav-tab-selected"  onclick="openTab('business','btn-tab-business')"><i class="fas fa-th"></i>Business</a></li>
            <li><a  id="btn-tab-expertise" class="btn-tab" onclick="openTab('expertise', 'btn-tab-expertise')"><i class="fas fa-th"></i>Expertise</a></li>
            <li><a  id="btn-tab-quality" class="btn-tab" onclick="openTab('quality', 'btn-tab-quality')"><i class="fas fa-th"></i>Quality</a></li>
        </ul>
    </body>
    

    CSS sample:

    .nav-tabs{
      position: relative;
      padding-bottom: 50px;
    }
    
    .nav-tabs li {
      display: inline-block;  
      position: absolute;
      list-style: none;
    }
    .nav-tabs li:first-child{
      top: 0px;
      left: 0px;
    }
    .nav-tabs li:last-child{
      top: 0px;
      right: 0px;
    }
    .nav-tabs li:nth-child(2){
      top: 0px;
      left: 50%;
      transform: translate(-50%, 0%);
    }
    
    0 讨论(0)
提交回复
热议问题