Class and Id Selectors

Css Style Types

3. Selectors:

When we apply generic css to a tag, that tag has the properties given wherever it is used.

Using selectors, we can apply different styles to the same tag in different places.

In other words, we can apply the css selector we created to a tag wherever we want and not apply it where we don't want it. We can even apply different formatting to the same label in different places.

There are two types of selectors:

a. id Selectors:

  • When defining, “ # ” is put in front of the name .
  • When applying to any tag, the " id " parameter is used.
  • Since the id parameter specifies the identity of the element it is used with, there may be problems when the same id selector is applied to more than one tag. For example, in the example below, the capitalized style template has been applied to both the b tag and the u tag. There will be no problems with formatting and the specified properties will be applied to both tags. However, if there is a programming language (such as Asp.Net, Php, Javascript) that uses these ids on the page, there will be problems because there will be two elements with the same name on the page.
  • It does not change the tags to which it is not applied. For example, in the example below, a large-name style selector is applied to the first b tag and a small-name style selector is applied to the second b tag, while no css template is applied to the third b tag.

<head><style type="text/css">

#large

{

Font-size:24pt;

}

#small

{

font-size:9pt;

}

</style></head>

<body>

<b id=" big ">Computer</b> <br/>

<b id=" small ">Computer</b> <br/>

<b>Computer</b> <br/>

<u id=" big ">Computer</u>

</body>

When the above page is run in the browser, it will look like this:

Computer

Computer

Computer

Computer

b. Class Selectors:

  • When defining " . ” is put.
  • While applying, the " class " parameter is used.
  • It can be applied to more than one tag, it does not affect the tags to which it is not applied.

<head><style type="text/css">

.large

{

Font-size:24pt;

}

.small

{

Font-size:11pt;

}

</style></head>

<body>

<b class=" big ">Computer</b> <br/>

<b class=" small ">Computer</b> <br/>

<p class=" small ">As you can see, I can apply the class selector I created to the tag I want.</p>

<b>We did not apply any style here.</b>

</body>

Creating a Tag-Specific Class

We can use the class selectors we created as .className for all tags. However, we can create only one tag-specific class if we want. E.g;

p.large{ ... }

Properties written in this example will only be applied to p tags with the upper class applied. In other tags, the big class will not work.

Applying Multiple Classes to a Label

<p class="class1 class2">...</p>

Multiple classes can be applied to an html tag with a space between them as shown above.

Note: If a tag is both formatted with the global style template and has an id or class selector applied, the properties in both templates are applied to the tag. If some properties are specified both in the global template and in the selector, the values ​​in class or id are valid.

using css class and id, class selectors, id selectors, css class and id difference, css class and id examples

EXERCISES

Css id and class tutorials Try It

You can see, change and review the codes by clicking the try it button.

As you can see in the example, the way id and class are used are the same, the difference is that id can be applied to a single tag and class can be applied to many tags.

Creating rounded border for tables and boxes Try It

(In this example, some features are written differently for each browser.)

#tablo1

{

background-color:#ffffff;

color: #696969;

width: 360px;

/*We can adjust the roundness of the corners with the radius property */

-webkit-border-radius: 10px;

-moz-border-radius: 10px;

border-radius: 10px;

padding: 7px;

/* shadow feature also provides shadow effect */

-moz-box-shadow: 0 0 5px #779900;

-webkit-box-shadow: 0 0 5px #779900;

box-shadow: 0 0 5px #779900;

    }

 


COMMENTS




Read 551 times.

Online Users: 10



css-class-and-id