cs01 CSS Syntax

news/2024/11/17 12:03:32/文章来源:https://www.cnblogs.com/emanlee/p/18199721

A CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in your document. A style rule is made of three parts −

  • Selector − A selector is an HTML tag at which a style will be applied. This could be any tag like <h1> or <table> etc.

  • Property − A property is a type of attribute of HTML tag. Put simply, all the HTML attributes are converted into CSS properties. They could be color, border etc.

  • Value − Values are assigned to properties. For example, color property can have value either red or #F1F1F1 etc.

You can put CSS Style Rule Syntax as follows −

selector { property: value }

 

Syntax

Example − You can define a table border as follows −

table{ border :1px solid #C00; }

Here table is a selector and border is a property and given value 1px solid #C00 is the value of that property.

You can define selectors in various simple ways based on your comfort. Let me put these selectors one by one.

The Type Selectors

This is the same selector we have seen above. Again, one more example to give a color to all level 1 headings −

h1 {color: #36CFFF; 
}
 

The Universal Selectors

Rather than selecting elements of a specific type, the universal selector quite simply matches the name of any element type −

* { color: #000000; 
}

This rule renders the content of every element in our document in black.

The Descendant Selectors

Suppose you want to apply a style rule to a particular element only when it lies inside a particular element. As given in the following example, style rule will apply to <em> element only when it lies inside <ul> tag.

ul em {color: #000000; 
}
 

The Class Selectors

You can define style rules based on the class attribute of the elements. All the elements having that class will be formatted according to the defined rule.

.black {color: #000000; 
}

This rule renders the content in black for every element with class attribute set to black in our document. You can make it a bit more particular. For example −

h1.black {color: #000000; 
}

This rule renders the content in black for only <h1> elements with class attribute set to black.

You can apply more than one class selectors to given element. Consider the following example −

<p class = "center bold">This para will be styled by the classes center and bold.
</p>

The ID Selectors

You can define style rules based on the id attribute of the elements. All the elements having that id will be formatted according to the defined rule.

#black {color: #000000; 
}

This rule renders the content in black for every element with id attribute set to black in our document. You can make it a bit more particular. For example −

h1#black {color: #000000; 
}

This rule renders the content in black for only <h1> elements with id attribute set to black.

The true power of id selectors is when they are used as the foundation for descendant selectors, For example −

#black h2 {color: #000000; 
}

In this example all level 2 headings will be displayed in black color when those headings will lie with in tags having id attribute set to black.

 

The Child Selectors

You have seen the descendant selectors. There is one more type of selector, which is very similar to descendants but have different functionality. Consider the following example −

body > p {color: #000000; 
}

This rule will render all the paragraphs in black if they are direct child of <body> element. Other paragraphs put inside other elements like <div> or <td> would not have any effect of this rule.

The Attribute Selectors

You can also apply styles to HTML elements with particular attributes. The style rule below will match all the input elements having a type attribute with a value of text

input[type = "text"] {color: #000000; 
}

The advantage to this method is that the <input type = "submit" /> element is unaffected, and the color applied only to the desired text fields.

There are following rules applied to attribute selector.

  • p[lang] − Selects all paragraph elements with a lang attribute.

  • p[lang="fr"] − Selects all paragraph elements whose lang attribute has a value of exactly "fr".

  • p[lang~="fr"] − Selects all paragraph elements whose lang attribute contains the word "fr".

  • p[lang|="en"] − Selects all paragraph elements whose lang attribute contains values that are exactly "en", or begin with "en-".

 

Multiple Style Rules

You may need to define multiple style rules for a single element. You can define these rules to combine multiple properties and corresponding values into a single block as defined in the following example −

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

Here all the property and value pairs are separated by a semicolon (;). You can keep them in a single line or multiple lines. For better readability, we keep them in separate lines.

For a while, don't bother about the properties mentioned in the above block. These properties will be explained in the coming chapters and you can find complete detail about properties in CSS References

Grouping Selectors

You can apply a style to many selectors if you like. Just separate the selectors with a comma, as given in the following example −

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

This define style rule will be applicable to h1, h2 and h3 element as well. The order of the list is irrelevant. All the elements in the selector will have the corresponding declarations applied to them.

You can combine the various id selectors together as shown below −

#content, #footer, #supplement {position: absolute;left: 510px;width: 200px;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/708675.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

手写Word2vec算法实现

1. 语料下载:https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2 【中文维基百科语料】 2. 语料处理 (1)提取数据集的文本 下载的数据集无法直接使用,需要提取出文本信息。 安装python库:pip install numpy pip install scipy pip install …

IP地址与子网掩码的关系

IP地址与子网掩码的关系IP地址与子网掩码的关系网站:http://shibowl.topgithub:https://github.com/hanbinjxnc博客园:https://www.cnblogs.com/hool 博客:https://blog.shibowl.top 作者:世博 2019年4月28日

2024/05/19

复盘金龙汽车 是趋势/不能突破买入,首次突破时假突破,有时候二次突破也是假突破,第三次才成功,假突破后会深度快速回踩深蹲,如果买入会吃大面!真突破后往往连续快速上涨,不会给上车机会。 像这种一涨三回头的票,经常有假突破,不适合用突破买入,适合用低吸,均线位置缩…

操作系统基础——01 操作系统基本概念

操作系统基础——01 操作系统基本概念目录计算机系统的层次结构操作系统的定义操作系统的功能和目标作为系统资源的管理者向上层提供方便易用的服务作为最接近硬件的层次操作系统的四个特征并发共享虚拟异步操作系统的发展与分类操作系统的运行机制中断和异常中断的作用中断类型…

SpringCloud(3)-OpenFeign相关配置

OpenFeign 是个声明式 WebService 客户端,使用 OpenFeign 让编写 Web Service 客户端更简单。 Spring Cloud 对 OpenFeign 进 行 了 封 装 使 其 支 持 了 Spring MVC 标 准 注 解 和 HttpMessageConverters。 OpenFeign 可以与 Eureka 和 Ribbon 组合使用以支持负载均衡。1.配…

如此丝滑的API设计,用起来真香

谈及软件中的设计,无论是架构设计还是程序设计还是说API设计, 原则其实都差不多,要能够松耦合、易扩展、注意性能。遵循上述这些API的设计规则, 相信大家都能设计出比较丝滑的API。当然如果还有其他的API设计中的注意点也欢迎在评论区留言。分享是最有效的学习方式。 博客:…

《user-agent(UA)识别 Api 接口助力智能应用开发》

在现代智能应用的开发中,往往需要对用户的设备和浏览器进行识别,以便适配不同的操作系统和浏览器。而user-agent是一种非常重要的信息,它包含了用户设备、操作系统和浏览器的相关信息。在本文中,我们将介绍一个强大的user-agent识别 API 接口,它可以帮助开发者轻松实现用户…

科学时如何更快进行DNS解析及微信双开

如何更快进行DNS解析科学了,发现访问很慢,有时还无法访问,明显是被某种神秘的东方力量给阻断了。 DNS解析就起作用了。可以快速寻址,目前国内比较知名的且比较快的就是阿里云的:223.5.5.5。但是呢,这还需要看你自己的网络是哪家的,去访问国际的时候路由节点是否在国内来…

eclipse安装tomcat

一、确保Tomcat服务器处于关闭状态在配置之前确保tomcat服务器处于关闭状态,若tomcat处于启动状态则将其关闭,Service Status的值为Stopped表明Tomcat已经关闭 二、在Eclipse中配置Tomcat打开Eclipse---->点击Window---->点击Preferences点击Server---->点击Runtime…

Redis安装之集群-集群(cluster)模式

一、背景 Redis 哨兵模式在一定程度上解决的系统的高可用问题,但单 master 节点的写入也成为了系统处理高并发请求时的瓶颈。 二、方案原理采用多个 master 节点集群模式实现 Redis 水平扩容,提供并发请求处理能力; cluster 自带 sentinel 故障转移机制,无需再使用哨兵功能…

主流原型设计工具介绍

当谈到原型设计工具时,Axure 和墨刀是两个备受推崇的选择。它们各自拥有独特的特点和优势,适用于不同的设计需求和团队工作流程。今天我会重点介绍这两种工具的特点以及使用方法,并且简单介绍其他的一些原型设计工具例如:Sketch,Figma Axure Axure 是一款功能强大的原型设计…