用实例详细讲解将PSD转成HTML&CSS

本教程通过一个实例,来详细讲解如何将psd转为html/css,以及js插件的使用。

将PSD转成一个静态的html/css对一些朋友来说是一个很大的困扰。因此本教程通过对一个完整实例的详细讲解来教大家如何将psd转成html/css,转换后的html/css可兼容各种主流浏览器。

我们将设计页面分成基本的5个部分,每一个部分都有自己的容器wrapper和内容。基本的流程是先编写Html代码,然后通过编写CSS来还原psd设计稿。

点击下载PSD文件

1.我们需要从PSD文件得到什么?

如下图,我们只需要从这个psd文件得到4个非常基本的东西。推荐内容(featured)的背景、底部背景、欢迎文字(Welcome)、推荐图片的框(少女面部图的白色透明边框)。其余的部分我们只需要通过CSS生成或者在html里嵌入相应图片。

下图红线圈起来的地方,就是我们需要从psd获取的内容。本部分内容需要读者具备一定的Photoshop的基础知识和操作能力。

你可以借助Photoshop的切片工具或者自己手动切片,并将其保存成相应的图片格式。

psd转html/css教程


2.设置站点

为一个网站设置良好的开发环境是非常重要的,我已经创建了一个非常基本的文件夹结构来建立这个网站,下面是文件夹结构设置,也可称之为模板。

文件夹结构

下面这个文件夹结构是由html文件、CSS文件、js等文件组成,你可以根据自己的需要调整文件夹结构。

psd转html/css教程

根目录
  • 这是放置index首页文件的地方
  • 这是网站的顶级目录
JavaScript 文件夹
  • 这是放置JS文件或者js框架jquery的地方
  • 我们在这个教程用到的jquery插件Nivo-Slider(做推荐内容)也放在这个文件夹里
样式(Styles )文件夹
  • 这里是存放css文件的地方,网页所要用到的图片放在另一个独立的文件夹,图片文件夹还包括两个子文件夹:
  • Images: 这是我们存放推荐内容部分的图片以及网站展示的图片的地方
  • Template: 这是存放和网页设计样式相关图片的地方,比如说底部背景图
具体文件的放置

将index.html文件放置在根目录,这个文件也是我们编写html代码的文件。
将jquery插件Nivo-Slider里面的三个文件jQuery-1.4.2.min.js和HTML5-Shiv.js以及Nivo-Slider.min.js放到Javascript文件夹。
将两个css文件,reset.css和global.css放到样式文件夹。
这样我们的文件夹结构设置就差不多完成了。


3.编写HTML代码


1)编写首页Html文件

将psd转换成html/css的工作流程是先编写出html文件,随后再编写css,并加入js动态效果。

下面我们会一步步讲解html的编写,先编写出大概完整的框架,代码如下:

<!DOCTYPE html>
<html lang="en">
	<head>
		<!-- Site Title -->
		<title>PSD to HTML: A Photography Site</title>


		<!-- Meta Data -->
		<meta charset="UTF-8" />
	</head>
	<body>


	</body>
</html>

2)编写html文件的head部分


下面我们要开始为刚才的html文件添加细节。首先是head部分,在head部分我们要添加一些meta信息,这样有利于搜索引擎读取。


我们增加了keywords、authors、description、copyright等meta信息,并把语言编码设置为utf-8。代码如下:

<meta name="keywords" content="photography, commercials, exposure videos, senior pictures">
<meta name="description" content="Your company description.">
<meta name="author" content="TutToaster.com/authors/CodyRobertson">
<meta name="copyright" content="Copyright 2010 TutToaster.com">
<meta name="robots" content="follow, index">

现在要开始在html载入一些必须js和css的文件,以便让这些文件起作用,同时为了考虑到浏览器的兼容性,还增加了一些相应的判断条件。具体代码如下:

<!DOCTYPE html>
<html lang="en">
	<head>
		<!-- Site Title -->
		<title>PSD to HTML: A Photography Site</title>


		<!-- Meta Data -->
		<meta charset="UTF-8" />
		<meta name="keywords" content="photography, commercials, exposure videos, senior pictures">
		<meta name="description" content="Your company description.">
		<meta name="author" content="TutToaster.com/authors/CodyRobertson">
		<meta name="copyright" content="Copyright 2010 TutToaster.com">
		<meta name="robots" content="follow, index">


		<!-- Site Theme Styling -->
		<link rel="stylesheet" href="style/reset.css" />
		<link rel="stylesheet" href="style/global.css" />


		<!--[if lt IE 9]>
		<script type="text/javascript" src="javascript/HTML5-Shiv.js"></script>
		<![endif] -->
	</head>
	<body>


		<!-- jQuery and Nivo Slider -->
		<script type="text/javascript" src="javascript/jQuery-1.4.2.min.js"></script>
		<script type="text/javascript" src="javascript/Nivo-Slider.min.js"></script>
	</body>
</html>

3)编写html的body部分


body部分是html文件的主要部分,网页上显示主要是body部分的内容,我们会将这个网页的布局分成以下5个部分:header、featured、status、content、footer。我们为每个部分增加一个div并在每个部分的内部加一个类名为container的div。

<div id="header">
	<div class="container">


	</div>
</div>
<div id="featured">
	<div class="container">


	</div>
</div>
<div id="status">
	<div class="container">


	</div>
</div>
<div id="cotent">
	<div class="container">


	</div>
</div>
<div id="footer">
	<div class="container">


	</div>
</div>

04)编写#header部分

这个网页的header部分主要由两部分组成,一个是logo,另一个是导航栏。logo是简单div,而导航栏则是列表ul。

</pre><pre name="code" class="html"><!-- Logo -->
<h1 id="logo">YourLogo</h1>

<!-- Navigation Menu -->
<ul id="menu">
	<li class="active"><a href="#">HOME</a></li>
	<li><a href="#">PHOTOGRAPHY</a></li>
	<li><a href="#">COMMERCIALS</a></li>
	<li><a href="#">SPORTS</a></li>
	<li><a href="#">EXPOSURE VIDEOS</a></li>
	<li><a href="#">CONTACT</a></li>
</ul>

05)编写#featured部分

网页的#featured是我们放置推荐内容的部分,这部分也有两部分组成,一个是欢迎文字(welcome),另一个滑动图片Slider。

Welcome区域

welcome区域是一个id为welcome的div,由标题文字和描述文字组成,标题文字h2的显示用图片来代替。描述的文字用p标签包起来。

<div id="welcome">
	<h2>Welcome</h2>
	<p>Welcome to Your Site! We are a full service production company, and are a one-stop shop for your production needs. We love interacting with people and have a passion for creating a product that wows! As a husband and wife team and graduates of Specs Howard School of Broadcast Arts, we love to learn new things and are continually striving to perfect our craft.</p>
	<p>Whether it be Family Photographs, a College Sports Exposure Video, Senior Pictures, or Video Editing, we do it all! Thanks for stopping by and feel free to look around!!!</p>
</div>

Slider区域

slider区域由一个透明边框和几张滑动图片组成。滑动图片放在class为slides的列表里边。

<div id="slider">
	<div id="frame"> </div>
	<ul class="slides">
		<li><img src="style/images/slider-placeholder.png" /></li>
		<li><img src="style/images/slider-placeholder2.png" /></li>
		<li><img src="style/images/slider-placeholder.png" /></li>
		<li><img src="style/images/slider-placeholder2.png" /></li>
	</ul>
</div>

完整的#featured部分的代码

下面是完整的#featured部分的代码,读者可以参照对比查看。

<div id="featured">
	<div class="container">
		<!-- Welcome Text/Site Welcome -->
		<div id="welcome">
			<h2>Welcome</h2>
			<p>Welcome to Your Site! We are a full service production company, and are a one-stop shop for your production needs. We love interacting with people and have a passion for creating a product that wows! As a husband and wife team and graduates of Specs Howard School of Broadcast Arts, we love to learn new things and are continually striving to perfect our craft.</p>
			<p>Whether it be Family Photographs, a College Sports Exposure Video, Senior Pictures, or Video Editing, we do it all! Thanks for stopping by and feel free to look around!!!</p>
		</div>


		<!-- Nivo-Slider -->
		<div id="slider">
			<div id="frame"> </div>
			<ul class="slides">
				<li><img src="style/images/slider-placeholder.png" /></li>
				<li><img src="style/images/slider-placeholder2.png" /></li>
				<li><img src="style/images/slider-placeholder.png" /></li>
				<li><img src="style/images/slider-placeholder2.png" /></li>
			</ul>
		</div>
	</div>
</div>

6)编写#status部分

这部分非常简单,由一段文字和一个按钮组成。

<div id="status">
	<div class="container">
		<span>I am currently accepting new projects/appointmentsat this time.</span>
		<button>Hire Me</button>
	</div>
</div>

7)编写#content部分

在psd文件中,你可以看到#content部分的内容会比较多一点,所以我将它按上下分成两个部分。

第一部分

在psd文件中,你可以看到,第一部分由两块区域组成,这两块区域各占据了50%的宽度,一个区域是About Us,另一个区域是What others think,两个区域都有一个标题和一些文字描述。

<div id="about_us">
	<h3>A little about us...</h3>
	<p>We are a full service production companel that company that is your one stop needs, weither it be family photographs, a college sport exposure video, senior pictures or.</p>
</div>
<div id="others_say">
	<h3>What others think...</h3>
	<p>When I first came to Johnny for a first time job I was a bit nervous n how the service would be, but after the first job and how smooth it went i’ll never go anywhere else.
		<br /><cite>- Cody Robertson (<a href="#">Website</a>)</cite>
	</p>
</div>

第二部分

第二部分是由四个各占据25%宽度的小区域组成,这些小区域是由一些预览图、标题和文字组成。

<ul id="services">
	<!-- Photography -->
	<li>
		<img src="style/images/service_preview_1.gif" alt="placeholder" />
		<h4>Photography</h4>
		<p>Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel, semper a, posuere in, orci. Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel.</p>
	</li>


	<!-- Commercials -->
	<li>
		<img src="style/images/service_preview_2.gif" alt="placeholder" />
		<h4>Commercials</h4>
		<p>Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel, semper a, posuere in, orci. Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel.</p>
	</li>


	<!-- Sports -->
	<li>
		<img src="style/images/service_preview_3.gif" alt="plceholder" />
		<h4>Sports</h4>
		<p>Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel, semper a, posuere in, orci. Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel.</p>
	</li>


	<!-- Exposure Videos -->
	<li>
		<img src="style/images/service_preview_1.gif" alt="placeholder" />
		<h4>Exposure Videos</h4>
		<p>Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel, semper a, posuere in, orci. Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel.</p>
	</li>
</ul>

8)编写#footer部分

现在是#footer部分的编写,包含一些联系信息和版权信息等。

<ul id="footer-widgets">
	<li>
		<h5>Contact Us</h5>
		<ul>
			<li>3352 Streetname rd.</li>
			<li>Commerce Township, MI, 48382.</li>
			<li> </li>
			<li>Tell: (248)838-9823</li>
			<li>Fax: (248)942-2342</li>
			<li>Email: [email protected]</li>
		</ul>
	</li>
	<li>
		<h5>Services</h5>
		<ul>
			<li><a href="#">Home</a></li>
			<li><a href="#">Photography</a></li>
			<li><a href="#">Commercials</a></li>
			<li><a href="#">Sports</a></li>
			<li><a href="#">Exposure Videos</a></li>
			<li><a href="#">Contact</a></li>
		</ul>
	</li>
	<li>
		<h5>About Johnny</h5>
		<p>Hey, my name is Johnny, this is a quick little one or two sentences about how im awesome and you should pay me money to do stuff for you!</p>
	</li>
	<li>
		<h5>About Amber</h5>
		<p>Hey, my name is Amber, this is a quick little one or two sentences about how im awesome and you should pay me money to do stuff for you!</p>
	</li>
</ul>

<!-- Positioning Fix -->
<br class="clear" />
<br /><br />

<!-- Copyright -->
<div id="copyright">
	<span>&copy; Copywright 2010 TutToaster.com. All Rights Reserved.</span>
	<a href="#">Back to top</a>
</div>

Html最终显示结果
psd转html/css教程
下图是我们编写好的html文件的最终显示结果,下面还有一份完整的html的代码,方便读者阅读对比。

<!DOCTYPE html>
<html lang="en">
	<head>
		<!-- Site Title -->
		<title>PSD to HTML: A Photography Site</title>


		<!-- Meta Data -->
		<meta charset="UTF-8" />
		<meta name="keywords" content="photography, commercials, exposure videos, senior pictures">
		<meta name="description" content="Your company description.">
		<meta name="author" content="TutToaster.com/authors/CodyRobertson">
		<meta name="copyright" content="Copyright 2010 TutToaster.com">
		<meta name="robots" content="follow, index">


		<!-- Site Theme Styling -->
		<link rel="stylesheet" href="style/reset.css" />
		<link rel="stylesheet" href="style/global.css" />


		<!--[if lt IE 9]>
		<script type="text/javascript" src="javascript/HTML5-Shiv.js"></script>
		<![endif] -->
	</head>
	<body>
		<div id="header">
			<div class="container">
				<!-- Logo -->
				<h1 id="logo">YourSite Title</h1>


				<!-- Navigation Menu -->
				<ul id="menu">
					<li class="active"><a href="#">HOME</a></li>
					<li><a href="#">PHOTOGRAPHY</a></li>
					<li><a href="#">COMMERCIALS</a></li>
					<li><a href="#">SPORTS</a></li>
					<li><a href="#">EXPOSURE VIDEOS</a></li>
					<li><a href="#">CONTACT</a></li>
				</ul>
			</div>
		</div>
		<div id="featured">
			<div class="container">
				<!-- Welcome Text/Site Welcome -->
				<div id="welcome">
					<h2>Welcome</h2>
					<p>Welcome to Johnny Waller Productions! We are a full service production company, and are a one-stop shop for your production needs. We love interacting with people and have a passion for creating a product that wows! As a husband and wife team and graduates of Specs Howard School of Broadcast Arts, we love to learn new things and are continually striving to perfect our craft.</p>
					<p>Whether it be Family Photographs, a College Sports Exposure Video, Senior Pictures, or Video Editing, we do it all! Thanks for stopping by and feel free to look around!!!</p>
				</div>


				<!-- Nivo-Slider -->
				<div id="slider">
					<div id="frame"> </div>
					<ul class="slides">
						<li><img src="style/images/slider-placeholder.png" /></li>
						<li><img src="style/images/slider-placeholder2.png" /></li>
					</ul>
				</div>
			</div>
		</div>
		<div id="content">
			<!-- Working Status -->
			<div id="status">
				<div class="container">
					<span id="avalibility">I am currently accepting new projects/appointmentsat this time.</span>
					<button>Hire Me</button>
				</div>
			</div>


			<div class="container" id="main">
				<!-- About Us/What They Say -->
				<div id="about_us">
					<h3>A little about us...</h3>
					<p>We are a full service production companel that company that is your one stop needs, weither it be family photographs, a college sport exposure video, senior pictures or.</p>
				</div>
				<div id="others_say">
					<h3>What others think...</h3>
					<p>When I first came to Johnny for a first time job I was a bit nervous n how the service would be, but after the first job and how smooth it went i’ll never go anywhere else.
						<br /><cite>- Cody Robertson (<a href="#">Website</a>)</cite>
					</p>
				</div>


				<!-- Clear Fix -->
				<br class="clear" />


				<!-- Services -->
				<ul id="services">
					<!-- Photography -->
					<li>
						<img src="style/images/service_preview_1.gif" alt="placeholder" />
						<h4>Photography</h4>
						<p>Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel, semper a, posuere in, orci. Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel.</p>
					</li>


					<!-- Commercials -->
					<li>
						<img src="style/images/service_preview_2.gif" alt="placeholder" />
						<h4>Commercials</h4>
						<p>Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel, semper a, posuere in, orci. Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel.</p>
					</li>


					<!-- Sports -->
					<li>
						<img src="style/images/service_preview_3.gif" alt="plceholder" />
						<h4>Sports</h4>
						<p>Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel, semper a, posuere in, orci. Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel.</p>
					</li>


					<!-- Exposure Videos -->
					<li>
						<img src="style/images/service_preview_1.gif" alt="placeholder" />
						<h4>Exposure Videos</h4>
						<p>Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel, semper a, posuere in, orci. Praesent imperdiet nulla at tortor. Phasellus non lectus eget massa rhoncus consequat. Donec lectus ligula, posuere vel.</p>
					</li>
				</ul>
			</div>
		</div>
		<div id="footer">
			<div class="container">
				<!-- Footer Widgets -->
				<ul id="footer-widgets">
					<li>
						<h5>Contact Us</h5>
						<ul>
							<li>3352 Streetname rd.</li>
							<li>Commerce Township, MI, 48382.</li>
							<li> </li>
							<li>Tell: (248)838-9823</li>
							<li>Fax: (248)942-2342</li>
							<li>Email: [email protected]</li>
						</ul>
					</li>
					<li>
						<h5>Services</h5>
						<ul>
							<li><a href="#">Home</a></li>
							<li><a href="#">Photography</a></li>
							<li><a href="#">Commercials</a></li>
							<li><a href="#">Sports</a></li>
							<li><a href="#">Exposure Videos</a></li>
							<li><a href="#">Contact</a></li>
						</ul>
					</li>
					<li>
						<h5>About Johnny</h5>
						<p>Hey, my name is Johnny, this is a quick little one or two sentences about how im awesome and you should pay me money to do stuff for you!</p>
					</li>
					<li>
						<h5>About Amber</h5>
						<p>Hey, my name is Amber, this is a quick little one or two sentences about how im awesome and you should pay me money to do stuff for you!</p>
					</li>
				</ul>


				<!-- Positioning Fix -->
				<br class="clear" />
				<br /><br />


				<!-- Copyright -->
				<div id="copyright">
					<span>&copy; Copywright 2010 TutToaster.com. All Rights Reserved.</span>
					<a href="#">Back to top</a>
				</div>
			</div>
		</div>


		<!-- jQuery and Nivo Slider -->
		<script type="text/javascript" src="javascript/jQuery-1.4.2.min.js"></script>
		<script type="text/javascript" src="javascript/Nivo-Slider.min.js"></script>
	</body>
</html>

4.为HTML编写CSS样式

我们将css文件分成两个,一个是css重置文件,css reset可以让html元素在各个浏览器上有相同的显示效果,有关于css的重置(css reset)读者可以去查阅相关的资料,这里的CSS reset文件的代码主要来自于雅虎的YUI reset。另一个文件css文件是这个网页的样式。

1)Reset.css

body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td {
	margin:0;
	padding:0;
}
table {
	border-collapse:collapse;
	border-spacing:0;
}
fieldset,img {
	border:0;
}
address,caption,cite,code,dfn,em,strong,th,var {
	font-style:normal;
	font-weight:normal;
}
ol,ul {
	list-style:none;
}
caption,th {
	text-align:left;
}
h1,h2,h3,h4,h5,h6 {
	font-size:100%;
	font-weight:normal;
}
q:before,q:after {
	content:'';
}
abbr,acronym { border:0;
}

2)基本的样式

首先我们要为网页做一些基本的css样式,包括文字字体、基本结构等。下面开始设置背景色、字体、去掉超链接的下划线以及一些容器的尺寸。

body {
	background: #F2F2F2;
	font: 10px Verdana;
}

a {
	text-decoration: none;
}

p, span {
	line-height: 20px;
}

#header, #featured, #content, #footer {
	overflow: hidden;
}

.container {
	margin: 0 auto;
	width: 1024px;
}

.clear {
	clear: both;
}

现在的css样式效果如下:
psd转html/css教程
3)编写#header的样式

现在开始为header部分编写样式,将header部分的高设为68px,背景为灰色,2px的下边框。

通过将logo文字的text-indent设为-9999px,可以让图像来代替文字的显示。然后我们为导航栏设置左浮动(float:left),让它变成横向导航。通过增加一个.active来编写导航栏当前页的样式。

#header {
	background: #171717;
	border-bottom: 2px solid #252525;
	height: 68px;
}

#header #logo {
	float: left;
	text-indent: -9999px;
}

#header ul {
	float: right;
	height: 68px;
	padding: 17px 0;
}

#header ul li {
	color: #D1D1D1;
	display: block;
	float: left;
	font: bold 10px Verdana;
	padding: 10px 15px;
}

#header ul li a {
	color: #D1D1D1;
	text-decoration: none;
}

#header ul li a:hover {
	color: #FFF;
}

#header ul li:hover a {
	color: #FFF;
}

#header ul li.active {
	background: #252525;
	border-radius: 2px;
		-webkit-border-radius: 2px;
		-moz-border-radius: 2px;
		-o-border-radius: 2px;
}

#header ul li.active a {
	color: #FFF;
}

#header ul li:last-child {
	padding: 10px 0 10px 15px;
}

现在的css样式效果如下:
psd转html/css教程
4)编写#featured部分样式

我们为featured部分添加相应的背景,这个背景设为水平居中,这样即使图片比一些屏幕分辨率大,也能居中显示。然后我们要将welcome部分的文字设为不可见,这样我们就可以用图像来代替它。

同时为welcome下的文字加一些css3的属性,文字阴影。

#featured {
	background: #476D13 url('layout/header.png') no-repeat center top;
	height: 290px;
}

#featured #welcome {
	float: left;
	width: 440px;
}

#featured #welcome h2 {
	background: url('layout/welcome.png') no-repeat left top;
	line-height: 57px;
	padding: 0 0 20px;
	text-indent: -9999px;
}

#featured #welcome p {
	color: #FFF;
	font: 10px Verdana;
	line-height: 20px;
	margin: 15px 0;
	text-shadow: 0px 1px 0px #3F6211;
		-webkit-text-shadow: 0px 1px 0px #3F6211;
		-moz-text-shadow: 0px 1px 0px #3F6211;
		-o-text-shadow: 0px 1px 0px #3F6211;
}

5)编写#slider的样式

slider部分样式编写也比较简单,我们将slider的overflow设为hidden,这样超出slider区域部分的内容就不会显示出来。边框的部分用绝对定位的方式(position:absolute),将透明的边框覆盖在slider的图片上。

#featured #slider {
	float: right;
	position: relative;
	width: 512px;
	overflow: hidden;
}

#slider #frame {
	background: transparent url('layout/frame.png') no-repeat center top;
	height:300px;
	position: absolute;
		top: 0;
		left: 0;
	width: 512px;
	z-index: 100;
}

现在的css样式效果如下图:

psd转html/css教程
6)编写#status样式

这个部分只是一段文字和一个按钮,因为#featured部分有一个40px的margin,所以我们要为#status加一个-40px的margin来抵消它。

按钮的制作是用了一些css的属性,这样一些不支持css3的浏览器将会显示不同的效果,如果需要所有浏览器显示相同效果,可以考虑用图片。具体代码如下:

#status {
background: #F1EEEE;
border-bottom: 1px solid #DCDEDE;
margin: -40px 0 0;
padding: 20px 0;
}

#status span {
	color: #CCC9C9;
	font: bold 20px Verdana;
	line-height: 50px;
}

#status button {
	background-color: #E9E5E5;
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#FFF), to(#E9E5E5));
	background: -moz-linear-gradient(top, #FFF 0%, #E9E5E5 100%);
	filter:  progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#FFFFFF', endColorstr='#E9E5E5'); /* IE6 & IE7 */
	-ms-filter: "progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#FFFFFF', endColorstr='#E9E5E5')"; /* IE8 */
	border: 1px solid #D9D9D9;
	box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
		-webkit-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
		-moz-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
		-o-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
	color: #333;
	float: right;
	font: bold 20px Verdana;
	height: 50px;
	line-height: 30px;
	width: 175px;
}

#status button:hover {
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#E9E5E5), to(#FFF));
	background: -moz-linear-gradient(top, #E9E5E5 0%, #FFF 100%);
}

现在css样式效果如下:
psd转html/css教程
7)编写#content第一部分的样式

#content的第一部分由两个各占据50%宽度的div组成,标题部分文字大小我们设为20px并且设为粗体、斜体,让它更为突出:

#content #about_us, #content #others_say {
	float: left;
	padding: 20px 0 30px;
	width: 50%;
}

#content #about_us h3, #content #others_say h3 {
	color: #474747;
	font: bold italic 20px Verdana;
	line-height: 40px;
}

#content #others_say p cite {
	font: bold italic 10px Verdana;
	padding: 0 0 0 60px;
}

#content #others_say p cite a {
	color: #000;
}

8)编写#content第二部分的样式

第二部分的四块区域每个大概占据23%左右的宽度,这样我们就能为各区域之间增加一些margin,同时由于我们为图片加了1px的边框,所以宽度要计算好。同时最后一块区域的右边距要设为0。

#content ul li {
	float: left;
	margin: 0 28px 0 0;
	width: 235px;
}

#content ul li img {
	background: #F1EEEE;
	border: 1px solid #DCDEDE;
	padding: 5px;
}

#content ul li h4 {
	color: #474747;
	font: bold 14px Verdana;
	line-height: 30px;
}

#content ul li:last-child {
	margin: 0;
}

现在的css样式效果如下:
psd转html/css教程
9)编写#footer的样式

footer部分只是一些简单的css属性,比如背景、填充(padding)、颜色等。

#footer {
	background: #171717 url('layout/footer.png') no-repeat center top;
	height: 250px;
}

#footer ul {
	padding: 40px 0 0;
}

#footer ul li {
	float: left;
	margin: 0 28px 0 0;
	width: 235px;
}

#footer ul li h5 {
	color: #CCCDD2;
	font: bold 14px Verdana;
	padding: 0 0 10px;
}

#footer ul li p {
	color: #7E7E7E;
	font: bold 11px Verdana;
	line-height: 20px;
}

#footer ul li ul {
	margin: 0;
	padding: 0;
}

#footer ul li ul li {
	color: #7E7E7E;
	font: bold 11px Verdana;
	line-height: 20px;
}

#footer ul li ul li a {
	color: #7E7E7E;
}

#footer ul li ul li a:hover {
	color: #9E9E9E;
}

#footer ul li:last-child {
	margin: 0;
}

#footer #copyright {
	border-top: 1px solid #424242;
	font-size: 11px;
	width: 100%;
}

#footer #copyright span {
	color: #424242;
	float: left;
	line-height: 30px;
}

#footer #copyright a {
	color: #63961A;
	float: right;
	line-height: 30px;
}

现在的css样式效果如下图。基本的CSS样式已经编写完成,这是css样式的最终效果。
psd转html/css教程
5.为HTML的js动态效果添加样式

现在我们要开始为滑动图片slider部分增加一些样式。图片的滑动我们用到了nivo-slider这个插件,这个插件自身带有默认的css样式,我们只需要把它默认的css样式添加到我们的css样式文件即可。如果有兴趣的话,也可以自己更改里面的css样式。

nivo-slider默认的css样式如下,我们把它复制到我们的css文件的最后面。

/*
 * jQuery Nivo Slider v2.1
 * http://nivo.dev7studios.com
 *
 * Copyright 2010, Gilbert Pellegrom
 * Free to use and abuse under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 *
 * March 2010
 */

/* The Nivo Slider styles */
.nivoSlider {
	position:relative;
}
.nivoSlider img {
	position:absolute;
	top:0px;
	left:0px;
}
/* If an image is wrapped in a link */
.nivoSlider a.nivo-imageLink {
	position:absolute;
	top:0px;
	left:0px;
	width:100%;
	height:100%;
	border:0;
	padding:0;
	margin:0;
	z-index:60;
	display:none;
}
/* The slices in the Slider */
.nivo-slice {
	display:block;
	position:absolute;
	z-index:50;
	height:100%;
}
/* Caption styles */
.nivo-caption {
	position:absolute;
	left:0px;
	bottom:0px;
	background:#000;
	color:#fff;
	opacity:0.8; /* Overridden by captionOpacity setting */
	width:100%;
	z-index:89;
}
.nivo-caption p {
	padding:5px;
	margin:0;
}
.nivo-caption a {
	display:inline !important;
}
.nivo-html-caption {
	display:none;
}
/* Direction nav styles (e.g. Next & Prev) */
.nivo-directionNav a {
	position:absolute;
	top:45%;
	z-index:99;
	cursor:pointer;
}
.nivo-prevNav {
	left:0px;
}
.nivo-nextNav {
	right:0px;
}
/* Control nav styles (e.g. 1,2,3...) */
.nivo-controlNav a {
	position:relative;
	z-index:99;
	cursor:pointer;
}
.nivo-controlNav a.active {
	font-weight:bold;
}

插入Java Script语句来调用插件

我们需要在html里面加入一段js语句来调用这个插件,记住这个语句要放在导入Nivo-slider.min.js之后。至于具体的一些选项,读者可以到这个插件的官网查询相应的文档。

</pre><pre name="code" class="javascript"><script type="text/javascript">
$(window).load(function() {
	// Slider
	$('#slider .slides').nivoSlider({
		effect: 'random',
		directionNav: false,
		controlNav: false
	});
});
</script>

总结

经过漫长的操作,终于完成了将psd转成html/css的过程,现在读者应该对这个工作的流程有一个系统的了解,转换psd到html/css的工作包括psd图片的切片、html和css文件的编写、js插件的使用等。现在你可以开始尝试做一个网页,相信很快就会懂得如何转换psd到html/css。

原文链接:How to Convert a Slick PSD Design to XHTML/CSS
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章