HTML <p> style Attribute

The style attribute on a <p> tag assigns a unique style to the paragraph. A style contains one or more CSS property/value pairs that define the appearance of the paragraph.

Example

#

A style attribute on a <p> element.

The Palace Museum is a national museum housed in the Forbidden City at the core of Beijing, China. It was established in 1925 after the last Emperor of China was evicted from his palace, and opened its doors to the public.

<p style="width: 350px;padding: 20px;background: papayawhip;">
    The Palace Museum is a national museum housed in 
    the Forbidden City at the core of Beijing, China. 
    It was established in 1925 after the last Emperor 
    of China was evicted from his palace, and opened 
    its doors to the public.
</p>

Using style

The style attribute specifies the style, i.e. look and feel, of the <p> 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 <p> element only.


Syntax

<p 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 <p> element.
Clicking the button toggles the height of the paragraph.

The Palace Museum is a national museum housed in the Forbidden City at the core of Beijing, China. It was established in 1925 after the last Emperor of China was evicted from his palace, and opened its doors to the public.


<p id="myparagraph"
   style="width:300px; height:200px; padding:20px;
          background:papayawhip; overflow:auto;">
    The Palace Museum is a national museum housed in 
    the Forbidden City at the core of Beijing, China. 
    It was established in 1925 after the last Emperor 
    of China was evicted from his palace, and opened 
    its doors to the public.
</p>

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

<script>
  let toggle = () => {
    let element = document.getElementById("myparagraph");

    if (element.style.height === "200px") {
       element.style.height = "auto";
    } else {
       element.style.height = "200px";
    }
  }
</script>

Code explanation

The style attribute assigns a custom height to the <p> element.

Clicking the button calls JavaScript which changes the paragraph height, displaying all the its content.


Browser support

Here is when style support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <p>
Guides