Home > Web Front-end > CSS Tutorial > How to implement CSS and share sample code of selectors

How to implement CSS and share sample code of selectors

黄舟
Release: 2017-06-04 11:45:25
Original
1517 people have browsed it

CSS implementation and selector

Contents of this lesson:

1. Four ways to implement CSS

1, in each htmltag There is a style style attribute, and the value of this attribute is the css code. (For a tag)
2, use style tag . Generally defined in the head tag. (For multiple identical tags)
3, when multiple pages use the same style, the style can be individually packaged as a CSS fileImport

4, link a CSS file through the link tag in the HTML header tag
Tag selector p{}
Class selector .haha
id selector #qq (The value of id is in It is unique in the page, because in addition to being used by CSS, this attribute is also used by javascript)
The class selector is used the same as the id selector, and the priority of the id selectorHigher than the class selector, The lower the priority, the higher

3. CSS Notes

css: Separate web page content and display style, improved display function.

CSS cascadingCascading style sheets

Separate the styles in the web page and completely control them by CSS to enhance style reusability and scalability.

Format: selector {Attribute name: attribute value; Attribute name: attribute value;...}

4 ways to combine CSS with HTML:

1. Each Each HTML tag has the style attribute

2. When there are multiple tags on the page with the same style, you can define the style tag to encapsulate the style for reuse

       <style type=”text/css”>css代码</style>
Copy after login

3 . When multiple pages use the same style, the style can be individually packaged as a CSS file and imported

       <style type=”text/css”>@import url(“1.css”);</style>
Copy after login

4. Link a CSS file through the link tag in the HTML header tag

       <link rel=”stylesheet” href=”1.css” media属性可选,默认设备屏幕/>
Copy after login

Tips: In order to improve the reusability and scalability of styles, define and encapsulate multiple styles separately into CSS files, such as p.css, p.css... In a total CSS file, use import to import These CSS files can then be imported into the HTML page using the link tag.

Priority: Proximity principle The style attribute set on the label can override other styles

Selector:

1. Label selector : Each HTML tag name is a selector

2. Class selector: The class attribute in the tag is specified. When defining the style, add a dot js for reference. Use className when

3. ID selector: The id attribute of the tag should be as unique as possible to facilitate JavaScript to obtain the element

4. Extended selector:

a , Association selector: tag in the label table p represents the p area in table

b. combination selector: multiple selections Devices separated by commas

c. For element selector: element’s state such as hyperlink’s default state, click state, hover state Wait

a:link a:visited a:hover a:active LVHA order

删除超链接默认下划线:text-decoration:none

p:first-letter p:first-line focus:IE6不支持

CSS滤镜:通过一些代码丰富了的样式

网页设计时,p+CSS

p和P标签都属于行级区域,回车效果,SPAN标签为块级区域,无回车效果

可加border、color等属性,P标签中不要嵌套p标签

四、代码一

<!--
本课内容:
一、实现CSS四种方式
1,每个html标签中都有一个style样式属性,该属性的值就是css代码。(针对一个标签)
2,使用style标签的方式。 一般都定义在head标签中。(针对多个相同的标签)
3,当多个页面使用相同样式时,可将样式单独封装为CSS文件导入
    <style type=”text/css”>@import url(“1.css”);</style>
4,通过HTML头标签中的link标签链接一个CSS文件
    <link rel=”stylesheet” href=”1.css” media属性可选,默认设备屏幕/>
二、选择器
标签选择器 div{}
类选择器 .haha
id选择器 #qq  (id的取值在页面中是唯一的,因为该属性除了给CSS使用,还要给javascript使用)
类选择器和id选择器用法一样,id选择器的优先级比类选择器高,优先级越少越高 
 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- type指定下面的css以什么方式解析 -->
<!--指定这两个样式都作用与div,所以用div后的大括号括起来-->
<!-- css一般放在头部,因为要预先加载,所以每个出现div的位置都已经被换了样式 -->
<!--
第二种方式:
标签选择器
div{
        background-color: #000;
        color: #FFF    
    }

 -->
<!-- @IMPORT url("div.css");第三种方式 -->
<!-- 第四种 link -->
<link rel="stylesheet" href="div.css" type="text.css">
<style type="text/css">
/*
    @IMPORT url(1.css);
    @IMPORT url(div.css);
@IMPORT url(span.css);
*/
div.haha{
    background-color: #000;
}
</style>
<!-- 类选择器div.haha 优先级比标签选择器高 约少数,优先级越高 -->
<!-- 类选择器不仅可以相同标签的少部分做,还可以对不同的标签做 -->
<!-- div.haha中的div可以不写,.haha,那就所有的有哈哈类的都变了 -->
<!-- 按钮加多套这样的.haha实现动态样式 -->
</head>
<body>
    <!--
    css和html相结合的第一种方式
    1,每个html标签中都有一个style样式属性,该属性的值就是css代码。(针对一个标签)
    2,使用style标签的方式。 一般都定义在head标签中。(针对多个相同的标签)
    3,当多个页面使用相同样式时,可将样式单独封装为CSS文件导入
    <style type=”text/css”>@import url(“1.css”);</style>
    4,通过HTML头标签中的link标签链接一个CSS文件
    <link rel=”stylesheet” href=”1.css” media属性可选,默认设备屏幕/>

     -->
     <!--  -->


    <!-- 样式的重叠 重复样式一最后加载为主,不重复样式层叠,其实都是层叠 -->
    <!-- color: #F00 为颜色的缩写 -->
    <div style ="color: #F00">这是一个div区域1</div>
    <div class="haha">这是一个div区域2</div>
    <span>span区域1</span>
    <span>span区域2</span>
    <p>这是一个段落1</p>
    <p>这是一个段落2</p>

    
</body>
</html>
Copy after login

五、代码二


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 
<html xmlns=" 
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title><!--link rel="stylesheet" href="1.css" type="text/css" /-->
<style type="text/css">/*@import url(1.css);

p{
        background-color:#09F;
        color:#FFF;
}
.haha{
    background-color:#FF3;
    color:#0C0;
}*//*预定样式,实现动态的加载。.hehe{
    background-color:#C93;
    color:#00F;
}*//*#qq{通常ID的取值在页面中是唯一的,因为该属性除了给css使用,还可以被js使用。id通常都是为了去标示页面中一些特定区域使用的。
    background-color:#000;
    color:#FFF;
}*//*span b{关联选择器 选择器中的选择器
    background-color:#09F;
    color:#FFF;
}*//*组合选择器*//*.haha,p b{对多种选择器进行相同样式定义
    background-color:#000;
    color:#C00;
}*//*伪元素

超链接的状态。*//*未访问*/a:link{
    background-color:#06F;
    color:#FFF;
    text-decoration:none;
    font-size:18px;}/*鼠标悬停*/a:hover{
    background-color:#FFF;
    color:#F00;
    font-size:24px;}/*点击效果*/a:active{
    background-color:#000;
    color:#FFF;
    font-size:36px;}/*访问后效果*/a:visited{
        background-color:#FF9;
        color:#000;
        text-decoration:line-through;}p:first-letter{
        font-size:36px;
        color:#F00;}p:hover{
        background-color:#F00;
        color:#FFF;}input:focus{
    background-color:#09F;}#qq{
    float:left;}/*L  V  H  A*/</style></head><body><input type="text" /><input type="text" />
    <hr /><a href="http://www.sina.com.cn" target="_blank">伪元素选择器演示</a><hr />

    <!--
    css和html相结合的第一种方式。
    1,每一个html标签中都有一个style样式属性。该属性的值就是css代码。 
    2,使用style标签的方式。 一般都定义在head标签中
    
    
    优先级:    标签选择器<类选择器<id选择器<style属性    
    -->

        <p id="qq">这是一个p<b>区域</b>1</p>
        <p class="haha" id="qq">这是一个p区域2</p>
        <span>span<b>区域</b>1</span>
        <span class="haha">span区域2</span>
        <p>这是一个段落1</p>
        <p class="haha">这是一个段落2</p></body></html>
Copy after login

 

The above is the detailed content of How to implement CSS and share sample code of selectors. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template