次のコードでこれを実現できます。
HTML:
<div id="wrapper">
<div id="container">
<div id="variable">test</div>
<div id="fixed">test</div>
</div>
</div>
CSS:
#wrapper { overflow: hidden; }
#container {
float: left;
position: relative;
left: 50%; }
#container > div {
float: left;
position: relative;
right: 50%;
height: 300px; }
#variable {
background: red;
width: 300px; }
#fixed {
background: blue;
width: 200px; }
プレビュー:あなたはまた、ラップすることにより、この効果を達成できるhttp://jsfiddle.net/Wexcode/mreLt/
コンテナ内の2つの要素を両方ともに設定します最後にコンテナをtext-align: center
に設定します。
答えはこれより少し複雑ですので、代わりにこのルートを選択したい場合はお知らせください。
要素が次の行に入らないようにするには、inline-block
を使用します。
<div id="container">
<div id="variable">
<p>test</p>
</div><div id="fixed">
<p>test</p>
</div>
</div>
CSS:
body { margin: 0; }
#container {
color: #fff;
text-align: center;
white-space: nowrap; }
#container > div {
height: 300px;
display: inline-block; }
#variable {
background: red;
width: 100px; }
#fixed {
background: blue;
width: 200px; }
プレビュー:http://jsfiddle.net/Wexcode/mreLt/2/
出典
2012-01-09 08:41:27
Wex
OP、特にコンテナの幅が不明であると言います。 – Wex
私はコンテナのdivを追加せずにjavascriptを使用することができないと判断しました。実際には正常に動作しますが、クロスブラウザとページが読み込まれると「フラッシュ」が心配です。 – Darthg8r