search
HomeWeb Front-endHTML Tutorialdiv css layout web page_html/css_WEB-ITnose

div css布局网页(全过程)

一、创建html模板及文件目录等

1.创建html模板。
  代码如下:

br />"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



CompanyName - PageName











  将其保存为index.html,并创建文件夹css,images

 


2.创建网站的大框:
  建立一个宽760px的盒子,它将包含网站的所有元素。
  在html文件的

和之间写入


Hello world.


  创建css文件,命名为master.css,保存在/css/文件夹下。写入:

#page-container {
width: 760px;
background: red;
}


  控制html的id为page-container的盒子的宽为760px,背景为红色。

  现在为了让盒子居中,写入margin: auto;,使css文件为:

#page-container {
width: 760px;
margin: auto;
background: red;
}
  现在你可以看到盒子和浏览器的顶端有8px宽的空隙。这是由于浏览器的默认的填充和边界造成的。消除这个空隙,就需要在css文件中写入:

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

二、将网站分为五个div,网页基本布局的基础:

1.将“第一步”提到的五个部分都放入盒子中,在html文件中写入:





Content



2.为了将五个部分区分开来,我们将这五个部分用不同的背景颜色标示出来,在css文件写入:

#main-nav {
background: red;
height: 50px;
}
#header {
background: blue;
height: 150px;
}
#sidebar-a {
background: darkgreen;
}
#content {
background: green;
}
#footer {
background: orange;
height: 66px;
}

 

 

三、网页布局与div浮动等

1.浮动:首先让边框浮动到主要内容的右边。用css控制浮动。

#sidebar-a {
float: right;
width: 280px;
background: darkgreen;
}

  

2.往主要内容的盒子中写入一些文字。在html文件中写入:


Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam gravida enim ut risus.
Praesent sapien purus, ultrices a, varius ac, suscipit ut, enim. Maecenas in lectus.
Donec in sapien in nibh rutrum gravida. Sed ut mauris. Fusce malesuada enim vitae lacus
euismod vulputate. Nullam rhoncus mauris ac metus. Maecenas vulputate aliquam odio.
Duis scelerisque justo a pede. Nam augue lorem, semper at, porta eget, placerat eget,
purus. Suspendisse mattis nunc vestibulum ligula. In hac habitasse platea dictumst.

 

  但是你可以看到主要内容的盒子占据了整个page-container的宽度,我们需要将#content的右边界设为280px。以使其不和边框发生冲突。
  css代码如下:

#content {
margin-right: 280px;
background: green;
}


  同时往边框里写入一些文字。在html文件中写入:

 

 

这也不是我们想要的,网站的底框跑到边框的下边去了。这是由于我们将边框向右浮动,由于是浮动,所以可以理解为它位于整个盒子之上的另一层。因此,底框和内容盒子对齐了。
  因此我们往css中写入:

#footer {
clear: both;
background: orange;
height: 66px;
}

 

四、网页主要框架之外的附加结构的布局与表现

  除网页主要框架之外的附加结构的表现(Layout),包括以下内容:
  1.主导航条;
  2.标题(heading),包括网站名和内容标题;
  3.内容;
  4.页脚信息,包括版权,认证,副导航条(可选)。

  加入这些结构时,为了不破坏原有框架,我们需要在css文件"body"标签(TAG)下加入:

.hidden {
display: none;
}


  ".hidden"即我们加入的类(class),这个类可以使页面上任意属于hidden类的元素(element)不显示。这些会在稍后使用,现在请暂时忘记它。

现在我们加入标题(heading):
  先回到HTML的代码,

是我们常用的html标题代码。比如我们一般用

网站名

网站副标题

,

内容主标题

等。我们往html文件的Header层(Div)加入:


  刷新一下页面,你就可以看到巨大的标题,和标题周围的空白,这是因为

>标签的默认大小和边距(margin)造成的,先要消除这些空白,需要加入:

h1 {
margin: 0;
padding: 0;
}


接下来是导航条:
  控制导航条表现的css代码相对比较复杂,我们将在第九步或是第十步中详细介绍。现在html文件加入导航代码:


(Note: The original tutorial used dl and dt, jorux used the more commonly used ul and li tags here)
The current performance of the navigation bar is relatively poor, but it will be improved in the future Its special performance is introduced in the tutorial, so the navigation bar needs to be temporarily hidden, so add:

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft