You can edit almost every page by Creating an account and confirming your email.

CSS Syntax: Difference between revisions

From EverybodyWiki Bios & Wiki
WikiMasterBot2 (talk | contribs)
Moved page from wikipedia:en:Draft:CSS Syntax ([[Edithistory:|history]])
 
WikiMasterBot2 (talk | contribs)
m automatic correction by IA
 
Line 1: Line 1:
{{AfC submission|t||ts=20220319024534|u=Dontuseurrealname|ns=118|demo=}}<!-- Important, do not remove this line before article has been created. -->
The '''[[syntax]] of [[CSS]]''' is the arrangement and formatting when writing text in a CSS file (.css). The syntax has often been regarded as time-consuming by the programming community, as it's strikingly different from other programming languages like [[JavaScript syntax|JavaScript's syntax]].
 
The '''[[syntax]] of [[CSS]]''' is the arrangement and formatting when writing text in a CSS file (.css). The syntax often been regarded as time-consuming by the programming community, as it's strikingly different from other programming languages like [[JavaScript syntax|JavaScript's syntax]].


== HTML Referencing ==
== HTML Referencing ==
Line 10: Line 8:
     /* styles go here */
     /* styles go here */
</style>
</style>
</syntaxhighlight>However, styling if often found in an external file, known as a stylesheet, ending with the {{Code|.css|Text}} [[filename extension]]. The file must be referenced in the HTML using the {{Code|<link>|HTML}} element: <syntaxhighlight lang="html5">
</syntaxhighlight>However, styling is often found in an external file, known as a stylesheet, ending with the {{Code|.css|Text}} [[filename extension]]. The file must be referenced in the HTML using the {{Code|<link>|HTML}} element: <syntaxhighlight lang="html5">
<!-- Here, the filename is 'styles.css', but this can be modified later -->
<!-- Here, the filename is 'styles.css', but this can be modified later -->
<link rel="stylesheet" href="styles.css" type="text/css" />
<link rel="stylesheet" href="styles.css" type="text/css" />
Line 19: Line 17:
<span class="foo" id="bar" data-baz>Hello world!</span>
<span class="foo" id="bar" data-baz>Hello world!</span>
</syntaxhighlight>In the stylesheet, the element can be grabbed using the selector:<syntaxhighlight lang="css">
</syntaxhighlight>In the stylesheet, the element can be grabbed using the selector:<syntaxhighlight lang="css">
/* Here, all <span> elements are selected using it's tag */
/* Here, all <span> elements are selected using its tag */
span {
span {
     color: red;
     color: red;
Line 44: Line 42:
|-
|-
|{{Code|align-content|CSS}}
|{{Code|align-content|CSS}}
|Selects all elements that
|Selects all elements that are in their active state
are in their active state
|-
|-
|{{Code|align-items|CSS}}
|{{Code|align-items|CSS}}
Line 51: Line 48:
|-
|-
|{{Code|animation|CSS}}
|{{Code|animation|CSS}}
|Applies a new instance of
|Applies a new instance of an animation made with the {{Code|@keyframes|CSS}} at-rule
an animation made with
the {{Code|@keyframes|CSS}} at-rule
|-
|-
|{{Code|margin|CSS}}
|{{Code|margin|CSS}}
|Styles the margin of an element (see [[Draft:CSS Syntax#Box%2520Model|box model]])
|Styles the margin of an element (see [[Draft:CSS Syntax#Box%2520Model|box model]])
|-
|
|
|-
|-
|
|
Line 78: Line 70:
|-
|-
|{{Code|:active|CSS}}
|{{Code|:active|CSS}}
|Selects all elements that
|Selects all elements that are in their active state
are in their active state
|-
|-
|{{Code|:checked|CSS}}
|{{Code|:checked|CSS}}
|Selects all checkbox inputs
|Selects all checkbox inputs that are in their checked state
that are in their checked state
|-
|-
|{{Code|:hover|CSS}}
|{{Code|:hover|CSS}}
|Selects all elements that are
|Selects all elements that are in their hover state
in their hover state
|-
|-
|{{Code|:lang(selector)|CSS}}
|{{Code|:lang(selector)|CSS}}
|Selects all elements that have
|Selects all elements that have the {{Code|lang|HTML}} attribute set to the specified selector
the {{Code|lang|HTML}}attribute set to the
specified selector
|-
|-
|{{Code|:not(selector)|CSS}}
|{{Code|:not(selector)|CSS}}
|Selects all elements that
|Selects all elements that do not have the specified child element
do not have the specified
child element
|-
|-
|{{Code|:link|CSS}}
|{{Code|:link|CSS}}
|Selects all unvisited
|Selects all unvisited anchor elements
anchor elements
|-
|-
|{{Code|:visited|CSS}}
|{{Code|:visited|CSS}}
|Selects all visited
|Selects all visited anchor elements
anchor elements
|}
|}


Line 120: Line 103:
</syntaxhighlight>
</syntaxhighlight>
=== @keyframes ===
=== @keyframes ===
In CSS3, the {{Code|@keyframes|CSS}} at-rule was introduced. It is similar to the {{Code|transition|CSS}}property, but unlike {{Code|transition|CSS}}, which is for simple animations/transitions, {{Code|@keyframes|CSS}} is used for multi-step animations that can reiterate and perform complex actions. {{Code|@keyframes|CSS}}, and CSS3 animation as a whole, uses the animation concept of [[Key frame|keyframes]].  
In CSS3, the {{Code|@keyframes|CSS}} at-rule was introduced. It is similar to the {{Code|transition|CSS}} property, but unlike {{Code|transition|CSS}}, which is for simple animations/transitions, {{Code|@keyframes|CSS}} is used for multi-step animations that can reiterate and perform complex actions. {{Code|@keyframes|CSS}}, and CSS3 animation as a whole, uses the animation concept of [[Key frame|keyframes]].  


{{Code|@keyframes|CSS}} take in an identifier, which is defined as a unique name for an animation. {{Code|@keyframes|CSS}} then uses selectors to set keyframes on the animation. Usually, percentages are used, but the shorthand selectors {{Code|from|CSS}} and {{Code|to|CSS}} (0% and 100% respectively) are as well used. In the selectors, properties can be assigned. Some properties are able to be animated, such as {{Code|transform|CSS}} or {{Code|background-color|CSS}}, while others like {{Code|font-family|CSS}} cannot be animated.  
{{Code|@keyframes|CSS}} take in an identifier, which is defined as a unique name for an animation. {{Code|@keyframes|CSS}} then uses selectors to set keyframes on the animation. Usually, percentages are used, but the shorthand selectors {{Code|from|CSS}} and {{Code|to|CSS}} (0% and 100% respectively) are also used. In the selectors, properties can be assigned. Some properties are able to be animated, such as {{Code|transform|CSS}} or {{Code|background-color|CSS}}, while others like {{Code|font-family|CSS}} cannot be animated.  
Below is an example of a CSS3 animation using {{Code|@keyframes|CSS}}: <syntaxhighlight lang="css">
Below is an example of a CSS3 animation using {{Code|@keyframes|CSS}}: <syntaxhighlight lang="css">
@keyframes identifier {
@keyframes identifier {
Line 150: Line 133:
</syntaxhighlight>
</syntaxhighlight>


And can be shortened using the {{Code|animation|CSS}} shorthand:<syntaxhighlight lang="css">
And can be shortened using the {{Code|animation|CSS}} shorthand:<syntaxhighlight lang="css">
#my-element {
#my-element {
     background-color: red;
     background-color: red;

Latest revision as of 10:37, 3 September 2025

The syntax of CSS is the arrangement and formatting when writing text in a CSS file (.css). The syntax has often been regarded as time-consuming by the programming community, as it's strikingly different from other programming languages like JavaScript's syntax.

HTML Referencing

In order for CSS to work, an HTML file (.html or .htm) needs to exist. Inside the HTML, styling can be written in a handful of ways. CSS can be written using the style attribute in an element. For instance:

<p style="/* styles go here */">This span element will show red</p>

Moreover, styles can also be applied using the <style> element:

<style type="text/css">
    /* styles go here */
</style>

However, styling is often found in an external file, known as a stylesheet, ending with the .css filename extension. The file must be referenced in the HTML using the <link> element:

<!-- Here, the filename is 'styles.css', but this can be modified later -->
<link rel="stylesheet" href="styles.css" type="text/css" />

Basics

HTML Elements are styled within rules in the stylesheet. Rules contain two main segments. The selector grabs the HTML Element by an identifier (e. g. class, id, tag name, etc). After which are declarations, which are wrapped in curly brackets. Declarations include a property (e. g. background-color, position, border, etc) and an assigned value that can be modified to produce a specified resulting style. First, an element must exist in the document:

<span class="foo" id="bar" data-baz>Hello world!</span>

In the stylesheet, the element can be grabbed using the selector:

/* Here, all <span> elements are selected using its tag */
span {
    color: red;
}

/* Here, all elements with the class of 'foo' are styled */
.foo {
    color: red;
}

/* Here, all elements with the id of 'bar' are styled */
#bar {
    color: red;
}

/* Here, all elements with the attribute of 'data-baz' are styled */
[data-baz] {
    color: red;
}

Elements contain properties, such as margin, that are able to be modified in CSS. These properties then have values assigned to them, such as auto

Property Description
align-content Selects all elements that are in their active state
align-items
animation Applies a new instance of an animation made with the @keyframes at-rule
margin Styles the margin of an element (see box model)

Pseudo-classes

Pseudo-class Description
:active Selects all elements that are in their active state
:checked Selects all checkbox inputs that are in their checked state
:hover Selects all elements that are in their hover state
:lang(selector) Selects all elements that have the lang attribute set to the specified selector
:not(selector) Selects all elements that do not have the specified child element
:link Selects all unvisited anchor elements
:visited Selects all visited anchor elements

At-rules

At-rules are special selectors that are used mainly to apply stylistic effects onto the page.

@import

/* Imports the example font */
@import url('https://www.exampleapi.com/importfoo');
* {
    font-family: 'foo', Sans-Serif;
}

@keyframes

In CSS3, the @keyframes at-rule was introduced. It is similar to the transition property, but unlike transition, which is for simple animations/transitions, @keyframes is used for multi-step animations that can reiterate and perform complex actions. @keyframes, and CSS3 animation as a whole, uses the animation concept of keyframes.

@keyframes take in an identifier, which is defined as a unique name for an animation. @keyframes then uses selectors to set keyframes on the animation. Usually, percentages are used, but the shorthand selectors from and to (0% and 100% respectively) are also used. In the selectors, properties can be assigned. Some properties are able to be animated, such as transform or background-color, while others like font-family cannot be animated.

Below is an example of a CSS3 animation using @keyframes:

@keyframes identifier {
    from {
        background-color: red;
        transform: rotate(0deg);
    }
    
    to {
        background-color: blue;
        transform: rotate(30deg);
    }
}

The animation can then be referenced later in the stylesheet using the many CSS3 properties:

#my-element {
    background-color: red;
    animation-name: identifier;
    animation-duration: 500ms;
    animation-timing-function: ease;
    animation-delay: 300ms;
    animation-direction: normal;
    animation-fill-mode: forwards;
    animation-iteration-count: 3;
}

And can be shortened using the animation shorthand:

#my-element {
    background-color: red;
    animation: identifier 500ms ease 300ms forwards normal 3;
}

@media

#my-element {
    font-size: 20px;
    display: flex;
    flex-direction: row;
}

In the @media, a device with a minimum width of 500 pixels is styled accordingly:

@media screen and (min-width: 500px) {
    #my-element {
        font-size: 12px;
        display: flex;
        flex-direction: column;
    }
}


References


This article "CSS Syntax" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:CSS Syntax. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.