DEV Community

Sampson Ovuoba for Devwares

Posted on • Originally published at devwares.com on

CSS Syntax and Selector

CSS Syntax and Selector

There are some rules that guild writing code for CSS style. These rules help the browser to interpret the code and apply it to the appropriate HTML element. The CSS rule is made of three parts which include

Contrast Bootstrap UI Kit

Selector: a selector shows the HTML element you wish to style. This could be any tag like <p> or even a class name.

Property: this is like a HTML attribute. It simply that all HTML attributes are converted to CSS properties. Example color, border, height, weight etc.

Value: these are assigned to the properties example color: blue

CSS Code:

p {
  color: blue;
  height: 12px
}

Enter fullscreen mode Exit fullscreen mode

The p is the selector in CSS. It is followed by curly braces. In side the curly braces are known as the declaration box which contains CSS property, in this case is the color and height and their values which in this case is blue and 12px.

CSS Selector

A CSS selector selects the HTML element(s) you want to style. These selectors are divided into five categories Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selector (select elements based on a certain state) Pseudo-elements selectors (select and style a part of an element) Attribute selector (select elements based on an attribute value)

Element selector

These selects elements based on the element name. when an element is selected and a style is given to it all the content of that element takes the style on the webpage.

CSS Code:

p {
  background-color: red;
  text-align: center
}

Enter fullscreen mode Exit fullscreen mode

CSS ID Selector

An id is unique in itself. The id selector is used to select this id within the webpage. No two elements can have the same id. To select a specific id, write the harsh tag character(#), followed by the id of the element.

CSS Code:

#wrapper {
  text-align: center:
  color: grey;
}

Enter fullscreen mode Exit fullscreen mode

CSS Class Selector

The CSS class selector works in the same way as the id selector but with slight difference. An ID is unique to the element whereas a class can be common to multiple elements. To select a element with specific class, write a period character(.), followed by the class name.

CSS Code:

.center {
  margin: 0px
  padding:2px;
}

Enter fullscreen mode Exit fullscreen mode

CSS Universal selector

The universal selector is just as it is called. It selects all the element in the webpage. It has the star sign (*) as its symbol

CSS Code:

*{
margin: 0px;
padding: 0px;
}

Enter fullscreen mode Exit fullscreen mode

CSS Grouping selector

It is usually used when the same CSS styling is applied to different elements. It helps to increase efficiency and save time.

CSS Code:

p, h3, div {
  color: green;
}

Enter fullscreen mode Exit fullscreen mode

Create Stunning Websites and Web Apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.

Contrast

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc. project with a clean, prebuilt and well documented template and UI components. Learn more about contrast.UI components. [Learn more about Contrast Pro]

Resources

Top comments (0)