首页 > 其他分享 >cs03 CSS Inclusion

cs03 CSS Inclusion

时间:2024-05-19 12:08:19浏览次数:24  
标签:style sheet Inclusion rules cs03 file document CSS

There are four ways to associate styles with your HTML document. Most commonly used methods are inline CSS and External CSS.

Embedded CSS - The <style> Element

You can put your CSS rules into an HTML document using the <style> element. This tag is placed inside the <head>...</head> tags. Rules defined using this syntax will be applied to all the elements available in the document. Here is the generic syntax −

 

<!DOCTYPE html>
<html>
   <head>
      <style type = "text/css" media = "all">
         body {
            background-color: linen;
         }
         h1 {
            color: maroon;
            margin-left: 40px;
         }
      </style>
   </head>   
   <body>
      <h1>This is a heading</h1>
      <p>This is a paragraph.</p>
   </body>
</html>

It will produce the following result −

Attributes

Attributes associated with <style> elements are −

AttributeValueDescription
type text/css Specifies the style sheet language as a content-type (MIME type). This is required attribute.
media

screen

tty

tv

projection

handheld

print

braille

aural

all

Specifies the device the document will be displayed on. Default value is all. This is an optional attribute.
 

Inline CSS - The style Attribute

You can use style attribute of any HTML element to define style rules. These rules will be applied to that element only. Here is the generic syntax −

<element style = "...style rules....">

Attributes

AttributeValueDescription
style style rules The value of style attribute is a combination of style declarations separated by semicolon (;).

Example

Following is the example of inline CSS based on the above syntax −

 

<html>
   <head>
   </head>

   <body>
      <h1 style = "color:#36C;"> 
         This is inline CSS 
      </h1>
   </body>
</html>

It will produce the following result −

External CSS - The <link> Element

The <link> element can be used to include an external stylesheet file in your HTML document.

An external style sheet is a separate text file with .css extension. You define all the Style rules within this text file and then you can include this file in any HTML document using <link> element.

Here is the generic syntax of including external CSS file −

<head>
   <link type = "text/css" href = "..." media = "..." />
</head>

Attributes

Attributes associated with <style> elements are −

AttributeValueDescription
type text css Specifies the style sheet language as a content-type (MIME type). This attribute is required.
href URL Specifies the style sheet file having Style rules. This attribute is a required.
media

screen

tty

tv

projection

handheld

print

braille

aural

all

Specifies the device the document will be displayed on. Default value is all. This is optional attribute.

Example

Consider a simple style sheet file with a name mystyle.css having the following rules −

h1, h2, h3 {
   color: #36C;
   font-weight: normal;
   letter-spacing: .4em;
   margin-bottom: 1em;
   text-transform: lowercase;
}

Now you can include this file mystyle.css in any HTML document as follows −

<head>
   <link type = "text/css" href = "mystyle.css" media = " all" />
</head>
 

Imported CSS - @import Rule

@import is used to import an external stylesheet in a manner similar to the <link> element. Here is the generic syntax of @import rule.

<head>
   @import "URL";
</head>

Here URL is the URL of the style sheet file having style rules. You can use another syntax as well −

<head>
   @import url("URL");
</head>

Example

Following is the example showing you how to import a style sheet file into HTML document −

<head>
   @import "mystyle.css";
</head>

CSS Rules Overriding

We have discussed four ways to include style sheet rules in a an HTML document. Here is the rule to override any Style Sheet Rule.

  • Any inline style sheet takes highest priority. So, it will override any rule defined in <style>...</style> tags or rules defined in any external style sheet file.

  • Any rule defined in <style>...</style> tags will override rules defined in any external style sheet file.

  • Any rule defined in external style sheet file takes lowest priority, and rules defined in this file will be applied only when above two rules are not applicable.

 

Handling old Browsers

There are still many old browsers who do not support CSS. So, we should take care while writing our Embedded CSS in an HTML document. The following snippet shows how you can use comment tags to hide CSS from older browsers −

<style type = "text/css">
   <!--
      body, td {
         color: blue;
      }
   -->
</style>

CSS Comments

Many times, you may need to put additional comments in your style sheet blocks. So, it is very easy to comment any part in style sheet. You can simple put your comments inside /*.....this is a comment in style sheet.....*/.

You can use /* ....*/ to comment multi-line blocks in similar way you do in C and C++ programming languages.

Example

 

<!DOCTYPE html>
<html>
   <head>
      <style>
         p {
            color: red;
            /* This is a single-line comment */
            text-align: center;
         }
         /* This is a multi-line comment */
      </style>
   </head>

   <body>
      <p>Hello World!</p>
   </body>
</html>

It will produce the following result −

 

标签:style,sheet,Inclusion,rules,cs03,file,document,CSS
From: https://www.cnblogs.com/emanlee/p/18199725

相关文章

  • cs04 CSS Measurement Units
    Valuesandunits,inCSS,aresignificantastheydeterminethesize,proportions,andpositioningofelementsonawebpage.Units,definethemeasurementsystemusedtospecifythevalues.CSSoffersanumberofdifferentunitsforexpressinglengthand......
  • HTML 31 - Layout using CSS
     NowweallhavelearnedvarioustechniquestodesignanHTMLlayoutincludingtablesandsemanticelements.Weareverymuchcomfortableandefficientatcreatinglayouts.Inthistutorial,wearegoingtolookathowCSSpropertieshelpinarrangingdi......
  • 博客园美化:CSS更改鼠标样式
    话不多说,先上效果图:实现这个效果真的好简单,哎,还搞了好久............
  • HTML 15 - CSS IDs
     HTML"id"isanattributeusedtouniquelyidentifyanelementwithinawebpage.ItservesasalabelforthatelementandenablesJavaScriptandCSStotargetitspecifically.Thisidentificationhelpsinapplyingcustomstyles,makinginter......
  • HTML 14 - CSS Classes
    InHTML,aclassisanattributethatcanbeappliedtooneormoreelementsandisusedtostyleandcategorizeelementsbasedoncommoncharacteristicsorpurpose.Classesallowsmultipleelementstosharethesamestylingrules.Byassigningthesamec......
  • css 让文字不被选中之-moz-user-select 属性介绍
    让文字不被选中,应该有一个css属性进行控制,结果网上查了下发现了-moz-user-select属性介绍 他在ie下也能选中文字,但是选中其他列表,不会选中文字,原来它是在不同div中,属于不同的范围,而同事是放在同一个table中,当然会选中。而在firefox下,文字不会被选中,查看googlecalender的css,原......
  • CSS动画-数字轮盘滚动效果实现(组件封装,快速使用)
    效果图:原理分析:这玩意就和垂直方向的轮播图差不多,只是把轮播的图换成数字 主要实现:父组件:父组件接收一个curNum属性,这个属性代表当前需要显示的数字。它将这个数字传递给子组件AnimateNum,以便子组件可以正确地显示和滚动数字。子组件一(AnimateNum):这个组件接收父组件传递......
  • DevExpress WinForms中文教程 - HTML & CSS支持的实战应用(一)
    DevExpressWinForms拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!在这篇文章中,我们将概述使用DevExpressWinFormsH......
  • CSS实现浮动效果
    一、浮动早期用于实现文字环绕图片(环绕布局),现在用于让元素并列布局(块元素并排)。浮动元素会脱离文档流,后边的元素会把空出来的位置补上去。但是又因为浮动元素的层级高,所以会将未浮动的元素进行覆盖,但是文字不会。float:none无浮动,left左浮动,right右浮动。1、css布局的三种机......
  • CSS cursor(鼠标样式)
    属性值示意图描述auto 默认值,由浏览器根据当前上下文确定要显示的光标样式default默认光标,不考虑上下文,通常是一个箭头none 不显示光标initial 将此属性设置为其默认值inherit 从父元素基础cursor属性的值context-menu表示上下文菜单可用......