id and class in css code example

Example 1: css class

.myclass {
  border:1px solid black;
  background-color: #d2d2d2;
  height:20px;
  width:60px;
}
/*If you give this class to a div in html, it will get the styles you gave
to that class. You can add multiple classes to a div, and multiple divs
can get that class. */
/* I hope I helped! */

Example 2: css class and id

Select an element with the ID "id" and the class "class":
#id.class {
}
example:
<div>
  <strong id="id" class="class">
      Foobar
  </strong>
  <strong class="class">
      Foobar
  </strong>
</div>
=> Will select the first <strong> element

Select all elements with the class "class",
which are decendents of a element with an ID of "id":
#id .class {
}
example:
<div id="id">
	<strong class="class">Foobar</strong>
</div>
=> Will select the <strong> element

Example 3: css class id

css
.class
#id

Example 4: . and # in css

<!DOCTYPE html>
<html>
<head>
<style>
.classname {
  background-color: green;
  color: white;
}
#idname {
  background-color: pink;
  color: white;
}
</style>
</head>
<body>
<div class="classname">I am  green colour<div>
<div id="idname">I am  pink colour</div>
</body>
</html>

Tags:

Css Example