url() and src(), CSS functions.

url()
src()

Summary of characteristics of the url() function

Quick description
Specifies the address of a resource such as an image for example.
Status
Standard
Percentages
Not applicable.
Discrète: during an animation, the url() property passes from one value to another without transition.
W3C Module
CSS Values and Units Module
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: WD (Working Draft)

Document status: CR (Candidate Recommendation)

Description of the functions url() and src().

The function url() allows you to specify the address of an external resource, such as an image, a sound, a font, etc. The address can be indicated in quotes, in apostrophes, or without anything at all.

Example. The three syntaxes below are equivalent:

background-image:url("path/bg-image.jpg"); background-image:url('path/bg-image.jpg'); background-image:url(path/bg-image.jpg);

The function src() has exactly the same role, but the address of the resource must be included in quotes or apostrophes. This allows the use of functions instead of the address. However, the function src() is still poorly recognized by current browsers (2024).

--bkg:'path/bkg-image.jpg'; /* Does not work with url(): */ background-image:url(var(--bkg)); /* Work with src(): */ background-image:src(var(--bkg));

Fragments of image.

When the file designated by url() is an image, the syntax allows for retrieving only a portion of that image. Note! Few browsers support the feature to handle an image fragment.

The url() value can be used with the following properties:

  • @font-face : Defines all settings for a downloadable font.
  • @import : Importing a stylesheet, with the possibility of choosing a cascade layer.
  • background-image : Defines the background image or gradient (multiple images or gradients can coexist).
  • border-image-source : Sets the path and file name for the image used to build the border.
  • content : Add content to the document, mainly with the ::before and ::after selectors.
  • cursor : Sets the shape of the mouse cursor.
  • list-style-image : Defines a custom image to be used in place of bullets, in the context of a list enumeration.
  • mask-image : Specifies the image or SVG file that will be used as a mask.
  • src : Defines the source of a font file to download, or an image to display.

Syntax of the functions url() and src().

  • <property>: url('path/file.ext'); <property>: src('path/file.ext'); u

    The u is the address of an external resource. With the url() function, if this resource contains spaces, parentheses, quotes, or apostrophes, it will be necessary to escape these characters with a backslash () or to enclose the address in apostrophes or quotes.

    Examples :

    url(img/logo.png)No prohibited characters in the address.
    url(img/small logo.png)Space character forbidden.
    url(img/small\ logo.png)Escape space with backslash \.
    url('img/small logo.png')Address enclosed with apostrophes.
    url("img/small logo.png")Address enclosed with double quotes.

    Due to the fact that the delimiter characters (quotes or apostrophes) are not mandatory, it is not possible to use the calc() or var() functions with the function url().

    The function src() requires that the address u be enclosed in quotes or apostrophes. This allows for a calculation expression to be specified instead of the address.

    src(img/logo.png)Invalid.
    src("img/logo.png")Correct.
    src('img/logo.png')Correct.
    src(var(--adr))Correct.

    The address u can be relative or absolute:

    url(img/logo.png)A relative address, evaluated in relation to the location of the stylesheet (1).
    url(http://domain/img/logo.png)An absolute address that can refer to a resource possibly available on another domain.
    url(/img/logo.png)An absolute address relative to the root of the current site.

    (1) If the style is directly included in the document (HTML), it is the address of this document that serves as the basis for determining the absolute address.

  • <property>: url('path/file.png#xywh=100,100,300,200'); x y l h

    When the function url() is used to refer to an image, it is possible to retrieve only a part of that image.

    The xywh parameter (short for x, y, width, height) is treated as a bookmark indicating a portion of the image. Four numbers must be specified, separated by commas. These numbers correspond by default to pixels.

    Beware of the absence of units, and the unusual equal sign in CSS.

    1. x and y are the coordinates of the point at the top left of the area to be processed.
    2. w and h are respectively the width and height of the area to be processed.
  • <property>: url('path/file.ext' m);

    The parameter m is presented as a 'url-modifier' but interestingly no more precise definition exists in the standard.

    If a url-modifier parameter is specified, the address must be enclosed in quotes or apostrophes.

Examples of use of the function url().

Relative address.

The background image is saved on the same site. The relative address is resolved in relation to the location of the file where the style is defined. Here it concerns the current HTML page, but in most cases, it will be a separate style sheet.

Absolute address to another domain.

In this example, the background image can be saved on another site (here the W3C site).

Absolute address on the same domain.

You just need to start the address with the slash character (/) which indicates the root of the site. Here url() is used to specify the border image.

Browsers compatibility with url() function.

The function url() is very well recognized by all browsers. It can be used in many scenarios: defining a cursor, a bullet point, a font, etc. Refer to the corresponding property to see if a feature is supported. The table below only includes the two syntax that have been long-standing compatibility issues.

On the other hand, the function src() is very poorly supported even by browsers, even modern ones (2024).

Column 1
Support by browsers for the url() function which provides access to an external resource (image, sound, font, etc.).
Column 2
Support by browsers for the url() function in the context of the cursor property, to specify an image to be used as a cursor.
Column 3
Support by browsers of the src() function equivalent to url(), with a more rigorous syntax.
1
url() function
with content
2
url() function
with cursor
3
src()
function
Estimated overall support.
96%
95%
0%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

Baidu Browser

QQ Browser

Opéra Mobile

Firefox

Chrome

Safari

Edge

Opéra

Chrome pour Androïd

Safari sur IOS

Samsung Internet

UC Browser pour Androïd

Androïd Brower

Firefox pour Androïd

KaiOS Browser

Opéra mini

Historic of the url() function.

  • Cascading Style Sheets specification - Version 1

    The url() function has existed since the very first version of the CSS language. It is used to indicate any external resource: image, font, file to import, etc.
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    No change to the url() function.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • CSS Values and Units Module Level 3

    No change relating to the url() function.
    WD
    July 13, 2001
    Working Draft.
    CR
    August 28, 2012
    Candidate Recommendation.
    PR
    REC
  • CSS Values and Units Module Level 4

    Introduction of the src() function, synonymous with url() but with more rigorous syntax (obligation to specify the address between apostrophes or quotation marks).
    WD
    August 14, 2018
    Working Draft.
    CR
    PR
    REC
  • CSS Values and Units Module Level 5

    No change to url() and src() functions.
    WD
    September 03, 2024
    Working Draft.
    CR
    PR
    REC

See also: other calculation functions.

The following functions are also described in the same specification from W3C, called CSS Values and Units Module.

Properties:

interpolate-size
Langue française
Determines whether animations or transitions should be abrupt or gradual when a keyword defining an intrinsic dimension is used.

Functions:

abs()
Calculates the absolute value of a number.
acos()
Calculates the angle at which the cosine is equal to the specified value.
asin()
Calculates the angle at which the sine is equal to the specified value.
atan()
Calculates the angle whose tangent is equal to the specified value.
atan2()
Calculates the angle between the x-axis and the line connecting the origin point to the specified point.
attr()
Returns the value of an attribute (standard or custom) from HTML or XML code.
calc()
Langue française
Performs a calculation. Can be used in place of a value for a property.
calc-size()
Langue française
calc-size() allows calculations on the intrinsic dimension of an element (defined by the content).
clamp()
Langue française
Calculates a value between two extreme values. This function is useful when the units of the parameters are different but of the same type (dimensions, angle...).
cos()
Calculate the cosine of an angle.
exp()
Calculates the exponential of a number.
hypot()
Calculates the square root of a sum of squares of numbers.
if()
Langue française
This function allows you to select a value from a proposed list, each value being associated with a condition.
log()
Calculates the logarithm of a number.
max()
Langue française
Determines the largest value among a series of values.
min()
Langue française
Determines the smallest value among a series of values.
mod()
Langue française
Calculates the remainder of the integer division between two numbers.
pow()
Calculates a power boost.
rem()
Langue française
Calculates the remainder of the integer division between two numbers.
round()
Langue française
Calculates the rounding of a number.
sibling-count()
Langue française
Returns the number of siblings of the element in the document tree, including the element itself.
sibling-index()
Langue française
Returns the index of the element among its siblings, that is, among the other elements that have the same parent.
sign()
Determines the sign of a number.
sin()
Calculate the sine of an angle.
sqrt()
Calculates the square root of a number.
src()
Langue française
Specifies the address of a resource such as an image for example. Synonym of the url() function.
tan()
Calculates the tangent of an angle.

Values:

e
The predefined value of the number e (the basis of natural logarithms), which is approximately 2.7182818284590452354.
infinity
An error value indicating that the result of a calculation is an infinite value.
NaN
An error value indicating that a calculation could not be performed because of a non-numeric parameter.
pi
The preset value of the number pi (about 3.1415926535897932).