2017-02-24 49 views
0

在标记[重复]之前,请务必理解问题。动态宽度并排(内联)div(CSS,无JavaScript)

我很难找出最好的方法来并排放置2个div,其中第一个是可以添加和删除的内容。检查实际的代码比我解释得更多。它基本上是一个编辑器,其中有行计数器和代码/文本部分。

JSFiddle和代码片段:

body, html { 
 
    height: 100%; 
 
    margin: 0px; 
 
} 
 

 
#editor { 
 
    cursor: default; 
 
    font-family: consolas; 
 
    font-size: 12px; 
 
    height: 100%; 
 
    user-select: none; 
 
} 
 

 
#lines { 
 
    background-color: #2e3436; 
 
    color: #fff; 
 
    display: inline-block; 
 
    height: 100%; 
 
    padding: 0 7px; 
 
} 
 

 
#text { 
 
    background-color: #141414; 
 
    color: #B5834A; 
 
    cursor: text; 
 
    display: inline-block; 
 
    height: 100%; 
 
    vertical-align: top; 
 
    width: calc(100% - 34px); 
 
}
<div id="editor"> 
 
    <div id="lines"> 
 
    <span>1</span> 
 
    <br><span>2</span> 
 
    <br><span>10</span> 
 
    <br><span>etc</span> 
 
    </div><div id="text"> 
 
    <span>&lt;html><br>// Code<br>&lt;/html></span> 
 
    </div> 
 
</div>

现在,我使用width: calc(100% - 34px);不解决,当我添加/删除行号,使其格变动幅度的问题。

我试过CSS属性display中的多个值。我怀疑与表相关的东西会起作用,但我宁愿没有解决方法,如果可能的话,也不要JavaScript。

如果您还没有理解该问题,只需删除10etc,您将在编辑器/文本div的末尾发现一个空白区域。的

Result

回答

1

您可以使用flexboxes:

body, html { 
 
    height: 100%; 
 
    margin: 0px; 
 
} 
 

 
#editor { 
 
    cursor: default; 
 
    font-family: consolas; 
 
    font-size: 12px; 
 
    height: 100%; 
 
    user-select: none; 
 
    display: flex; /* add */ 
 
} 
 

 
#lines { 
 
    background-color: #2e3436; 
 
    color: #fff; 
 
    /*display: inline-block;*/ 
 
    height: 100%; 
 
    padding: 0 7px; 
 
} 
 

 
#lines span { 
 
    display: block; /*add and remove <br>s*/ 
 
} 
 

 
#text { 
 
    background-color: #141414; 
 
    color: #B5834A; 
 
    cursor: text; 
 
    /*display: inline-block;*/ 
 
    height: 100%; 
 
    /*vertical-align: top;*/ 
 
    width: 100%; 
 
}
<div id="editor"> 
 
    <div id="lines"> 
 
    <span>1</span> 
 
    <span>2</span> 
 
    <span>10</span> 
 
    <span>etc</span> 
 
    </div><div id="text"> 
 
    <span>&lt;html><br>// Code<br>&lt;/html></span> 
 
    </div> 
 
</div>

+0

我认为你的答案是最好的。我喜欢它不会有太大的改变,并且它不需要元素'
'。 –

1

float代替inline-block

body, html { 
 
    height: 100%; 
 
    margin: 0px; 
 
} 
 

 
#editor { 
 
    cursor: default; 
 
    font-family: consolas; 
 
    font-size: 12px; 
 
    height: 100%; 
 
    user-select: none; 
 
} 
 

 
#lines { 
 
    background-color: #2e3436; 
 
    color: #fff; 
 
    /*display: inline-block; nooooo */ 
 
    float:left; /* yeeeeees */ 
 
    height: 100%; 
 
    padding: 0 7px; 
 
    text-align: right; /* missing this :) */ 
 
} 
 

 
#lines span{ display: block;} /* yeeeeyyy */ 
 

 
#text { 
 
    background-color: #141414; 
 
    color: #B5834A; 
 
    cursor: text; 
 
    /*display: inline-block; noooooo */ 
 
    /* width: calc(100% - 34px); noooooo */ 
 
    height: 100%; 
 
    vertical-align: top; 
 
}
<div id="editor"> 
 

 
    <div id="lines"> 
 
    <span>1</span> 
 
    <span>2</span> 
 
    <span>10</span> 
 
    <span>etccccc</span> 
 
    </div> 
 
    
 
    <div id="text"> 
 
    <span>&lt;html><br>// Code<br>&lt;/html></span> 
 
    </div> 
 
    
 
</div>

+0

@ J.Vie你反正做了错误的方式:) - 如果我有代码长行 - 突然它包装成两行当年不处理这种情况。想一想 - 我会尽快告诉你我该怎么做。如果这一切都很重要...... –

+0

我承认我不是那么好。我正在尽我所能编写一个看起来像Notepad ++(暮光主题)只是为了好玩的编辑器。 –

1

,我做的自己使用浮动的线路(不使用BR的)

<div id="editor"> 
    <div id="lines"> 
     <span>1</span> 
     <span>2</span> 
     <span>10</span> 
     <span>etc</span> 
    </div> 
    <div id="text"> 
     <span>&lt;html></span> 
     <span>// Code</span> 
     <span>&lt;/html></span> 
    </div> 
</div> 

和显示表属性,以适应所有的屏幕,并获得更好的路线。

继承人的例子,正常工作对我说:https://jsfiddle.net/wkd0ssw3/

这在任何导航工作,要小心使用像display:flexcalc另一个属性,因为可能无法在所有的导航工作...

+0

如果你不想使用表格的东西,我试图删除所有显示方法,只是把它与绝对位置漂浮:https://jsfiddle.net/wkd0ssw3/1/ – TriForce

1

.editor元件内部.text元件,使它成为一个浮动块和得到.text元件100%的宽度:

body, html { 
 
    height: 100%; 
 
    margin: 0px; 
 
} 
 

 
#editor { 
 
    cursor: default; 
 
    font-family: consolas; 
 
    font-size: 12px; 
 
    height: 100%; 
 
    user-select: none; 
 
} 
 

 
#lines { 
 
    background-color: #2e3436; 
 
    color: #fff; 
 
    float: left; 
 
    display: block; 
 
    height: 100%; 
 
    padding: 0 7px; 
 
} 
 

 
#text { 
 
    background-color: #141414; 
 
    color: #B5834A; 
 
    cursor: text; 
 
    display: inline-block; 
 
    height: 100%; 
 
    vertical-align: top; 
 
    width: 100%; 
 
}
<div id="editor"> 
 
    
 
    <div id="text"> 
 
    <div id="lines"> 
 
    <span>1</span> 
 
    <br><span>2</span> 
 
    <br><span>10000</span> 
 
    <br><span>etc</span> 
 
    </div> 
 
    <span>&lt;html><br>// Code<br>&lt;/html></span> 
 
    </div> 
 
</div>