Using CSS in HTML

Table of Contents

CSS is intended to be used with HTML (or SVG). There are three ways you can include the CSS in your HTML file:

  • Embed CSS inside the style attribute of HTML elements.
  • Embed CSS inside style HTML elements
  • Link to external CSS files.

Each of these options will be described below.

CSS in style Attributes

The simplest way to include CSS in an HTML page is to embed the CSS inside the style attribute of an HTML element. CSS properties embedded inside a style attribute are only applied to the HTML element into which they are embedded. Here is how that looks:

<div style="border: 1px solid black;"> Style This! </div>

This example inserts the CSS property border into the style attribute of the div element. The value of theborder property is 1px solid black which sets the border of the div element to a one pixel wide, black border.

If you need to set more than one CSS property inside the style element, separate the CSS properties with a semicolon, like this:

<div style="border: 1px solid black; font-size: 18px;"> Style This! </div>

  

This example sets both the border CSS property and the font-size CSS property.

CSS in style Elements

Another option of using CSS in HTML is to embed your CSS inside style HTML elements inside your HTML page. If you need to apply the same styles to multiple HTML elements, this is much easier than setting the styles inside each HTML element in their style attribute. Here is a CSS example using the style element:

<style>
    div {
        border: 1px solid black;
    }
</style>

<div> Style This! </div>
<div> Style This Too! </div>

  

This example shows a single style element which defines a CSS rule which is applied to all div elements. Thus, the CSS property inside the style element (the border property) is applied to both of the div elements in the example.

You can define more than one CSS rule inside the same style element. Here is an example:

<style>
    div {
        border: 1px solid black;
    }
    span {
        border: 1px solid blue;
    }

  

</style>

This example defines two CSS rules. The first CSS rule applied to all div elements, and the second CSS rule is applied to all span elements.

You can also embed more than one style element within the same HTML page. Here is an example:

<style>
    div {
        border: 1px solid black;
    }
</style>

<style>
    span {
        border: 1px solid blue;
    }
</style>

  

This example shows the CSS rules from the previous example embedded in their own style element.

The style elements can be embedded either inside the HTML head element or the body element. Here is an example:

<!DOCTYPE html>
<html>

<head>
    <style>
        div {
            border: 1px solid black;
        }
    </style>
</head>

<body>

    <style>
        span {
            border: 1px solid blue;
        }
    </style>

    <div> Style This! </div>
    <span> Style This Too! </span>

</body>
</html>

  

In this example the CSS rule for div elements is embedded inside its own style element inside the head HTML element, and the CSS rule for span elements is embdded inside its own style element inside the body HTML element.

CSS in External Files

If you need to apply the same CSS styling to multiple HTML pages, it is easier to move your CSS rules to a CSS file, and then link to that file from your HTML pages. You can reference an external style sheet in two ways:

  • Via a link element inside the head element.
  • Via an @import instruction inside a style element.

Both of these mechanisms are explained in the following sections.

The link Element

You reference an external CSS file using the link element (inside the head element). Here is an example:

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="my-css-file.css">
</head>

<body>
    <div> Style This! </div>
    <span> Style This Too! </span>

</body>
</html>

  

This example links to the external CSS file named my-css-file.css. This file has to be accessible online, and in this case it has to be located in the same directory as the HTML file. Thus, if the HTML file is located athttp://jenkov.com/my-website/my-html-file.html, then the CSS file should be available athttp://jenkov.com/my-website/my-css-file.css.

Actually, the href attribute of the link element can contain an absolute or relative URL. For more information about absolute and relative URLs, see HTML and URLs in my HTML4 tutorial.

If we move the CSS rules from the earlier examples to the my-cess-file.css file, then the contents would look like this:

div {
    border: 1px solid black;
}
span {
    border: 1px solid blue;
}

  

Notice that the CSS file has no style element. It only has the CSS rules themselves.

The @import Instruction

You can also import an external CSS file from inside a style element using the @import instruction. Here is a CSS @import example:

<style>
    @import url(‘http://jenkov.com/my-website/my-css-file.css‘);
</style>

  

This example will include the CSS file http://jenkov.com/my-website/my-css-file.css into the HTML page which contains that style element.

时间: 2024-10-15 09:14:26

Using CSS in HTML的相关文章

css中的px、em、rem 详解

概念介绍: 1.px (pixel,像素):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物理长度,需要指定精度DPI(Dots Per Inch,每英寸像素数),在扫描打印时一般都有DPI可选.Windows系统默认是96dpi,Apple系统默认是72dpi. 2.em(相对长度单位,相对于当前对象内文本的字体尺寸):是一个相对长度单位,最初是指字母M的宽度,故名em.现指的是字符宽度的倍数,用法类似百分比,如:0.8em, 1.2em,2em等.通常1em=16px

CSS样式的优先级

1.相同权值情况下,CSS样式的优先级总结来说,就是--就近原则(离被设置元素越近优先级别越高): 内联样式表(标签内部)> 嵌入样式表(当前文件中)> 外部样式表(外部文件中). 2.权值不同时,浏览器是根据权值来判断使用哪种css样式的,哪种样式权值高就使用哪种样式. 下面是权值的规则: 标签的权值为1,类选择符的权值为10,ID选择符的权值最高为100.例如下面的代码: p{color:red;} /*标签,权值为1*/ p span{color:green;} /*两个标签,权值为1+

CSS 之怀疑自己的审美 2 (Day50)

阅读目录 伪类 选择器的优先级 css 属性操作 一.伪类 anchor伪类:专用于控制链接的显示效果 ''' a:link(没有接触过的链接),用于定义了链接的常规状态. a:hover(鼠标放在链接上的状态),用于产生视觉效果. a:visited(访问过的链接),用于阅读文章,能清楚的判断已经访问过的链接. a:active(在链接上按下鼠标时的状态),用于表现鼠标按下时的链接状态. 伪类选择器 : 伪类指的是标签的不同状态: a ==> 点过状态 没有点过的状态 鼠标悬浮状态 激活状态

CSS颜色代码大全

CSS颜色代码大全 颜色代码表(一): EEEEEE FFCCFF FF66FF FF00FF DDDDDD FFCCCC FF66CC FF00CC CCCCCC FFCC99 FF6699 FF0099 BBBBBB FFCC66 FF6666 FF0066 AAAAAA FFCC33 FF6633 FF0033 999999 FFCC00 FF6600 FF0000 888888 CCCCFF CC66FF CC00FF 777777 CCCCCC CC66CC CC00CC 666666

如何使用Flexbox和CSS Grid,实现高效布局

CSS 浮动属性一直是网站上排列元素的主要方法之一,但是当实现复杂布局时,这种方法不总是那么理想.幸运的是,在现代网页设计时代,使用 Flexbox 和 CSS Grid 来对齐元素,变得相对容易起来. 使用 Flexbox 可以使元素对齐变得容易,因此 Flexbox 已经被广泛使用了. 同时,CSS Grid 布局也为网页设计行业带来了很大的便利.虽然 CSS Grid 布局未被广泛采用,但是浏览器逐渐开始增加对 CSS Grid 布局的支持. 虽然 Flexbox 和 CSS Grid 可

css基础

css绝对是一个能够写到爆炸的东西,so,机智的小北方才不会写各种css样式具体的效果,相比之下更推荐大家记一些常用的key,至于效果,每次用的时候百度下就可以了, css的作用是对符合条件的标签进行渲染,那么首先就要匹配到对应标签啦,我萌有三种基础的模式来匹配希望改变样式的标签 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8">

前端 css 垂直居中及自适应问题

此css作用为将下面div结构中的Container-fluid背景自适应屏幕,content自适应居中 1.Div结构 all Head Container-fluid Content Under <div id="all">   <div  class="head" style="height: 81px;width: 100%;min-width: 1000px;position: relative;">      

css遮罩层

父元素:position:fixed; 让子元素居中对齐:position:absolute;top:0;bottom:0;left:0;right:0;margin:auto; <style> .loading{width:100%;height:100%;position:fixed;top:0;left:0;z-index:100;background:#fff;} .loading .pic {width:50px;height:50px;background:url(images/l

CSS作用域问题

今天去长虹面试,面试官问了一个问题,就是给一个div在三个地方设置不同的background,最后div显示的颜色是哪一个?当时我第一次回答的是最后一个,但是后来又改口说是第一个,回来一验证,证明自己错了,今天就总结一下CSS样式的作用域问题吧. 首先对HTML引入样式的优先级排序,数字越大优先级越高#### 样式优先级1. 浏览器缺省设置2. 外部样式表3. 内部样式表(位于 <head> 标签内部)4. 内联样式(在 HTML 元素内部) ---#### 外部样式表>浏览器缺省设置H

CSS学习

CSS css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 存在方式有三种:元素内联.页面嵌入和外部引入,比较三种方式的优缺点. 语法:style = 'key1:value1;key2:value2;' 在标签中使用 style='xx:xxx;' 在页面中嵌入 < style type="text/css"> </style > 块 引入外部css文件 css写在head里面,style标签中写样式 ID用