How To Create A Responsive Two Column Layout In HTML And CSS
Có thể bạn quan tâm
As a web developer, you have a variety of layout you can use to present content on a website.
You can use a two column layout to display:
- Related content for example a list of features, pricing cards, testimonials.
- Additional content for example sidebar with newsletter, related posts, author’s bio.
- Two separate products or services from one creator for example a photography who is also a designer can choose to create a single website with both services on it.
A two column layout is commonly used on screen wider than smartphone screen that is tablet, desktop and wide screen. With responsive design, the two columns are reorganized into one column on smartphone screens.
Using a two columns layout, you can split and structure the content into related sections.
Content in the same section with the same background appear to be relate.
Content in same section but different background appear to be contrasted with each other.
This helps the reader to navigate your website smoothly.
Advertisement
How to divide a div into two columns in HTML and CSS
1. Two equal columns Layout HTML CSS
a. Using CSS Float
Add the HTML code:
<div class="columns"> <div class="column"> /// Column 1: Put any relevant content here /// </div> <div class="column"> /// Column 2: Put any relevant content here /// </div> </div>Add the CSS code:
.columns { Width: 100%; } .column { width:100%; } @media (min-width: 48em) { .column { width: 50%; float:left; } .columns { content: ""; display: table; clear: both; } }b. Using Flexbox
<div class="columns"> <div class="column"> /// Column 1: Put any relevant content here /// </div> <div class="column"> /// Column 2: Put any relevant content here /// </div> </div> .columns { display: flex; flex-direction: row; flex-wrap: wrap; width: 100%; } .column { flex: 50%; }c. 2 equal columns layout Using CSS Grid
<div class="columns"> <div class="column"> /// Column 1: Put any relevant content here /// </div> <div class="column"> /// Column 2: Put any relevant content here /// </div> </div> .columns { display: grid; grid-template-columns: 50% 50%; }d. Two Column Layout Using Bootstrap
Add Bootstrap links in your <head> </head> section:
Add your HTML code:
<div class="container"> <div class="row"> <div class="col-sm"> One of three columns </div> <div class="col-sm"> One of three columns </div> <div class="col-sm"> One of three columns </div> </div> </div>e. Two Equal Columns Using Bulma
Add Bulma link in your <head> </head> section:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css">Then add the following HTML code within your <body> </body> tags.
<div class="columns"> <div class="column"> First column </div> <div class="column"> Second column </div> </div>Advertisement
2. Create Sidebar Layout: Two column layout
a. Create sidebar layout CSS
Add the HTML code:
<div class="columns"> <div class="column column-1"> /// Column 1: Put any relevant content here /// </div> <div class="column column-2"> /// Column 2: Put any relevant content here /// </div> </div>Add the CSS code:
.columns { Width: 100%; } .column-1, .column-2 { width:100%; } @media (min-width: 48em) { .column-1 { width: 70%; float:left; } .column-2 { width: 30%; float:left; } .columns { content:""; display: table; clear: both; } }b. Create Sidebar Layout with Flexbox
<div class="columns"> <div class="column"> /// Column 1: Put any relevant content here /// </div> <div class="column"> /// Column 2: Put any relevant content here /// </div> </div> .columns { display: flex; flex-direction: row; flex-wrap: wrap; width: 100%; } .column { flex: 50%; }Từ khóa » Html Css Div 2 Column Layout
-
How To Create A Two Column Layout - W3Schools
-
CSS Multiple Columns - W3Schools
-
CSS Multi-column Layout - CSS: Cascading Style Sheets | MDN
-
2 Column Layouts (Responsive Demos & Example HTML/CSS)
-
How To Create A 2-column Layout Grid With CSS? - Tutorialspoint
-
How To Create Columns In HTML
-
Ridiculously Easy Row And Column Layouts With Flexbox
-
CSS Layout Techniques | Comm244 Notes
-
Grid System - Bootstrap
-
CSS Floating 2 Column Layout With Multiple Elements - Stack Overflow
-
Simple 2 Column CSS Layout | 456 Berea Street
-
How To Build A 2-column Layout Using Flexbox | HTML/CSS - YouTube
-
How To Define Two Column Layout Using Flexbox ? - GeeksforGeeks
-
2-column Layout With Margin In Between