代码之家  ›  专栏  ›  技术社区  ›  user1219278

如何强制内联div保持在同一行?

  •  51
  • user1219278  · 技术社区  · 11 年前

    我正在尝试做一个三列的布局。我希望左列和右列的宽度只能和孩子们的内容一样宽。我希望中间的柱子展开以填满剩余的空间。

    我正在尝试以下操作(概述,下面包含jsfiddle链接):

    #colLeft {
      display: inline;
      float: left;
    }
    #colCenter {
      float: left;
      display: inline;
      overflow: none;
      white-space: nowrap;
    }
    #colRight {
      display: inline;
      float: right;
    }
    
    <div id="parent" style="width:100%">
      <div id="colLeft">left</div>
      <div id="colCenter">Some really long text in the center. Some really long text in the center.</div>
      <div id="colRight">right</div>
    </div>
    

    小提琴: http://jsfiddle.net/5kszQ/

    但当内容太长时,中心列会将右列推到其下方。我希望所有三列都是内联的,并根据需要缩小中心列。这就是上面给我的:

    enter image description here

    相反,我想:

    enter image description here

    谢谢你的帮助

    5 回复  |  直到 11 年前
        1
  •  24
  •   Daniel Imms    11 年前

    以下是一种使用 inline-block 用于左侧和中间以及 position:absolute 对于正确的元素。

    Example

    jsFiddle

    HTML格式

    <div id="parent" style="width:100%">
        <div id="colLeft">left</div><!--
        --><div id="colCenter">Some really long text in the center. Some really long text in the center.</div>
        <div id="colRight">right</div>
    </div>
    

    CSS格式

    html, body {
        margin: 0px;
        padding: 0px;
    }
    #parent {
        background-color: #eee;
        height: 48px;
        position:relative;
        overflow:hidden;
        white-space: nowrap;
    }
    #colLeft {
        background-color: #ff8b8b;
        height: 48px;
        display: inline-block;
    }
    #colCenter {
        background-color: orange;
        height: 48px;
        display: inline-block;
        overflow: hidden;
    }
    #colRight {
        background-color: #c3d0ff;
        height: 48px;
        display: inline;
        float: right;
        position:absolute;
        top:0;
        right:0;
    }
    

    因为它依赖 内联块 ,在 <div> s以消除此图中所示的间距:

    Ugly spacing

    文本溢出:省略号

    为了在使用时实现这一点 text-overflow:ellipsis 您可能需要回退JavaScript,这里有一个可能的解决方案( jsFiddle ).

    Ellipsis using JavaScript

    window.onresize = updateDimensions;
    
    function updateDimensions() {
        var parent = document.getElementById('parent');
        var left = document.getElementById('colLeft');
        var right = document.getElementById('colRight');
        var middle = document.getElementById('colCenter');
    
        middle.style.width = (parent.offsetWidth - right.offsetWidth - left.offsetWidth)  + 'px';
    }
    
        2
  •  20
  •   Dinesh Gajbhiye    11 年前

    如果您对一些HTML更改持开放态度,那么这应该会给您带来您想要的东西:

    <div id="parent" style="width:100%">  
      <div id="colLeft">left</div>
      <div id="colwrap">
          <div id="colRight">right</div>
          <div id="colCenter">Some really long text in the center. Some really long text in the center.</div>  
        </div>
    </div>
    

    和css为:

    html, body {
      margin: 0px;
      padding: 0px;
    }
    #parent {
      background-color: #eee;
      height: 48px;
    }
    #colLeft {
      background-color: #ff8b8b;
      height: 48px;
      float: left;
    }
    #colwrap{
        overflow:hidden;
        background-color: orange;      
    }
    #colCenter {
      height: 48px;  
    }
    #colRight {
      background-color: #c3d0ff;
      height: 48px;
      float: right;
    }
    

    jsFiddle: http://jsfiddle.net/gajbhiye/ZX97K/ 希望能有所帮助。

        3
  •  3
  •   NGLN Selim Serhat Çelik    11 年前

    通过在中间和右侧元素上添加一些大的边距,让浏览器说所有元素都很适合一行,并用相对定位来补偿这一点,以此来欺骗浏览器。看见 updated fiddle .

    标记:保持不变。

    样式:

    #parent {
      background-color: #eee;
      height: 48px;
      overflow: hidden;
    }
    #colLeft {
      background-color: #ff8b8b;
      height: 48px;
      float: left;
    }
    #colCenter {
      background-color: orange;
      height: 48px;
      float: left;
      margin-left: -2000px;
      position: relative;
      left: 2000px;
    }
    #colRight {
      background-color: #c3d0ff;
      height: 48px;
      float: right;
      margin-right: -2000px;
      position: relative;
      left: -2000px;
    }
    
        4
  •  0
  •   Hensembryan    11 年前

    试试这个

    <html>
    <head>
        <style type="text/css">
            #parent {
              word-break:break-all;
            }
            #colLeft {
              float:left;
              max-width: 5%;
            }
            #colCenter {
              float:left;
              max-width: 90%;
            }
            #colRight {
              float: right;
              max-width: 5%;
            }
        </style>
    </head>
    <body>
        <div id="parent" style="width:100%">
          <div id="colLeft">leftawefawefawefawef</div>
          <div id="colCenter">Some really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjlSome really long text in the center. Some really long text in the center.khjjjjjjjjjjjjjjjjjjjjjjjkjhkjhklkjl</div>
          <div id="colRight">rightaewfaewfawefawef</div>
        </div>
    </body>
    

        5
  •  -1
  •   Suraj Rao Soumya Sengupta    3 年前

    只需将子div放在表单元格中,而将表放在父div中。不需要样式

    <div id="parent" style="width: 100%">
     <table>
      <tr>
       <td><div id="colLeft">left</div></td>
       <td><div id="colCenter">Some really long text in the center. Some really long text in the center.</div></td>
       <td><div id="colRight">right</div></td>
      </tr>
     </table>
    </div>