Home >Web Front-end >CSS Tutorial >How to achieve adaptive left and right sidebars through CSS Flex layout
How to realize the adaptive layout of the left and right sidebars through Css Flex elastic layout
Introduction: With the continuous development of web design, realizing the adaptive layout of the page has become An important requirement. CSS Flex layout is a good way to solve this problem. This article will introduce how to implement the adaptive layout of the left and right sidebars through CSS Flex elastic layout, and give detailed code examples.
1. Introduction to Flex Layout
1.1 Flexible Container and Flexible Item
Flex layout implements layout by setting the sub-elements in the container as flexible items. The parent element is called a flex container and the child elements are called flex items. In a flexible container, we can control the arrangement of child elements and the space they occupy by setting some properties.
1.2 Properties of flexible containers
1. Properties of elastic items
2. Example of adaptive layout of left and right sidebars
Let’s use a specific example to demonstrate how to implement adaptive layout of left and right sidebars through Css Flex elastic layout.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>左右侧边栏自适应布局示例</title> <style> body { margin: 0; padding: 0; } .container { display: flex; flex-direction: row; } .sidebar { background-color: #f1f1f1; width: 20%; flex-grow: 1; } .content { background-color: #eee; width: 80%; flex-grow: 3; } .sidebar, .content { padding: 20px; } </style> </head> <body> <div class="container"> <div class="sidebar"> <h2>左侧边栏</h2> <p>左侧边栏内容</p> </div> <div class="content"> <h1>主要内容区域</h1> <p>主要内容</p> </div> </div> </body> </html>
The above code is a simple left and right sidebar layout example. We arrange the child elements in the horizontal direction by setting the display: flex;
and flex-direction: row;
of the container.
width: 20%;
of the left sidebar and width: 80%;
of the right content area control the proportion of the two in the horizontal direction. That is, the left column occupies 20% of the width and the content area occupies 80% of the width.
By setting the flex-grow: 1;
of the left sidebar and the flex-grow: 3;
of the right content area, we implement the left and right sidebars adaptive. This means that the left sidebar will take up 1/4 of the available space, and the right content area will take up 3/4 of the available space.
Conclusion:
It is relatively simple to implement the adaptive layout of the left and right sidebars through Css Flex elastic layout. We only need to set the parent container to a flex container and use the relevant properties of flex to control the layout of the child elements. Sorting, alignment, and proportion of space taken up. This article gives a specific code example for readers to refer to and learn from. Hope this article is helpful to you!
The above is the detailed content of How to achieve adaptive left and right sidebars through CSS Flex layout. For more information, please follow other related articles on the PHP Chinese website!