热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

响应式电子邮件_四工厂技术可创建无媒体查询的响应电子邮件

响应式电子邮件byRmiParmentier由RmiParmentier四工厂技术可创建无媒体查询的响应电子邮件(TheFabFourtechniquetocreateRespon

响应式电子邮件

by Rémi Parmentier

由RémiParmentier

四工厂技术可创建无媒体查询的响应电子邮件 (The Fab Four technique to create Responsive Emails without Media Queries)

I think I found a new way to create responsive emails, without media queries. The solution involves the CSS calc() function and the three width, min-width and max-width properties.

我想我找到了一种无需媒体查询即可创建响应式电子邮件的新方法。 该解决方案涉及CSS calc()函数以及width , min-widthmax-width三个属性。

Or as I like to call them all together: the Fab Four (in CSS).

或者,我喜欢将它们全部称为:Fab四(在CSS中)。

问题 (The problem)

Making responsive emails is hard, especially since email clients on mobile (like Gmail, Yahoo or Outlook.com) don’t support media queries. An hybrid approach, a Gmail first strategy, or a responsive email without media queries are great ways to adapt to this situation.

制作响应式电子邮件非常困难,尤其是因为移动设备上的电子邮件客户端(例如Gmail,Yahoo或Outlook.com)不支持媒体查询。 混合方法 , Gmail优先策略或不带媒体查询的响应电子邮件是适应这种情况的好方法。

That last approach has been my favorite so far. The big idea is to have columns as iv>s with a fixed width aligned with “display:inline-block”. Once a screen can no longer contain two blocks side by side, they will naturally flow below each other. But I’ve always had a problem with this.

到目前为止,最后一种方法是我最喜欢的。 最好的主意是使列 iv>的宽度固定为与“ display:inline- block”对齐。 一旦屏幕不再能够并排包含两个块,它们自然就会在彼此之间流动。 但是我一直对此有疑问。

Once all the blocks are stacked, they don’t take the full width of the email.

一旦所有块堆叠在一起,它们就不会占用电子邮件的全部宽度。

I’ve been looking for ways to solve this problem for a long time. Flexbox is a great contender, but unfortunately Flexbox support in an email is abysmal.

我长期以来一直在寻找解决此问题的方法。 Flexbox是一个很好的竞争者,但是不幸的是,电子邮件中对Flexbox的支持非常糟糕。

一个办法 (A solution)

记住width , min-widthmax-width (Remembering width, min-width and max-width)

On top of the calc() function, the solution I found involves these three CSS properties. In order to fully understand how it works, here’s a reminder of how width, min-width and max-width behave when used together (as clearly summarized by fellow french front-end developer Raphaël Goetter).

calc()函数之上,我发现的解决方案涉及这三个CSS属性。 为了完全理解它是如何工作的,这里提醒人们,当在一起使用width , min-widthmax-width时(法国前端开发人员RaphaëlGoetter 明确总结了这一点)。

  • If the width value is greater than the max-width value, max-width wins.

    如果宽度值大于最大宽度值, 则以最大宽度为准。

  • If the min-width value is greater than the width or max-width values, min-width wins.

    如果最小宽度值大于宽度最大宽度值, 则以最小宽度为准。

Can guess what the width of a box with the following styles would be ?

可以猜出具有以下样式的盒子的宽度是多少?

.box { width:320px; min-width:480px; max-width:160px;}

(Answer : the box would be 480px wide.)

(答案:该框的宽度为480px。)

介绍calc()和魔术公式 (Introducing calc() and the magic formula)

Without further ado, here is an example of the Fab Four to create two columns that will stack and grow below 480px.

事不宜迟,下面是Fab四工厂创建两个列的示例,这些列将堆叠并增长到480px以下。

.block { display:inline-block; min-width:50%; max-width:100%; width:calc((480px — 100%) * 480);}

Let’s break it down for each width property.

让我们对每个width属性进行分解。

min-width:50%;

The min-width property defines our column widths on what we could call our desktop version. We can change this value to add more columns (for example, 25% for a four columns layout), or set columns with fixed pixel widths.

min-width属性定义了我们可以称为桌面版本的列宽。 我们可以更改此值以添加更多列(例如,四列布局为25%),或设置像素宽度固定的列。

max-width:100%;

The max-width property defines our column widths on what we could call our mobile version. At 100%, each column will grow and adapt to the full width of their parent container. We can change this value to keep columns on mobile (for example, 50% for a two columns layout).

max-width属性定义了我们可以称为移动版本的列宽。 每列以100%的比例增长并适应其父容器的整个宽度。 我们可以更改此值以使列保持移动状态(例如,两列布局为50%)。

width:calc((480px — 100%) * 480);

Thanks to the calc() function, the width property is where the magic happens. The 480 value matches our desired breakpoint value. The 100% corresponds to the width of the parent container of our columns. The goal of this calculation is to create a value bigger than our max-width or smaller than our min-width, so that either one of those property is applied instead.

多亏了calc()函数, width属性才是神奇的地方。 480值与我们所需的断点值匹配。 100%对应于我们列的父容器的宽度。 此计算的目标是创建一个大于我们的max-width或小于我们的min-width的值 ,以便改为应用其中一个属性。

Here are two examples.

这是两个例子。

With a parent of 500px, the calculated width equals -9600px. It is smaller than the min-width. So the min-width of 50% wins. Thus we have a two columns layout.

父级为500px时,计算出的宽度等于-9600px。 它小于最小宽度。 因此,最小宽度为50%获胜。 因此,我们有两列布局。

With a parent of 400px, the calculated width equals 38400px. It is bigger than the min-width, but max-width is smaller. So the max-width of 100% wins. Thus we have a one column layout.

父级为400px时,计算出的宽度等于38400px。 它大于最小宽度,但最大宽度较小。 因此,最大宽度为100%会获胜。 因此,我们有一列布局。

演示版 (Demo)

Here is a demo of what this technique can do. You can see the full demo online here (or on Litmus Builder or on CodePen).

这是此技术可以做什么的演示。 您可以在此处 (或在Litmus Builder或CodePen上 ) 在线查看完整的演示 。

And here are two screenshots of this demo in Gmail, on the desktop webmail and on the mobile app on iOS. Same code, different render.

这是Gmail中此演示的两个屏幕截图,分别位于桌面网络邮件和iOS上的移动应用程序中。 相同的代码,不同的渲染。

In this demo, I’ve set a few examples of different grids (with two, three, four columns). The first grid, with the images, is built to go from four columns on desktop to two columns on mobile. The other grids are built to grow full width on mobile.

在此演示中,我设置了一些不同网格的示例(具有两,三,四列)。 建立带有图像的第一个网格,使其从台式机上的四列移动到移动设备上的两列。 其他网格的目的是在移动设备上增加整个宽度。

Also, notice how the title switches from a left aligned position on desktop to a centered position on mobile. This is achieved by giving the title a fixed width of 190px and a “margin:0 auto;” to center it. On desktop, the title’s parent container has a min-width of 190px applied, so the logo stays on the left. On mobile, the parent container grows full width, so the logo becomes centered.

另外,请注意标题如何从台式机上的左对齐位置切换到移动设备上的居中位置。 这是通过为标题设置固定宽度190px和“ margin:0 auto; 以使其居中。 在桌面上,标题的父容器的最小宽度为190px,因此徽标保留在左侧。 在移动设备上,父容器的宽度会增加,因此徽标会居中。

A great aspect of this technique is that, since everything is based on the grid’s parent width, an email can adapt even on a desktop webmail. For example, on Outlook.com, no matter if you chose to have the reading pane on the bottom or on the right, the email will correctly responds to the reading pane’s width. This would be impossible to do with media queries.

该技术的一个重要方面是,由于一切都基于网格的父级宽度,因此即使在桌面Web邮件上,电子邮件也可以适应。 例如,在Outlook.com上,无论选择在底部还是在右侧放置阅读窗格,电子邮件都将正确响应阅读窗格的宽度。 这对于媒体查询是不可能的。

翻译自: https://www.freecodecamp.org/news/the-fab-four-technique-to-create-responsive-emails-without-media-queries-baf11fdfa848/

响应式电子邮件



推荐阅读
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
    本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • position属性absolute与relative的区别和用法详解
    本文详细解读了CSS中的position属性absolute和relative的区别和用法。通过解释绝对定位和相对定位的含义,以及配合TOP、RIGHT、BOTTOM、LEFT进行定位的方式,说明了它们的特性和能够实现的效果。同时指出了在网页居中时使用Absolute可能会出错的原因,即以浏览器左上角为原始点进行定位,不会随着分辨率的变化而变化位置。最后总结了一些使用这两个属性的技巧。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • Gitlab接入公司内部单点登录的安装和配置教程
    本文介绍了如何将公司内部的Gitlab系统接入单点登录服务,并提供了安装和配置的详细教程。通过使用oauth2协议,将原有的各子系统的独立登录统一迁移至单点登录。文章包括Gitlab的安装环境、版本号、编辑配置文件的步骤,并解决了在迁移过程中可能遇到的问题。 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 本文介绍了解决java开源项目apache commons email简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apache commons email发送邮件。 ... [详细]
  • 从批量eml文件中提取附件的Python代码实现方法
    本文介绍了使用Python代码从批量eml文件中提取附件的实现方法,包括获取eml附件信息、递归文件夹下所有文件、创建目的文件夹等步骤。通过该方法可以方便地提取eml文件中的附件,并保存到指定的文件夹中。 ... [详细]
  • gitlab重置password
    ruby没怎么学,自己搭建的gitlab的rootpassword又忘了。幸好看见此帖子,试验okhttp:roland.kierkels.netgitreset-your-git ... [详细]
  • x86 linux的进程调度,x86体系结构下Linux2.6.26的进程调度和切换
    进程调度相关数据结构task_structtask_struct是进程在内核中对应的数据结构,它标识了进程的状态等各项信息。其中有一项thread_struct结构的 ... [详细]
author-avatar
丝兰郁香
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有