<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>前端实战库 &#187; 布局</title>
	<atom:link href="http://www.butong.net/archives/category/layout/feed" rel="self" type="application/rss+xml" />
	<link>http://www.butong.net</link>
	<description>品味web前端技术</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:21:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>侧栏定宽，主栏宽度自适应且优先载入</title>
		<link>http://www.butong.net/archives/9</link>
		<comments>http://www.butong.net/archives/9#comments</comments>
		<pubDate>Mon, 12 Jul 2010 15:29:47 +0000</pubDate>
		<dc:creator>picheng</dc:creator>
				<category><![CDATA[布局]]></category>

		<guid isPermaLink="false">http://pi-cheng.com/?p=9</guid>
		<description><![CDATA[<h2>需求</h2>
<p>两列布局，左栏宽度固定为200px，右栏宽度自适应且优先载入；<br />
两列间距为10px；<br />
两列高度不确定。</p>]]></description>
			<content:encoded><![CDATA[<h2>需求</h2>
<p>两列布局，左栏宽度固定为200px，右栏宽度自适应且优先载入；<br />
两列间距为10px；<br />
两列高度不确定。</p>
<h2>方案1</h2>
<h3 class="title-code">html</h2>
<div class="code">
<pre class="brush: html; gutter: false;">&lt;div id=&quot;content&quot;&gt;
	&lt;div id=&quot;main&quot;&gt;主栏&lt;/div&gt;
	&lt;div id=&quot;sidebar&quot;&gt;侧栏&lt;/div&gt;
&lt;/div&gt;</pre>
</div>
<h3 class="title-code">css</h3>
<div class="code">
<pre class="brush: css gutter: false;">#content {
	padding-left: 210px;
}
#main {
	float: right;
	width: 100%;
}
#sidebar {
	float: left; _display: inline; /* IE6 Bug */
	width: 200px;
	margin-left: -210px;
}</pre>
</div>
<p><a href="http://www.butong.net/demo/20100712/">查看demo</a></p>
<h2>方案2</h2>
<div class="code">
<pre class="brush: css; gutter: false;">#content {
	display: box;
	box-direction: reverse;
}
#main {
	box-flex: 1;
}
#sidebar {
	width: 200px;
	margin-right: 10px;
}</pre>
</div>
<p>注：实际应用中需要加上-moz和-webkit前缀，在此省略，以便示例代码的清晰。</p>
<h2>分析</h2>
<p>主栏需要优先载入，那么在html中两块内容的顺序就确定了，main要放置于sidebar前面。</p>
<p>两块内容并列放置，通常会用浮动或绝对定位来实现，绝对定位实现很方便，需要固定宽度的侧栏绝对定位，主栏写一个margin-left就OK。但这个需求中有一个“两列高度不确定”，有可能sidebar比main更高，如果sidebar绝对定位了，它就会脱离普通文档流，其后的内容不会随着它而流动。“两列高度不确定”也就意味着它们都要处于普通文档流当中。</p>
<p>如果用浮动呢？元素浮动后其宽度会自动收缩到最小，那么main浮动后，需要使其宽度自适应就只能依赖于父元素了，所以这里添加一个额外的div（#content），并设置其padding-left值为侧栏宽度以限制main的宽度范围，接下来定义sidebar的margin-left为负值让其与main并列显示。</p>
<p>对于这类弹性布局，CSS3的Flexible Box Layout当然是更好的选择，如方案二，Gecko和WebKit已提供了对它的支持。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.butong.net/archives/9/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

