CSS两个div用于Text,一个master,一个slave

时间:2015-12-23 19:56:00

标签: html css

我想创建两个彼此相邻的div,一个是左边,一个是右边,两个都包含文本。

左边的文本需要占用其文本所需的空间以保持一行。

正确的应该占用余数空间,而不管文本的大小。

3 个答案:

答案 0 :(得分:1)

经过一些谷歌搜索,并做了一些修改我认为以下解决了你的问题。另请参阅下面的代码段。

HTML

<ul>
  <li>
    <div class="left">some text</div>
    <div class="right">This is a very big text which is supposed to behave as wanted</div>
  </li>
  <li>
    <div class="left">a bit longer one</div>
    <div class="right">And a long last line to make sure that everything is fine with no side effects</div>
  </li>
</ul>

CSS

ul {
  display: table;
  width: 100%;
}

li {
  display: table-row;
}

li div {
  display: table-cell;
}

.left {
  width: 1px;
  background-color: blue;
  color: white;
  white-space: nowrap;
}

.right {
  background-color: gray;
}

http://jsfiddle.net/cj6PR/71/

答案 1 :(得分:0)

这样的事情可以做到:

<div id="container">
  <div id="left_div">

  </div>
  <div id="right_div">

  </div>
</div>

你的CSS:

#container {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
}

#left_div {
  position: absolute;
  top: 0;
  right: 50%;
  left: 0;
  bottom: 0;
  background-color:#EEE;
  text-align:center;
  border-right: 2px solid black;
}

#right_div {
  position: absolute;
  top: 0;
  right: 0;
  left: 50%;
  bottom: 0;
  background-color:#f9f9f9;
  text-align:center;
  color:#FFFFFF;
}

您正在寻找的空间功能可以通过一些Jquery插件甚至是香草JS实现,但我不太确定我理解您正在尝试做什么。

答案 2 :(得分:0)

我不确定您在寻找什么,但希望它可以帮助您:jsfiddle

HTML

<div class="container">
  <div class="left_div">
    It's my text, It's my text, It's my text, It's my text, It's my text, It's my text, It's my text, It's my text, It's my text, It's my text
  </div>
  <div class="right_div">
    More, more, more text, More, more, more text, More, more, more text
  </div>
</div>

CSS

.container {
  display: inline-flex;
}
.left_div {
  float: left;
  padding: 5px;
}
.right_div {
  float: right;
  padding: 5px;
}
相关问题