css grid布局_如何使用CSS Grid重新创建Medium的文章布局

css grid布局

When people think of CSS Grid they normally envision image grid layouts and full web pages. However, CSS Grid is actually a superb technology for laying out articles as well, as it allows you to do things which previously was tricky to achieve.

人们想到CSS网格时,通常会想到图像网格布局和完整的网页。 但是,CSS Grid实际上也是一种用于布置文章的精湛技术,因为它允许您执行以前很难完成的事情。

In this tutorial, I’ll explain how to recreate the famous Medium article layout using CSS Grid.

在本教程中,我将说明如何使用CSS Grid重新创建著名的Medium文章布局。

Note: I’ve also been part of creating a free 13-part CSS Grid course at Scrimba. Get access to the course here.

注意:我还参与了在Scrimba创建一个由13个部分组成的免费CSS网格课程。 在此获取该课程的访问权限。

In the course, my colleague Magnus Holm will go through how to create an article layout using CSS Grid. So if you prefer watching instead of reading, be sure to check out his screencast.

在课程中,我的同事Magnus Holm将介绍如何使用CSS Grid创建文章布局。 因此,如果您喜欢观看而不是阅读,请务必查看他的截屏视频。

内容 (The content)

We’re going to start off with a basic HTML file, which contains the type of content you’ll typically find in a Medium article. For example title, paragraphs, subtitles, images, quotes and so forth. Here’s an outtake:

我们将从一个基本HTML文件开始,该文件包含您在“中型”文章中通常会找到的内容类型。 例如标题,段落,字幕,图像,引号等。 这是一个额外费用:

<article><h1>Running any NPM package in the browser locally</h1><p>JavaScript has never had any official solution for distributing packages, and every web platform (Rails, Django etc) has their own idea of how to structure and package JavaScript. In the last few years, NPM has started becoming the canonical way of distribution, with Webpack as the build system, but there’s no way to load NPM packages in the browser without a server-side component.</p><blockquote><p>Scrimba is a platform for interactive coding screencast where           
you can run the code at any moment in time.</p></blockquote><figure><img src="https://mave.me/img/projects/full\_placeholder.png"></figure>

If you open this file in a website without adjusting any layout it’ll look like this:

如果您在网站上打开此文件而不调整任何布局,它将看起来像这样:

Not particularly elegant. So let’s fix it with CSS Grid. We’ll do it step by step so that it’ll be easy for you to follow.

不是特别优雅。 因此,让我们使用CSS Grid对其进行修复。 我们将逐步进行操作,以使您易于遵循。

边距的基本设置 (Basic setup for margins)

The first thing we need to do is turn the whole article tag into a grid and give it at least three columns.

我们需要做的第一件事就是把整个article标签为一个网格,并给它至少三列。

article {  display: grid;  grid-template-columns: 1fr 740px 1fr;  
}

The first and last columns are responsive and act as margins. They’ll contain white space in most cases. The middle column is fixed at 740 pixels and will hold the content of the article.

第一列和最后一列是响应式的,并充当边距。 在大多数情况下,它们将包含空格。 中间栏固定为740像素,将容纳文章的内容。

Notice that we’re not defining the rows as they’ll simply be as tall as they need to be in order to fit their content. Each content block in the article (paragraph, image, title) will get its own row.

请注意,我们并未定义行,因为它们只是为了符合其内容而需要的高度。 文章中的每个内容块(段落,图像,标题)都将拥有自己的一行。

The next step is to make sure all the content in the grid starts at the second column line by default.

下一步是确保默认情况下网格中的所有内容都从第二列开始。

article > \* {  grid-column: 2;  
}

We now have the following result:

现在,我们得到以下结果:

We can instantly see that this looks better, as the white space on each side makes the text easier to read.

我们可以立即看到它看起来更好,因为两侧的空白使文本更易于阅读。

However, this effect could have been achieved just as easily using by setting the left and right margin property to auto. So why use CSS Grid?

但是,通过将left和right的margin属性设置为auto可以轻松实现此效果。 那么为什么要使用CSS Grid?

Well, the problem arises when we want to mimic Medium’s image features. For example creating full-width images, like this one:

好吧,当我们要模仿Medium的图像功能时,就会出现问题。 例如,创建全幅图像,如下所示:

If we had used margin: 0 auto this would have forced us to apply negative margins to the images to make them take up the entire website width, which is hacky.

如果我们使用margin: 0 auto这将迫使我们对图像应用负的页边距,以使其占据整个网站宽度,这实在是骇人听闻。

With CSS Grid though, this becomes a piece of cake, as we’ll simply use columns to set the width. To make our image take up the entire width we’ll just tell it to span from the first to the last column line.

但是,对于CSS Grid,这只是小菜一碟,因为我们将仅使用列来设置宽度。 为了使图像占据整个宽度,我们只需要告诉它从第一列到最后一列就可以跨越。

article > figure {  grid-column: 1 / -1;  margin: 20px 0;  
}

We’ve also set some margin on the top and bottom. And then we have a nice full-width image:

我们还在顶部和底部设置了一些边距。 然后我们有一个不错的全角图像:

扩展更多列 (Expanding with more columns)

However, this doesn’t get us all the way, as Medium has a few other layouts which we need to account for. Let’s look at a couple of them:

但是,这并不能完全解决问题,因为Medium还需要考虑其他一些布局。 让我们看看其中的几个:

中型图片 (Mid-sized images)

This is the image option in between the normal one and the full width one, which we’ll call a mid-sized one. It looks like this:

这是介于正常宽度和全角宽度之间的图像选项,我们将其称为中型宽度。 看起来像这样:

NOTE: If you’re watching on mobile, this image is identical to the full width one. In this article, we focus on the desktop layout only.

注意:如果 您正在手机上观看,此图像与全角图像相同。 在本文中,我们仅关注桌面布局。

This will require at least two new columns to our layout.

这将需要至少两个新列用于我们的布局。

行情 (Quotes)

In addition, Medium also places a vertical line on the left-hand side of the article if you add a quote:

此外,如果添加引号,Medium还会在文章的左侧放置一条垂直线

← Notice the vertical line. We’ll need to add an extra column to our grid because of it.

←注意垂直线。 因此,我们需要在网格中添加一个额外的列。

This requires a tiny column on the left-hand side of the grid. To make things symmetric, we’ll also add a similar column on the right-hand side.

这需要在网格的左侧有一小列。 为了使事物对称,我们还将在右侧添加一个类似的列。

So to support both quotes and mid-sized images we’ll need to split the entire width into seven columns instead of three, like this:

因此,为了同时支持引号中型图片,我们需要将整个宽度分成七列而不是三列,如下所示:

article {  display: grid;  **grid-template-columns: 1fr 1fr 10px 740px 10px 1fr 1fr;**  
}

If we use the Chrome inspector we can actually see the underlying grid lines (see image below). Plus, I’ve added pointers to make it easier to recognise the different columns.

如果使用Chrome检查器,我们实际上可以看到底层的网格线(请参见下图)。 另外,我添加了指针以使其更容易识别不同的列。

I’ve added pointers to make it easier to recognise the different columns.

我添加了指针,以使其更容易识别不同的列。

The first thing we need to do it to make all default items to start at the fourth column line instead of the second one.

我们需要做的第一件事是使所有默认项都从第四列而不是第二列开始。

article > \* {  grid-column: 4;  
}

Then we can create the mid-sized image by doing:

然后,我们可以通过执行以下操作来创建中型图片:

article > figure {  grid-column: 2 / -2;  margin: 20px 0;  
}

Here’s how that looks with the Chrome inspector activated:

启用Chrome检查器后的外观如下:

The quotes are easily created by doing the following:

通过执行以下操作可以轻松创建引号

article > blockquote {  grid-column: 3 / 5;  padding-left: 10px;  color: #666;  border-left: 3px solid black;  
}

We make it span from the third to the third to the fifth column line. We’re also adding padding-left: 10px; so that the text will seem to start at the fourth column line (the third column is 10 pixels wide as well). Here’s how it looks on the grid.

我们将其从第三列扩展到第三列到第五列。 我们还添加了padding-left: 10px; 这样文本就好像从第四列开始(第三列也是10像素宽)。 这是它在网格上的外观。

边标 (Sidemarks)

Now there’s one last thing we need to support. Medium has a pretty nice way of signalling which content in the article is most highlighted. The text turns into green, and it gets a Top highlight on the right hand side.

现在,我们需要支持的最后一件事。 Medium具有一种很好的方式来表示文章中哪些内容是最突出的。 文本变为绿色,并且在右侧获得顶部突出显示

The Top highlight text element would be a nightmare to create if we’d used margin: 0 auto; instead if CSS Grid. This is because the element acts different from all the other elements in the article. Instead of appearing on a new line, it’s suppose to appear on the right hand side of the previous element. If we didn’t use CSS Grid we’d probably have to start messing with position: absolute; to make this work.

如果使用margin: 0 auto;Top高亮文本元素将是一个噩梦margin: 0 auto; 相反,如果是CSS Grid。 这是因为该元素的行为不同于本文中的所有其他元素。 假设它没有出现在新行上,而是出现在上一个元素的右侧。 如果我们不使用CSS Grid,那么我们可能不得不开始搞乱position: absolute; 使这项工作。

But with CSS Grid it’s super simple. We’ll just make that kind of element start on the fourth column line.

但是使用CSS Grid超级简单。 我们将使这种元素从第四列开始。

.aside {  grid-column: 5;  
}

That’ll automatically make it place itself to the right of the article:

这将自动使其位于文章的右侧:

Note: I haven’t highlighted the text in green, as that’s got nothing to do with CSS Grid.

注意:我没有用绿色突出显示文本,因为这与CSS Grid无关。

And that’s it! We’ve now recreated most of Medium’s article layout using CSS Grid. And it was actually pretty easy. Note however that we’ve not touched responsiveness, as that requires a whole new article in itself.

就是这样! 现在,我们已经使用CSS Grid重新创建了Medium的大部分文章布局。 而且实际上很容易。 但是请注意,我们还没有涉及响应能力,因为这本身就需要全新的文章。

Check out this Scrimba playground to look at all the code.

查看此Scrimba游乐场,查看所有代码。



Thanks for reading! My name is Per Borgen, I'm the co-founder of Scrimba – the easiest way to learn to code. You should check out our responsive web design bootcamp if want to learn to build modern website on a professional level.

谢谢阅读! 我叫Per Borgen,我是Scrimba的共同创始人–学习编码的最简单方法。 如果要学习以专业水平构建现代网站,则应查看我们的响应式Web设计新手训练营 。

翻译自: https://www.freecodecamp.org/news/how-to-recreate-mediums-article-layout-with-css-grid-b4608792bad1/

css grid布局

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394903.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2017视频监控行业应用趋势与市场发展分析

安防行业的发展&#xff0c;从传统单一的业务形态到业务多元化与国际化的转变&#xff0c;是社会安全需求变化与视频监控技术双向驱动的结果。在新的行业生态体系下&#xff0c;传统监控技术与新兴技术的融合&#xff0c;跨行业的业务协同&#xff0c;以及以客户为中心的产业形…

oracle 11.2.4联机文档,ORACLE 11G 联机文档partition_extended_name的一个错误

在看11G联机文档的PARTITION EXTENDED NAME限制的时候&#xff0c;测试发现与书上描述不符。Restrictions on Extended Names Currently, the use of partition-extended and subpartition-extended table names has the following restrictions:No remote tables: A partition…

mongodb 安装、启动

MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1 最近有太多的同学向我提起MongoDB,想要学习MongoDB,还不知道MongoDB到底是什么鬼,或者说,知道是数据库,知道是文件型数据库,但是不知道怎么来用 那么好,所谓千呼万唤始出来,现在我就拉给你们看: 一.初识MongoDB 之 什么东西都…

python os path_python os.path模块

os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径)中&#xff0c;所有path共有的最长的路径。 os.path.dirname(path) #返回文件路径 os.path.exists(path) #路径存在则返回True,路径损坏返回False os.…

[转载]PSCAD调用MATLAB/SIMULINK之接口元件设计

原文地址&#xff1a;PSCAD调用MATLAB/SIMULINK之接口元件设计作者&#xff1a;luckyhappier1)接口元件 接口元件包括Graphics&#xff0c;Parameters和Script。注意&#xff1a;变量要与DSDYN要一致&#xff08;PSCAD根据变量名区别变量&#xff09;。 2&#xff09;Circuit 定…

css flexbox模型_Flexbox教程:了解如何使用CSS Flexbox编写响应式导航栏

css flexbox模型In this article, I’ll explain how to create a navbar which adapts to various screen sizes using Flexbox along with media queries.在本文中&#xff0c;我将解释如何使用Flexbox和媒体查询来创建适应各种屏幕尺寸的导航栏。 This tutorial can also b…

oracle数字类型ef映射,Entity Framework 学习中级篇5—使EF支持Oracle9i - ♂风车车.Net - 博客园...

从Code MSDN上下载下来的EFOracleProvider不支持Oracle9i.但是,目前我所使用的还是Oracle9i。为此,对EFOracleProvider修改了以下&#xff0c;以便使其支持Oracle9i.下面说说具体修改地方.(红色部分为添加或修改的代码部分)一&#xff0c;修改EFOracleProvider1,修改EFOraclePr…

Oracle 数据库之最:你见过最高的 SQL Version 是多少?

Oracle数据库中执行的SQL&#xff0c;很多时候会因为种种原因产生多个不同的执行版本&#xff0c;一个游标的版本过多很容易引起数据库的性能问题&#xff0c;甚至故障。 有时候一个SQL的版本数量可能多达数万个&#xff0c;以下是我之前在"云和恩墨大讲堂”分享过的一个案…

mybatis传参问题总结

一、 传入单个参数 当传入的是单个参数时&#xff0c;方法中的参数名和sql语句中参数名一致即可 List<User> getUser(int id);<select id"getUser" parameterType"java.lang.Integer" resultType"com.lee.test.pojo.User">select *…

C 怎么读取Cpp文件_opencv从yaml文件中读取矩阵(c++)

PS:由于我是新手&#xff0c;因此记录的比较罗里吧嗦&#xff0c;本文也属于一个没有任何技术的编程积累。在SLAM系统中&#xff0c;经常需要从配置文件中读取参数文件&#xff0c;读取整型&#xff0c;浮点型都是比较常见的操作&#xff0c;在读取矩阵卡了一下&#xff0c;记录…

oracle中的判断大小,sql语句判断大小

如何用sql语句查看某个数据库中的表的大小--读取库中的所有表名select name from sysobjects where xtypeu--读取指定表的所有列名select name from syscolumns where id(select max(id) from sysobjects where xtypeu and name表名)获取数据库表名和字段sqlserver中各个系统表…

超越Android:探索Kotlin的应用领域

by Adam Arold亚当阿罗德(Adam Arold) 超越Android&#xff1a;探索Kotlin的应用领域 (Going beyond Android: exploring Kotlin’s areas of application) If you have written something in Kotlin, chances are that you wrote it for Android. Kotlin, however, has other…

3.SFB标准版前端安装

SFB服务器准备部分&#xff1a;1.修改服务器名称&#xff0c;sfb加入域&#xff0c;用域管理员账户登录2.配置服务器IP地址&#xff0c;DNS3.安装Windows组件Add-WindowsFeature NET-Framework-Core, RSAT-ADDS, Windows-Identity-Foundation, Web-Server, Web-Static-Content,…

向spark standalone集群提交任务

文档链接 #切换到spark安装目录,执行下面一条命令,192.168.0.10是master的ip, examples/src/main/python/pi.py 是python 文件的路径 ./bin/spark-submit --master spark://192.168.0.106:7077 examples/src/main/python/pi.py任务已经执行完毕,耗时10秒 转载于:https://www.c…

SQLite学习手册

一、聚合函数&#xff1a; SQLite中支持的聚合函数在很多其他的关系型数据库中也同样支持&#xff0c;因此我们这里将只是给出每个聚集函数的简要说明&#xff0c;而不在给出更多的示例了。这里还需要进一步说明的是&#xff0c;对于所有聚合函数而言&#xff0c;distinct关键字…

oracle全局索引 效率,关于插入,全局索引和局部索引的情况,那种效率高

分区表上的索引表可以按range&#xff0c;hash&#xff0c;list分区&#xff0c;表分区后&#xff0c;其上的索引和普通表上的索引有所不同&#xff0c;oracle对于分区表上的索引分为2类&#xff0c;即局部索引和全局索引&#xff0c;下面分别对这2种索引的特点和局限性做个总结…

python excelwriter保存路径_Python和Excel 终于可以互通了!!

点击“开发者技术前线”&#xff0c;选择“星标&#x1f51d;”在看|星标|留言, 真爱作者&#xff1a;小天真_5eed 链接&#xff1a;https://www.jianshu.com/p/6ecf414f3372今天为大家分享一篇使用python将大量数据导出到Excel中的技巧心得&#xff0c;可以让Python和Excel…

nodejs 调用微服务器_无服务器NodeJS:构建下一个微服务的快速,廉价方法

nodejs 调用微服务器by Filipe Tavares由Filipe Tavares 无服务器NodeJS&#xff1a;构建下一个微服务的快速&#xff0c;廉价方法 (Serverless NodeJS: the fast, inexpensive way to build your next microservice) I love Node.js. I’ve re-discovered Javascript through…

(蓝桥杯)2018JAVA B组 日志分析

日志统计 小明维护着一个程序员论坛。现在他收集了一份"点赞"日志&#xff0c;日志共有N行。其中每一行的格式是&#xff1a; ts id 表示在ts时刻编号id的帖子收到一个"赞"。 现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长…

MySQL 导出数据

2019独角兽企业重金招聘Python工程师标准>>> 1、导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 存放位置比如&#xff1a; mysqldump -u root -p project > c:/a.sql 2.导出一个表的结构&#xff0c;并且带表中的数据 mysqldump -u 用户名 -p 数据库名 …