CSS 指的是层叠样式表(Cascading StyleSheet),在网页制作时采用层叠样式表技术,可以有效地对页面的布局、字体、颜色、背景和其它效果实现更加精确的控制,本文对 css 一些基本内容及常用功能进行一下总结,总结的内容主要是来自实验楼的 CSS 速成教程 ,这篇文章会实时更新,后续如果遇到什么好的有用功能,也会更新到这篇文章中。 css 基础语法CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明。 selector { declaration1; declaration2; ... declarationN;} 选择器通常是需要改变的 HTML 元素,每条声明都由一个属性和一个值组成,每个属性都有一个值,属性和值被冒号分开。 h1{ color:red; font-size:14px;} css 基本样式介绍 css 的一些基本样式,这些都是 css 中一些常用的设置。 背景css 是允许使用纯色作为背景,也允许使用背景图像实现一些相当复杂的效果。
举个例子,如下所示 body{ background-color: red; background-image: url('hha.jpg'); background-repeat: no-repeat; background-position: center top; background-attachment: fixed; background-size:100px 100px;}p{ width: 150px; padding: 10px; background-color: #0014ff;} html> head> metacharset='UTF-8'> title>title> linkrel='stylesheet'href='style.css'type='text/css'> head> body> p>matt's blogp> body>html> 效果如下下图所示:
文本css 文本可定义文本的外观,通过文本的属性,可以改变文本的颜色、字符间距、对齐方式等等。
应用时,可以进行以下设置 body{ color: red; text-align: center;}p {text-indent: 5em;} 在网页展现时,标签 链接在 css 的链接属性中,可以对其颜色、字体、背景进行相应的设置,不同的状态我们可以设置对应的样式。 4种链接状态css 共有以下几种链接状态:
在进行设置中,有以下两种要求:
修改链接下划线 只需要在链接属性中添加 a:link{ background-color:#B2FF99; text-decoration:none;}a:visited {background-color:#FFFF85;}a:hover {background-color:#FF704D;}a:active {background-color:#FF704D;} 列表在 html 中学习过列表的一些设置,这里主要讲述的是如何通过 css 进行列表的设置。 简单的列表类型 列表有无序、有序之分,无序列表又可以用不同的标记来区分,而 /* html 中添加以下内容 */ulclass='circle'> li>hahali> li>wawali>ul>olclass='square'> li>hahali> li>wawali>ol>/* css 中的设置 */ul.circle {list-style-type:circle}ol.square {list-style-type:upper-roman}} 列表项图片在无序列表中,除了进行一些默认的设置外,并没有其他可选的内容,但是 css 可以提供图片来作为标记。 ul.img1{list-style-image:url('1.ico')} 表格在 css 表格的设置中,需要先了解一下属性:
这里也以一个例子来说明: /* html 部分 */tableid='tb'> tr> th>nameth> th>ageth> th>numberth> tr> tr> td>litd> td>3td> td>4td> tr> trclass='tr2'> td>litd> td>3td> td>4td> tr> tr> td>litd> td>3td> td>4td> tr> trclass='tr2'> td>litd> td>3td> td>4td> tr>table> /* css 部分*/#tb td,th{ border: 1px solid green; padding: 5px;}#tb{ border-collapse: collapse; width: 500px; text-align: center;}#tb th{ text-align: center; color: black; background-color: lightseagreen;}#tb tr.tr2 td{ color: black; background-color: #B2FF99;} 显示效果如下图
轮廓轮廓(outline)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用。CSS outline 属性规定元素轮廓的样式、颜色和宽度。涉及到的属性有:
/* html 部分 */<>'p1'>matt's blog 显示效果如下图所示:
css 选择器选择器是 css 中最常用的组件,本节就介绍一下 css 中最常见的几种选择器。 元素选择器 最常见的选择器就是元素选择器,文档的元素的就是最基本的选择器。比如 /*第一种,直接对某个元素进行相应的设置*/h1{ color: cadetblue; }/*第二种,对多个元素执行同样的操作*/h1,h2,h3,h4{ color: cadetblue;}/*第三种,对没有特别特定元素设置的元素都执行同样的操作(除 h4外,其他执行的操作都一样)*/*{ color: cadetblue;}h4{ color: darkslategray;} 类选择器 类选择器允许以一种独立与文档元素的方式来制定样式。 /*第一种,最简单的使用方法*//* 调用方式: */h1.div{ color: cadetblue;}/*第三种,多类选择器(.class.class{}),它可以继承多个类的作用*//* 调用方式:matt */.div{ color: cadetblue;}/*第二种,将类选择器结合元素选择器来使用,下面的例子这个 .div 就只会对 h1 起作用*//* 调用方式:mattshiyanlou is my home */.p1{ color: cadetblue;}.p2{ font-size: 20px;}.p1.p2{ font-style: italic;}id 选择器 id 选择器类似于类选择器,id 选择器的引入是用
/* html 中的用法 */ 'div'>matt's blog /* css 中的配置*/#div{ color: cadetblue;}属性选择器对带有指定属性的 HTML 元素设置样式。 /* 第一种,对带有 title 属性的所有元素设置样式 */[title] {color:red;}/* 第二种,为 title='te' 的所有元素设置样式 */[title=te]{ color: red;}/* 第三种,为 href='http://' 的标签 a 设置元素样式 *//* 调用方式:matt's blog */a[href='http://']{ color: cornflowerblue;} 其他选择器其他的还有:
下面仅列出一种后代选择器 /* html */ This is strong>mystrong> blog. /* css */p strong{ color: cadetblue;}css 盒子模型css 的盒子模型主要适用于网页的布局。 盒子模型概述盒子的组成包括:
其中,内边距、边框和外边距都是可选的,默认值是0。下面用一张简单的图来描述它们的结构
内边距 内边据在正文(content)外、边框(border)内,控制该区域最简单的属性是
举个栗子 /* html 部分 */tableborder='1'> tr> td> h1>正文h1> td> tr>table>/* css 部分 */h1 { padding-left: 5cm; padding-right: 5cm; padding-top: 30px; padding-bottom: 30px;}
边框元素的边框 (border) 是围绕元素内容和内边距的一条或多条线。
外边距外边距就是围绕在内容框的区域,也可以使用任何长度的单位、百分数来进行设置。
/* html 部分 */divclass='wb'> divclass='bk'> divclass='nj'> divclass='zw'> matt's blog div> div> div>div>/* css 部分 */.wb{ margin: 100px;}.bk{ border-style: groove;}.nj{ padding: 10px;}.zw{ background-color: cornflowerblue;}
css 一些高级用法这里是 css 中一些高级的常见用法 定位定位,就是定义元素框相对于其正常位置应该出现的位置,或者相对于父元素、另一个元素甚至浏览器窗口本身的位置。 在 css 中,有三种基本的定位机制:
定位有以下几个属性:
.position1{ width: 100px; height: 100px; background-color: cornflowerblue; position: relative; left: 60px;} 浮动 这里涉及到的属性就是
/* html 部分 */<>'qd'>> 效果如下图所示:
尺寸尺寸属性允许你控制元素的高度和宽度。同样,它允许你增加行间距。涉及到的属性有:
导航栏这里通过一个示例来实现导航栏的功能。 /* html 部分 */ul> li>ahref='http://'>blog1a>li> li>ahref='http://'>blog2a>li> li>ahref='http://'>blog3a>li> li>ahref='http://'>blog4a>li>ul>/* css 部分 */ul{ list-style: none;}li{ float:left;}a:link,a:visited{ text-decoration: none; background-color: lightgray; display: block; width: 100px; margin:5px 10px;}a:active,a:hover{ background-color: cadetblue;} 显示效果如下:
图片 插入一张图片,加上一句描述符,使用 /* html 部分 */<>'image'> <>'./hha.jpg'target='_self'> <>'hha.jpg'width='150px'height='150px'> <>'text'>haha /* css 部分 */.image{ border: 2px solid darkgrey; width: auto; height: auto; float: left; text-align: center; padding: 5px;}img{ padding: 5px;}.text{ font-size: 20px; margin-bottom: 5px;} 显示效果如下:
然后可以通过 到这里,css 基本内容已经总结完了,不过本文后续会一直更新,遇到什么常用的设置,都会更新到本文中。 |
|