Write jQuery code to change the color and size of all paragraphs

Description: Make the text of all paragraphs blue and set the font size to 18px.

Hint:

  • Pass an object to .css() to set many properties.

HTML of the page

<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>

Solution

$('p').css({
  'color': 'blue',
  'font-size': '18px'
});
HTML after the code runs

<p style="color: blue; font-size: 18px;">First paragraph</p>
<p style="color: blue; font-size: 18px;">Second paragraph</p>
<p style="color: blue; font-size: 18px;">Third paragraph</p>

How it works

  • The selector p finds all three paragraphs, and the method works on every one of them without a loop.

Read CSS Methods.


Learn more about the similar topics:
Tutorials
jQuery Get Content and Attributes
jQuery Set Content and Attributes
jQuery Add Elements: append, prepend, after, before
jQuery Remove Elements: remove, empty, detach
jQuery CSS Classes: addClass, removeClass, toggleClass
jQuery CSS Methods
jQuery Dimensions and Position
Exercises & Assignments
Write jQuery code to change the text of a heading when the page loads
Write jQuery code to change the color and size of all paragraphs
Write jQuery code to add a new list item from a text field
Interview Questions & Answers
What is the difference between text(), html() and val() in jQuery?
What is the difference between attr() and prop() in jQuery?
How do you add, remove and toggle a class in jQuery?
What is the difference between remove(), empty() and detach() in jQuery?
What is the difference between append(), prepend(), after() and before()?
What is the difference between data() and attr(‘data-*’) in jQuery?
What is the difference between width(), innerWidth() and outerWidth() in jQuery?