Gxl网
  • 首页
  • 编程
    PHP基础 PHP教程 php框架 JavaScript asp.net AJAX 正则表达式 ASP html代码 css 前端框架 Python 服务器
  • 数据库
    mysql mssql redis 数据库问题
  • 系统教程
    window10教程 window8教程 window11教程 window7教程 windowxp教程 linux教程 U盘教程
  • 操作系统
    Windowsxp windows7 windows8 windows10 其他操作系统
  • 程序下载
    企业程序 小说/有声 网站模板 第三方软件 新闻资讯 第三方源码 小程序 商城源码
  • 框架书籍
    前端开发 服务器端开发 数据库 开发软件 其他手册
  • jquery插件库
    输入 banner图 图片脚本 导航/分类 播放器 css3 jQuery脚本 jqueryhtml5 进度条 贴图/客服
  • 在线工具
    编码转换工具 在线IDE编码工具
  • 开发手册
    linux命令大全 Bootstrap HTML参考手册 css手册/教程 ThinkPHP5.0 ThinkPHP3.2
当前位置:Gxlcms > html代码 > 如何使用Flexbox构建新闻站点布局_html/css_WEB-ITnose

如何使用Flexbox构建新闻站点布局_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:49人阅读

英文原文: 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.

2/3 column
1/3 column

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:

 

.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.

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/css_WEB-ITnose

  • 下一篇 >

    CSS清除浮动_html/css_WEB-ITnose

人气教程排行

  • 230次 1 如何生成一个调查问卷_html/css_WEB-ITnose
  • 230次 2 在页面直接显示日历_html/css_WEB-ITnose
  • 230次 3 如何点击a标签实现弹出inputfile上传文件对话框_HTML/Xhtml_网页制作
  • 229次 4 关于列表标记的详细介绍
  • 229次 5 cssborder-bottom(指定下边线的样式、宽度及颜色)
  • 229次 6 html新闻详情页_html/css_WEB-ITnose
  • 229次 7 为何页面内容和网页边缘有空隙_html/css_WEB-ITnose
  • 228次 8 position:fixed定位时“高度坍塌”问题的解决_html/css_WEB-ITnose
  • 227次 9 htmlp标签怎么换行?htmlp标签添加br换行标签的应用
  • 226次 10 HTML的<!DOCTYPE>标签
  • 226次 11 html页面中友情链接怎么进行添加设置?(代码示例)
  • 226次 12 form表单中属性及功能应用介绍汇总
  • 226次 13 详解form标签中的method属性
  • 226次 14 HTML5Canvas逼真烟雾效果js插件
  • 226次 15 页面跳转特效_html/css_WEB-ITnose
  • 226次 16 改变鼠标选中区域的样式。_html/css_WEB-ITnose
  • 225次 17 关于$("body").append()一段html代码,在页面写能触发事件,写在js文件里写就没有_html/css_WEB-ITnose
  • 225次 18 CSS3悬浮动画效果_html/css_WEB-ITnose
  • 225次 19 纯C语言实现的CSS解析器:katana_html/css_WEB-ITnose
  • 225次 20 body在默认情况下是具有margin外边距的_html/css_WEB-ITnose
本站所有资源全部来源于网络,若本站发布的内容侵害到您的隐私或者利益,请联系我们删除!
登录

忘记密码?

登录

看不清楚换一张

注册