英文原文: http://webdesign.tutsplus.com/tutorials/how-to-build-a-news-website-layout-with-flexbox--cms-26611
It’s not necessary to understand every aspect of Flexbox before you can jump in and get started. In this tutorial, we’re going to introduce a few features of Flexbox whilst designing a “news layout” like the one you can find on The Guardian .
The reason we’re using Flexbox is that it provides very powerful features:
- we can easily make responsive columns
- we can make columns of equal height
- we can push content to the bottom of a container
So let’s get started!
1. Start with Two Columns
Creating columns in CSS has always been a challenge. For a long time, the only options were to use floats or tables, but they both had their own issues.
Flexbox makes the process easier, giving us:
- cleaner code : we only need a container with display: flex
- no need to clear floats, preventing unexpected layout behavior
- semantic markup
- flexibility : we can resize, stretch, align the columns in a few lines of CSS
Let’s start by making two columns; one that’s 2/3 of the width of our container, and one that’s 1/3.
<div class="columns"> <div class="column main-column"> 2/3 column </div> <div class="column"> 1/3 column </div></div>
There are two elements here:
- the columns container
- two column children, one with an additional class of main-column which we’ll use to make it wider
.columns { display: flex;}.column { flex: 1;}.main-column { flex: 2;}
As the main column has a flex value of 2, it will take up twice as much space as the other column.
By adding some additional visual styles, here’s what we get:
2. Make Each Column a Flexbox Container
Each of these two columns will contain several articles stacked vertically, so we’re going to turn the column elements into Flexbox containers too. We want:
- the articles to be stacked vertically
- the articles to stretch and fill the available space
.column { display: flex; flex-direction: column; /* Makes the articles stacked vertically */}.article { flex: 1; /* Stretches the articles to fill up the remaining space */}
The flex-direction: column rule on the container, combined with the flex: 1 rule on the children ensures that the articles will fill up the whole vertical space, keeping our first two columns the same height.
3. Make Each Article a Flexbox Container
Now, to give us extra control, let’s turn each article into a Flexbox container too. Each of them will contain:
- a title
- a paragraph
- an information bar with the author and the number of comments
- an optional responsive image
We’re using Flexbox here in order to “push” the information bar to the bottom. As a reminder, this is the article layout we’re aiming for:
Here’s the code:
<a class="article first-article"> <figure class="article-image"> <img src="" alt="如何使用Flexbox构建新闻站点布局_html/css_WEB-ITnose" > </figure> <div class="article-body"> <h2 class="article-title"> <!-- title --> </h2> <p class="article-content"> <!-- content --> </p> <footer class="article-info"> <!-- information --> </footer> </div></a>
.article { display: flex; flex-direction: column; flex-basis: auto; /* sets initial element size based on its contents */}.article-body { display: flex; flex: 1; flex-direction: column;}.article-content { flex: 1; /* This will make the content fill up the remaining space, and thus push the information bar at the bottom */}
The article’s elements are laid out vertically thanks to the flex-direction: column; rule.
We apply flex: 1 to the article-content element so that it fills up the empty space, and “pushes” the article-info to the bottom, no matter the height of the columns.
4. Add Some Nested Columns
In the left column, what we actually want is another set of columns. So we’re going to replace the second article with the same columns container we’ve already used.
<div class="columns"> <div class="column nested-column"> <a class="article"> <!-- Article content --> </a> </div> <div class="column"> <a class="article"> <!-- Article content --> </a> <a class="article"> <!-- Article content --> </a> <a class="article"> <!-- Article content --> </a> </div></div>
As we want the first nested column to be wider, we’re adding a nested-column class with the additional style:
.nested-column { flex: 2;}
This will make our new column twice as wide as the other.
5. Give the First Article a Horizontal Layout
The first article is really big. To optimize the use of space, let’s switch its layout to be horizontal.
.first-article { flex-direction: row;}.first-article .article-body { flex: 1;}.first-article .article-image { height: 300px; order: 2; padding-top: 0; width: 400px;}
The order property is very useful here, as it allows us to alter the order of HTML elements without affecting the HTML markup. The article-image actually comes before the article-body in the markup, but it will behave as if it comes after.
6. Make the Layout Responsive
This is all looking just as we want, though it’s a bit squished. Let’s fix that by going responsive.
One great feature of Flexbox is that you need only remove the display: flex rule on the container to disable Flexbox completely, while keeping all the other Flexbox properties (such as align-items or flex) valid.
As a result, you can trigger a “responsive” layout by enabling Flexbox only above a certain breakpoint.
We’re going to remove display: flex from both the .columns and .column selectors, instead wrapping them in a media query:
@media screen and (min-width: 800px) { .columns, .column { display: flex; }}
That’s it! On smaller screens, all the articles will be on top of each other. Above 800px, they will be laid out in two columns.
7. Add Finishing Touches
To make the layout more appealing on larger screens, let’s add some CSS tweaks:
@media screen and (min-width: 1000px) { .first-article { flex-direction: row; } .first-article .article-body { flex: 1; } .first-article .article-image { height: 300px; order: 2; padding-top: 0; width: 400px; } .main-column { flex: 3; } .nested-column { flex: 2; }}
The first article has its content laid out horizontally, with the text on the left and the image on the right. Also, the main column is now wider (75%) and the nested column too (66%). Here’s the final result!
Conclusion
I hope I’ve shown you that you needn’t understand every aspect of Flexbox to jump in and start using it! This responsive news layout is a really useful pattern; pull it apart, play with it, let us know how you get on!

HTML은 웹 페이지를 작성하는 데 사용되는 언어로, 태그 및 속성을 통해 웹 페이지 구조 및 컨텐츠를 정의합니다. 1) HTML과 같은 태그를 통해 문서 구조를 구성합니다. 2) 브라우저는 HTML을 구문 분석하여 DOM을 빌드하고 웹 페이지를 렌더링합니다. 3) 멀티미디어 기능을 향상시키는 HTML5의 새로운 기능. 4) 일반적인 오류에는 탈수 된 레이블과 인용되지 않은 속성 값이 포함됩니다. 5) 최적화 제안에는 시맨틱 태그 사용 및 파일 크기 감소가 포함됩니다.

WebDevelopmentReliesonHtml, CSS 및 JavaScript : 1) HtmlStructuresContent, 2) CSSSTYLESIT, 및 3) JAVASCRIPTADDSINGINTERACTIVITY, BASISOFMODERNWEBEXPERIENCES를 형성합니다.

HTML의 역할은 태그 및 속성을 통해 웹 페이지의 구조와 내용을 정의하는 것입니다. 1. HTML은 읽기 쉽고 이해하기 쉽게하는 태그를 통해 컨텐츠를 구성합니다. 2. 접근성 및 SEO와 같은 시맨틱 태그 등을 사용하십시오. 3. HTML 코드를 최적화하면 웹 페이지로드 속도 및 사용자 경험이 향상 될 수 있습니다.

"Code"는 "Code"BroadlyIncludeLugageslikeJavaScriptandPyThonforFunctureS (htMlisAspecificTypeofCodeFocudecturecturingWebContent)

HTML, CSS 및 JavaScript는 웹 개발의 세 가지 기둥입니다. 1. HTML은 웹 페이지 구조를 정의하고 등과 같은 태그를 사용합니다. 2. CSS는 색상, 글꼴 크기 등과 같은 선택기 및 속성을 사용하여 웹 페이지 스타일을 제어합니다.

HTML은 웹 구조를 정의하고 CSS는 스타일과 레이아웃을 담당하며 JavaScript는 동적 상호 작용을 제공합니다. 세 사람은 웹 개발에서 의무를 수행하고 화려한 웹 사이트를 공동으로 구축합니다.

HTML은 간단하고 배우기 쉽고 결과를 빠르게 볼 수 있기 때문에 초보자에게 적합합니다. 1) HTML의 학습 곡선은 매끄럽고 시작하기 쉽습니다. 2) 기본 태그를 마스터하여 웹 페이지를 만들기 시작하십시오. 3) 유연성이 높고 CSS 및 JavaScript와 함께 사용할 수 있습니다. 4) 풍부한 학습 리소스와 현대 도구는 학습 과정을 지원합니다.

anexampleStartingtaginhtmlis, whithbeginsaparagraph.startingtagsareessentialinhtmlastheyinitiate rements, definetheirtypes, andarecrucialforstructurituringwebpages 및 smanstlingthedom.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

DVWA
DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

WebStorm Mac 버전
유용한 JavaScript 개발 도구
