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

前端 未结 7 1633
抹茶落季
抹茶落季 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: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
    }

    http://jsfiddle.net/ys2eogxm/

提交回复
热议问题