Html 两个div在同一行,一个动态宽度,一个固定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6056759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:40:40  来源:igfitidea点击:

two divs the same line, one dynamic width, one fixed

csshtml

提问by bingjie2680

I have two divs under one parent div, the parent div has 100% width:

我在一个父级下有两个 div div,父级 div 的宽度为 100%:

<div id="parent">
  <div class="left"></div>
  <div class="right"></div>
</div>

The conditions are:

条件是:

  • I want two divs on the same line.
  • The right div may or may not be present. When it is present, I want it to always be fixed on the right. However, the left div must be elastic - it's width depends on its content.
  • 我想要在同一行上的两个 div。
  • 正确的 div 可能存在也可能不存在。当它出现时,我希望它始终固定在右侧。然而,左边的 div 必须是弹性的——它的宽度取决于它的内容。

I have tried both float:left, and dispaly:inline-block but neither solution seems to work.

我已经尝试过 float:left 和 dispaly:inline-block 但两种解决方案似乎都不起作用。

Any suggestion would be appreciated.

任何建议将不胜感激。

回答by thirtydot

I'd go with @sandeep's display: table-cellanswer if you don't care about IE7.

display: table-cell如果您不关心 IE7,我会选择@sandeep 的回答。

Otherwise, here's an alternative, with one downside: the "right" divhas to come first in the HTML.

否则,这是另一种选择,但有一个缺点:“正确”div必须在 HTML 中排在第一位。

See:http://jsfiddle.net/thirtydot/qLTMf/
and exactly the same, but with the "right div" removed: http://jsfiddle.net/thirtydot/qLTMf/1/

请参阅:http : //jsfiddle.net/thirtydot/qLTMf/
,完全相同,但删除了“正确的 div”:http: //jsfiddle.net/thirtydot/qLTMf/1/

#parent {
    overflow: hidden;
    border: 1px solid red
}
.right {
    float: right;
    width: 100px;
    height: 100px;
    background: #888;
}
.left {
    overflow: hidden;
    height: 100px;
    background: #ccc
}
<div id="parent">
    <div class="right">right</div>
    <div class="left">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam semper porta sem, at ultrices ante interdum at. Donec condimentum euismod consequat. Ut viverra lorem pretium nisi malesuada a vehicula urna aliquet. Proin at ante nec neque commodo bibendum. Cras bibendum egestas lacus, nec ullamcorper augue varius eget.</div>
</div>

回答by sandeep

@Yijie; Check the link maybe that's you want http://jsfiddle.net/sandeep/NCkL4/7/

@易杰;检查链接可能是你想要的http://jsfiddle.net/sandeep/NCkL4/7/

EDIT:

编辑

http://jsfiddle.net/sandeep/NCkL4/8/

http://jsfiddle.net/sandeep/NCkL4/8/

OR SEE THE FOLLOWING SNIPPET

或查看以下片段

#parent{
    overflow:hidden;
    background:yellow;
    position:relative;
    display:table;
}
.left{
    display:table-cell;
}
.right{
    background:red;
    width:50px;
    height:100%;
    display:table-cell;
}
body{
    margin:0;
    padding:0;
}
<div id="parent">
  <div class="left">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
  <div class="right">fixed</div>
</div>

回答by a-second-mix

HTML:

HTML:

<div id="parent">
  <div class="right"></div>
  <div class="left"></div>
</div>

(div.right needs to be before div.left in the HTML markup)

(div.right 需要在 HTML 标记中的 div.left 之前)

CSS:

CSS:

.right {
float:right;
width:200px;
}

回答by Tor Iver Wilhelmsen

I've had success with using white-space: nowrap; on the outer container, display: inline-block; on the inner containers, and then (in my case since I wanted the second one to word-wrap) white-space: normal; on the inner ones.

我在使用空格方面取得了成功: nowrap; 在外部容器上,显示:inline-block;在内部容器上,然后(在我的情况下,因为我想要第二个自动换行) white-space: normal; 在内部的。

回答by Guillaume Esquevin

So left div style depends on the presence of right div. I can't think of a CSS selector allowing that kind of behavior yet.

所以左 div 样式取决于右 div 的存在。我还想不出允许这种行为的 CSS 选择器。

Thus it seems to me that you'll need to programmatically add a class server side (or in JS) on parent div or left div to do that.

因此,在我看来,您需要以编程方式在父 div 或左 div 上添加一个类服务器端(或在 JS 中)来做到这一点。

<div id="parent twocols">
  <div class="left"></div>
  <div class="right"></div>
</div>

or

或者

<div id="parent">
  <div class="left"></div>
</div>

So right style is always :

所以正确的风格总是:

.right {
    float: right;
    width: 200px; /* or whatever value you need */
    /* margin and padding at your discretion */
}

and left style is :

左风格是:

.parent.twocols .left {
    margin-right: 200px; /* according to right div width + margin + padding*/
}

回答by Anish

I think this is you want:

我想这是你想要的:

<html>
<head>
<style type="text/css">
#parent 
{width:100%;
height:100%;
border:1px solid red;
}
.left
{
float:left;
width:40%;
height:auto;
border:1px solid black;
}
.right
{
    float:left;

width:59%;
height:auto;
border:1px solid black;
}
</style>
</head>
<body>
<div id="parent">
  <div class="left">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
  <div class="right">This is the right side content</div>
</div>
</body>
</html>

Here is the demo:http://jsfiddle.net/anish/aFBmN/

这是演示:http: //jsfiddle.net/anish/aFBmN/