This paper mainly introduces the implementation of the front-end layout scheme and the existing problems, involving the layout scheme includes:
- Fluid layout
- Float layout
- The table layout
- Locate the layout
- Flex layout
- Responsive layout
- The grid layout
Front-end layout from the beginning: Table layout => DIV+CSS layout => Flex layout => Grid layout has experienced a series of changes above, these changes are accompanied by the development of front-end technology and the program ape on the convenience of the pursuit of layout scheme, the final result is to let our web design becomes more and more convenient, fast, efficient, The web we see is increasingly diverse and personal. It has been twenty years since the birth of the Internet, so let’s look at how the front-end layout has grown step by step from the development of the Internet. First, let’s look at the evolution of the Web:In 1991, the web 1996 yahoo! In 1999, Google In 2020,
When Tim Berners-Lee and others invented the Web in 1990, it was really more like a giant file access system, and the web was just plain text, but over time, plain text was considered ugly, So he’s ready to dress up the web, which is only a few years old, in other languages. The language that was finally adopted was the style Grammar proposed by Hakon Wium in October 1994. It’s called a cascading style list, or CSS for short. At the time CSS was used more to decorate web pages than to lay them out. At this point the layout scheme is mainly: fluid layout
Fluid layout
Fluid layout is a normal layout flow, so what is a normal layout flow? Think of our layout page as an inverted square fish tank (HTML). When you add water (DIV), the water will fill the entire horizontal plane, and when you add water again, the water will fill the entire horizontal plane. If the tank becomes wider and narrower (assuming the tank is flexible), The water also widens, Narrows and spreads across the entire width of the tank. This is the fluid layout. The DIV mentioned above is a block tag in HTML, which is actually a box model, composed as follows:
There are very few layout styles that we can manipulate in this layout, but I’m going to focus on the margin property, which we can pass throughmargin: auto; Horizontally centers the child elements. The reason you can center elements horizontally is because auto works: Auto fills the box model with the remaining width of the parent element. Let’s look at the picture
In this image, the remaining width of the parent element is 200px, and the left and right margin is auto, so that the left and right are evenly divided. The remaining width of the parent element is 100px, so that the element is horizontally centered. We can also use margin to achieve vertical horizontal center, as follows: The problem with this layout: margin merging Margin merging: The top and bottom margins of block-level elements are sometimes merged (only in the vertical direction) into a single margin, and the merged margins are equal to the larger values of the previous two margins. This phenomenon is also known as margin collapse.
Margin merge type:
- The adjacent elements
- Father and son elements
- Empty elements
Schematic diagram of adjacent elements:The ideal is to have two parent elements at 200px height:The black line is the previous element, the gray is the parent element, and the orange is the child element. Ideally a margin-top of 50px between the child element and the parent element results in a height of 50px between the parent element and the previous element.
Schematic diagram of empty elements:The upper and lower margins should be 10px, and the element height should be 20px.
Solution: parent trigger landing (grade block formatting context) not to tangle of landing the words explanation, just need to know the impact of landing a function is used to isolate elements, trigger, after landing the container element how casually thread will not affect the outside elements, elements outside how casually change will not affect the container element. It’s like adding a partition. BFC trigger mode:
- Body element
- Float to none
- Display: flex/inline – flex
- Position: Absolute, fixed, and table-cells
- Overflow values other than visible
———— Fluid layout complete —————-
Float layout
Float layouts were originally designed to wrap text around images, but float layouts have become popular for structural layouts. Implementation code:
Float: right/leftCopy the code
Floating elements float out of the document flow (above the normal document flow), and the text behind the floating element wraps around the image, but is not overwritten. This layout is simple, but the downside is that it affects subsequent elements, so we need to clear the float. Clear float:
Clear: left/right/bothCopy the code
But do you really understand how Clear works? Take a look at the following clear float does it look the same as you thought it would?Official explanation of the clear property:”Element boxThe sides of phi cannot be addedIn front ofFloat elements adjacent. So, clearing the float doesn’t really remove the float element, it just keeps the element from being affected by the previous element. ————float layout complete —————-
The table layout
Fast forward to 1997, and there was a guy named David Siegel who creatively changed the way the Web was laid out, but at the same time “destroyed” the way the web was laid out: the table layout.
Table layout (HTML implementation) As we can see from the picture, the layout displayed is very nice, so this layout became very popular after it was introduced, and it was basically the same layout after 1997 until the 2000s, and even now we can find it in some websites. But if you look at the source code on the right, you can see that the structure is very confusing and very expensive to maintain. Even David himself said:Some people say I ruined the Web, and I tell them I did. I ruined the Web because I mixed chocolate and peanut butter and couldn’t separate them anymore. I made the mistake of mixing structure with presentation.Therefore, it is not recommended to use this method for layout unless necessary. And then CSS came out and said, well, the HTML table method sucks, and I can do the same thing with my properties, and it works great.Table layout (CSS implementation) The CSS style corresponds to the HTML table attribute, and the combination of the two is incredible. This is the famous DIV+CSS layout scheme. ———— Table layout is complete —————-Locate the layoutImplementation code:
position: static/relative/absolute/fixed/sticky
static: Element default value, normal layoutrelativeThe left/right/top/bottom attribute is valid relative to its position in the document flow and does not leave the document flowabsolute: Relative to the first nonstaticThe left/right/top/bottom attributes are in effect, leaving the document flowfixed: The left/right/top/bottom attributes are valid relative to the window, leaving the document flowsticky: is the combination of "relative" and "fixed".//www.runoob.com/try/try.php?filename=trycss_position_sticky)
Copy the code
With location layout, we can achieve all kinds of layout as long as we use it properly, but I want to ask a question here. When absolute positioning, left, right, or top, bottom exist at the same time, who should listen to? If you don’t know the answer, look at the picture:When the height is not specified, both top and bottom exist at the same time. (in the same way, when the width is not specified, left and right there at the same time, listening to the left) — — — — — — — — — — — — location layout after — — — — — — — — — — — — — — — –
Flex layout
Flex stands for Flexible Box, which I personally find a bit like an abacus:The container corresponds to the abacus frame, and the item corresponds to the beads inside. When you apply flex layout, you’re actually flipping beads. But for those of you who want more details, pleaseClick on theThe overall learning of Flex is relatively difficult compared to the previous layout, especially the calculation principle of flex-grow and Flex-shrink. I would like to briefly explain it. Both flex-grow and flex-shrink are calculated based on flex-basis, which indicates the amount of space that an item occupies on the main axis before allocating extra space. Listen to Flex-basis).
.father {
height: 100px;
width: 800px;
display: flex;
background: gainsboro;
}
.child1,
.child2,
.child3,
.child4 {
height: 100px;
}
.child1 {
width: 50px;
background: tan;
}
.child2 {
flex-basis: 100px;
flex-grow: 1;
background: teal;
}
.child3 {
flex-basis: 150px;
flex-grow: 3;
background: thistle;
}
.child4 {
flex-basis: 200px;
flex-grow: 6;
background: tomato;
}
Copy the code
What is the width of each child? (I hope you do the math and go down to….)
That is how flex-grow is calculated. In the same way, I know that you, who are smart enough, can deduce the flex-grow calculation. ———— Flex layout complete —————-
Responsive layout
Responsive layouts are essentially the same Web page that can be rendered perfectly on different devices (phone, tablet, PC). Why reactive layouts? The reason is that the web used to be tailored to the width of a computer (before smartphones, Ethan Marcotte proposed a responsive layout in 2010), and each type of computer could fit perfectly with a single web page, but with the development of smartphones, each phone came in different sizes. Making a Web page for every cell phone size would be too expensive. Wouldn’t it be nice if one page could fit all display devices? This is a responsive layout implementation:
Add the following meta tags to the HTML head to make the page width equal to the device width, with the original zoom ratio of1
<meta name="viewport" content="width=device-width, initial-scale=1" />
Copy the code
In layout, we also need to pay attention to the following issues:
- Use relative units (%, Vh, VW, REM, em)
- Max-width: 100%, srcset
- Flow layout (Float, Flex)
- Media Enquiries (@media)
As long as you write your HTML and CSS code in this way, your web pages will be responsive. I have written a simple responsive demo for your reference. ———— Responsive layout complete —————-
The Grid layout
Grid layout is a new CSS layout model. It is currently the only 2D CSS layout, and I personally think it is the future of CSS layout (if compatibility is ok). I could write a new article about grid if I talked about it in detail, because it’s too much, andA great godI’ve made that very clear. I just want to give an example of how powerful a Grid layout can be.If we want to implement the layout in the diagram, you might want to use a positioning layout or a floating layout? Today, we use a powerful Grid layout. We need to understand some basic concepts before using it (not too difficult)
display: grid;
grid-template-columns: 100px 100px 100px // Create three columns, each 100px wide
grid-template-rows: 100px 100px; // Create two rows, each 100px wide
Copy the code
The above code creates a two-dimensional layout of two rows and three columns, which looks like this:The code in the middle implements the following layout result, which is easy to understand: the landlord has six two-dimensional matrices of land to be allocated to several sons with different names (gird-area: name). Each field is marked with the names of the sons (grid-template-areas). Said he kept it. With that in mind, we’ll start implementing the layout above:In just a few lines, the layout of the image is implemented. Isn’t the Grid powerful? ———— Grid layout complete —————-
conclusion
Here we introduce several common ways of Web layout from the development of the Internet. Each way has its advantages and disadvantages, and each way also has its historical significance. We may not use or recommend some layouts any more because of better layouts (such as semantic layout). Some of the layouts I’ve introduced today may be a product of the past, but it’s enough that they’ve lived their lives. I hope that after reading this article, you will have a clear understanding of how the Web should be laid out, and will be pleased to know what works best when it comes to layout and where the holes are.
Due to my limited level, it is inevitable to make mistakes in the process of writing. Please help me to point out that part of the pictures used in the article are found on the Internet. If there is infringement, please contact me.