流體式布局與響應式布局_將固定像素設計轉換為流體比例布局

流體式布局與響應式布局

Responsive web design has been a prime necessity for every enterprise ever since Google announced that responsive, mobile-friendly websites will see a hike in their search engine rank in 2015.

自Google宣布響應式,移動友好型網站將在2015年提高搜索引擎排名以來,響應式網頁設計一直是每個企業的首要要求。

Fluid web designs, if implemented with precision and proper technique, can adjust themselves to be seen correctly on large screens, small screens, and even tiny PDA screens. Fluid grids enable you to position elements on your page and deploy them out in a visually appealing way, following a specific hierarchy. In this article, we will learn how to transform a rigid design into a fluid proportional layout effectively.

流暢的網頁設計,如果以精確和適當的技術實施,則可以調整自己,使其在大屏幕,小屏幕甚至微型PDA屏幕上都能正確顯示。 流體網格使您可以在頁面上放置元素,并按照特定的層次結構以視覺上吸引人的方式部署它們。 在本文中,我們將學習如何將剛性設計有效地轉換為流體比例布局。

This article is an excerpt from the book Responsive-Web-Design with HTML5 and CSS, Third Edition by Ben Frain, a completely updated version of one of the biggest best selling and most comprehensive books on the latest HTML5 and CSS tools and techniques for responsive web design.

本文摘自 Ben Frain 撰寫的《 使用HTML5和CSS進行響應式網絡設計》第三版 這是有關 響應式 HTML5和CSS工具和技術的最暢銷,最全面的書籍之一的完整更新版本。網頁設計。

Graphic composites, or “comps,” as they are often called, made in a program like Photoshop, Illustrator, or Sketch all have fixed pixel dimensions. At some point, the designs need to be converted to proportional dimensions when recreating the design as a fluid layout for the browser.

在諸如Photoshop,Illustrator或Sketch之類的程序中制作的通常稱為“圖形合成”或“合成”的像素尺寸都是固定的。 在某些時候,將設計重新創建為瀏覽器的流暢布局時,需要將設計轉換為比例尺寸。

There is a beautifully simple formula for making this conversion that the father of responsive web design, Ethan Marcotte, set down in his 2009 article, Fluid Grids (http://alistapart.com/article/FLUIDGRIDS):

響應式網頁設計之父Ethan Marcotte在其2009年的文章Fluid Grids( http://alistapart.com/article/FLUIDGRIDS )中設定了一個漂亮的簡單轉換公式:

target / context = result

目標/環境=結果

Put another way, divide the units of the thing you want by the thing it lives in. Let’s put that into practice. Understanding it will enable you to convert any fixed dimension layouts into responsive/fluid equivalents.

換句話說,將所需事物的單位除以其所生活的事物。讓我們將其付諸實踐。 了解它可以使您將任何固定尺寸的布局轉換為響應/流體等效項。

Consider a very basic page layout intended for desktop. In an ideal world, we would always be moving to a desktop layout from a smaller screen layout; however, for the sake of illustrating the proportions, we will look at the two situations back to front.

考慮用于桌面的非常基本的頁面布局。 在理想的世界中,我們總是會從較小的屏幕布局轉向桌面布局。 但是,為了說明比例,我們將重新審視這兩種情況。

Here’s an image of the layout:

這是布局的圖像:

Image for post
A basic “desktop” layout基本的“桌面”布局

The layout is 960px wide. Both the header and footer are the full widths of the layout. The left-hand area is 200px wide, and the right-hand area is 100px wide. That leaves 660px for the main content area. Our job is to convert this fixed-width design into a fluid layout that retains its proportions as it is resized. For our first task, we need to convert the middle and side sections into proportional dimensions.

布局為960px寬。 頁眉和頁腳均為布局的整個寬度。 左側區域為200px寬,右側區域為100px寬。 剩下的660px用于主要內容區域。 我們的工作是將這種固定寬度的設計轉換為流暢的布局,并在調整大小時保留其比例。 對于我們的第一個任務,我們需要將中間部分和側面部分轉換為比例尺寸。

We will begin by converting the left-hand side. The left-hand side is 200 units wide. This value is our target value. We will divide that target size by 960 units, our context, and we have a result: .208333333. Now, whenever we get our result with this formula, we need to shift the decimal point two points to the right. That gives us a value that is the target value described as a percentage of its parent. In this case, the left-hand section is 20.8333333% of its parent.

我們將從轉換左側開始。 左側為200個單位寬。 該值是我們的目標值。 我們將根據目標大小將該目標大小除以960個單位,結果為.208333333。 現在,只要使用此公式獲得結果,就需要將小數點向右移動兩點。 這給了我們一個值,該值是描述為其父級百分比的目標值。 在這種情況下,左側部分是其父級的20.8333333%。

Let’s practice the formula again in the middle section. Our target value is 660. Divide that by our context of 960 and we get .6875. Move the decimal two points to the right and we have 68.75%.

讓我們在中間部分再次練習公式。 我們的目標值為660。將其除以960的上下文,得出.6875。 將小數點后兩位移到右邊,我們有68.75%。

Finally, let’s look at the right-hand section. Our target is 100. We divide that by the context of 960 and we get .104166667. Move the decimal point and we have a value of 10.4166667%.

最后,讓我們看一下右側部分。 我們的目標是100。我們將其除以960,則得到.104166667。 移動小數點,我們得到一個值10.4166667%。

That’s as difficult as it gets. Say it with me: target, divided by context, equals result.

那樣困難。 跟我說:目標,除以上下文,等于結果。

You can use values with long decimal values with no issues in the CSS. Or, if you would rather see more palatable numbers in your code, rounding them to two decimal points will work just as well for the browser.

您可以使用帶有長十進制值的值,而CSS中沒有問題。 或者,如果您希望在代碼中看到更多可口的數字,則將它們四舍五入到小數點后兩位對瀏覽器同樣適用。

To prove the point, let’s quickly build that basic layout as blocks in the browser. To make it easier to follow along, I have added a class to the various elements that describe which piece of the “comp” they are referring to. It’s not a good idea to name things based on their location ordinarily. The location can change, especially with a responsive design. In short, do as I say and not as I do here! You can view the layout as example_04–01 in https://github.com/PacktPublishing/Responsive-Web-Design-with-HTML5-and-CSS-Third-Edition.

為了證明這一點,讓我們快速將基本布局構建為瀏覽器中的塊。 為了使后續操作更容易,我在各種元素中添加了一個類來描述它們所指的“ comp”部分。 通常,根據事物的位置來命名事物不是一個好主意。 位置可能會更改,尤其是采用響應式設計時。 簡而言之,按照我說的去做,不要像我在這里做的那樣! 您可以在https://github.com/PacktPublishing/Responsive-Web-Design-with-HTML5-and-CSS-Third-Edition中以example_04-01的形式查看布局。

Here’s the HTML:

這是HTML:

<div class=”Wrap”> 
<header class=”Header”></header>
<div class=”WrapMiddle”>
<aside class=”Left”></aside>
<main class=”Middle”></main>
<aside class=”Right”></aside>
</div>
<footer class=”Footer”></footer>
</div>

And here is the CSS:

這是CSS:

html, 
body {
margin: 0;
padding: 0;
}

.Wrap {
max-width: 1400px;
margin: 0 auto;
}

.Header {
width: 100%;
height: 130px;
background-color: #038c5a;
}

.WrapMiddle {
width: 100%;
font-size: 0;
}

.Left {
height: 625px;
width: 20.83%;
background-color: #03a66a;
display: inline-block;
}

.Middle {
height: 625px;
width: 68.75%;
background-color: #bbbf90;
display: inline-block;
}

.Right {
height: 625px;
width: 10.41%;
background-color: #03a66a;
display: inline-block;
}

.Footer {
height: 200px;
width: 100%;
background-color: #025059;
}

If you open the example code in a browser and resize the page, you will see the dimensions of the .Left, .Middle, and .Right sections remain proportional to one another. You can also play around with the max-width of the .Wrap values to make the bounding dimensions for the layout bigger or smaller (in the example, it’s set to 1400px).

如果在瀏覽器中打開示例代碼并調整頁面大小,您將看到.Left.Middle.Right部分的尺寸保持成比例。 您還可以使用.Wrap值的最大寬度來增大或減小布局的邊界尺寸(在示例中,其設置為1400px)。

Now, let’s consider how we would have the same content on a smaller screen that flexes to a point and then changes to the layout we have already seen. You can view the final code of this layout in example_04–02 in https://github.com/PacktPublishing/Responsive-Web-Design-with-HTML5-and-CSS-Third-Edition.

現在,讓我們考慮一下如何在較小的屏幕上具有相同的內容,該內容可以彎曲到一個點,然后更改為我們已經看到的布局。 您可以在https://github.com/PacktPublishing/Responsive-Web-Design-with-HTML5-and-CSS-Third-Edition的 example_04-02中查看此布局的最終??代碼。

The idea is that, for smaller screens, we will have a single “tube” of content. The left-hand area will only be viewable as an “off-canvas” area; typically, an area for a menu or similar, which sits off the viewable screen area and slides in when a menu button is clicked on. The main content sits below the header, then the right-hand section below that, and finally the footer area. In our example, we can expose the left-hand menu area by clicking anywhere on the header. Typically, when making this kind of design pattern for real, a menu button would be used to activate the side menu.

這個想法是,對于較小的屏幕,我們將只有一個“管”的內容。 左側區域只能作為“畫布外”區域查看; 通常是菜單區域或類似區域,它位于可見的屏幕區域之外,并在單擊菜單按鈕時滑入。 主要內容位于頁眉下方,然后是該頁眉下方的右側部分,最后是頁腳區域。 在我們的示例中,我們可以通過單擊標題上的任意位置來顯示左側菜單區域。 通常,當使這種設計模式成為現實時,將使用菜單按鈕來激活側面菜單。

As you would expect, when combining this with our newly mastered media query skills, we can adjust the viewport and the design just “responds” — effortlessly moving from one layout to another and stretching between the two. I’m not going to list out all of the CSS here, it’s all in example_04–02 in https://github.com/PacktPublishing/Responsive-Web-Design-with-HTML5-and-CSS-Third-Edition.

如您所料,將其與我們新掌握的媒體查詢技能結合使用時,我們可以調整視口和設計,從而“響應” —輕松地從一種布局移動到另一種布局,并在兩者之間進行擴展。 我不會在這里列出所有CSS,全部在https://github.com/PacktPublishing/Responsive-Web-Design-with-HTML5-and-CSS-Third-Edition中的example_04-02中。

However, here’s an example — the left-hand section:

但是,這是一個示例-左側部分:

.Left { 
height: 625px;
background-color: #03a66a;
display: inline-block;
position: absolute;
left: -200px;
width: 200px;
font-size: 0.9rem;
transition: transform 0.3s;
}
@media (min-width: 40rem) {
.Left {
width: 20.83%;
left: 0;
position: relative;
}
}

You can see that, up first, without a media query, is the small screen layout. Then, at larger screen sizes, the width becomes proportional, the positioning relative, and the left value is set to zero. We don’t need to rewrite properties such as height, display, or background-color as we aren’t changing them.

您可以看到,首先,無需進行媒體查詢,便是小屏幕布局。 然后,在較大的屏幕尺寸下,寬度變為比例,相對位置,并將左值設置為零。 我們不需要重寫高度,顯示或背景顏色之類的屬性,因為我們無需對其進行更改。

This is progress. We have combined two of the core responsive web design techniques we have covered; converting fixed dimensions to proportions and using media queries to target CSS rules relevant to the viewport size.

這是進步。 我們結合了我們涵蓋的兩種核心響應式Web設計技術; 將固定尺寸轉換為比例,并使用媒體查詢來定位與視口大小相關CSS規則。

In a real project, we should be making some provision if JavaScript isn’t available and we need to view the content of the menu.

在一個真實的項目中,如果JavaScript不可用,我們應該做一些準備,我們需要查看菜單的內容。

We have now covered the essentials of fluid design. To surmise, where needed, make the dimensions of elements proportional rather than fixed. This way, designs adapt to the size of their container. And you now have the simple target/context = result formula to make the necessary calculations.

現在,我們已經涵蓋了流體設計的要點。 要進行推測,請根據需要使元素的尺寸成比例而不是固定。 這樣,設計便可以適應其容器的大小。 現在,您有了簡單的target / context = result公式來進行必要的計算。

In this article, we looked at a general example that showed how fixed dimension layouts can be turned into responsive/fluid equivalents using media queries. For more details on responsive web designing using modern CSS3 features such as flexbox and grid, I recommend giving Responsive-Web-Design with HTML5 and CSS by Ben Frain a read.

在本文中,我們看了一個一般示例,該示例顯示了如何使用媒體查詢將固定尺寸的布局轉換為響應/流體等效項。 有關使用現代CSS3功能(例如flexbox和grid)的自適應Web設計的更多詳細信息,我建議閱讀Ben Frain撰寫的使用HTML5和CSS自適應Web設計

關于作者 (About the Author)

Ben Frain has been a web designer/developer since 1996. He is currently employed as a Senior Front-end Developer at Bet365. Before the web, he worked as an underrated (and modest) TV actor and technology journalist, having graduated from Salford University with a degree in Media and Performance.

Ben Frain自1996年以來一直是一名Web設計師/開發人員。他目前受聘為Bet365的高級前端開發人員。 在上網之前,他曾是索爾福德大學(Salford University)媒體與表演學位的一名被低估(適度)的電視演員和技術記者。

He has written four equally underrated (his opinion) screenplays and still harbors the (fading) belief he might sell one. Outside of work, he enjoys simple pleasures. Playing indoor football while his body and wife still allow it, and wrestling with his two sons. His other book, Sass and Compass for Designers is available now.

他已經寫了四部同樣被低估(他的觀點)的劇本,但仍然懷有(可能會)相信他可能會賣掉一部的想法。 在工作以外,他享受簡單的樂趣。 在他的身體和妻子仍然允許的情況下進行室內足球比賽,并與兩個兒子搏斗。 他的另一本書《設計師的Sass和Compass》現已上市。

翻譯自: https://medium.com/javarevisited/converting-a-fixed-pixel-design-to-a-fluid-proportional-layout-1c36f495ca1e

流體式布局與響應式布局

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275097.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275097.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275097.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

怎樣開發一個 Node.js 命令行工具包

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。源碼共讀活動很多都是讀…

redis完全攻略

安裝篇 聲明&#xff1a;以下環境均是在ubuntu下進行 wget http://redis.googlecode.com/files/redis-2.4.4.tar.gztar zxf redis-2.4.4.tar.gz 然后進入目錄后直接make就可以了、如果邇的系統是32位的那么執行 make 32bit 安裝完成后、執行一下make test看是否正常、如果出現“…

印刷報價系統源碼_皇家印刷術-設計系統案例研究

印刷報價系統源碼重點 (Top highlight)Typography. It’s complicated. With Product Design, it’s on every screen. Decisions for a type scale affect literally every aspect of a product. When you’re working with an existing product, defining typography can fee…

Python簡單試題3

1&#xff0c;水仙花數 水仙花數是指一個 3 位數&#xff0c;它的每個位上的數字的 3次冪之和等于它本身 &#xff08;例如&#xff1a;1^3 5^3 3^3 153&#xff09; 代碼如下&#xff1a; 方法一&#xff1a; for i in range(100,1000): # 進行for循環num ia num % 10 # …

React Hooks 完全使用指南

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。React HooksHook 是什么…

重新設計Videoland的登錄頁面— UX案例研究

In late October of 2019 me and our CRO lead Lucas, set up a project at Videoland to redesign our main landing page for prospect customers (if they already have a subscription, they will go to the actual streaming product).在2019年10月下旬&#xff0c;我和我…

【常見Web應用安全問題】---5、File Inclusion

Web應用程序的安全性問題依其存在的形勢劃分&#xff0c;種類繁多&#xff0c;這里不準備介紹所有的&#xff0c;只介紹常見的一些。 常見Web應用安全問題安全性問題的列表&#xff1a;   &#xff11;、跨站腳本攻擊(CSS or XSS, Cross Site Scripting)   &#xff12;、S…

全新的 Vue3 狀態管理工具:Pinia

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。Vue3 發布已經有一段時間…

JS中變量和函數的使用

一、變量的介紹 1、啥是變量&#xff1f; 變量的本質是一塊有名字的內存空間。變量由變量名和變量值構成。變量名指的是內存空間的別名&#xff0c;一般位于賦值運算符的左邊&#xff1b;而變量值指的是內存空間中的數據&#xff0c;一般位于賦值運算符的右邊。例如:var balanc…

Win32 API消息函數:GetMessagePos

Win32 API消息函數:GetMessagePos 函數功能&#xff1a;該函數返回表示屏幕坐標下光標位置的長整數值。此位置表示當上一消息由GetMessage取得時鼠標占用的點。 函數原型&#xff1a;DWORD GetMessagePos&#xff08;VOID&#xff09; 參數&#xff1a;無。 返回值&…

都快 2022 年了,這些 Github 使用技巧你都會了嗎?

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。最近經常有小伙伴問我如…

單線程+異步協程

一 . 線程池和進程池 可以適當的使用,在大量的IO情況下有更好的方法 import time from multiprocessing.dummy import Pool def request(url):print(正在下載->,url)time.sleep(2)print(下載完畢->,url) start time.time() urls [www.baidu.com,www.taobao.com,www.sou…

Repeater\DataList\GridView實現分頁,數據編輯與刪除

一、實現效果 1、GridView 2、DataList 3、Repeater 二、代碼 1、可以去Csdn資源下載&#xff0c;包含了Norwind中文示例數據庫噢&#xff01;&#xff08;放心下&#xff0c;不要資源分&#xff09; 下載地址&#xff1a;數據控件示例源碼Norwind中文數據庫 2、我的開發環境&a…

網站快速成型_我的老板對快速成型有什么期望?

網站快速成型Some of the top excuses I have gotten from clients when inviting them into a prototyping session are: “I am not a designer!” “I can’t draw!” “I have no creative background!”在邀請客戶參加原型制作會議時&#xff0c;我從客戶那里得到的一些主…

碎片化學前端,融入到積極上進的環境,我推薦~

眾所周知&#xff0c;關注公眾號可以了解學習掌握技術方向&#xff0c;學習優質好文&#xff0c;落實到自己項目中。還可以結交圈內好友&#xff0c;讓自己融入到積極上進的技術氛圍&#xff0c;促進自己的技術提升。話不多說&#xff0c;推薦這些優質前端公眾號前端之神 80w閱…

重學JavaScript深入理解系列(六)

JavaScript深入理解—-閉包(Closures) 概要 本文將介紹一個在JavaScript經常會拿來討論的話題 —— 閉包&#xff08;closure&#xff09;。閉包其實已經是個老生常談的話題了&#xff1b; 有大量文章都介紹過閉包的內容&#xff0c;盡管如此&#xff0c;這里還是要試著從理論角…

EXT.NET復雜布局(四)——系統首頁設計(上)

很久沒有發帖了&#xff0c;很是慚愧&#xff0c;因此給各位使用EXT.NET的朋友獻上一份禮物。 本篇主要講述頁面設計與效果&#xff0c;下篇將講述編碼并提供源碼下載。 系統首頁設計往往是個難點&#xff0c;因為往往要考慮以下因素&#xff1a; 重要通知系統功能菜單快捷操作…

figma設計_在Figma中使用隔片移交設計

figma設計I was quite surprised by how much the design community resonated with the concept of spacers since I published my 自從我發表論文以來&#xff0c;設計界對間隔件的概念產生了多少共鳴&#xff0c;我感到非常驚訝。 last story. It encouraged me to think m…

axios源碼中的10多個工具函數,值得一學~

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。本文來自讀者Ethan01投稿…

安裝jenkins時出現 No such plugin: cloudbees-folder的解決辦法

今天安裝了一下jenkins&#xff0c;在初始化安裝插件時出現“ No such plugin: cloudbees-folder”錯誤&#xff0c;根據網上的教程&#xff1a; 1、打開鏈接“http://ftp.icm.edu.pl/packages/jenkins/plugins/cloudbees-folder/”&#xff0c;在最下面找到并打開“latest”目…