JavaWeb:(三)CSS

本文介绍了设置CSS样式的三种方法:HTML标签内、head标签内及外部CSS文件。详细讲解了CSS代码语法,包括选择器和声明的组成。接着阐述了三种选择器——标签选择器、id选择器和类选择器的用法,通过实例展示了它们在HTML元素中的应用。
摘要由CSDN通过智能技术生成

3.1 设置CSS样式的三种方式

3.1.1 在HTML标签内设置

仅对当前标签有效

<div style="border: 1px solid black;width: 100px; height: 100px;">&nbsp;</div>

image-20220610025728447

3.1.2 在head标签内设置

对当前页面有效

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        .one {
            border: 1px solid black;
            width: 100px;
            height: 100px;
            background-color: lightgreen;
            margin-top: 5px;
        }
    </style>
</head>
<body>

    <div style="border: 1px solid black;width: 100px; height: 100px;">&nbsp;</div>

    <div class="one">&nbsp;</div>
    <div class="one">&nbsp;</div>
    <div class="one">&nbsp;</div>

</body>

image-20220610025704437

3.1.3 引入外部CSS样式文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J1vF0Ozc-1654801430414)(../AppData/Roaming/Typora/typora-user-images/image-20220610030030663.png)]

编辑CSS文件

.two {
    border: 1px solid black;
    width: 100px;
    height: 100px;
    background-color: yellow;
    margin-top: 5px;
}

引入外部CSS文件

在需要使用这个CSS文件的HTML页面的head标签内加入:

<link rel="stylesheet" type="text/css" href="/aaa/pro01-HTML/style/example.css" />

于是下面HTML代码的显示效果是:

    <div class="two">&nbsp;</div>
    <div class="two">&nbsp;</div>
    <div class="two">&nbsp;</div>

image-20220610025612012

3.2 CSS代码语法

  • CSS样式由选择器和声明组成,而声明又由属性和值组成。
  • 属性和值之间用冒号隔开。
  • 多条声明之间用分号隔开。
  • 使用/* … */声明注释。

./images

3.3 CSS选择器

3.3.1 标签选择器

HTML代码:

<p>Hello, this is a p tag.</p>
<p>Hello, this is a p tag.</p>
<p>Hello, this is a p tag.</p>
<p>Hello, this is a p tag.</p>
<p>Hello, this is a p tag.</p>

CSS代码:

        p {
            color: blue;
            font-weight: bold;
        }

image-20220610025540767

3.3.2 id选择器

HTML代码:

    <p>Hello, this is a p tag.</p>
    <p>Hello, this is a p tag.</p>
    <p id="special">Hello, this is a p tag.</p>
    <p>Hello, this is a p tag.</p>
    <p>Hello, this is a p tag.</p>

CSS代码:

        #special {
            font-size: 20px;
            background-color: aqua;
        }

image-20220610025433079

3.3.3 类选择器

HTML代码:

    <div class="one">&nbsp;</div>
    <div class="one">&nbsp;</div>
    <div class="one">&nbsp;</div>

CSS代码:

        .one {
            border: 1px solid black;
            width: 100px;
            height: 100px;
            background-color: lightgreen;
            margin-top: 5px;
        }

image-20220610025511248

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值