Understand overflow in depth
Writing in the front
How well you know CSS layouts determines how fast you can develop pages in Web development. As Web technology continues to evolve, there are countless ways to implement layouts.
I recently put together a series of articles over the course of half a month, using fragment time, that summarized the various layouts in CSS, as well as their implementation methods and common techniques. This series gives you a new look at CSS layouts.
This series of navigation post I enter, which can quickly jump to the article you want to know (suggested favorites)
Basic properties of Overflow
An overview of the
The overflow property is used for what needs to be done when an element is too big to fit the size of the parent container. There are four common values for this property:
visible
: Default value. Content is not pruned back and can be rendered outside the element box.hidden
If the content exceeds the parent container, the excess will be hiddenscroll
: A scroll bar appears whether the container is exceeded or not.auto
: If there are no displays outside the container, they will display normally, if there are, a scroll bar will appear.
Example code is as follows:
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="Width = device - width, initial - scale = 1.0" />
<title>Overflow basic attributes</title>
<style>
.main {
width: 1200px;
display: flex;
justify-content: space-between;
margin: 0 auto;
}
.container {
height: 210px;
width: 260px;
background-color: #70a1ff;
}
img {
width: 300px;
opacity: 0.7;
}
.visible {
/* The overflow part is displayed */
overflow: visible;
}
.hidden {
/* The excess part is hidden */
overflow: hidden;
}
.scroll {
/* Horizontal and vertical scroll bar, can scroll display */
overflow: scroll;
}
.auto {
/* The scroll bar appears when the department is exceeded
overflow: auto;
}
</style>
</head>
<body>
<div class="main">
<div class="container visible"><img src=".. /image/img.jpg" /></div>
<div class="container hidden"><img src=".. /image/img.jpg" /></div>
<div class="container scroll"><img src=".. /image/img.jpg" /></div>
<div class="container auto"><img src=".. /image/img.jpg" /></div>
</div>
</body>
</html>
Copy the code
The following information is displayed:
Overflow – x and overflow – y
Overflow-x and overflow-y can set what to do with overflow when horizontal or vertical, respectively.
Note that if overflow-x has the same value as overflow-y, the result is the same as overflow; If overflow-x and overflow-y have different values, and one property is assigned a visible value and the other a non-Visible value, the first value assigned visible will automatically change to Auto.
Use overflow premises
For overflow to be effective, the container must satisfy the following conditions:
dispaly
The value of theinline
。- Has size limits. (
width
/height
/max-width
/max-height
/absolute
Stretch) - For cells
td
And so ontable
为table-layout: fixed
Can.
Overflow and scroll bars
The conditions under which the scroll bar appears
The conditions for the emergence of scroll bars are mainly divided into the following two types:
- use
overflow
Property to appear in the scroll bar - HTML element, for example
<html>
和<textarea>
Properties.
Note that the default scroll bar comes from the < HTML > element, not the element. Scrollbars also take up the available width or height of the container
Gets the scrollbar height
The JavaScript code to get the scrollbar height is as follows:
let st = document.documentElement.scrollTop || document.body.scrollTop;
Copy the code
Custom scroll bar
The WebKit kernel provides the following browser-custom scrollbar style attributes for the WebKit kernel, as follows:
::-webkit-scrollbar
– The entire scroll bar.::-webkit-scrollbar-button
– Buttons (up and down arrows) on the scroll bar.::-webkit-scrollbar-thumb
– Scroll slider on the scroll bar.::-webkit-scrollbar-track
– Scroll bar track.::-webkit-scrollbar-track-piece
– Scroll bar track part without slider.
The sample code looks like this:
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="Width = device - width, initial - scale = 1.0" />
<title>Custom scroll bar</title>
<style>
body {
height: 1000px;
}
/* The entire scroll bar. */
::-webkit-scrollbar {
width: 12px;
}
/* Scroll bar track */
::-webkit-scrollbar-track {
background-color: #ff7875;
}
/* The slider on the scroll bar */
::-webkit-scrollbar-thumb {
background-color: #ffc069;
border-radius: 6px;
}
</style>
</head>
<body></body>
</html>
Copy the code
The effect is as follows:
Block formatting context
Block Formatting Context (BFC). The CSS rendering mode for a box layout in a Web page refers to a separate rendering area or a separate container.
BFC layout rules
- The internal elements will be placed vertically, one after the other, starting from the top.
- The vertical distance of the element is determined by
margin
Decision. Belonging to two adjacent elements of the same BFCmargin
It’s going to add up - It all starts on the far left. Per element
margin box
To the left, with the containing blockborder box
Left of (for formatting from left to right, otherwise reverse). This is true even if there is a float - BFC areas are not associated with
float box
Stack. - A BFC is a separate container on a page, and the child elements in the container do not affect the outside elements and vice versa.
- When calculating the height of the BFC, the floating element is also involved in the calculation. (When there is a float inside the BFC, the HEIGHT of the floating element is included in the calculation so as not to affect the layout of external elements.)
Create the landing
Block formatting contexts are created in the following ways:
- The root element (
<html>
) - Float element (element’s
float
notnone
) - Absolutely locate the element (element’s
position
为absolute
或fixed
) - Inline block elements (of elements
display
为inline-block
) - Table cells (element
display
为table-cell
, HTML table cells default to this value) - Table title (element’s
display
为table-caption
.HTML
Table title defaults to this value) overflow
The calculated value is notvisible
The block elementdisplay
A value offlow-root
The elements of the- Elastic element (
display
为flex
或inline-flex
The immediate child of the element) - Grid elements (
display
为grid
Or a direct child of an inline-grid element)
CSS properties that depend on Overflow
Overflow-dependent CSS properties are invalid if their value is not set to visible. There are two overflow-dependent CSS properties:
1. The resize attribute
This property is used to set whether an element is resizable.
This property has the following values:
none
: Default value, elements cannot be scaled by the user.both
: allows the user to resize elements horizontally and vertically.horizontal
: allows the user to resize elements horizontally.vertical
: allows the user to resize elements vertically.
2. The text – overflow attributes
This property is used to specify what to do when text overflows.
This property has the following values:
clip
: Default “truncates the text at the limit of the content areaellipsis
:.
To represent truncated text<string>
: The string content will be appended to the content area. If the space is too small, the string will also be truncated.