CSS

CSS:页面美化和布局控制

* 概念Cascading Style Sheets 层叠样式表
  * 层叠:多个样式可以作用在同一个HTML的元素上,同时生效

* 好处
  * 功能强大
  * 将内容的展示和样式控制分离
    * 降低了耦合度,解耦
    * 让分工协作更加容易
    * 提高开发的效率

* CSS的使用:CSS和HTML的结合方式
  * 内联样式
    * 在标签内使用 style 属性指定CSS代码(耦合度高,不推荐使用)
  * 内部样式
    * 在<head>标签内,定义<style>标签,<style>标签体的内容就是CSS代码
  * 外部样式
    * 将CSS代码抽取到外部文件中,并在HTML文件中定义<link>标签进行引入,或在<style>标签体内写入   @import"文件名"
  * 注意:三种方式的作用范围越来越大,优先级越来越小

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        div {
            color: blue;
        }
    </style>

    <link rel="stylesheet" href="a.css">
    
    <style>
        @import "a.css";
    </style>

</head>
<body>

<!--
内联样式
    * 在标签内使用style属性指定css代码

-->

<div style="color: darkred">hello css</div>


<!--
内部样式
    * 在head标签内定义style标签,style标签的标签体内容就是css代码
-->

<div>hello css</div>
<p>hello css</p>
<dav>hello css</dav>

<!--
外部样式
    * 定义css的资源文件
    * 在head标签内,定义link标签,引入外部的资源文件
-->

</body>
</html>
View Code
 
* CSS的基本语法
  * 格式
    选择器 {
      属性名1 : 属性值1;
      属性名2 : 属性值2
    }
  * 选择器:筛选具有相似特征的标签元素
    * 注意:
      * 每一对属性需要使用分号隔开,最后一对属性值可以不加分号
* 选择器分类
  * 基础选择器
    * id 选择器:选择具体的 id属性值的元素
      * 语法:# id属性值 { }
      * 建议:在一个HTML文件中 id值唯一
    * 元素选择器:选择具有相同标签名称的元素
      * 语法:标签名称 { }
      * 注意:id选择器的优先级高于元素选择器
    * 类选择器:选择具有相同 class属性值的元素
      * 语法:.class属性值 { }
      * 注意:类选择器的优先级高于元素选择器
  * 扩展选择器
    * 通用选择器 * { }
      * 选中所有元素
    * 并集选择器选择器1, 选择器2 { }
      * 选中多个元素
    * 子选择器选择器1 选择器2 { }
      * 筛选选择器1下的选择器2 元素
    * 父选择器选择器1 > 选择器2 { }
      * 筛选选择器2上的选择器1 元素
    * 属性选择器元素名称 [ 属性名 = "属性值" ] { }
      * 选择元素名称,属性名 = 属性值 的元素
    * 伪类选择器元素 : 状态 { }
      * 选择一些元素具有的状态
      * 如:<a>
        * 状态:
          * link:初始化的状态
          * visited:被访问过的状态
          * active:正在访问的状态
          * hover:鼠标悬浮状态
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>扩展选择器</title>

    <style>
        .cls1 {

            color: yellow;
        }

        div #ppp {
            font-size: 30px;
        }

        div > p {
            border: 1px solid;
        }

        input[type="text"] {
            border: 10px solid;
        }

        a:link {
            color: pink;
        }

        a:hover {
            color: green;
        }

        a:active {
            color: yellow;
        }

        a:visited {
            color: red;
        }
    </style>
</head>
<body>
<div class="cls1">aaa</div>

<div>
    <p id="ppp">
        传智播客
    </p>

    <div>
        德玛西亚
    </div>
</div>

<input type="text">
<p>黑马程序员</p><br>

<a href="#">传智播客</a><br><br><br>

<a href="#">传智播客</a>
</body>
</html>
View Code
 
* 属性
  * 字体、文本
    * font - size:字体大小
    * color:文本颜色
    * text - align:对齐方式
    * line - height:行高
  * 背景
    * backgrand:设置背景(复合属性)
  * 边框
    * border:设置四个边框(复合属性)
    * border - left:设置左边框
  * 尺寸
    * width:宽度
    * height:高度
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字体属性</title>

    <style>
        p{
            color: red;
            font-size: 30px;
            text-align: center;
            line-height: 1;
            border:1px solid red;
        }

        div{
            border:1px solid red;
            height: 200px;
            width: 200px;
            background: url("../img/logo.jpg") no-repeat center;
        }

    </style>

</head>
<body>

<p>传智播客</p>
<div>黑马</div>

</body>
</html>
View Code
  * 盒子模型:控制布局
    * padding :内边距
      * 默认情况下,内边距会影响盒子的大小
      * box - sizing : border - box;  设置之后就不会影响了
    * margin :外边距
    * float:浮动
      * left:左浮动
      * right:右浮动
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>盒子模型</title>
    <style>
        div {
            border: 1px solid red;
            width: 100px;
        }

        .div1 {
            width: 100px;
            height: 100px;
            margin: 50px;
        }

        .div2 {
            width: 200px;
            height: 200px;
            padding: 50px;
            box-sizing: border-box;
        }

        .div3 {
            float: left;
        }
    </style>

</head>
<body>


<div class="div2">

    <div class="div1">

    </div>
</div>

<div class="div3">aaaaa</div>
<div class="div3">bbbbb</div>
<div class="div3">ccccc</div>
<div class="div3">ddddd</div>

</body>
</html>
View Code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>美化后的注册页面</title>

    <style>
        * {
            margin: 5px;
            padding: 0px;
            box-sizing: border-box;
        }

        body {
            background: url("../img/register_bg.png") no-repeat center;
        }

        .rg_layout {
            width: 900px;
            height: 500px;
            border: 5px solid #EEEEEE;
            background-color: white;
            margin: 15px auto auto;
        }

        .rg_left {
            /*border: 1px solid red;*/
            float: left;
        }

        .rg_left > p:first-child {
            color: #FFD026;
            font-size: 20px;
        }

        .rg_left > p:last-child {
            color: #A6A6A6;
            font-size: 20px;
        }

        .rg_center {
            float: left;
            width: 450px;
            height: 800px;
        }

        .rg_right {
            float: right;
            margin: 15px;
        }

        .rg_right > p:last-child {
            font-size: 15px;
        }

        .rg_right p a {
            color: pink;
        }

        .td_left {
            height: 40px;
            width: 200px;
            text-align: right;
        }

        .td_right {
            padding-left: 30px;
        }

        #username, #password, #email, #name, #tel, #birthday {
            width: 251px;
            height: 25px;
            border: 1px solid gray;
            border-radius: 5px;
            padding-left: 10px;
        }

        #checkCode {
            width: 110px;
            height: 25px;
            border: 1px solid gray;
            border-radius: 5px;
            padding-left: 10px;
        }

        #img_check {
            height: 32px;
            vertical-align: middle;
        }

        #btn_sub {
            width: 150px;
            height: 40px;
            background-color: #FFD026;
            border: 1px solid #FFD026;
        }
    </style>

</head>
<body>

<div class="rg_layout">
    <div class="rg_left">
        <p>新用户注册</p>
        <p>USER REGISTER</p>
    </div>

    <div class="rg_center">
        <div class="rg_form">
            <!--定义表单-->
            <form action="#" method="post">
                <table>
                    <tr>
                        <td class="td_left"><label for="username">用户名</label></td>
                        <td class="td_right"><input type="text" name="username" id="username" placeholder="请输入用户名"></td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="password">密码</label></td>
                        <td class="td_right"><input type="password" name="password" id="password" placeholder="请输入密码">
                        </td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="email">邮箱</label></td>
                        <td class="td_right"><input type="email" name="email" id="email" placeholder="请输入邮箱"></td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="name">名字</label></td>
                        <td class="td_right"><input type="text" name="name" id="name" placeholder="请输入姓名"></td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="tel">手机号</label></td>
                        <td class="td_right"><input type="tel" name="tel" id="tel" placeholder="请输入手机号"></td>
                    </tr>

                    <tr>
                        <td class="td_left"><label>性别</label></td>
                        <td class="td_right"><input type="radio" name="gender" value="male">男
                            <input type="radio" name="gender" value="female">女
                        </td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="birthday">出生日期</label></td>
                        <td class="td_right"><input type="date" name="birthday" id="birthday"></td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="checkCode">验证码</label></td>
                        <td class="td_right"><input type="text" name="checkCode" id="checkCode" placeholder="请输入验证码">
                            <img id="img_check" src="../img/verify_code.jpg" alt=""></td>
                    </tr>

                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="注册" id="btn_sub"></td>
                    </tr>
                </table>
            </form>
        </div>
    </div>

    <div class="rg_right">
        <p>已有帐号 <a href="#">立即登录</a></p>
    </div>

</div>
</body>
</html>
Test
 
 
原文地址:https://www.cnblogs.com/zhaochuming/p/12800824.html