diff options
Diffstat (limited to 'files/ru/web/css')
-rw-r--r-- | files/ru/web/css/css_flow_layout/block_and_inline_layout_in_normal_flow/index.html | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/files/ru/web/css/css_flow_layout/block_and_inline_layout_in_normal_flow/index.html b/files/ru/web/css/css_flow_layout/block_and_inline_layout_in_normal_flow/index.html deleted file mode 100644 index 48b0353176..0000000000 --- a/files/ru/web/css/css_flow_layout/block_and_inline_layout_in_normal_flow/index.html +++ /dev/null @@ -1,124 +0,0 @@ ---- -title: Блочное и строчное расположение в нормальном потоке -slug: Web/CSS/CSS_Flow_Layout/Block_and_Inline_Layout_in_Normal_Flow -tags: - - CSS - - Макет - - Макет потока CSS - - Отступы - - Руководство - - Средний уровень - - поток -translation_of: Web/CSS/CSS_Flow_Layout/Block_and_Inline_Layout_in_Normal_Flow -original_slug: Web/CSS/CSS_Flow_Layout/Блочное_и_строчное_размещение_в_нормальном_потоке ---- -<div>{{CSSRef}}</div> - -<p class="summary">В этом руководстве мы исследуем основы поведения блочных и строчных элементов - участников нормального потока.</p> - -<p>Normal Flow is defined in the <a href="https://www.w3.org/TR/CSS2/visuren.html#normal-flow">CSS 2.1 specification</a>, which explains that any boxes in normal flow will be part of a <em>formatting context</em>. They can be either block or inline, but not both at once. We describe block-level boxes as participating in a <em>block formatting context</em>, and inline-level boxes as participating in an <em>inline formatting context</em>.</p> - -<p>The behaviour of elements which have a block or inline formatting context is also defined in this specification. For elements with a block formatting context, the spec says:</p> - -<blockquote> -<p>“In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block. The vertical distance between two sibling boxes is determined by the 'margin' properties. Vertical margins between adjacent block-level boxes in a block formatting context collapse.<br> - <br> - In a block formatting context, each box's left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch).” - 9.4.1</p> -</blockquote> - -<p>For elements with an inline formatting context:</p> - -<blockquote> -<p>“In an inline formatting context, boxes are laid out horizontally, one after the other, beginning at the top of a containing block. Horizontal margins, borders, and padding are respected between these boxes. The boxes may be aligned vertically in different ways: their bottoms or tops may be aligned, or the baselines of text within them may be aligned. The rectangular area that contains the boxes that form a line is called a line box.” - 9.4.2</p> -</blockquote> - -<p>Note that the CSS 2.1 specification describes documents as being in a horizontal, top to bottom writing mode. For example, by describing vertical distance between block boxes. The behavior on block and inline elements is the same when working in a vertical writing mode, and we will explore this in a future guide on Flow Layout and Writing Modes.</p> - -<h2 id="Elements_participating_in_a_block_formatting_context">Elements participating in a block formatting context</h2> - -<p>Block elements in a horizontal writing mode such as English, layout vertically, one below the other.</p> - -<p><img alt="" src="https://mdn.mozillademos.org/files/16125/mdn-horizontal.png" style="height: 382px; width: 521px;"></p> - -<p>In a vertical writing mode then would lay out horizontally.</p> - -<p><img alt="" src="https://mdn.mozillademos.org/files/16126/mdn-vertical.png" style="height: 382px; width: 418px;"></p> - -<p>In this guide, we will be working in English and therefore a horizontal writing mode. However, everything described should work in the same way if your document is in a vertical writing mode.</p> - -<p>As defined in the specification, the margins between two block boxes are what creates separation between the elements. We see this with a very simple layout of two paragraphs, to which I have added a border. The default browser stylesheet adds spacing between the paragraphs by way of adding a margin to the top and bottom.</p> - -<p>{{EmbedGHLiveSample("css-examples/flow/block-inline/normal-flow.html", '100%', 700)}}</p> - -<p>If we set margins on the paragraph element to <code>0</code> then the borders will touch.</p> - -<p>{{EmbedGHLiveSample("css-examples/flow/block-inline/normal-flow-margin-zero.html", '100%', 700)}}</p> - -<p>By default block elements will consume all of the space in the inline direction, so our paragraphs spread out and get as big as they can inside their containing block. If we give them a width, they will continue to lay out one below the other - even if there would be space for them to be side by side. Each will start against the start edge of the containing block, so the place at which sentences would begin in that writing mode.</p> - -<p>{{EmbedGHLiveSample("css-examples/flow/block-inline/normal-flow-width.html", '100%', 700)}}</p> - -<h3 id="Margin_collapsing">Margin collapsing</h3> - -<p>The spec explains that margins between block elements <em>collapse</em>. This means that if you have an element with a top margin immediately after an element with a bottom margin, rather than the total space being the sum of these two margins, the margin collapses, and so will essentially become as large as the larger of the two margins.</p> - -<p>In the example below, the paragraphs have a top margin of <code>20px</code> and a bottom margin of <code>40px</code>. The size of the margin between the paragraphs is <code>40px</code> as the smaller top margin on the second paragraph has collapsed with the larger bottom margin of the first.</p> - -<p>{{EmbedGHLiveSample("css-examples/flow/block-inline/normal-flow-collapsing.html", '100%', 500)}}</p> - -<p>You can read more about margin collapsing in our article <a href="/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing">Mastering Margin Collapsing</a>.</p> - -<div class="note"> -<p>Note: if you are not sure whether margins are collapsing, check the Box Model values in your browser DevTools. This will give you the actual size of the margin which can help you to identify what is happening.</p> - -<p><img alt="" src="https://mdn.mozillademos.org/files/16124/box-model.png" style="height: 848px; width: 828px;"></p> -</div> - -<h2 id="Elements_participating_in_an_inline_formatting_context">Elements participating in an inline formatting context</h2> - -<p>Inline elements display one after the other in the direction that sentences run in that particular writing mode. While we don’t tend to think of inline elements as having a box, as with everything in CSS they do. These inline boxes are arranged one after the other. If there is not enough space in the containing block for all of the boxes a box can break onto a new line. The lines created are known as line boxes.</p> - -<p>In the following example, we have three inline boxes created by a paragraph with a {{HTMLElement("strong")}} element inside it.</p> - -<p>{{EmbedGHLiveSample("css-examples/flow/block-inline/inline.html", '100%', 500)}}</p> - -<p>The boxes around the words before the <code><strong></code> element and after the <code><strong></code> element are referred to as anonymous boxes, boxes introduced to ensure that everything is wrapped in a box, but ones that we cannot target directly.</p> - -<p>The line box size in the block direction (so the height when working in English) is defined by the tallest box inside it. In the next example, I have made the <code><strong></code> element 300%; that content now defines the height of the line box on that line.</p> - -<p>{{EmbedGHLiveSample("css-examples/flow/block-inline/line-box.html", '100%', 500)}}</p> - -<p>Find out more about how Block and Inline Boxes behave in our Guide to the <a href="/en-US/docs/Web/CSS/Visual_formatting_model">Visual Formatting Model</a>.</p> - -<h2 id="The_display_property_and_flow_layout">The display property and flow layout</h2> - -<p>In addition to the rules existing in CSS2.1, new levels of CSS further describe the behaviour of block and inline boxes. The {{cssxref("display")}} property defines how a box and any boxes inside it behave. In the CSS Display Model Level 3, we can learn more about how the <code>display</code> property changes the behaviour of boxes and the boxes they generate.</p> - -<p>The display type of an element defines the outer display type; this dictates how the box displays alongside other elements in the same formatting context. It also defines the inner display type, which dictates how boxes inside this element behave. We can see this very clearly when considering a flex layout. In the example below I have a {{HTMLElement("div")}}, which I have given <code>display: flex</code>. The flex container behaves like a block element: it displays on a new line and takes up all of the space it can in the inline direction. This is the outer display type of <code>block</code>.</p> - -<p>The flex items however are participating in a flex formatting context, because their parent is the element with <code>display: flex</code>, which has an inner display type of <code>flex</code>, establishing the flex formatting context for the direct children.</p> - -<p>{{EmbedGHLiveSample("css-examples/flow/block-inline/flex.html", '100%', 500)}}</p> - -<p>Therefore you can think of every box in CSS working in this way. The box itself has an outer display type, so it knows how to behave alongside other boxes. It then has an inner display type which changes the way its children behave. Those children then have an outer and inner display type too. The flex items in the previous example become flex level boxes, so their outer display type is dictated by way of them being part of the flex formatting context. They have an inner display type of <em>flow</em> however, meaning that their children participate in normal flow. Items nested inside our flex item lay themselves out as block and inline elements unless something changes their display type.</p> - -<p>This concept of the outer and inner display type is important as this tells us that a container using a layout method such as Flexbox (<code>display: flex</code>) and Grid Layout (<code>display: grid</code>) is still participating in block and inline layout, due to the outer display type of those methods being <code>block</code>.</p> - -<h3 id="Changing_the_Formatting_Context_an_element_participates_in">Changing the Formatting Context an element participates in</h3> - -<p>Browsers display items as part of a block or inline formatting context in terms of what normally makes sense for that element. For example, a {{HTMLElement("strong")}} element is used to highlight a word and displays bold in browsers. It would not generally make sense for that <code><strong></code> element to be displayed as a block level element, breaking onto a new line. If you did want all <code><strong></code> elements to display as block elements, you could do so by setting <code>display: block</code> on <code><strong></code>. This means that you can always use most of the semantic HTML elements to markup your content, and then change the way it displays using CSS.</p> - -<p>{{EmbedGHLiveSample("css-examples/flow/block-inline/change-formatting.html", '100%', 500)}}</p> - -<h2 id="Summary">Summary</h2> - -<p>In this guide, we have looked at how elements display in normal flow, as block and inline elements. Due to the default behaviour of these elements, an HTML document with no CSS styling at all, will display in a readable way. By understanding how normal flow works you will find layout easier, as you understand the starting point for making changes to how elements are displayed.</p> - -<h2 id="See_Also">See Also</h2> - -<ul> - <li><a href="/en-US/docs/Web/CSS/CSS_Box_Model">CSS Basic Box Model</a></li> - <li><em><a href="/en-US/docs/Learn/CSS/CSS_layout/Normal_Flow">Normal Flow</a></em> - Learn Layout</li> - <li><a href="/en-US/docs/Web/HTML/Inline_elements">Inline elements</a></li> - <li><a href="/en-US/docs/Web/HTML/Block-level_elements">Block-level elements</a></li> -</ul> |