diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/css/flex | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/css/flex')
-rw-r--r-- | files/zh-cn/web/css/flex/index.html | 299 |
1 files changed, 299 insertions, 0 deletions
diff --git a/files/zh-cn/web/css/flex/index.html b/files/zh-cn/web/css/flex/index.html new file mode 100644 index 0000000000..9911e9c77d --- /dev/null +++ b/files/zh-cn/web/css/flex/index.html @@ -0,0 +1,299 @@ +--- +title: flex +slug: Web/CSS/flex +tags: + - CSS + - CSS Flexible Boxes + - CSS Property + - CSS 弹性盒子布局 + - Flex + - Reference +translation_of: Web/CSS/flex +--- +<div>{{CSSRef}}</div> + +<p><code>flex</code> <a href="/zh-CN/docs/Web/CSS/Shorthand_properties">CSS简写属性</a>设置了弹性项目如何增大或缩小以适应其弹性容器中可用的空间。</p> + +<div>{{EmbedInteractiveExample("pages/css/flex.html")}}</div> + +<p class="hidden">这个交互案例的源码存储在github仓库。如果你想对此案例做贡献,请克隆项目<a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> 并且给我们推送一个请求(PR)。</p> + +<h2 id="成分属性">成分属性</h2> + +<p>此属性是以下CSS属性的简写:</p> + +<ul> + <li>{{cssxref("flex-grow")}}</li> + <li>{{cssxref("flex-shrink")}}</li> + <li>{{cssxref("flex-basis")}}</li> +</ul> + +<div id="flex"></div> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="brush:css no-line-numbers notranslate">/* 关键字值 */ +flex: auto; +flex: initial; +flex: none; + +/* 一个值, 无单位数字: flex-grow */ +flex: 2; + +/* 一个值, width/height: flex-basis */ +flex: 10em; +flex: 30px; +flex: min-content; + +/* 两个值: flex-grow | flex-basis */ +flex: 1 30px; + +/* 两个值: flex-grow | flex-shrink */ +flex: 2 2; + +/* 三个值: flex-grow | flex-shrink | flex-basis */ +flex: 2 2 10%; + +/*全局属性值 */ +flex: inherit; +flex: initial; +flex: unset; +</pre> + +<p>可以使用一个,两个或三个值来指定 <code>flex</code>属性。</p> + +<p><strong>单值语法</strong>: 值必须为以下其中之一:</p> + +<ul> + <li>一个无单位<strong>数({{cssxref("<number>")}})</strong>: 它会被当作<code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-grow"><flex-grow></a>的值。</code></li> + <li>一个有效的<strong>宽度({{cssxref("width")}})</strong>值: 它会被当作 <code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-basis"><flex-basis></a>的值。</code></li> + <li>关键字<code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex$edit#none">none</a></code><font face="Open Sans, arial, x-locale-body, sans-serif">,</font><code>auto</code>或<code>initial</code>.</li> +</ul> + +<p><strong>双值语法</strong>: 第一个值必须为一个无单位数,并且它会被当作 <code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-grow"><flex-grow></a></code> 的值。第二个值必须为以下之一:</p> + +<ul> + <li>一个无单位数:它会被当作 <code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-shrink"><flex-shrink></a></code> 的值。</li> + <li>一个有效的宽度值: 它会被当作 <code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-basis"><flex-basis></a></code> 的值。</li> +</ul> + +<p><strong>三值语法:</strong></p> + +<ul> + <li>第一个值必须为一个无单位数,并且它会被当作 <code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-grow"><flex-grow></a></code> 的值。</li> + <li>第二个值必须为一个无单位数,并且它会被当作 <code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-shrink"><flex-shrink></a></code> 的值。</li> + <li>第三个值必须为一个有效的宽度值, 并且它会被当作 <code><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/flex-basis"><flex-basis></a></code> 的值。</li> +</ul> + +<h3 id="Values" name="Values">取值</h3> + +<dl> + <dt><code>initial</code></dt> + <dd>元素会根据自身宽高设置尺寸。它会缩短自身以适应 flex 容器,但不会伸长并吸收 flex 容器中的额外自由空间来适应 flex 容器 。相当于将属性设置为"<code>flex: 0 1 auto</code>"。</dd> +</dl> + +<dl> + <dt><code>auto</code></dt> + <dd>元素会根据自身的宽度与高度来确定尺寸,但是会伸长并吸收 flex 容器中额外的自由空间,也会缩短自身来适应 flex 容器。这相当于将属性设置为 "<code>flex: 1 1 auto</code>".</dd> + <dt><code>none</code></dt> + <dd>元素会根据自身宽高来设置尺寸。它是完全非弹性的:既不会缩短,也不会伸长来适应 flex 容器。相当于将属性设置为"<code>flex: 0 0 auto</code>"。</dd> + <dt><code><'flex-grow'></code></dt> + <dd>定义 flex 项目的 {{cssxref("flex-grow")}} 。负值无效。省略时默认值为 <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">1</span></font>。 (初始值为 <code>0</code>)</dd> + <dt><code><'flex-shrink'></code></dt> + <dd>定义 flex 元素的 {{cssxref("flex-shrink")}} 。负值无效。省略时默认值为<code>1</code>。 (初始值为 <code>1</code>)</dd> + <dt><code><'flex-basis'></code></dt> + <dd>定义 flex 元素的 {{cssxref("flex-basis")}} 属性。若值为<code>0</code>,则必须加上单位,以免被视作伸缩性。省略时默认值为 <code>auto</code>。(初始值为 <code>0</code>)</dd> + <dt> + <h3 id="描述">描述</h3> + </dt> +</dl> + +<p>大多数情况下,开发者需要将 <code>flex</code> 设置为以下值之一: <code>auto</code>,<code>initial</code>,<code>none</code>,或一个无单位正数。要查看这些值的效果,请尝试调整以下 flex容器的大小:</p> + +<div id="flex"> +<pre class="hidden brush: html notranslate"><div class="flex-container"> + <div class="item auto">auto</div> + <div class="item auto">auto</div> + <div class="item auto">auto</div> +</div> + +<div class="flex-container"> + <div class="item auto">auto</div> + <div class="item initial">initial</div> + <div class="item initial">initial</div> +</div> + +<div class="flex-container"> + <div class="item auto">auto</div> + <div class="item auto">auto</div> + <div class="item none">none</div> +</div> + +<div class="flex-container"> + <div class="item initial">initial</div> + <div class="item none">none</div> + <div class="item none">none</div> +</div> + +<div class="flex-container"> + <div class="item four">4</div> + <div class="item two">2</div> + <div class="item one">1</div> +</div> +</pre> + +<pre class="hidden brush: css notranslate">* { + box-sizing: border-box; +} + +.flex-container { + background-color: #F4F7F8; + resize: horizontal; + overflow: hidden; + display: flex; + margin: 1em; +} + +.item { + margin: 1em; + padding: 0.5em; + width: 110px; + min-width: 0; + background-color: #1B5385; + color: white; + font-family: monospace; + font-size: 13px; +} + +.initial { + flex: initial; +} + +.auto { + flex: auto; +} + +.none { + flex: none; +} + +.four { + flex: 4; +} + +.two { + flex: 2; +} + +.one { + flex: 1; +} +</pre> + +<p>{{EmbedLiveSample("flex", 1200, 370, "", "", "example-outcome-frame")}}</p> + +<p>默认情况下,元素不会缩短至小于内容框尺寸,若想改变这一状况,请设置元素的{{cssxref("min-width")}} 与 {{cssxref("min-height")}}属性。</p> +</div> + +<h2 id="正式定义">正式定义</h2> + +<p>{{cssinfo}}</p> + +<h2 id="正式语法">正式语法</h2> + +<pre class="notranslate">{{csssyntax}}</pre> + +<h2 id="示例">示例</h2> + +<h3 id="设置_flex_auto">设置 flex: auto</h3> + +<h4 id="HTML">HTML</h4> + +<pre class="brush: html notranslate"><div id="flex-container"> + <div class="flex-item" id="flex">Flex box (click to toggle raw box)</div> + <div class="raw-item" id="raw">Raw box</div> +</div> +</pre> + +<h4 id="CSS">CSS</h4> + +<pre class="brush: css notranslate">#flex-container { + display: flex; + flex-direction: row; +} + +#flex-container > .flex-item { + flex: auto; +} + +#flex-container > .raw-item { + width: 5rem; +} + +</pre> + +<div class="hidden"> +<h4 id="JS">JS</h4> + +<pre class="brush: js notranslate">var flex = document.getElementById("flex"); +var raw = document.getElementById("raw"); +flex.addEventListener("click", function() { + raw.style.display = raw.style.display == "none" ? "block" : "none"; +}); +</pre> + +<h4 id="CSS_2">CSS</h4> + +<pre class="brush: css notranslate">#flex-container { + width: 100%; + font-family: Consolas, Arial, sans-serif; +} + +#flex-container > div { + border: 1px solid #f00; + padding: 1rem; +} + +#flex-container > .raw-item { + border: 1px solid #000; +} +</pre> +</div> + +<h3 id="Result">Result</h3> + +<p>{{EmbedLiveSample('示例','100%','60')}}</p> + +<h2 id="Specifications" name="Specifications">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th>规范</th> + <th>状态</th> + <th>备注</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('CSS3 Flexbox', '#flex-property', 'flex')}}</td> + <td>{{Spec2('CSS3 Flexbox')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<p>{{cssinfo}}</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external external-icon" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("css.properties.flex")}}</p> + +<h2 id="See_also" name="See_also">参见</h2> + +<ul> + <li>CSS Flexbox 指南: <em><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox">Flexbox 基本概念</a></em></li> + <li>CSS Flexbox 指南: <em><a href="/zh-CN/docs/Web/CSS/CSS_Flexible_Box_Layout/Controlling_Ratios_of_Flex_Items_Along_the_Main_Ax">控制 Flex 子元素在主轴上的比例</a></em></li> +</ul> |