How to make an inline-block element fill the remainder of the line?

前端 未结 7 1573
抹茶落季
抹茶落季 2020-11-28 01:11

Is such a thing possible using CSS and two inline-block (or whatever) DIV tags instead of using a table?

The table version is this (borders added so you can see it):

相关标签:
7条回答
  • 2020-11-28 01:36

    Compatible with common modern browers (IE 8+): http://jsfiddle.net/m5Xz2/3/

    .lineContainer {
        display:table;
        border-collapse:collapse;
        width:100%;
    }
    .lineContainer div {
        display:table-cell;
        border:1px solid black;
        height:10px;
    }
    .left {
        width:100px;
    }
     <div class="lineContainer">
        <div class="left">left</div>
        <div class="right">right</div>
    </div>

    0 讨论(0)
  • 2020-11-28 01:38

    See: http://jsfiddle.net/qx32C/36/

    .lineContainer {
        overflow: hidden; /* clear the float */
        border: 1px solid #000
    }
    .lineContainer div {
        height: 20px
    } 
    .left {
        width: 100px;
        float: left;
        border-right: 1px solid #000
    }
    .right {
        overflow: hidden;
        background: #ccc
    }
    <div class="lineContainer">
        <div class="left">left</div>
        <div class="right">right</div>
    </div>


    Why did I replace margin-left: 100px with overflow: hidden on .right?

    EDIT: Here are two mirrors for the above (dead) link:

    • archive.is
    • web.archive.org
    • https://colinaarts-com.herokuapp.com/#making-room-for-floats/articles/the-magic-of-overflow-hidden
    0 讨论(0)
  • 2020-11-28 01:40

    If you can't use overflow: hidden (because you don't want overflow: hidden) or if you dislike CSS hacks/workarounds, you could use JavaScript instead. Note that it may not work as well because it's JavaScript.

    var parent = document.getElementsByClassName("lineContainer")[0];
    var left = document.getElementsByClassName("left")[0];
    var right = document.getElementsByClassName("right")[0];
    right.style.width = (parent.offsetWidth - left.offsetWidth) + "px";
    window.onresize = function() {
      right.style.width = (parent.offsetWidth - left.offsetWidth) + "px";
    }
    .lineContainer {
      width: 100% border: 1px solid #000;
      font-size: 0px;
      /* You need to do this because inline block puts an invisible space between them and they won't fit on the same line */
    }
    
    .lineContainer div {
      height: 10px;
      display: inline-block;
    }
    
    .left {
      width: 100px;
      background: red
    }
    
    .right {
      background: blue
    }
    <div class="lineContainer">
      <div class="left"></div>
      <div class="right"></div>
    </div>

    http://jsfiddle.net/ys2eogxm/

    0 讨论(0)
  • 2020-11-28 01:42

    When you give up the inline blocks

    .post-container {
        border: 5px solid #333;
        overflow:auto;
    }
    .post-thumb {
        float: left;
        display:block;
        background:#ccc;
        width:200px;
        height:200px;
    }
    .post-content{
        display:block;
        overflow:hidden;
    }
    

    http://jsfiddle.net/RXrvZ/3731/

    (from CSS Float: Floating an image to the left of the text)

    0 讨论(0)
  • 2020-11-28 01:47

    You can use calc (100% - 100px) on the fluid element, along with display:inline-block for both elements.

    Be aware that there should not be any space between the tags, otherwise you will have to consider that space in your calc too.

    .left{
        display:inline-block;
        width:100px;
    }
    .right{
        display:inline-block;
        width:calc(100% - 100px);
    }
    
    
    <div class=“left”></div><div class=“right”></div>
    

    Quick example: http://jsfiddle.net/dw689mt4/1/

    0 讨论(0)
  • 2020-11-28 01:47

    I've used flex-grow property to achieve this goal. You'll have to set display: flex for parent container, then you need to set flex-grow: 1 for the block you want to fill remaining space, or just flex: 1 as tanius mentioned in the comments.

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