CSS ID Syntax

IDs allow you to assign a unique identifier to an HTML element. This allows you to define a style that can only be used by the element you assign the ID to.

CSS ID Syntax

The syntax for declaring a CSS ID is the same as for classes - except that instead of using a dot, you use a hash (#).

#id-name { property:value; }
Again, similar to classes, if you want to use the same id name for multiple elements, but each with a different style, you can prefix the hash with the HTML element name.

html-element-name#id-name { property:value; }

CSS ID Example

<style type="text/css">
h1#css-section { color:#000099 }
p#css-section { color:#999999; }
<h1 id="css-section">CSS ID</h1>
<p id="css-section">CSS IDs can be very useful</p>
This results in:


CSS IDs can be very useful

IDs vs Classes

Given classes and IDs are very similar, you may be wondering which one to use. This depends on the situation.

When to use classes

You should use classes when your style needs to be applied multiple times on the same page. For example, you might have many h1 elements that need the same style applied.

When to use IDs

You should use IDs if only one element on the page should have the style applied, and/or you need a unique identifier for that element. For example, you might assign an ID to a div tag which contains your left menu. The styles for this ID could contain the position, background-color, float properties, size etc. You probably wouldn't want any other element on the page to use this particular style.
Another useful thing about IDs is that you can use the Document Object Model (DOM) to refer to them. This enables you to use JavaScript/DHTML techniques to build a much more interactive web site.

تنبيه : المرجوا عدم نسخ الموضوع بدون ذكر مصدره المرفق بالرابط المباشر للموضوع الأصلي وإسم المدونة وشكرا
Samy Mustufa

0 التعليقات:

leave your comment to help us to develope the website.


اخر التعليقات

اصدقاء المدونه على الفيس بوك