列表 list
HTML 列表和 CSS 列表属性
在 HTML 中列表主要有两种类型:
- 无序列表
- 有序列表
CSS 列表属性使您可以:
- 为有序列表设置不同的列表项标记
- 为无序列表设置不同的列表项标记
- 将图像设置为列表项标记
- 为列表和列表项添加背景色
不同的列表项目标记
list-style-type 属性指定列表项标记的类型
可用的列表项标记
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
list-style-image 属性将图像指定为列表项标记
ul {
list-style-image: url('/i/photo/sqpurple.gif');
}
定位列表项标记
list-style-position 属性指定列表项标记(项目符号)的位置
"list-style-position: outside;"表示项目符号点在列表项之外,列表项每行的开头将垂直对齐,outside是默认的
"list-style-position: inside;"表示项目符号将在列表项之内,作为列表项的一部分其将成为文本的一部分并在开头推开文本
删除默认设置
list-style-type: none 属性还可以用于删除标记(项目符号)
需要注意的是列表还拥有默认的外边距与内边距
若要删除则需要在<ul>或<ol>中添加 margin:0 和 padding:0
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
列表的简写属性
list-style属性,用于在一条声明中设置所有列表属性
ul {
list-style: square inside url(sqpurple.gif");
}
顺序:
1. list-style-type - 若指定了list-style-image,则无法显示图像时显示此属性值
2. list-style-position - 指定列表项标记应显示在内容流的内部还是外部
3. list-style-image - 将图像指定为列表项标记
如果缺少上述属性值之一,则将插入缺失属性的默认值
我们还可以使用颜色设置列表样式,使列表看起来更有趣
添加到 <ol> 或 <ul> 标记的任何样式都会影响整个列表,而添加到 <li> 标记的属性将影响各个列表项
ol {
background: #ff9999;
padding: 20px;
}
ul {
background: #3399ff;
padding: 20px;
}
ol li {
background: #ffe5e5;
padding: 5px;
margin-left: 35px;
}
ul li {
background: #cce5ff;
margin: 5px;
}
更多的有趣实例
红色左边框的自定义列表
ul {
border-left: 5px solid red;
background-color: #f1f1f1;
list-style-type: none;
padding: 10px 20px;
}
无项目符号的全屏宽度边框列表
ul {
list-style-type: none;
padding: 0;
border: 1px solid #ddd;
}
ul li {
padding: 8px 16px;
border-bottom: 1px solid #ddd;
}
ul li:last-child {
border-bottom: none;
}
表格 table
使用 CSS 可以极大地改善 HTML 表格的外观
<!DOCTYPE html>
<html>
<head>
<style>
#customers {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td, #customers th {
border: 1px solid #ddd;
padding: 8px;
}
#customers tr:nth-child(even){background-color: #f2f2f2;}
#customers tr:hover {background-color: hotpink;}
#customers th {
padding-top: 12px;
padding-bottom: 12px; /* 内边距用于将方框空间填充变大,营造舒适视觉效果*/
text-align: left;
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<table id="customers">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Address</th>
<th>City</th>
</tr>
<tr>
<td>Alibaba</td>
<td>Ma Yun</td>
<td>No. 699, Wangshang Road, Binjiang District</td>
<td>Hangzhou</td>
</tr>
<tr>
<td>APPLE</td>
<td>Tim Cook</td>
<td>1 Infinite Loop Cupertino, CA 95014</td>
<td>Cupertino</td>
</tr>
<tr>
<td>BAIDU</td>
<td>Li YanHong</td>
<td>Lixiang guoji dasha,No 58, beisihuanxilu</td>
<td>Beijing</td>
</tr>
<tr>
<td>Canon</td>
<td>Tsuneji Uchida</td>
<td>One Canon Plaza Lake Success, NY 11042</td>
<td>New York</td>
</tr>
<tr>
<td>Google</td>
<td>Larry Page</td>
<td>1600 Amphitheatre Parkway Mountain View, CA 94043</td>
<td>Mountain View</td>
</tr>
<tr>
<td>HUAWEI</td>
<td>Ren Zhengfei</td>
<td>Putian Huawei Base, Longgang District</td>
<td>Shenzhen</td>
</tr>
<tr>
<td>Microsoft</td>
<td>Bill Gates</td>
<td>15700 NE 39th St Redmond, WA 98052</td>
<td>Redmond</td>
</tr>
<tr>
<td>Nokia</td>
<td>Olli-Pekka Kallasvuo</td>
<td>P.O. Box 226, FIN-00045 Nokia Group</td>
<td>Helsinki</td>
</tr>
<tr>
<td>SONY</td>
<td>Kazuo Hirai</td>
<td>Park Ridge, NJ 07656</td>
<td>Park Ridge</td>
</tr>
<tr>
<td>Tencent</td>
<td>Ma Huateng</td>
<td>Tencent Building, High-tech Park, Nanshan District</td>
<td>Shenzhen</td>
</tr>
</table>
</body>
</html>
表格边框
table, th, td {
border: 1px solid black;
}
上面的表格拥有双边框
全宽表格
需要一个可以覆盖整个屏幕(全宽)的表格,请为 <table> 元素添加 width: 100%
table {
width: 100%;
}
删除(合并)表格边框
border-collapse 属性设置是否将表格边框折叠
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
如果只希望表格周围有边框,则仅需为 <table> 指定 border 属性
table {
border: 1px solid black;
}
表格宽度与高度由width和height属性定义
table {
width: 100%; /* 宽度自适应其父元素大小 */
}
table th {
height: 50px;
}
text-align 属性设置 <th> 或 <td> 中内容的水平对齐方式(左、右或居中)
默认情况下,<th> 元素的内容居中对齐,而 <td> 元素的内容左对齐
要使 <td> 元素的内容也居中对齐,请使用 text-align: center
vertical-align 属性设置 <th> 或 <td> 中内容的垂直对齐方式(上、下或居中)
默认情况下,表中内容的垂直对齐是居中(<th> 和 <td> 元素都是)
下例将 <td> 元素的垂直文本对齐方式设置为下对齐
td {
height: 50px;
vertical-align: bottom;
}
控制边框和表格内容之间的间距,请在 <td> 和 <th> 元素上使用 padding 属性
th, td {
padding: 15px;
text-align: left;
}
向 <th> 和 <td> 添加 border-bottom 属性,以实现水平分隔线
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
可悬停表格
在 <tr> 元素上使用 :hover选择器,突出显示鼠标悬停时的表格行:
tr:hover {background-color: #f5f5f5;}
条状表格
为了实现斑马纹表格效果,请使用 nth-child() 选择器,并为所有偶数(或奇数)表行添加 background-color
tr:nth-child(even) {background-color: #f2f2f2;}
表格颜色
指定 <th> 元素的背景颜色和文本颜色
th {
background-color: #4CAF50;
color: white;
}
如果屏幕太小而无法显示全部内容,则响应式表格会显示水平滚动条
在 <table> 元素周围添加带有 overflow-x:auto 的容器元素(例如 <div>),以实现响应式效果
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even) {background-color: #f2f2f2;}
</style>
</head>
<body>
<h1> 响应式表格 </h1>
<p>如需创建响应式表格,请用 <strong>overflow-x:auto</strong> 的容器元素(比如 div)包围表格元素:</p>
<div style="overflow-x:auto;">
<table>
</table>
</div>
</body>
</html>
在 OS X Lion(在 Mac 上)中,滚动条默认情况下是隐藏的,并且仅在使用时显示(即使设置了 "overflow:scroll")
设置表格标题的位置
caption {
caption-side: bottom;
}
<table>
<caption>Table 1.1 Customers</caption>
<tr>
<th>Company</th>
<th>Contact</th>
<th>City</th>
</tr>
<tr>
<td>Alibaba</td>
<td>Ma Yun</td>
<td>Hangzhou</td>
</tr>
<tr class="alt">
<td>Hangzhou</td>
<td>Tim Cook</td>
<td>Cupertino</td>
</tr>
<tr>
<td>BAIDU</td>
<td>Li YanHong</td>
<td>Beijing</td>
</tr>
<tr class="alt">
<td>HUAWEI</td>
<td>Ren Zhengfei</td>
<td>Shenzhen</td>
</tr>
<tr>
<td>Tencent</td>
<td>Ma Huateng</td>
<td>Shenzhen</td>
</tr>
</table>