`

css样式教程 14、CSS网页布局Page Layout

阅读更多
使用CSS布局非常简单,如果你习惯使用tables布局,可能开始时有点困难,但其实很容易,事实上只是观念的不同。
  你需要把网页的每个部分看成独立的块,你可以绝对或相对定位块。

Positioning定位

  positon属性可以指定元素为absolute,relative,static或是fixed。
  static是元素默认属性,按HTML出现的先后顺序。
  relative比较像static,但元素可以使用top,right,bottom和left设定初始属性。
  absolute把元素从HTML里面拉出,一切由它自己决定,在这里,绝对定位元素可以使用top,right,bottom,left定位在任何地方。
  fixed行为像absolute,但它绝对定位的元素参照浏览器窗口与网页没有关系。所以,理论上,fixed元素可以固定在屏幕上当页面滚动时。为什么说是理论上的?因为IE7以下的浏览器不支持。

  使用绝对定位布局
  可以使用绝对定位创建传统的两列布局,如下:


程序代码 程序代码
<divid="navigation">
<ul>
<li><ahref="this.html">This</a></li>

<li><ahref="that.html">That</a></li>
<li><ahref="theOther.html">TheOther</a></li>
</ul>

</div>
<divid="content">
<h1>Rarabanjobanjo</h1>
<p>WelcometotheRarabanjobanjopage.Rarabanjobanjo.Rarabanjobanjo.Rarabanjobanjo.</p>
<p>(Rarabanjobanjo)</p>

</div>

  上面加上CSS:


程序代码 程序代码
#navigation{
position:absolute;
top:0;
left:0;

width:10em;
}
#content{
margin-left:10em;
}

  上面导航条设定在左边,宽度是10em。因为导航条是绝对定位,所以流动的页面上什么也不动,需要设定内容的左边margin等于导航条的宽度。

  真是简单。你没有限制这两列的距离,使用聪明的布局,你可以随心所欲安排许多块。如果你想添加第三列,比如:


程序代码 程序代码
#navigation{
position:absolute;
top:0;
left:0;
width:10em;
}
#navigation2{
position:absolute;
top:0;
right:0;
width:10em;
}
#content{
margin:010em;/*settingtopandbottommarginto0andrightandleftmarginto10em*/
}

  最后阶段去绝对定位元素,因为它们独立存在,无法正确了解它们哪里结束。如果你使用上面的例子,所有的页面有小的导航条和大的内容区域到可以,但是,特别是宽度和大小使用相对值,你必须经常放弃定位的希望,比如位于元素最下面的底部布局。如果你想定位底部,采用浮动模式比绝对定位好。

Floating浮动

  浮动元素可以在一条线上移动。
  浮动一般使用在定位页面里的小型元素,但也可使用在大块里,比如导航。
  上面的HTML例子使用下面CSS:


程序代码 程序代码
#navigation{
float:left;
width:10em;
}
#navigation2{
float:right;
width:10em;
}
#content{
margin:010em;
}

  如果你不希望接下来的元素包围浮动对象,可以使用clear属性。clear:left清除浮动在左边的元素,clear:right清除浮动在右边的元素,clear:both清除所有。如果你想添加底部footer,可以像下面:


程序代码 程序代码
#footer{
clear:both;
}

  footer将在所有列下面,不管它们的长度如何。
  这篇介绍了定位和浮动的基本情况,强调页面的大块,但记住,上面方法可以运用到块里面的任何元素。通过组合使用position,floating,margins,padding和border,你可以表现出任何网页设计,table布局能做的CSS没有什么不能做。
  使用表格布局的理由就是考虑古老的浏览器。CSS的优势在于拥有很高的可用性,而且体积上只有使用table布局的部分大小。
分享到:
评论

相关推荐

    css-pro-layout:CSS库,用于构建响应式和可自定义的布局

    CSS Pro布局 CSS库,用于构建响应式和可自定义的页面布局 文献资料 有关完整文档,请访问 入门 您可以使用NPM或Yarn安装CSS专业版 NPM npm install css-pro-layout 纱 yarn add css-pro-layout 将css-pro-layout....

    CSS40个布局实例

    关于CSS40个布局实列CSS布局大全:40多个教程,提示,例子和最佳实践 CSS布局教程 1-Three column fixed layout structure using CSS- 这篇文章解释了如何显示... 2-Design page layout using CSS- 如何设计页面布局教程。

    Grid Layout in CSS - 2016

    With the introduction of the CSS Grid Layout spec, you will not need to include a separate stylesheet just to use the grid system. Another advantage is that you will not have to rely on properties ...

    PageFlipLayout基于CSS网格的模板拥有杂志式布局和平面翻页动画

    Page Flip Layout - 基于CSS网格的模板,拥有杂志式布局和平面翻页动画

    CSS Quick Syntax Reference

    The CSS Quick Syntax Reference is a 150-page syntax reference to the Cascading Style Sheet specification and style sheet language. It presents the essentials of CSS in a well-organized format that can...

    CSS3 The Missing Manual - CSS3 秘籍 - 第3版

    CSS3 The Missing Manual - CSS3 秘籍 - 第3版 Cascading Style Sheets—CSS for short—give you creative control over the layout and design of your web pages. Using CSS, you can dress up your site’s text...

    CSS Cookbook, 3rd Edition

    Work with web typography and page layout Create effects for images and other page elements Learn techniques for configuring lists, forms, and tables Design effective web navigation and create custom ...

    Flexbox.in.CSS.2017.5.pdf

    Layout designers rejoice: now you can greatly simplify the task of laying out your web page or application with Flexbox, the CSS Flexible Box Module. In this concise guide, author Estelle Weyl shows ...

    CSS The Definitive Guide 4th Edition 2017年出版1088页

    If you’re a web designer or app developer interested in sophisticated page styling, improved accessibility, and saving time and effort, this book is for you. This revised edition provides a ...

    Beginning CSS3 (pdf)

    the language that in the hands of a skilled designer turns the ugly duckling of unadorned ... In one sense, they’re right. Instead of one massive speci cation, CSS3 has been broken up into more than 40 ...

    css-Layout-page2

    css-Layout-page2

    html 版 CSS中文速查手册

    heightmax-heightmin-heightwidthmax-widthmin-width 布局 Layout clearfloatclipoverflowoverflow-xoverflow-ydisplayvisibility 外补丁 Margins marginmargin-topmargin-rightmargin-bottommargin-left ...

    Apress.Pro.HTML5.and.CSS3.Design.Patterns.Nov.2011

    The book’s layout, with a pattern’s example on the left page and its explanation on the right, makes it easy to find a pattern and study it without having to flip between pages. The book is also ...

    CSS The Definitive Guide 4th Edition

    If you’re a web designer or app developer interested in sophisticated page styling, improved accessibility, and saving time and effort, this book is for you. This revised edition provides a ...

    CSS for Windows 8 App Development

    CSS is a powerful styling and layout language that greatly simplifies the selection of page elements and their visual display, layout, and animation. Additionally, Windows 8 apps appear on a large ...

    Beginning HTML5 and CSS3 For Dummies

    Step out in (CSS) style — use Cascading Style Sheets to control layout and enhance the look and feel of your pages Go mobile — ensure that your pages play well with mobile devices Open the book and ...

    HTML & CSS: The Good Parts

    Whether you handcraft individual pages or build templates, HTML & CSS: The Good Parts will help you get the most out of these tools in all aspects of web page design-from layout to typography and to ...

    wksp-module-1-htmlcss-3-css-page-layout

    1.3-CSS:页面布局-讲习班练习1至练习12 每个练习文件夹都包含一个带有练习参数的自述文件。练习13至练习18(可选) 以下链接中的练习将帮助您使用CSS选择器,CSS Grid和Flexbox进行更多练习。 当前,没有为这些练习...

Global site tag (gtag.js) - Google Analytics