十步教会你css怎样布局 (英文)

css

不给大家翻译了!懂英文的应该能看懂!

This tutorial examines the different layout properties available in CSS: position:static, position:relative, position:absolute, and float.

1. position:static

The default positioning for all elements is position:static, which means the element is not positioned and occurs where it normally would in the document.

Normally you wouldn't specify this unless you needed to override a positioning that had been previously set.

#div-1 { position:static;}

2. position:relative

If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document.

Let's move div-1 down 20 pixels, and to the left 40 pixels:

#div-1 { position:relative; top:20px; left:-40px;}

Notice the space where div-1 normally would have been if we had not moved it: now it is an empty space. The next element (div-after) did not move when we moved div-1. That's because div-1 still occupies that original space in the document, even though we have moved it.

It appears that position:relative is not very useful, but it will perform an important task later in this tutorial.

3. position:absolute

When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go.

Let's move div-1a to the top right of the page:

#div-1a { position:absolute; top:0; right:0; width:200px;}

Notice that this time, since div-1a was removed from the document, the other elements on the page were positioned differently: div-1b, div-1c, and div-after moved up since div-1a was no longer there.

Also notice that div-1a was positioned in the top right corner of the page. It's nice to be able to position things directly the page, but it's of limited value.

What I really want is to position div-1a relative to div-1. And that's where relative position comes back into play.

Footnotes

  • There is a bug in the Windows IE browser: if you specify a relative width (like "width:50%") then the width will be based on the parent element instead of on the positioning element.

4. position:relative + position:absolute

If we set relative positioning on div-1, any elements within div-1 will be positioned relative to div-1. Then if we set absolute positioning on div-1a, we can move it to the top right of div-1:

#div-1 { position:relative;}#div-1a { position:absolute; top:0; right:0; width:200px;}

5. two column absolute

Now we can make a two-column layout using relative and absolute positioning!

#div-1 { position:relative;}#div-1a { position:absolute; top:0; right:0; width:200px;}#div-1b { position:absolute; top:0; left:0; width:200px;}

One advantage to using absolute positioning is that we can position the elements in any order on the page, regardless of the order they appear in the HTML. So I put div-1b before div-1a.

But wait - what happened to the other elements? They are being obscured by the absolutely positioned elements. What can we do about that?

6. two column absolute height

One solution is to set a fixed height on the elements.

But that is not a viable solution for most designs, because we usually do not know how much text will be in the elements, or the exact font sizes that will be used.

#div-1 { position:relative; height:250px;}#div-1a { position:absolute; top:0; right:0; width:200px;}#div-1b { position:absolute; top:0; left:0; width:200px;}

7. float

For variable height columns, absolute positioning does not work, so let's come up with another solution.

We can "float" an element to push it as far as possible to the right or to the left, and allow text to wrap around it. This is typically used for images, but we will use it for more complex layout tasks (because it's the only tool we have).

#div-1a { float:left; width:200px;}

8. float columns

If we float one column to the left, then also float the second column to the left, they will push up against each other.

#div-1a { float:left; width:150px;}#div-1b { float:left; width:150px;}

9. float columns with clear

Then after the floating elements we can "clear" the floats to push down the rest of the content.

#div-1a { float:left; width:190px;}#div-1b { float:left; width:190px;}#div-1c { clear:both;}

10. Disclaimer & Resources

These examples are extremely simplified and do not trigger some of the CSS bugs in the Windows IE browser (of which there are many).

The following page was invaluable:
Relatively Absolute

While you're here check out the following:

  • BarelyFitz Designs Open-Source Software Projects
  • BarelyFitz Designs Screencasts

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索div
, position
, width
, absolute
, it top
, The
relative
十步提升英文阅读能力、div css布局入门教程、css布局、css flex布局、div css 布局教程,以便于您获取更多的相关知识。

时间: 2024-09-27 17:40:39

十步教会你css怎样布局 (英文)的相关文章

DIV CSS网页布局实例:十步学会用CSS建站

css|网页 Update:本篇已得到原作者Steve Dennis的翻译准予,在此Jorux表示感谢! 本教程主要参考Creating a CSS Layout from scratch,由Jorux翻译,以意译为主,其间加入了不少Jorux的个人观点,省略了一些多余的说明,请读者明鉴. 目录: 第一步:规划网站,本教程将以图示为例构建网站: 第二步:创建html模板及文件目录等: 第三步:将网站分为五个div,网页基本布局的基础: 第四步:网页布局与div浮动等: 第五步:网页主要框架之外的

学习CSS教程:学习CSS网页布局

文章简介:你也许知道什么叫选择器,什么叫属性,什么叫数值,也许你对css布局略懂一二,但这还远远不够.如果你想着从头开始学习HTML和CSS的话,我建议你认真查看下 this tutorial. 否则,在工作的时候,你依然陷入迷惘的泥潭中苦苦挣扎. 这个篇文章介绍的是现在广泛使用于网站布局领域的CSS基础. 你也许知道什么叫选择器,什么叫属性,什么叫数值,也许你对css布局略懂一二,但这还远远不够.如果你想着从头开始学习HTML和CSS的话,我建议你认真查看下 this tutorial. 否则

CSS网页制作技巧:DIV+CSS网页布局常犯错误汇总

文章简介:随着CSS网页布局越来越普及,国内大部分网站已经采用CSS网页布局的制作方法.在应用DIV+CSS编码时很容易犯一些错误,这里列举一些常见的错误,帮助新手朋友更好的学习与进步. 随着CSS网页布局越来越普及,国内大部分网站已经采用CSS网页布局的制作方法.在应用DIV+CSS编码时很容易犯一些错误,这里列举一些常见的错误,帮助新手朋友更好的学习与进步. 一.检查HTML元素是否有拼写错误 是否忘记结束标记 即使是老手也经常会弄错div的嵌套关系.可以用dreamweaver的验证功能检

Div+CSS网页布局中CSS无效的十个常见原因

核心提示:我们学习Div+CSS网页布局的知识,可是W3C validation有时难以操作,但用它你可以查看由版面设计引起的差错.验证程序抛出大量差错和警告,说明你的XHTML尚未完善,可能无法在不同浏览器上保持一致功能. 我们学习Div+CSS网页布局的知识,可是W3C validation有时难以操作,但用它你可以查看由版面设计引起的差错.验证程序抛出大量差错和警告,说明你的XHTML尚未完善,可能无法在不同浏览器上保持一致功能.下面十个细微的失效问题难住了大批程序员,本文就告诉你如何解决

CSS网页布局学习过程中选择什么软件来辅助?

css|过程|网页 CSS网页布局开发应该使用什么软件呢?这是一个非常常见的问题.往往会令新手朋友感觉迷茫,今天就这个问题我们来说一说吧.如果你有什么想法可以参与评论,大家集思广议吧. 有很多朋友是用DreamWeaver进行开发,DreamWeaver是三剑客之一,在传统的table布局中,可视化的环境让我们编辑网页非常轻松,各项功能面板进行设置即可达到想要的效果.虽然简化了操作,方便了我们的制作,但我们丢掉了很多东西,以至于有朋友从业几年了,却完全看不懂HTML代码,看不明白那些标签的真正含

学习css网页布局口诀

用CSS设计布局时遇到BUG,请认真阅读以下内容,非常容易记忆的! 一.IE边框若显若无,须注意,定是高度设置已忘记; 二.浮动产生有缘故,若要父层包含住,紧跟浮动要清除,容器自然显其中; 三.三像素文本慢移不必慌,高度设置帮你忙; 四.兼容各个浏览须注意,默认设置行高可能是杀手; 五.独立清除浮动须铭记,行高设无,高设零,设计效果兼浏览; 六.学布局须思路,路随布局原理自然直,轻松驾驭html,流水布局少hack,代码清爽,兼容好,友好引擎喜欢迎. 七.所有标签皆有源,只是默认各不同,span

十步搭建 OpenVPN,享受你的隐私生活

十步搭建 OpenVPN,享受你的隐私生活 我们支持保护隐私,不为我们有自己的秘密需要保护,只是我们认为保护隐私应该成为一项基本人权.所以我们坚信无论谁在什么时候行使这项权利,都应该不受拘束的获取必须的工具和服务.OpenVPN就是这样一种服务并且有多种工具(客户端) 来让我们利用并享受这种服务. 通过与一个OpenVPN服务器建立连接,我们基本上在我们的设备和远端运行OpenVPN的主机之间建立了一个安全的通信通道.尽管在两个端点之间的通信可能被截获,但是信息是经过高强度加密的所以实际上它对于

谈谈一些有趣的CSS题目(十五)-- 谈谈 CSS 关键字 initial、inherit 和 unset

开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉到生僻的 CSS 属性,赶紧去补习一下吧. 不断更新,不断更新,不断更新,重要的事情说三遍. 谈谈一些有趣的CSS题目(一)-- 左边竖条的实现方法 谈谈一些有趣的CSS题目(二)-- 从条纹边框的实现谈盒子模型 谈谈一些有趣的CSS题目(三)-- 层叠顺序与堆栈上下文知多少 谈谈一些有趣

css页面布局vertical-align:middle;和float:

问题描述 css页面布局vertical-align:middle;和float: <div class="row"> <div class="cell regist regist_show"> <b:message key="validatecode" /> </div> <div class="cell regist" style="float:left;&q