整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

一篇文章带你了解CSS 文本样式

家好,我是IT共享者,人称皮皮。这篇文章我们来讲讲CSS的文本样式。

一、文本颜色Color

颜色属性被用来设置文字的颜色。

颜色是通过CSS最经常的指定:

  • 十六进制值 - 如"#FF0000"。
  • 一个RGB值 - "RGB(255,0,0)"。
  • 颜色的名称 - 如"红"。

一个网页的文本颜色是指在主体内的选择:

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=640, user-scalable=no">
        <title>项目</title>
        <style>
            body {
                color: blue;
            }


            h1 {
                color: #00ff00;
            }


            h2 {
                color: rgb(255, 0, 0);
            }
</style>
    </head>


    <body>
        <h2>hello world</h2>
        <h1>welcome to CaoZhou</h1>
    </body>


</html>

注:对于W3C标准的CSS:如果你定义了颜色属性,你还必须定义背景色属性。


二、属性

1. text-align 文本的对齐方式

文本排列属性是用来设置文本的水平对齐方式。

文本可居中或对齐到左或右,两端对齐。

当text-align设置为"justify",每一行被展开为宽度相等,左,右外边距是对齐(如杂志和报纸)。

<!doctype html>
<html lang="en">


    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            h1 {
                text-align: center;
            }


            p.date {
                text-align: right;
            }


            p.main {
                text-align: justify;
            }
</style>
    </head>


    <body>


        <p class="date">2015 年 3 月 14 号</p>
        <p class="main"> 从前有个书生,和未婚妻约好在某年某月某日结婚。到那一天,未婚妻却嫁给了别人。书生受此打击, 一病不起。  这时,路过一游方僧人,从怀里摸出一面镜子叫书生看。书生看到茫茫大海,一名遇害的女子一丝不挂地躺在海滩上。路过一人, 看一眼,摇摇头,走了。又路过一人,将衣服脱下,给女尸盖上,走了。再路过一人,过去,挖个坑,小心翼翼把尸体掩埋了。  僧人解释道, 那具海滩上的女尸,就是你未婚妻的前世。你是第二个路过的人,曾给过他一件衣服。她今生和你相恋,只为还你一个情。但是她最终要报答一生一世的人,是最后那个把她掩埋的人,那人就是他现在的丈夫。书生大悟,病愈。


        </p>
        <p><b>注意:</b> 重置浏览器窗口大小查看 "justify" 是如何工作的。</p>
    </body>


</html>

2. text-decoration文本修饰

text-decoration 属性用来设置或删除文本的装饰。

从设计的角度看 text-decoration属性主要是用来删除链接的下划线:

<!doctype html>
<html lang="en">


    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            .none {}


            .del {
                text-decoration: none;
            }
</style>
    </head>


    <body>
        <p>原来的样子</p>
        <a href="#" class="none">wwwwwwwwwwwwwwwwww</a>
        <p>去掉下划线</p>
        <a href="#" class="del">wwwwwwwwwwwwwwwwwwwww</a>
    </body>


</html>

也可以这样装饰文字:

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=640, user-scalable=no">
        <title>项目</title>
        <style>
            h1 {
                text-decoration: overline;
            }


            h2 {
                text-decoration: line-through;
            }


            h3 {
                text-decoration: underline;
            }
</style>
    </head>


    <body>
        <h1>This is heading 1</h1>
        <h2>This is heading 2</h2>
        <h3>This is heading 3</h3>
    </body>


</html>

注:不建议强调指出不是链接的文本,因为这常常混淆用户。


3. text-transform文本转换

text-transform文本转换属性是用来指定在一个文本中的大写和小写字母。

  • uppercase:转换为全部大写。
  • lowercase:转换为全部小写。
  • capitalize :每个单词的首字母大写。
<!DOCTYPE html>
<html>


    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=640, user-scalable=no">
        <title>项目</title>
        <style>
            p.uppercase {
                text-transform: uppercase;
            }


            p.lowercase {
                text-transform: lowercase;
            }


            p.capitalize {
                text-transform: capitalize;
            }
</style>
    </head>


    <body>
        <p class="uppercase">This is some text.</p>
        <p class="lowercase">This is some text.</p>
        <p class="capitalize">This is some text.</p>
    </body>


</html>

4. text-indent文本缩进

text-indent文本缩进属性是用来指定文本的第一行的缩进。

p {text-indent:50px;}

5. letter-spacing 设置字符间距

增加或减少字符之间的空间。

<style>
     h1 {
       letter-spacing:2px;
}
      h2 {
        letter-spacing:-3px;
}
</style>

6. line-height设置行高

指定在一个段落中行之间的空间。

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=640, user-scalable=no">
        <title>项目</title>
        <style>
            p.small {
                line-height: 70%;
            }


            p.big {
                line-height: 200%;
            }
</style>
    </head>


    <body>
        <p>
            This is a paragraph with a standard line-height.<br> This is a paragraph with a standard line-height.<br> The default line height in most browsers is about 110% to 120%.<br>
        </p>


        <p class="small">
            This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br>
        </p>


        <p class="big">
            This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br>
        </p>


    </body>


</html>

7. word-spacing 设置字间距

增加一个段落中的单词之间的空白空间。

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=640, user-scalable=no">
        <title>项目</title>
        <style type="text/css">
            p {
                word-spacing: 30px;
            }
</style>
    </head>


    <body>


        <p>
            This is some text. This is some text.
        </p>


    </body>


</html>

8. vertical-align 设置元垂直居中

设置文本的垂直对齐图像。

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=640, user-scalable=no">
        <title>项目</title>
        <style>
            img{
                width: 200px;
                height: 100px;
            }
            img.top {
                vertical-align: text-top;


            }


            img.bottom {
                vertical-align: text-bottom;


            }
</style>
    </head>


    <body>
        <p>An <img src="img/logo.png"  /> image with a default alignment.</p>
        <p>An <img class="top" src="img/logo.png" /> image with a text-top alignment.</p>
        <p>An <img class="bottom" src="img/logo.png" /> image with a text-bottom alignment.</p>
    </body>


</html>

9. text-shadow 设置文本阴影

设置文本阴影。

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=640, user-scalable=no">
        <title>项目</title>
        <style>
         h1{
            text-shadow: 2px 2px #FF0000;
     }
</style>
    </head>


    <body>
    <h1>Text-shadow effect</h1>
    </body>


</html>

三、总结

本文主要介绍了CSS文本样式实际应用中应该如何去操作,通过讲解文本中对应的属性去改变文本的表现形式。使用丰富的效果图的展示,能够更直观的看到运行的效果,能够更好的理解。使用Html语言,代码结构更佳的清晰,能够帮助你更好的学习。

这里是云端源想IT,帮你轻松学IT”

嗨~ 今天的你过得还好吗?

睡眠等同于希望

每次醒来都是一个新的开始

一个新的希望


- 2024.03.22 -


在Web开发的世界中,CSS(层叠样式表)是构建视觉吸引力和定义网页布局的不可或缺的工具。

掌握如何恰当地引入CSS样式以及理解它们的优先级规则,对于前端开发者来说至关重要。今天,我们就来深入探讨CSS的四种引入方式,以及选择器的优先级之谜,了解常用的CSS样式及使用方法!



一、CSS四种样式引入方式

CSS(层叠样式表)为网页提供了丰富的样式定义,允许开发者通过多种方式将样式应用到HTML文档中。以下是四种主要的CSS引入方式:


1.1 行内样式

这是最直接也最简单的方法,通过在HTML元素的style属性中直接编写CSS规则。

示例:

<p style="color: red; font-size: 20px;">这是一段红色的文字。</p>


这种方式的优点是简单快捷,但缺点是它使得HTML代码与样式混合,不够纯净,且不利于样式的复用和维护。


1.2 内嵌样式

在一个HTML文档中使用<style>标签将CSS规则嵌入到HTML的head部分。这种方式适用于定义特定于某一页面的样式。

示例:

<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>



1.3 外部样式

这是最常用的方法,它通过<link>标签将外部的CSS文件链接到HTML文档中。这种方法的优势在于可以在多个页面间共享同一个样式文件,有助于保持代码的整洁和一致性。

示例:

<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
其中,mystyle.css的内容可能如下:
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}


1.4 导入样式

使用@import语句在CSS文件中导入另一个CSS文件。尽管这种方法可以分离样式表,但它通常不被推荐使用,因为其加载时序可能会影响页面渲染效率。

示例:

@import url('https://fonts.googleapis.com/css?family=Roboto');
body {
font-family: 'Roboto', sans-serif;
}


1.5 样式单优先级

作用域范围:外部样式表>内部样式表>行内样式表


优先级:

  • 行内样式表>内部样式表>外部样式表
  • 相同的样式作用在同一个标签上:就近原则;不同的样式作用在同一个标签上:叠加。
  • 加载外部样式表或者内部样式表时候,需要注意加载顺序:加载html文件是从上向下加载的,也就是后面加载的样式会覆盖前面的样式。


二、CSS常用样式

2.1 字体样式

normal - 文字正常显示

italic - 文本以斜体显示

oblique - 文本为“倾斜”(倾斜与斜体非常相似,但支持较少)

font-weight 属性指定字体的粗细

示例:

<!DOCTYPE html>
<html>
<head>
<style>
.sp1{
color: darkorange;
font-size: 20px;
font-weight: bolder; /* bolder 字体是否加粗*/

font-style: italic; /* italic 字体是否倾斜*/

font-family: "宋体"; /* 设置字体样式*/
}

.sp2{
/* 简写 */
/* 顺序不能能变:style-weigth-size-family */
font:italic bolder 15px 宋体 ;
color:rgb(28, 235, 97);
}
</style>
<body>
<span>
编程学习,从云端源想开始!
</span><br>
<span>
让知识触手可及
</span>
<p>让知识触手可及</p>
</body>
</html>



2.2 文本样式

color: 字体颜色

text-align: center; - - 文本对齐方式

text-decoration:none; - - 文本的线

text-shadow: pink 5px 5px 2px; - - 文本的阴影 【阴影颜色-水平方向的偏移量-垂直方向的偏移量-模糊距离】

line-height: - - 行高 (文本在标签内所占的高度)

width:

height:

border: 1px #ffffff solid; - - 盒子边框【边框粗细-颜色-边框线样式】

示例:

<style>
.p{
color: rgb(0, 255, 21); /* 字体颜色 */
text-align: center; /* 文本对齐方式 */
text-decoration:none; /* 文本的线 */
text-shadow: pink 5px 5px 2px; /* 文本的阴影 【阴影颜色-水平方向的偏移量-垂直方向的偏移量-模糊距离】*/
line-height: 400px; /* 行高 (文本在标签内所占的高度)*/
width: 400px;
height: 300px;
border: 1px rgb(76, 14, 223) solid; /* 盒子边框【边框粗细-颜色-边框线样式】 */
}
</style>
</head>

<body>
<p>欢迎来到云端源想!</p>
<a href="https://www.baidu.com"></a>
</body>


2.3 背景样式

width: 500px;

height: 1200px;

background-color: pink; - - 背景颜色

background-image: url(…/img/background.jpg); - - 背景图片

background-repeat: no-repeat; - - 背景图片是否平铺

background-position: left top; - - 指定背景图片的位置

background-attachment: fixed; - - 背景图片是否随着标签滚动 【fixed-固定 scroll-滚动】

示例:

<style>
.d{
width: 500px;
height: 1200px;
background-color: pink; /* 背景颜色 */
background-image: url(../img/background.jpg); /* 背景图片 */
background-repeat: no-repeat; /* 背景图片是否平铺 */
background-position: left top; /* 指定背景图片的位置 */
background-attachment: fixed; /* 背景图片是否随着标签滚动 【fixed-固定 scroll-滚动】 */
}
</style>
</head>
<body>
<div>

</div>


2.4 列表样式

<!DOCTYPE html>
<html>
<head>
<style>
li{
background-color: lemonchiffon;
/*列表样式:常用取值:none-无样式 square-正方形 circle-空心圆 decimal-数字*/
list-style-type: circle;
/*列表样式为自定义图片*/
list-style-image: url(../img/2.jpg);
/*列表样式的放置位置*/
list-style-position: inside;
/*列表样式缩写*/
list-style: square url(../img/2.jpg) inside;
/*常用的列表样式*/
list-style: none;
}
</style>
</head>
<body>
<ul>
<li>列表项1</li>
<li>列表项2</li>
<li>列表项3</li>
</ul>
</body>
</html>


2.5 边框样式

<!DOCTYPE html>
<html>
<head>
<style>
.border{
/*边框宽度*/
border-width: 2px;
/*边框颜色*/
border-color: red;
/*边框样式:solid 实线 dotted 点线 dashed 虚线*/
border-style: dashed;
/*边框样式缩写:样式 颜色 宽度*/
border:solid green 5px;
/*边框可以为4个方向分别设置*/
border-top: dashed black 4px;
border-right: dashed #FF00FF 4px;
border-bottom: dotted darkblue 4px;
border-left: solid fuchsia 5px;
/*没有边框*/
border: none;
/*常用的细边框样式*/
border: solid 1px #ccc;
}
</style>
</head>
<body>
<div class="border">这是一个带有边框的元素</div>
</body>
</html>


2.6 盒子模型

所有的html元素可以看做是盒子,在css中,"box model"是用来设计和布局时使用。

CSS盒子模型本质是一个盒子,封装周围的html元素,它包括:边框、边距、填充、实际内容。

盒子模型允许我们在其他元素和周围元素边框之间的空间放置元素。

  • margin:外边距,清除边框外区域,外边距是透明的。
  • border:边框,围绕在内边距和内容外的边框。
  • padding:内边距,清除内容周围区域内边距是透明的。
  • content:内容,显示文字和图像。

想要快速入门前端开发吗?推荐一个前端开发基础课程,这个老师讲的特别好,零基础学习无压力,知识点结合代码,边学边练,可以免费试看试学,还有各种辅助工具和资料,非常适合新手!点这里前往学习哦!云端源想

示例:

<head>
<meta charset="UTF-8">
<title></title>
<style>
/* border:边框,分4个方向,同理margin、padding也分为四个方向
* margin:元素与元素之间对的距离
* padding:内容与边框之间的距离
* 设置的时候顺序:上 右 下 左
*/
.div{
border: solid red 10px;
/*四个方向上的元素与元素之间的距离都是50px*/
margin: 50px;
/*两个值的时候:第一个参数表示上下距离都是50px,第二个参数表示左右距离都是100px*/
margin: 50px 100px;
padding: 50px;
/*
一个元素真正的宽度=width+左右padding值+左右的border值
一个元素的真正高度=height+上下的padding值+上下的border值
* */
}
</style>
</head>
<body>
<div>111111111112222222222223333333333333333</div>
</body>

1)盒子的宽高

元素的实际宽度和高度:

  • 计算一个元素在实际在页面占据的总宽度=元素宽度+左填充+右填充+左边框+右边框+左边距+右边距
  • 元素实际在页面占据的总高度=元素高度+顶部填充+底部填充+上边框+下边框+上边距+下边距



2)设置宽度=元素实际宽度,box-sizing属性。

<head>
<meta charset="UTF-8">
<title></title>
<style>
/* box-sizing:确认元素的大小
content-box: 实际宽度=width+左右的psdding值+上下的border值
实际高度=height+上下的padding值+上下的border值
border-box:实际宽度=width;实际高度=height
padding和border不会影响元素的实际宽高
* */
.box{
width: 100px;
height: 200px;
border: 5px solid;
padding: 5px;
box-sizing: content-box;
}
</style>
</head>
<body>
<div>你好中国</div>
</body>


CSS的世界博大精深,以上只是冰山一角,希望通过这些基础的常用样式可以帮助你快速进入CSS世界的大门。


掌握CSS的引入方式和选择器优先级是构建高效、可维护网站的关键。通过这些知识,你可以更好地管理和优化你的样式代码,创造出既美观又专业的网页设计。现在,准备好迈入CSS的世界,开启你的创意之旅吧!


我们下期再见!


END

文案编辑|云端学长

文案配图|云端学长

内容由:云端源想分享

择器

  • 子孙后代选择器: 通过元素之间的层级关系选择元素

格式: body div div p{样式代码} 匹配body里面的div里面的div里面的所有p标签(包括后代)

  • 子元素选择器: 通过元素之间的层级关系选择元素

格式: body>div>div>p{样式代码} 匹配body里面的div里面的div里面的所有p子元素(不包含后代)

  • 伪类选择器: 选择的是元素的状态, 元素有哪些状态? 【包括】:未访问状态,访问过状态,悬停状态,点击状态

格式: a:link/visited/hover/active:{样式代码}

<head>
    <meta charset="UTF-8">
    <title>Title</title>
	<!--样式-->
<style>
    #l2{color: green}
    .c1{color: yellow}
    input[type="text"],body>p{background-color: red}
    /*子元素选择器*/
    body>div>div>p{color: pink}
		/*子孙后代选择器*/
    body div div p{background-color: yellow}
    /*伪类选择器*/
    a:link{color: red;}/*未访问*/
    a:visited{color: pink}/*访问过*/
    a:hover{color: green}/*悬停*/
    a:active{color: yellow}/*点击*/
</style>
</head>
<body>
<ul>
    <li>刘备</li><li id="l2">关羽</li><li class="c1">张飞</li>
</ul>
<p>香蕉</p><p class="c1">苹果</p>
<input type="text">
<input type="password">
<div>
    <p>p1</p>
    <div><p>p2</p></div>
    <div><div><p>p3</p></div></div>
</div>
<a href="http://www.celinfcom">注释助手</a>

颜色赋值

三原色: 红绿蓝 ,red green blue rgb ,每个颜色的取值范围0-255 颜色赋值的几种方式:

  • 颜色单词: 常见颜色单词都可以使用
  • 6位16进制赋值: #ff0000 3位16进制赋值: #f00
  • 3位10进制赋值: rgb(255,0,0)
  • 4位10进制赋值: rgba(255,0,0,0~1) a=alpha代表透明度
<style>
      h1{
        /*color: #ff0000;*/
        /* color: #f00;*/
        /*color: rgb(255,0,0);*/
        color: rgba(255,0,0,0.3);
      }
    #d2{
      width: 200px;
      height: 200px;
      background-color: pink;
      /*设置背景图片*/
      background-image: url("../b.jpg");
      /*设置背景图片尺寸*/
      background-size: 100px 100px;
      /*禁止重复*/
      background-repeat: no-repeat;
      /*控制位置:横向 纵向*/
      /*background-position: 50px 100px;*/
      background-position: 50% 50%;
    }
</style>
<body>
<div id="d2"></div>
<h1>颜色测试</h1>
</body>

背景图片

  • background-image:url("路径") 设置背景图片
  • background-size:100px 200px 设置背景图片尺寸
  • background-repeat:no-repeat; 禁止重复
  • background-position: 横向 纵向; 设置背景图片的位置,两种方式: ①像素 ②百分比
<style>
    #d1{
      width: 611px;
      height: 376px;
      background-color: #e8e8e8;
      background-image: url("http://celinf.org/itemCat/study_computer.png");
      background-repeat: no-repeat;
      background-position: 90% 90%;
      background-size: 318px 319px;
    }
</style>
<body>
<div id="d1"></div>
</body>

文本和字体相关样式

  • text-align:left/right/center; 文本水平对齐方式
  • line-height:20px; 设置行高, 多行文本时可以控制行间距, 单行文本时可以控制垂直居中(因为文本默认是在当前所在行内居中)
  • text-decoration:overline上划线/underline下划线/line-through删除线/none去掉文本修饰
  • text-shadow:颜色 x偏移值 y偏移值 浓度; 阴影
  • font-size:20px; 设置字体大小
  • font-weight:bold加粗/normal去掉加粗
  • font-style:italic; 设置斜体
  • font-family: xxx,xxx,xxx; 设置字体
  • font:20px xxx,xxx,xxx; 这只字体大小+字体
<style>
      div{
        width: 200px;
        height: 50px;
        border: 1px solid red;
        /*水平对齐方式*/
        text-align: center;
        /*行高*/
        line-height: 50px;
        /*文本修饰 overline上划线underline下划线 line-through删除线
                none去掉文本修饰*/
        text-decoration: line-through;
        /*文本阴影:颜色 x偏移值 y偏移值  浓度*/
        text-shadow: red -15px -15px 5px;
        /*字体大小*/
        font-size: 20px;
        /*字体加粗 bold加粗  normal去掉加粗*/
        font-weight: bold;
      }
    a{
      text-decoration: none;/*去掉自带下划线*/
    }
    h3{
      font-weight: normal;/*去掉自带加粗*/
      /*设置斜体*/
      font-style: italic;
      /*设置字体*/
      /*font-family: cursive;*/
      font: 30px cursive;
    }
    </style>
<body>
<h3>这是个h3</h3>
<a href="">超链接</a>
<div>文本和字体测试</div>
</body>

元素的显示方式display

  • block: 块级元素的默认值, 特点: 独占一行 可以修改宽高, 包括: h1-h6 , p, div
  • inline: 行内元素的默认值, 特点: 共占一行 不可以修改宽高, 包括: span, b,i,s,u,超链接a
  • inline-block:行内块元素默认值, 特点: 共占一行 并且可以修改宽高, 包括: img,input
  • none: 隐藏元素
  • 行内元素不能直接修改宽高, 如必须修改则先将元素的显示方式改成blockinline-block
<style>
      div{
        width: 100px;
        height: 100px;
        border: 1px solid red;
      }
    span{
      border: 1px solid blue;
      /*行内元素不能修改宽高*/
      width: 100px;
      height: 100px;
      /*把行内元素改成了块级元素或行内块元素都可以修改宽高 */
      display: inline-block;
    }
    img{
      width: 100px;
      height: 100px;
      display: none;/*隐藏元素*/
    }
    a{
      width: 132px;
      height: 40px;
      background-color: #0aa1ed;
      /*行内元素不能修改宽高*/
      display: block;
      text-align: center;
      line-height: 40px;
      color: white;
      text-decoration: none;
      font-size: 20px;
      /*圆角 值越大越圆*/
      border-radius: 3px;
}
</style>
<body>
<a href="">查看详情</a>
<img src="../b.jpg" alt="">
<img src="../b.jpg" alt="">
<img src="../b.jpg" alt="">
<div>div1</div>
<div>div2</div>
<div>div3</div>
<span>span1</span>
<span>span2</span>
<span>span3</span>
</body>

盒子模型

盒子模型用来控制元素的显示效果包括: 元素内容content+外边距margin+边框border+内边距padding

  • 元素内容content:控制元素的显示尺寸
  • 外边距margin:控制元素的显示位置
  • 边框border:控制边框效果
  • 内边距padding:控制元素内容的位置

border边框效果

盒子模型之内容content

  • 包括:width和height
  • 赋值方式有两种:①像素 ②上级元素的百分比
  • 行内元素不能直接修改宽高

盒子模型之外边距margin

  • 作用: 控制元素的显示位置
  • 赋值方式:
  1. margin-left/right/top/bottom:10px; 单独某个方向赋值
  2. margin:10px; 四个方向赋值
  3. margin:10px 20px; 上下10 左右20
  4. margin:10px 20px 30px 40px; 上右下左 顺时针赋值
  • 行内元素上下外边距无效
  • 上下相邻彼此添加外边距 取最大值
  • 左右相邻彼此添加外边距 两者相加
  • 粘连问题: 当元素的上边缘和上级元素的上边缘重叠时,给元素添加上外边距会出现粘连问题,给上级元素添加overflow:hidden解决

盒子模型之边框border

赋值方式:

  1. border:1px solid red; 给四个方向添加边框
  2. border-left/right/top/bottom:1px solid red; 单独给某个方向添加边框
  3. border-radius:10px; 值越大越圆 当值超过宽高的一半时为正圆(前提是正方形)
<style>
      #d1{
        width: 100px;
        height: 100px;
        border:1px solid red;
        /*margin-left: 100px;
                margin-top: 100px;*/
        /*margin-bottom: 50px;*/
        margin: 10px 20px 30px 40px;
      }
    #d2{
      width: 100px;height: 100px;border:1px solid red;
      /*上下相邻彼此添加外边距 取最大值*/
      margin-top: 100px;
    }
    #s1{
      /*行内元素上下外边距无效*/
      margin-right: 100px;
    }
    #s2{
      /*左右相邻彼此添加外边距 两者相加*/
      margin-left: 50px;
    }
    #big{
      width: 200px;height: 200px;background-color: green;
      overflow: hidden;/*解决粘连问题*/
    }
    #big>div{
      width: 50px;height: 50px;background-color: red;
      margin-left: 50px;
      /*当元素的上边缘和上级元素的上边缘重叠时,给元素添加上外边距会出现粘连问题*/
      margin-top: 50px;
    }
    #border_div{
      width: 400px;
      height: 200px;
      border: 10px solid blue;
      /*设置圆角*/
      border-radius: 200px;
    }
</style>

<body>
<div id="border_div"></div>
<div id="big">
    <div></div>
</div>
<span id="s1">span1</span><span id="s2">span2</span>
<div id="d1">外边距测试</div>
<div id="d2">div2</div>
</body>

盒子模型之内边距padding

  • 作用: 控制元素内容的位置
  • 赋值方式: 和外边距类似
  1. padding-left/right/top/bottom:10px; 单独某个方向赋值
  2. padding:10px; 四个方向赋值
  3. padding:10px 20px; 上下和 左右赋值
  4. padding:10px 20px 30px 40px; 上右下左顺时针赋值
  • 给元素添加内边距会影响元素的显示宽高
<style>
      div{
        width: 150px;
        height: 150px;
        border:1px solid red;
        /*内边距会影响元素的宽高*/
        padding-top: 50px;
        padding-left: 50px;
      }
</style>
<body>
<div>内边距</div>
</body>

CSS的三大特性

  • 继承: 元素可以继承上级元素文本和字体相关的样式,部分标签自带的效果不受继承影响, 比如超链接字体颜色
  • 层叠:多个选择器可能选择到同一个元素,如果添加的样式不同则全部层叠有效,如果作用的样式相同 则由优先级决定哪个生效
  • 优先级: 指CSS中的选择器具有优先级, 作用范围越小优先级越高, !important>id>class>标签名>继承(属于间接选中)
<style>
      #d1{
        color: red;
      }
    div{
      /*!important作用是提升优先级*/
      color: blue !important;
    }
</style>
<body>
<div id="d1">
    <p>这是个p标签</p>
    <span>这是div里面的span</span>
    <a href="">超链接</a>
</div>
<span>这是div外面的span</span>
</body>

综合性练习【Demo】

<style>
      body{
        font: 12px "simhei", Arial, Helvetica, sans-serif;
        color: #666;width: 1000px;
      }
    #d1{
      width: 611px;height: 376px;
      background-color: #e8e8e8;
      background-image: url("http://celinf.org/study_computer1.png");
      background-size: 318px 319px;
      background-repeat: no-repeat;
      background-position: 90% 90%;
      overflow: hidden; display: inline-block;
    }
    #d2{margin: 68px 0 0 36px; width: 245px;height: 232px;  }
    #d3{
      width: 375px;height: 376px;
      background-color: #e8e8e8; overflow: hidden;
      background-image: url("http://celinf.org/study_computer.png");
      background-repeat: no-repeat;
      background-size: 292px 232px;
      background-position: 85% 85%; display: inline-block;
    }
    div>div{width: 253px; height: 232px;margin: 39px 0 0 25px; }
    .title_p{
      color: #333333;font-size: 32px;
      margin-bottom: 12px;
      font-weight: lighter;
    }
    .intro_p{font-size: 12px;font-weight: lighter;margin-bottom: 24px;}
    .price_p{
      font-size: 24px;color: #0aa1ed;
      font-weight: bold;margin-bottom: 12px;
    }
    a{
      display: block; background-color: #0aa1ed;
      color: white;
      width: 132px;height: 40px;
      text-align: center; line-height: 40px;
      font-size: 20px; text-decoration: none;
      border-radius: 2px;
    }
</style>
<body>
<div id="d1">
    <div id="d2">
        <p class="title_p">灵越 燃7000系列</p>
        <p class="intro_p">
            酷睿双核i5处理器|256GB SSD| 8GB内存<br>
            英特尔HD显卡620含共享显卡内存
        </p>
        <p class="price_p">¥4999.00</p>
        <a href="#">查看详情</a>
    </div>
</div>
</div>
<div id="d3">
    <div>
        <p class="title_p">颜值 框不住</p>
        <p class="intro_p">
            酷睿双核i5处理器|256GB SSD| 8GB内存
            <br>
            英特尔HD显卡620含共享显卡内存
        </p>
        <p class="price_p">¥6888.00</p>
        <a href="#">查看详情</a>
    </div>
</div>
</body>

学习记录,如有侵权请联系删除