2个流体块元件并排共用空间

时间:2018-01-15 02:02:48

标签: html css responsive-design

我有2个元素保存文本,两个都是未知宽度,我不想添加固定宽度复制div。 如何让它们浮在一条线上并始终占据100%的宽度? (没有表格)

 .data{
   max-width:400px;
 }

.code {
  color: #fff;
  margin: 0;
  padding: 8px!important;
  line-height: 1.2;
  font-size: 11px;
  background: #bbb;
  border: 1px solid #333;
  word-wrap: break-word;
  float: left;
}

.copy {
  color: #ccc;
  display: inline-block;
  padding: 3px!important;
  font-size: 12px;
  cursor: pointer;
  border: 1px solid #999;
  float: right;
  margin-top: 1px;
}
<div class="data">
  <p class="code">Praesent molestie. Nunc Venenatis Sapien Ultrices Dui. Vivamus dolor. Integer vel ante. Proin felis. Maecenas non nisl eu mi hendrerit fringilla.</p>
  <div class="copy">COPY</div>
</div>

2 个答案:

答案 0 :(得分:1)

您可以使用 Flexbox 执行此操作:

.data {
  display: flex; /* displays flex-items (children) inline by default */
  align-items: flex-start; /* vertical alignment / optional but recommended / if you don't want that flex-items match in height, which by default they do (default value of stretch, which makes them fill the flex-containers height and where the height of all items is dictated by the height of the "tallest" one) / you can also try the value of center */
  max-width: 400px;
}

.code {
  color: #fff;
  margin: 0;
  padding: 8px !important;
  line-height: 1.2;
  font-size: 11px;
  background: #bbb;
  border: 1px solid #333;
  word-wrap: break-word;
  /*float: left; not necessary*/
}

.copy {
  color: #ccc;
  /*display: inline-block; not necessary*/
  padding: 3px !important;
  font-size: 12px;
  cursor: pointer;
  border: 1px solid #999;
  /*float: right; not necessary*/
  margin-left: 10px; /* design purposes */
}
<div class="data">
  <p class="code">Praesent molestie. Nunc Venenatis Sapien Ultrices Dui. Vivamus dolor. Integer vel ante. Proin felis. Maecenas non nisl eu mi hendrerit fringilla.</p>
  <div class="copy">COPY</div>
</div>

答案 1 :(得分:0)

使用可以使用灵活的盒子来处理这个问题。只需在display:flex中添加.data即可。有关详情,请参阅here

&#13;
&#13;
 .data{
   max-width:400px;
   display:flex;
 }

.code {
  color: #fff;
  margin: 0;
  padding: 8px!important;
  line-height: 1.2;
  font-size: 11px;
  background: #bbb;
  border: 1px solid #333;
  word-wrap: break-word;
  float: left;
}

.copy {
  color: #ccc;
  display: inline-block;
  padding: 3px!important;
  font-size: 12px;
  cursor: pointer;
  border: 1px solid #999;
  float: right;
  margin-top: 1px;
}
&#13;
<div class="data">
  <p class="code">Praesent molestie. Nunc Venenatis Sapien Ultrices Dui. Vivamus dolor. Integer vel ante. Proin felis. Maecenas non nisl eu mi hendrerit fringilla.</p>
  <div class="copy">COPY</div>
</div>
&#13;
&#13;
&#13;