Home Web Front-end CSS Tutorial CSS layout tutorial: The best way to implement a two-column responsive layout

CSS layout tutorial: The best way to implement a two-column responsive layout

Oct 18, 2023 am 11:04 AM
Responsive layout css layout two column layout

CSS layout tutorial: The best way to implement a two-column responsive layout

CSS layout tutorial: The best way to implement a two-column responsive layout

Introduction:
In web design, responsive layout is a very important technology that enables web pages to automatically adjust their layout according to the screen size and resolution of the user's device, providing a better user experience. In this tutorial, we'll show you how to use CSS to implement a simple two-column responsive layout, and provide specific code examples.

1. HTML structure:
First, we need to create a basic HTML structure, as shown below:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>两栏响应式布局</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <div class="left-column">
      <!-- 左侧内容 -->
    </div>
    <div class="right-column">
      <!-- 右侧内容 -->
    </div>
  </div>
</body>
</html>
Copy after login

2. CSS style:
Next, we need to This layout adds some CSS styles to achieve the desired effect. We will use flexbox layout to implement this responsive layout, so add the following code in the style.css file:

.container {
  display: flex; 
  /* 设为flex布局,子元素将自动排列 */
  flex-wrap: wrap; 
  /* 如果子元素太多放不下,换行显示 */
}

.left-column {
  flex: 1; 
  /* 左侧栏占据1份,即整个宽度的1/3 */
  background-color: #eee; 
  /* 左侧栏的背景颜色 */
  padding: 20px; 
  /* 内边距,让内容离边框有一定距离 */
}

.right-column {
  flex: 2; 
  /* 右侧栏占据2份,即整个宽度的2/3 */
  background-color: #ddd; 
  /* 右侧栏的背景颜色 */
  padding: 20px; 
  /* 内边距,让内容离边框有一定距离 */
}

/* 响应式设计 */
@media screen and (max-width: 768px) {
  .left-column, .right-column {
    flex: 1; 
    /* 在小屏幕上将左右侧栏宽度设为100% */
  }
}
Copy after login

3. Description and Demonstration:
In the above code , we first set the entire layout container .container to display: flex, so that the sub-elements .left-column and .right-column will automatically arrange them on one line.

Next, specify the width ratio of the left and right side columns through the flex attribute. In this example, the left column is set to flex: 1 and the right column is set to flex: 2, which means the right column is twice as wide as the left column .

Finally, we use media queries @media for responsive design. When the screen width is less than or equal to 768px, the width of the left and right sidebars is set to 100% to adapt to small screen devices.

4. Summary:
Through the above code example, we can implement a simple two-column responsive layout. By flexibly using CSS's flexbox layout and media queries, we can quickly implement layout effects that adapt to different devices.

At the same time, if you need to further beautify and optimize the layout, you can add other CSS styles and adjust the column width ratio according to your own needs.

I hope this tutorial will be helpful for you to learn and apply responsive layout!

The above is the detailed content of CSS layout tutorial: The best way to implement a two-column responsive layout. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

The evolution and application of CSS layout units: from pixels to relative units based on the font size of the root element The evolution and application of CSS layout units: from pixels to relative units based on the font size of the root element Jan 05, 2024 pm 05:41 PM

From px to rem: The evolution and application of CSS layout units Introduction: In front-end development, we often need to use CSS to implement page layout. Over the past few years, CSS layout units have evolved and developed. Initially we used pixels (px) as the unit to set the size and position of elements. However, with the rise of responsive design and the popularity of mobile devices, pixel units have gradually exposed some problems. In order to solve these problems, the new unit rem came into being and was gradually widely used in CSS layout. one

Unit Selection Guide for Responsive Layout Design Unit Selection Guide for Responsive Layout Design Jan 27, 2024 am 08:26 AM

With the popularity of mobile devices and the development of technology, responsive layout has become one of the essential skills for designers. Responsive layout is designed to provide the best user experience for screens of different sizes, allowing web pages to automatically adjust their layout on different devices to ensure the readability and usability of content. Choosing the right units is one of the key steps in responsive layout design. This article will introduce some commonly used units and provide suggestions for selecting units. Pixel (px): Pixel is the smallest unit on the screen. It is an absolute unit and does not automatically change as the screen size changes.

Explore the best responsive layout frameworks: the competition is fierce! Explore the best responsive layout frameworks: the competition is fierce! Feb 19, 2024 pm 05:19 PM

Responsive layout framework competition: who is the best choice? With the popularity and diversification of mobile devices, responsive layout of web pages has become more and more important. In order to cater to the different devices and screen sizes of users, it is essential to adopt a responsive layout framework when designing and developing web pages. However, with so many framework options out there, we can’t help but ask: which one is the best choice? The following will be a comparative evaluation of three popular responsive layout frameworks, namely Bootstrap, Foundation and Tailwind.

CSS Layout Guide: Best Practices for Implementing Grid Layout CSS Layout Guide: Best Practices for Implementing Grid Layout Oct 26, 2023 am 10:00 AM

CSS Layout Guide: Best Practices for Implementing Grid Layout Introduction: In modern web design, grid layout has become a very popular layout method. It can help us better organize the page structure and make it more hierarchical and readable. This article will introduce the best practices of grid layout and specific code examples to help you better implement grid layout. 1. What is grid layout? Grid layout refers to dividing the page into multiple columns and rows through a grid, so that the elements of the page can be easily arranged according to certain rules. grid layout

Practical tips for using HTML fixed positioning in responsive layouts Practical tips for using HTML fixed positioning in responsive layouts Jan 20, 2024 am 09:55 AM

Application skills of HTML fixed positioning in responsive layout, specific code examples are required. With the popularity of mobile devices and the increase in user demand for responsive layout, developers have encountered more challenges in web design. One of the key issues is how to implement fixed positioning to ensure that elements can be fixed at specific locations on the page under different screen sizes. This article will introduce the application skills of HTML fixed positioning in responsive layout and provide specific code examples. Fixed positioning in HTML is through the position attribute of CSS

Implementation method of HTML's responsive layout design guide Implementation method of HTML's responsive layout design guide Jan 27, 2024 am 08:26 AM

How to use HTML to implement responsive layout design. With the popularity of mobile devices and the rapid development of the Internet, responsive layout has become an essential skill for designers. Responsive layout allows the website to automatically adapt to different screen sizes and resolutions on different devices, allowing users to have a better browsing experience. This article will introduce how to use HTML to implement responsive layout design and provide specific code examples. Using @media query @media query is a feature in CSS3 that can be applied based on different media conditions

Front-end SEO—detailed explanation Front-end SEO—detailed explanation Mar 12, 2024 pm 06:13 PM

1. Working principle of search engine When we enter keywords in the input box and click search or query, we then get the results. Digging into the story behind it, search engines do a lot of things. Search engine websites, such as Baidu, have a very large database in the background, which stores a large number of keywords, and each keyword corresponds to many URLs. These URLs are extracted one by one from the vast Internet by the Baidu program. Collected by clicking downloads, these programs are called "search engine spiders" or "web crawlers". These hard-working "spiders" crawl on the Internet every day, from one link to another, download the content, analyze and refine it, and find the keywords. If the "spider" thinks that the keyword is not in the database and is not useful to the user, Useful

See all articles