CSS样式入门

CSS用来美化HTML标签

1,CSS应用方式

  • 直接写在标签上
  • 在head 标签中写style标签
  • 写到文件中
  1. 写在标签上
<div style="color: aqua;"> hello world</div>
  1. head标签中写style标签
<head>
    <title>hello</title>
    <style>
        .co{
            color: blueviolet;
        }
    </style>
</head>
<body>
    <div class="co">hi color</div>
</body>

3.写在文件中
暂不使用此方法

2,选择器

  • ID选择器
  • 类选择器
  • 标签选择器
<head>
    <title>样式</title>
    <style>
        .co1{
            color: red
        }
        #co2{
            color: gold;
        }
        li{
            color: pink;
        }
    </style>
</head>
<body>
    <div class=co1> cctv</div>
    <div id=co2> 中国黄金    </div>
    <ul>
        <li>长城汽车</li>
        <li>吉利汽车</li>
        <li>长安汽车</li>
    </ul>
</body>

在这里插入图片描述

  • 属性选择器
<head>
    <title>hello</title>
    <style>
        input[type='password']{
            border: 1px solid gold;
        }
        .co[xx="123"]{
            color: red;
        }
    </style>
</head>
<body>
    <input type="text">
    <input type="password">

    <div class="co" xx="123">hello</div>
    <div >world</div>

</body>
  • 后代选择器
<head>
    <title>hello</title>
    <style>
        .yy li{
            color: pink;
        }
        .yy a{
            color: blue;
        }
        .yy > a{
            color: green;
        }
    </style>
</head>
<body>
    <div class="yy">
        <a>百度</a>
        <div>
            <a>谷歌</a>
        </div>
        <ul>
            <li>facebook</li>
            <li>telegram</li>
        </ul>
    </div>
</body>

> a 表示只匹配儿子(a标签)

3.高度和宽度

<head>
    <title>hello</title>
    <style>
        .co{
        width: 100px;
        height: 10px; 
    }
    </style>
    
</head>
<body>
    <div class="co">
        world
    </div>
</body>

注意:

  • 宽度支持百分比, 但是高度不支持
  • 块级标签默认生效
  • 行内标签默认无效
<head>
    <title>hello</title>
    <style>
        .co{
        display: inline-block;
        width: 100px;
        height: 90px; 
        border: solid blue;
    }
    </style>
    
</head>
<body>
    <span class="co">
        world
    </span>
    <span> !!</span>
</body>

span换成则可以根据自身大小, 占据空间

<head>
    <title>hello</title>
    <style>
        .co{
        display: inline;
        width: 100px;
        height: 90px; 
        border: solid blue;
    }
    </style>
    
</head>
<body>
    <div class="co">
        world
    </div>
    <span> !!</span>
</body>

inline使得div具备块级标签的特性
block使得span 具备行内标签的特性

4.居中

水平方向居中 text-align: center;
垂直方向居中 line-height: <高度>px;

5.浮动

<head>
    <title>hello</title>
    
</head>
<body>
    <span ></span>
    <span style="float: right;"></span>
</body>

div是行级标签但是, 浮动起来就不一样了.

<head>
    <title>hello</title>
    <style>
        .co{
            float: left;
            width: 90px;
            height: 90px;
            border: 1px solid goldenrod;
        }
    </style>
</head>
<body>
    <div class="co"></div>
    <div class="co"></div>
    <div class="co"></div>
</body>


如果让标签浮动起来, 就会脱离文档流.
还会无法撑起父亲的宽度.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值