前端学习笔记之——其他 CSS 属性和特性

其他 CSS 属性和特性

1、设置元素的颜色和透明度

属性说明
color设置元素的前景色<颜色>
opacity设置颜色的透明度<数值>

1.1、设置前景色

color 属性一般用来设置文本的颜色。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            p {
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                font-family: sans-serif;   
            }
            #banana {
                font-size: x-large;
                border: medium solid white;                
                background-color: green;
                color: rgba(255, 255, 255, 0.7);            
            }
            a:hover {
                color: red;
            }
                    
        </style>
    </head>
    <body>
        <p id="fruittext">
            There are lots of different kinds of fruit - there are over 500
            varieties of <span id="banana">banana</span> alone. By the time we add the
            countless types of apples, oranges, and other well-known fruit, we are faced
            with thousands of choices.
            <a href="http://en.wikipedia.org/wiki/Banana">Learn more about Bananas</a>
        </p>
    </body>
</html>

在这里插入图片描述

1.2、设置元素的透明度

可以使用 opacity 属性让整个元素和文本内容透明。这个属性的取值范围是 0~1,前者代表完全透明,后者代表完全不透明。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            p {
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                font-family: sans-serif;   
            }
            #banana {
                font-size: x-large;
                border: medium solid white;                
                background-color: green;
                color: white;
                opacity: 0.4;
            }
            a:hover {
                color: red;
            }
        </style>
    </head>
    <body>
        <p id="fruittext">
            There are lots of different kinds of fruit - there are over 500
            varieties of <span id="banana">banana</span> alone. By the time we add the
            countless types of apples, oranges, and other well-known fruit, we are faced
            with thousands of choices.
            <a href="http://en.wikipedia.org/wiki/Banana">Learn more about Bananas</a>
        </p>
    </body>
</html>

在这里插入图片描述


2、设置表格样式

属性说明
border-collapse设置相邻单元格的边框处理样式collapse
separate
border-spacing设置相邻单元格边框的间距1~2 个长度值
caption-side设置表格标题的位置top
bottom
empty-cells设置空单元格是否显示边框hide
show
table-layout指定表格的布局样式auto
fixed

2.1、合并表格边框

border-collapse 用来控制 table 元素相邻单元格边框的样式:

在这里插入图片描述

浏览器为表格绘制了一个边框,同时还为每个单元格绘制了边框,显示出来就是双边框。使用 border-collapse 属性可以改变这种效果。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            table {
                border-collapse: collapse;
            }
            th, td {
                padding: 2px;
            }
        </style>
    </head>
    <body>
        <table border="1">
            <caption>Results of the 2011 Fruit Survey</caption>
            <colgroup id="colgroup1">
                <col id="col1And2" span="2"/>
                <col id="col3"/>
            </colgroup>
            <colgroup id="colgroup2" span="2"/>
            <thead>
                <tr>
                    <th>Rank</th><th>Name</th><th>Color</th>
                    <th colspan="2">Size & Votes</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>Favorite:</th><td>Apples</td><td>Green</td>
                    <td>Medium</td><td>500</td>
                </tr>
                <tr>
                    <th>2nd Favorite:</th><td>Oranges</td><td>Orange</td>
                    <td>Large</td><td>450</td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th colspan="5">&copy; 2011 Adam Freeman Fruit Data Enterprises</th>
                </tr>                
            </tfoot>
        </table>
    </body>
</html>

collapse 值告诉浏览器不要为相邻元素绘制两个边框。

在这里插入图片描述

2.2、配置独立边框

如果你一定要为 border-collapse 属性使用默认值 separate,再加几个其他属性同样可以改善表格外观。border-spacing 属性定义相邻元素边框的间距。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            table {
                border-collapse: separate;
                border-spacing: 10px;
            }
            th, td {
                padding: 2px;
            }
        </style>
    </head>
    <body>
        <table border="1">
            <caption>Results of the 2011 Fruit Survey</caption>
            <colgroup id="colgroup1">
                <col id="col1And2" span="2"/>
                <col id="col3"/>
            </colgroup>
            <colgroup id="colgroup2" span="2"/>
            <thead>
                <tr>
                    <th>Rank</th><th>Name</th><th>Color</th>
                    <th colspan="2">Size & Votes</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>Favorite:</th><td>Apples</td><td>Green</td>
                    <td>Medium</td><td>500</td>
                </tr>
                <tr>
                    <th>2nd Favorite:</th><td>Oranges</td><td>Orange</td>
                    <td></td><td></td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th colspan="5">&copy; 2011 Adam Freeman Fruit Data Enterprises</th>
                </tr>                
            </tfoot>
        </table>
    </body>
</html>

在边框之间指定了 10 像素的空白。

在这里插入图片描述

2.3、处理空单元格

默认情况下,即使单元格为空,浏览器也会为单元格设置独立的边框。可以使用 empty-cells 属性控制这种行为。empty-cells 的默认值为 show,如果将该属性设置为 hide,浏览器就不会绘制边框。

<style>
       table {
              border-collapse: separate;
              border-spacing: 10px;
			  empty-cells: hide;
        }
        th, td {
              padding: 2px;
        }
</style>

在这里插入图片描述

2.4、设置标题的位置

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            table {
                border-collapse: collapse;
                caption-side: bottom;
            }
            th, td {
                padding: 5px;
            }
        </style>
    </head>
    <body>
        <table border="1">
            <caption>Results of the 2011 Fruit Survey</caption>
            <colgroup id="colgroup1">
                <col id="col1And2" span="2"/>
                <col id="col3"/>
            </colgroup>
            <colgroup id="colgroup2" span="2"/>
            <thead>
                <tr>
                    <th>Rank</th><th>Name</th><th>Color</th>
                    <th colspan="2">Size & Votes</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>Favorite:</th><td>Apples</td><td>Green</td>
                    <td>Medium</td><td>500</td>
                </tr>
                <tr>
                    <th>2nd Favorite:</th><td>Oranges</td><td>Orange</td>
                    <td></td><td></td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th colspan="5">&copy; 2011 Adam Freeman Fruit Data Enterprises</th>
                </tr>                
            </tfoot>
        </table>
    </body>
</html>

在这里插入图片描述

2.5、指定表格布局

默认情况下,浏览器会根据表格每一列中最宽的单元格设置整列单元格的宽度。这意味着你不需要担心还要亲自解决单元格大小的问题,不过,这同时意味着在能够确定页面布局之前,浏览器必须获取所有的表格内容。

浏览器显示表格采用的方法是由 table-layout 属性决定的,它的默认值为 auto。使用另一个值 fixed 可以禁用自动布局。在 fixed 模式中,表格的大小由表格自身和单独每列的 width 值设定的。如果没有列宽值可用,浏览器会设置等距离的列宽。

因此,只要获取了一行的表格数据,浏览器就可以确定列宽。其他行内的数据会自动换行以适应列宽。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            table {
                border-collapse: collapse;
                caption-side: bottom;
                table-layout: fixed;
                width: 100%;
            }
            th, td {
                padding: 5px;
            }
        </style>
    </head>
    <body>
        <table border="1">
            <caption>Results of the 2011 Fruit Survey</caption>
            <colgroup id="colgroup1">
                <col id="col1And2" span="2"/>
                <col id="col3"/>
            </colgroup>
            <colgroup id="colgroup2" span="2"/>
            <thead>
                <tr>
                    <th>Rank</th><th>Name</th><th>Color</th>
                    <th colspan="2">Size & Votes</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>Really Really Really Long Title:</th>
                    <td>Apples</td><td>Green</td>
                    <td>Medium</td><td>500</td>
                </tr>
                <tr>
                    <th>2nd Favorite:</th><td>Oranges</td><td>Orange</td>
                    <td></td><td></td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th colspan="5">&copy; 2011 Adam Freeman Fruit Data Enterprises</th>
                </tr>                
            </tfoot>
        </table>
    </body>
</html>

在这里插入图片描述


3、设置列表样式

属性说明
list-style-type指定列表中使用的标记的类型
list-style-image指定图像作为列表标记<图像>
list-style-position指定标记相对于列表项目盒子的位置inside
outside
list-style设置所有列表特征的简写属性

list-style 简写属性的格式如下:

list-style: <list-style-type> <list-style-position> <list-style-image>

3.1、设置列表标记的类型

list-style-type 属性用来设置标记的样式。

说明
none没有标记
box
check
circle
diamond
disc
dash
square
使用指定形状的标记
decimal使用十进制数字作为标记
binary使用二进制数字作为标记
lower-alpha使用小写字母作为标记
upper-alpha使用大写字母作为标记
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            ol {
                list-style-type: lower-alpha;
            }
        </style>
    </head>
    <body>
        I like apples and oranges.
        
        I also like:
        <ol>
            <li>bananas</li>
            <li>mangoes</li>
            <li style="list-style-type: decimal">cherries</li>
            <li>plums</li>
            <li>peaches</li>
            <li>grapes</li>
        </ol>
    </body>
</html>

在这里插入图片描述

3.2、使用图像作为列表标记

list-style-image 属性可以将图像作为列表标记。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            ul {
                list-style-image: url('banana-vsmall.png');
            }
        </style>
    </head>
    <body>
        I like apples and oranges.
        
        I also like:
        <ul>
            <li>bananas</li>
            <li>mangoes</li>
            <li>cherries</li>
            <li>plums</li>
            <li>peaches</li>
            <li>grapes</li>
        </ul>
    </body>
</html>

在这里插入图片描述

3.3、设置列表标记的位置

可以使用 list-style-position 属性指定标记相对于 li 元素内容框的位置。这个属性有两个值:inside 和 outside,前者定义标记位于内容框的内部,后者定义标记位于内容框外部。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            li.inside {
                list-style-position: inside;
            }
            li.outside {
                list-style-position: outside;
            }
            li {
                background-color: lightgrey;
            }            
        </style>
    </head>
    <body>
        I like apples and oranges.
        
        I also like:
        <ul>
            These are the inside items:
            <li class="inside">bananas</li>
            <li class="inside">mangoes</li>
            <li class="inside">cherries</li>
            These are the outside items:
            <li class="outside">plums</li>
            <li class="outside">peaches</li>
            <li class="outside">grapes</li>
        </ul>
    </body>
</html>

在这里插入图片描述


4、设置光标样式

cursor 属性用来改变光标的外形。

属性说明
cursor设置光标的样式auto、crosshair、default、help、move、pointer、progress、text、wait、n-resize、e-resize、w-resize、ne-resize、nw-resize、se-resize
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            p {
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                font-family: sans-serif;   
            }
            #banana {
                font-size: x-large;
                border: medium solid white;                
                background-color: green;
                color: rgba(255, 255, 255, 0.7);
                cursor: progress;
            }
                    
        </style>
    </head>
    <body>
        <p id="fruittext">
            There are lots of different kinds of fruit - there are over 500
            varieties of <span id="banana">banana</span> alone. By the time we add the
            countless types of apples, oranges, and other well-known fruit, we are faced
            with thousands of choices.
        </p>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值