学习要点:
1.搜索区
2.插入大图
3.搜索框
主讲教师:李炎恢
本章主要开始使用学习用 HTML5 和 CSS3 来构建 Web 页面,第一个项目采用 PC 端固定布局来实现。
一.搜索区
本节课,我们接着 header 头部往下,来设计一块搜索区。这个区域,可以是广告大图,也可以是用户注册,也可以是一个搜索条,都是一个大幅背景,内嵌一个表单。具体造型如下:
从表面上来分析,就是插入一张背景大图,然后居中一个搜索条。但是,我们要求最小在 1280 分辨率、最大在 1920 分辨率能保持最佳的观看效果。而对于超过 1920 分辨率还要保持大图的位置合理。
二.插入大图
首先,为了满足最小的 1280 分辨率,大图本身的宽度必须大于 1280。而主流分辨率一般小于 1920,所以图片宽度设置为 1920 即可满足几乎所有用户。注:超过 1920 分辨率,即 2k+以上的分辨率一般不适合浏览网页了,会眼瞎。
我们从网上搜索一张风景图,原图的分辨率为:1920 x 1200。我们截取了中间一段变成:1920 x 600。那么被插入的背景区块应该怎么设置长度呢?
//创建一个搜索区域
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="search"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>
//可以直接设置宽度为 1920 吗?
<span style="color: #800000;">#search </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 1920px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 600px</span>; }
如果使用 1920 的宽度,势必在底部产生滚动条,非常的难看。那不采用 1920 的宽度,整张大图无法全面显示。那么我们的设计理念是,1280 分辨率显示大图中部区域的图片内容,而浏览器不断增大,就显示的内容越多。超过 1920 分辨率,让图片居中,两边空白即可。
//使用 100%,并插入背景图片
<span style="color: #800000;">#search </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 100%</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 600px</span>;<span style="color: #ff0000;"> background</span>:<span style="color: #0000ff;"> url(../img/search.jpg)</span>; }
当我们故意缩小分辨率时,小于 1280 时,底部会出现滚动条。当我们拉动滚动条时,发现右侧出现的大量空白。这时由于之前采用了 100%自适应导致的,那我们强行设置这里虽然是 100%。但如果小于 1280 分辨率,就必须固定在 1280 即可。
//不能小于 1280 分辨率
<span style="color: #800000;">#header </span>{<span style="color: #ff0000;"> min-width</span>:<span style="color: #0000ff;"> 1263px</span>; }<span style="color: #800000;"> #search </span>{<span style="color: #ff0000;"> min-width</span>:<span style="color: #0000ff;"> 1263px</span>; }
对于大于 1920 的分辨率,我们将背景图片居中显示即可,两边留白。当然,还有一种方式,是专门设计这张大图的过渡渐变,两边快要接近纯色是,添加背景过渡。
//大于 1920 分辨率时
<span style="color: #800000;">#search </span>{<span style="color: #ff0000;"> background</span>:<span style="color: #0000ff;"> url(../img/search.jpg) no-repeat center</span>; }
三.搜索框
我们希望在大图中间安插一个搜索框,先安插一个半透明的区块。
//创建一个区块
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="search"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="center"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="search"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">="请输入旅游景点或城市"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">button </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="button"</span><span style="color: #0000ff;">></span>搜索<span style="color: #0000ff;"></span><span style="color: #800000;">button</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span></span>
//将区块半透明且居中
<span style="color: #800000;">#search .center </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 600px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 60px</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> -30px 0 0 -300px</span>;<span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> opacity</span>:<span style="color: #0000ff;"> 0.6</span>; }
//父元素设置相对点
<span style="color: #800000;">#search </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>; }
//搜索框和按钮样式
<span style="color: #800000;">#search .search </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 446px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 54px</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> -27px 0 0 -296px</span>;<span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid #666</span>;<span style="color: #ff0000;"> font-size</span>:<span style="color: #0000ff;"> 24px</span>;<span style="color: #ff0000;"> color</span>:<span style="color: #0000ff;"> #666</span>;<span style="color: #ff0000;"> outline</span>:<span style="color: #0000ff;"> none</span>;<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 0 10px</span>; }<span style="color: #800000;"> #search .button </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 120px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 54px</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> -27px 0 0 175px</span>;<span style="color: #ff0000;"> font-size</span>:<span style="color: #0000ff;"> 22px</span>;<span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> none</span>;<span style="color: #ff0000;"> color</span>:<span style="color: #0000ff;"> #666</span>;<span style="color: #ff0000;"> font-weight</span>:<span style="color: #0000ff;"> bold</span>;<span style="color: #ff0000;"> outline</span>:<span style="color: #0000ff;"> none</span>; }

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

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

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

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools