css 计算属性的应用_如何使用一点CSS Grid魔术设计计算器应用

css 计算属性的应用

by Deepika Gunda

由Deepika Gunda

如何使用一点CSS Grid魔术设计计算器应用 (How to use a little CSS Grid magic to design a calculator app)

This article is a quick intro to CSS Grid. We will be making a calculator using it.

本文是CSS Grid的快速介绍。 我们将使用它来制作一个计算器。

This article is good for developers who have a basic understanding of CSS and for those who want to learn the newer tools CSS offers to style pages.

本文适合对CSS有基本了解的开发人员,以及想要学习CSS为样式页面提供的较新工具的开发人员。

I liked CSS Grid area templates from the very start! The examples all over the web can look complicated, but trust me, one attempt at using them and you will love them and use them in many of your projects.

我从一开始就喜欢CSS Grid区域模板! 网络上的示例看起来很复杂,但是请相信我,尝试使用它们,您会喜欢它们并在许多项目中使用它们。

This thought started after wanting to make something similar to the image below.

这个想法始于想要做出类似于下图的内容。

Note that the =, 0, and AC buttons are twice in size of regular buttons. At first I thought of using the table HTML element and colspan and rowspan to make this. But then I wondered if we could make it using CSS Flexbox. After failing to place the = and 0 and . in the same row, I began to realize the need for CSS Grid.

请注意,=,0和AC按钮的大小是常规按钮的两倍。 最初,我想到了使用表格HTML元素以及colspan和rowspan来实现这一点。 但是后来我想知道我们是否可以使用CSS Flexbox做到这一点。 未能放置=和0和之后。 在同一行中,我开始意识到对CSS Grid的需求。

The complete code for this is available here: CSS-Grid-Calculator.

此处提供了完整的代码: CSS-Grid-Calculator 。

什么是CSS网格? (What is CSS Grid?)

The CSS Grid Layout Module offers a grid-based layout system, with rows and columns, making it easier to design web pages without having to use floats and positioning. — from my fav w3schools.com

CSS网格布局模块提供了具有行和列的基于网格的布局系统,从而使设计网页变得更加容易,而不必使用浮动和定位。 —来自我的最爱w3schools.com

So I would assume the main layout and table-like content look like grids, so we can use CSS Grid to style them.

因此,我假定主布局和类似表格的内容看起来像网格,因此我们可以使用CSS Grid设置样式。

制作网格的第一件事 (First thing to make a grid)

We have to use the display:grid on any container which needs to be a grid. In case it is the whole main page of website, we can do it like this:

我们必须在需要为网格的任何容器上使用display:grid。 如果它是网站的整个主页,我们可以这样做:

<html>
<style> #main{       display:grid;     }
</style>
<body><div id="main"></div>
</body></html>

下一步是什么? (What’s next?)

Let’s say you want to make a website which has a navbar, right sidebar, left sidebar, and middle portion for content. Typically you would want the navbar and sidebars to have fixed width and height in terms of viewport percentages and the content portion to expand in whatever space is left.

假设您要制作一个包含导航栏,右侧,左侧和中间部分内容的网站。 通常,您会希望导航栏和侧边栏在视口百分比方面具有固定的宽度和高度,并且内容部分要在剩余的空间中扩展。

So how do we do that?

那么我们该怎么做呢?

<style>#main{ display:grid; grid-template-columns: 20vw auto 20vw; grid-template-rows: 15vh auto; grid-template-areas:"header header header"                     "leftSB content rightSB";
}#header{ grid-area:header;}#leftSB{  grid-area:leftSB;}#rightSB{  grid-area:rightSB;}#content{grid-area:content;}
</style><body> <div id="main">   <div id="header>header</div>   <div id="rightSB">right sidebar</div>   <div id="leftSB">left sidebar</div>   <div id="content">content</div> </div>
</body>

Here is what achieves the layout we needed.

这就是实现我们所需布局的方法。

详细说明 (Detailed explanation)

In the picture above, you can see that we just needed to create simple divs which hold our header, sidebars, and content and add it to the root.

在上面的图片中,您可以看到我们只需要创建简单的div,即可保存标题,侧边栏和内容并将其添加到根目录中。

In the style section, we have added “display:grid” for the main container.

在样式部分,我们为主容器添加了“ display:grid”。

Next, we can see that overall we need 2 rows and 3 columns. That is why we have added the line

接下来,我们可以看到总体上我们需要2行和3列。 这就是为什么我们添加了这一行

grid-template-columns: 20vw auto 20vw;

We are telling it that we need 3 columns, and that the first column should occupy 20% of the view’s width, that the next column is auto (that is all the space available to be taken by this column), and that the last column is again 20% of view’s width.

我们告诉它,我们需要3列,并且第一列应该占据视图宽度的20%,下一列是自动的(即该列可以使用的所有空间),最后一列还是视图宽度的20%。

grid-template-rows: 15vh auto;

We are here saying that we need two rows overall. The first row will be used for a header and it will be 15% of viewport height, and the remaining space is needed for the second row.

我们在这里说,我们总共需要两行。 第一行将用作标题,它将是视口高度的15%,第二行需要剩余空间。

Now comes grid-template-areas. Here we define in simple names what will occupy the grid. Let’s say we want the header to take the whole first row and there should be no divisions. Then we use the following:

现在是网格模板区域。 在这里,我们以简单的名称定义将占用网格的内容。 假设我们希望标题占据整个第一行,并且不应有任何分隔。 然后我们使用以下内容:

grid-template-areas:"header header header"

Because we have 3 columns, we need to mention header 3 times. By using the same name, the outcome will be a unified header region.

因为我们有3列,所以我们需要提到标题3次。 通过使用相同的名称,结果将是统一的标头区域。

grid-template-areas:"header header header"                     "leftSB content rightSB";

This is the complete grid-template-areas, which uses simple names to define each portion of our 2 * 3 grid.

这是完整的网格模板区域,它使用简单的名称来定义2 * 3网格的每个部分。

Now that we have defined the grid template, we are going to assign the divs to these areas. So, we have used the IDs of the div and assigned the template area name using grid-area.

现在我们已经定义了网格模板,我们将把div分配给这些区域。 因此,我们使用了div的ID,并使用grid-area分配了模板区域名称。

#header{ grid-area:header;}#leftSB{  grid-area:leftSB;}

Thats it, we have now defined how these divs should be positioned on all viewport sizes without using floats or widths on individual items and also without using bootstrap etc.

就是这样,我们现在定义了这些div应该如何在所有视口尺寸上定位,而不在单个项目上使用浮点数或宽度,也不在使用引导程序等。

Isn’t it mind-blowing? See what we created in these few lines in action here.

是令人难以置信吗? 在这里查看我们在这几行中创建的内容。

下一步是什么? (What’s next?)

We can now work on our divs to add sidebars, navbars etc. I will leave this example here and now proceed to see a little complicated calculator design using CSS Grid.

现在,我们可以在div上添加侧边栏,导航栏等。我将在此处保留此示例,然后继续使用CSS Grid来查看一些复杂的计算器设计。

定义组件 (Defining the components)

We have a formula display section, the current display section at the top. The rest are the buttons 0–9, the AC (clear) button, and the operator buttons + — * / and =.

我们有一个公式显示部分,当前显示部分在顶部。 其余的是按钮0–9,AC(清除)按钮以及操作员按钮+ — * /和=。

So, let’s create buttons and divs for all the components and keep them in a container.

因此,让我们为所有组件创建按钮和div,并将其保存在容器中。

<body> <div id="root">  <label id="display"> 0</label>  <label id="cdisplay" >0</label>  <button id="clear">AC </button>  <button id="divide">/</button>  <button id="multiply">*</button>  <button id="seven">7</button>  <button id="eight">8</button>  <button id="nine">9</button>  <button id="minus">-</button>  <button id="four">4</button>  <button id="five">5</button>  <button id="six">6</button>  <button id="plus">+</button>  <button id="one">1</button>  <button id="two">2</button>  <button id="three">3</button>  <button id="zero">0</button>  <button id="dot">.</button>  <button id="equal">=</button>   </div> </body>

Let’s look at the calculator image. You can see that we have 4 columns and 7 rows. So let’s define our grid:

让我们看一下计算器图像。 您可以看到我们有4列和7行。 因此,让我们定义网格:

#root{ padding:5px; background-color:black; width:240px; height:280px;
display:grid;   grid-template-columns: 1fr 1fr 1fr 1fr;   grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;   grid-gap:0.1px;
grid-template-areas:    "display display display display"   "cdisplay cdisplay cdisplay cdisplay"   "clear clear divide multiply"   "seven eight nine minus"   "four five six plus"   "one two three equal"   "zero zero dot equal"; }

Breaking this down…

分解这个……

display:grid;   grid-template-columns: 1fr 1fr 1fr 1fr;   grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;

Here we have said that we have 4 columns and 7 rows and each part of the grid is the same size. Note that the template columns and rows use 1fr. Fr is a fractional unit and 1fr is for 1 part of the available space.

在这里我们已经说过,我们有4列和7行,并且网格的每个部分都具有相同的大小。 请注意,模板的列和行使用1fr。 Fr是小数单位, 1fr是可用空间的一部分。

I have given the root div a width of 240 px and height of 280 px. So 1 fr is approximately 60px wide * 40 px high.

我给根div的宽度为240像素,高度为280像素。 所以1 fr大约是60像素宽* 40像素高。

grid-template-areas:    "display display display display"   "cdisplay cdisplay cdisplay cdisplay"   "clear clear divide multiply"   "seven eight nine minus"   "four five six plus"   "one two three equal"   "zero zero dot equal"; }

Here we have defined the grid template areas.

在这里,我们定义了网格模板区域。

Grid template areas are a set of row * column strings. You need to add as many strings as there are rows in your grid. In each row-string you need to mention what each column will contain. The number of items in each string should match the count of columns.

网格模板区域是一组行*列字符串。 您需要添加的字符串与网格中的行数一样多。 在每个行字符串中,您需要提及每列将包含的内容。 每个字符串中的项目数应与列数匹配。

Note how the display occupies the whole first row. So, it is added 4 times in first row-string.

请注意显示内容如何占据整个第一行。 因此,它在第一个行字符串中添加了4次。

cdisplay, that is current display, occupies the second row and is defined similar to display.

cdisplay(即当前显示)占据第二行,其定义类似于display。

Next come the buttons. The clear button is in 3rd row and first and second column put together. Hence it is mentioned twice on row-string 3.

接下来是按钮。 清除按钮位于第三行,第一和第二列放在一起。 因此,它在行字符串3上被两次提及。

And it goes on…

然后继续……

Now that the major work is over, we need to assign these grid areas to the divs.

现在主要工作已经结束,我们需要将这些网格区域分配给div。

#display{   grid-area:display; }#cdisplay{   grid-area:cdisplay; }#clear {   grid-area:clear; }#divide {   grid-area:divide; } #multiply {   grid-area:multiply; }

I have shown how the grid areas are being assigned for 4 div’s.

我已经展示了如何为4格分配网格区域。

The complete example can be found here where we have added a little more styling.

完整的示例可以在此处找到,我们在其中添加了更多样式。

结语 (Wrapping up)

As mentioned earlier, this is just an introduction to CSS Grid and more specifically to CSS Grid template areas. I hope this example will make you think of CSS Grid when you look at websites from now on and I hope you will use them in the future.

如前所述,这只是CSS Grid的简介,更具体地说是CSS Grid模板区域的简介。 我希望这个例子能使您从现在开始浏览网站时想到CSS Grid,并希望将来会使用它们。

If you liked my article, please clap. It is quite encouraging for me.

如果您喜欢我的文章,请鼓掌。 这对我来说是令人鼓舞的。

If you were to do the same task, how would you approach this? Let me know in the comments.

如果您要执行相同的任务,您将如何处理? 在评论中让我知道。

翻译自: https://www.freecodecamp.org/news/how-to-use-a-little-css-grid-magic-to-design-a-calculator-app-e162afb2fdb4/

css 计算属性的应用

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

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

相关文章

vc调试大全

一、调试基础 调试快捷键 F5&#xff1a; 开始调试 ShiftF5: 停止调试 F10&#xff1a; 调试到下一句&#xff0c;这里是单步跟踪 F11&#xff1a; 调试到下一句&#xff0c;跟进函数内部 ShiftF11: 从当前函数中跳出 CtrlF10: 调试到光标所在位置 F9&#xff1a; …

Google-Guava-EventBus源码解读

Guava是Google开源的一个Java基础类库&#xff0c;它在Google内部被广泛使用。Guava提供了很多功能模块比如&#xff1a;集合、并发库、缓存等&#xff0c;EventBus是其中的一个module&#xff0c;本篇结合EventBus源码来谈谈它的设计与实现。 概要 首先&#xff0c;我们先来预…

leetcode 1370. 上升下降字符串

给你一个字符串 s &#xff0c;请你根据下面的算法重新构造字符串&#xff1a; 从 s 中选出 最小 的字符&#xff0c;将它 接在 结果字符串的后面。 从 s 剩余字符中选出 最小 的字符&#xff0c;且该字符比上一个添加的字符大&#xff0c;将它 接在 结果字符串后面。 重复步骤…

mysql 设置事物自动提交_mysql事务自动提交的问题

1&#xff1a;mysql的aut0commit配置默认是开启的&#xff0c;也就是没执行一条sql都会提交一次&#xff0c;就算显示的开启事务也会导致多条SQL不在一个事务中&#xff0c;如果需要相关的SQL在同一个事务中执行&#xff0c;那么必须将autocommit设置为OFF&#xff0c;再显式开…

rest laravel_如何通过测试驱动开发来构建Laravel REST API

rest laravelby Kofo Okesola由Kofo Okesola 如何通过测试驱动开发来构建Laravel REST API (How to build a Laravel REST API with Test-Driven Development) There is a famous quote by James Grenning, one of the pioneers in TDD and Agile development methodologies:T…

python之numpy

numpy是一个多维的数组对象&#xff0c;类似python的列表&#xff0c;但是数组对象的每个元素之间由空格隔开。 一、数组的创建 1.通过numpy的array(参数)&#xff0c;参数可以是列表、元组、数组、生成器等 由arr2和arr3看出&#xff0c;对于多维数组来说&#xff0c;如果最里…

git 上传

转载于:https://www.cnblogs.com/benbentu/p/6543154.html

Liferay 部署war包时候的deployDirectory 细节分析

引入&#xff1a; 在上文中&#xff0c;我们从宏观上讲解了Liferay部署war包的动作是如何触发监听器并且完成部署过程的&#xff0c;但是其中最核心的一块deployDirectory我们没讲&#xff0c;它的作用是当有了临时目录并且已经把war包的内容展开到该目录之后&#xff0c;是如何…

leetcode 164. 最大间距(桶排序)

给定一个无序的数组&#xff0c;找出数组在排序之后&#xff0c;相邻元素之间最大的差值。 如果数组元素个数小于 2&#xff0c;则返回 0。 示例 1: 输入: [3,6,9,1] 输出: 3 解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。 示例 2: …

批处理定时mysql备份数据库_定时备份mysql数据库的批处理

定时备份mysql数据库的批处理代码&#xff0c;保存为backup_mysql.bat&#xff0c;运行即可。复制代码 代码如下:echo offset txt1%date:~0,4%::当前年set txt2%date:~5,2%::当前月set txt3%date:~8,2%::当前日set txt4%time:~0,2%::当前小时set txt5%time:~3,2%::当前分钟set …

算法训练营 重编码_您在编码训练营期间可能面临的最大挑战

算法训练营 重编码by Joanna Gaudyn乔安娜高登(Joanna Gaudyn) 您在编码训练营期间可能面临的最大挑战 (The biggest struggles you might face during a coding bootcamp) You think that during a coding bootcamp nothing can be more challenging than learning programmi…

1449 砝码称重(思维)

题目链接&#xff1a;https://www.51nod.com/onlineJudge/submitDetail.html#!judgeId259281 题解&#xff1a;这题有一个技巧&#xff0c;毕竟是w^0,w^1,w^2....这样&#xff0c;必然会想到w进制&#xff0c;而且就只能用一次。 那么就简单了&#xff0c;把m拆成w进制&#xf…

leetcode 454. 四数相加 II(哈希表)

给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) &#xff0c;使得 A[i] B[j] C[k] D[l] 0。 为了使问题简单化&#xff0c;所有的 A, B, C, D 具有相同的长度 N&#xff0c;且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间&#xf…

“换标”Intel的穷则思变

成语有云“穷则思变”&#xff0c;用这个词来形容早先的Intel换标也最恰当不过。当然这里“穷”&#xff0c;不是说Intel很贫穷&#xff0c;而是说Intel在自己的产业到了尽头。Intel推产品概念的水平是一流的&#xff0c;虽然某些概念事后被认为是错误的&#xff08;如&#xf…

mysql开发中遇到的坑_mysql优化过程中遇见的坑(mysql优化问题特别注意)

单条查询最后添加 LIMIT 1&#xff0c;停止全表扫描。对于char(4) 或者vachar(4)&#xff0c;无论是中文还是英文都是存储四个字符&#xff0c;注意是字符而不是字节。如果一个字段未int类型&#xff0c;此类型只有0、1两个状态&#xff0c;需要为此建立索引吗&#xff1f;过度…

初级开发人员的缺点_在您作为初级开发人员的第一年获得此建议

初级开发人员的缺点Are you a junior developer embarking on your software development career?您是从事软件开发事业的初级开发人员吗&#xff1f; Or a recent computer science graduate who has recently started a new job?还是最近刚开始从事新工作的计算机科学专业…

Spark日志分析

根据tomcat日志计算url访问了情况&#xff0c;具体的url如下&#xff0c; 要求&#xff1a;区别统计GET和POST URL访问量 结果为&#xff1a;访问方式、URL、访问量 输入文件&#xff1a; 196.168.2.1 - - [03/Jul/2014:23:36:38 0800] "GET /course/detail/3.htm HTTP/1.…

进程、线程和协程的区别

首先&#xff0c;给出“进程、线程和协程”的特点&#xff1a; 进程&#xff1a;拥有自己独立的堆和栈&#xff0c;既不共享堆&#xff0c;也不共享栈&#xff0c;进程由操作系统调度&#xff1b;线程&#xff1a;拥有自己独立的栈和共享的堆&#xff0c;共享堆&#xff0c;不共…

leetcode 493. 翻转对(分治算法)

给定一个数组 nums &#xff0c;如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对。 你需要返回给定数组中的重要翻转对的数量。 示例 1: 输入: [1,3,2,3,1] 输出: 2 代码 class Solution {public int reversePairs(int[] nums) {return getR…

使用brew安装软件

brew 又叫Homebrew&#xff0c;是Mac OSX上的软件包管理工具&#xff0c;能在Mac中方便的安装软件或者卸载软件&#xff0c; 只需要一个命令&#xff0c; 非常方便 brew类似ubuntu系统下的apt-get的功能 阅读目录 安装brew 使用brew安装软件 使用brew卸载软件 使用brew查询软…