JQuery – How To Find CSS Class In a Element

CSS is an acronym for Cascading Style Sheet which is used for describing the presentation semantics of document, the look and formatting of a document written in the markup language. The styles define how to display the elements in the document. The CSS method returns the CSS property value of an element. The CSS method allows changing the style property of specified element. The CSS method returns one or more style properties for the selected elements. It allows to style items within the same web page differently.

It defines layout of HTML documents such as fonts, size, colors, height, width, background color, and many other things.The CSS is designed to enable separation of document content from document presentation including elements such as layout, colors and fonts. One of the goals of CSS is to allow users greater control over presentation. Depending on the browser and web site, user may choose from various style sheets provided by designers or view the site using the browsers default styling.

This tutorial explains how to get the CSS of a particular element in the web page with simple example.

CSS Method Syntax

$(“.class”)

Here, class specifies the class of the elements to select. It is used to set particular style for several HTML elements. The CSS method performs following different tasks as follows :

As shown above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. There are two classes namely myclass and h2 which uses CSS properties to change styles of their particular elements. The mouseover event occurs when the mouse pointer is over the selected element. The mouseout event occurs when the mouse pointer leaves the selected element. In above example, when mouse leaves the selected element, it sets background-color to gray.

When you run the above example, you would get the following output:

We can also set one or more properties values using CSS properties as shown in the following example:

As shown above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. We have used class called, myclass which uses multiple CSS properties to change styles of their particular elements.

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.