search
HomeWeb Front-endCSS TutorialCSS common attributes color and unit

In css code writing, estimating colors and units is essential. However, there are many ways to write colors and unit values ​​​​in css, so it is necessary to clarify it.

Color

When I was just learning the front-end, a question arose: “How should I set it up? Web page color?" English color names or hexadecimal color values ​​are generally used to set colors, but there are some other more complicated
writing methods in CSS.

RGB color

The computer generates new colors through different levels of red, green, and blue, which are often referred to as RGB colors. With such a color-generating mechanism, you can determine the color by directly giving different color levels, and you can
combine the values ​​​​of the three colors yourself.

Writing method:
h1{color:rgb(100%,100%,100%)} Percent rgb color writing method, the number range is from 0% to 100%
h1{color:rgb(0,255,255)} Number rgb color writing method, the number range is from 0 to 255

Note: the values ​​​​of the two writing methods will be trimmed, the percentage will be trimmed to 0%-100%, that is, if it exceeds 100%, it will be trimmed to 100%, if it is less than 0%, it will be trimmed to 0%, and the number will be trimmed to 0-255. In addition, the two cannot be mixed. Write, mixed writing will not take effect.

Hexadecimal color and color name setting color we will not mention anymore

Unit

Length units: generally divided into absolute units and relative units. Absolute units have rarely been used since their birth, at least I rarely use them in projects. Therefore, the introduction of absolute units is skipped here.

Relative units: There are three relative length units, em, ex and px. The first two represent "the font height of the element" and "the height of the letter shezhi'x'" respectively. They are measurements in ordinary printing. The last px represents "pixels". The reason why it is
is relative because it relies on the resolution of the display.

em: In CSS, an "em" is the font-size value of a given font. The only exception is when setting the font size, in which case the em value is relative to the parent element.

such as

  • body{font-size:20px;}

  • p{font-size:1em;}


##The font size of p is 20px instead of the default 16px.

px: Set according to the pixel value px. For example,
body{font-size:20px;}
p{font-size:16px;}
p element font The size is 16px

Percentage value: Compared with the length unit, the percentage is very simple, it is a percentage value. For example:
h1{line-height:150%;} Set the line height of all h1 to half the normal line height.

In short, the percentage is always calculated through another value, which will be mentioned one by one in the explanation of properties later. Moreover, the percentage can be positive or negative. For some attributes, only positive values ​​are accepted, which is another story.

Today’s content is so much, and the writing is a bit messy. I hope that I will write better and better in the future.

For more articles related to the colors and units of common css attributes, please pay attention to the PHP Chinese website!

Statement
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
Weekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridWeekly Platform News: Web Apps in Galaxy Store, Tappable Stories, CSS SubgridApr 14, 2025 am 11:20 AM

In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

Weekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsWeekly Platform News: Internet Explorer Mode, Speed Report in Search Console, Restricting Notification PromptsApr 14, 2025 am 11:15 AM

In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

The Power (and Fun) of Scope with CSS Custom PropertiesThe Power (and Fun) of Scope with CSS Custom PropertiesApr 14, 2025 am 11:11 AM

You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

We Are ProgrammersWe Are ProgrammersApr 14, 2025 am 11:04 AM

Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

How Do You Remove Unused CSS From a Site?How Do You Remove Unused CSS From a Site?Apr 14, 2025 am 10:59 AM

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

An Introduction to the Picture-in-Picture Web APIAn Introduction to the Picture-in-Picture Web APIApr 14, 2025 am 10:57 AM

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Ways to Organize and Prepare Images for a Blur-Up Effect Using GatsbyWays to Organize and Prepare Images for a Blur-Up Effect Using GatsbyApr 14, 2025 am 10:56 AM

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

Oh Hey, Padding Percentage is Based on the Parent Element's WidthOh Hey, Padding Percentage is Based on the Parent Element's WidthApr 14, 2025 am 10:55 AM

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software