Dofactory.com
Dofactory.com
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML <time> style Attribute

A style attribute on a <time> tag assigns a unique style to the time element.

Its value is CSS that defines the appearance of the time element.

Example

#

A style attribute on a <time> element.

Vincent Van Gogh was born , in Zundert, the Netherlands.
He died , (age 37) in Auvers-sur-Oise, France.

<article>
  <p>
    Vincent Van Gogh was born 
    <time style="color: brown; background: moccasin;" datetime="1853-03-30">March 30, 1853</time>,
    in Zundert, the Netherlands.<br />He died 
    <time style="color: brown; background: moccasin;" datetime="1890-07-29">July 29, 1890</time>,
    (age 37) in Auvers-sur-Oise, France. 
  </p>
</article>

Using style

The style attribute specifies the style, i.e. look and feel, of the <time> element.

A style contains any number of CSS property/value pairs, separated by semicolons (;).

The style attribute overrides any other style that was defined in a <style> tag or an external CSS file.

This inline styling affects the current <time> element only.


Syntax

<time style="CSS-styles">

Values

#

Value Description
CSS-styles One or more CSS property/value pairs separated by semicolons (;).

More Examples

A style attribute on a <time> element.
Clicking the button toggles the background color.

Vincent Van Gogh was born , in Zundert, the Netherlands.
He died , (age 37) in Auvers-sur-Oise, France.


<article>
  <p id="myp">
    Vincent Van Gogh was born 
    <time style="color: brown; background: moccasin;" datetime="1853-03-30">March 30, 1853</time>,
    in Zundert, the Netherlands.<br />He died 
    <time style="color: brown; background: moccasin;" datetime="1890-07-29">July 29, 1890</time>,
    (age 37) in Auvers-sur-Oise, France. 
  </p>
</article>

<br />
<button type="button"onclick="toggle();">Toggle class</button>

<script>
  let toggle = () => {
    let elements = document.getElementById("myp").getElementsByTagName("time");

    [].forEach.call(elements, element => {
       if (element.style.backgroundColor === "moccasin") {
         element.style.backgroundColor = "aliceblue";
       } else {
          element.style.backgroundColor = "moccasin";
       } 
     }
   );
  }
</script>

Code explanation

The style attribute assigns a background color to the <time> element.

Clicking the button calls JavaScript which toggles the background to another color.


Browser support

Here is when style support started for each browser:

Chrome
6.0 Sep 2010
Firefox
4.0 Mar 2011
IE/Edge
9.0 Mar 2011
Opera
11.1 Mar 2011
Safari
5.0 Jun 2010

You may also like

 Back to <time>

Last updated on Sep 30, 2023

Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides