CSS: Everything you need to know about targeting elements
Tags: webdev, iwritecode, sm8uti, css
Published: 2022-11-13 09:28:08

In the front-end development journey the 3 things that are important. `HTML, CSS, and javascript`. in this article, we will cover the basics of CSS and how it works on a web page, and why it is an important thing in a website.

### What is CSS?

> CSS: Cascading Style Sheets

CSS is a language for styling a website and it helps to change the appearance of the website and made stunning looks for a web page. Frankly saying that the CSS is only used for targeting elements or tags  and changing the look of a website or webpage

> CSS  is a style sheet language that is used to describe the look and formatting of a document written in a markup language. It provides an additional feature to HTML. It is generally used with HTML to change the style of web pages and user interfaces. It can also be used with any kind of XML document including plain XML, SVG, and XUL. ( Source : [Javapoint](https://www.javatpoint.com/what-is-css) )


![html&css.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668325258555/Jot9U-yxd.png align="left")

### How CSS works?

The better way you understand how it works, I will give you an example : 

First, we create an HTML file and open the file in VS Code, you will use different IDE like Sublime Text, Atom, etc. then we write an element in a file, select the element using CSS and change some appearance like background color, etc.


![code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668326552648/9ElPm7wDU.png align="center")

##### Output : 

![screenshot-127.0.0.1_5500-2022.11.13-13_14_27.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668326585383/RULz-z1jo.png align="center")

#### Using CSS : 

![code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668326746036/8RG81IHBS.png align="center")

##### Output : 

![screenshot-127.0.0.1_5500-2022.11.13-13_36_36.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668326816063/c_lMIEtDC.png align="center")

Once you have loaded and parsed the HTML content and CSS styling. First, the web browser converts them into the DOM ( `Document Object Model` ).once the DOM combines the content and style of the web document and is a representation of the web document within the memory of the computer, the browser will display the content.


![Frame 3 (1).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668327688592/5dckaH6Gt.png align="center")

we can add the CSS file in 3 different ways:
- External CSS file : 

    In the External CSS file, we can create a CSS file and then link the CSS file to an HTML file. this method we use in real-life projects or the programming world.

> CSS File extension is `.css`


![code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668328681533/fBuAOd1yP.png align="center")


- Internal CSS file :

  In this method, we can write CSS code inside the HTML page or document using `<style>` tag.


![code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668328913270/Vvau7sQqm.png align="center")
 
- Inline Style :  

  In this method, we can write CSS rules or styles directly inside an HTML element using `style` attributes.

![code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668329105043/Love62NWd.png align="center")
  


### CSS Selector : 

Selectors are just used to select elements or tags on the web page or web document. in the above example, we select the h1 tag and use some styling. 

```css 
h1{
  background-color:blue;
}
```

Here h1 is the Element selector.

In CSS, We use different CSS selectors they are : 

#### Universal Selector : 

  This selector is used for styling all the elements present in an HTML Document.
`*` is a Universal Selector.

>CSS universal selectors select any type of element in an HTML page.



```css
*{
  color: blue;
}
```

Example : 

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iwritecode</title>
    <style>
             *{
                  background-color: blueviolet;
              }
    </style>
</head>
<body>
    <h1>Hello Smruti Ranjan Nayak,</h1>
    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Dignissimos eligendi ratione odit similique nostrum maiores dolor quas! Ipsum reiciendis soluta assumenda nesciunt dolores maiores amet pariatur ullam, culpa et odit molestiae aliquam! Molestiae optio voluptates quo? Nemo fugiat repellat eos vel necessitatibus, sequi, unde eligendi praesentium id officiis quam saepe?</p>
    <h2>I am a</h2>
    <ul>
        <li>Lorem, ipsum.</li>
        <li>Lorem ipsum dolor sit.</li>
        <li>Lorem, ipsum dolor.</li>
        <li>Lorem, ipsum.</li>
    </ul>
</body>
</html>
```

Before CSS : 

![screenshot-127.0.0.1_5500-2022.11.13-14_27_23.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668329861772/fLT0KIkpz.png align="center")

After CSS : 

![screenshot-127.0.0.1_5500-2022.11.13-14_28_59.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668329957517/KpftLX3pW.png align="center")


#### Individual or Element selector :

In this selector, we select a specific element on a web page that we want to style.

in an HTML file, we select only the `h1` tag and if we change the color from black to green then inside a  web page all h1 tags changed color from black to green. 

Example :

```html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iwritecode</title>
    <style>
        h1{
            background-color: blueviolet;
        }
    </style>
</head>
<body>
    <h1>Hello Smruti Ranjan Nayak,</h1>
    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Dignissimos eligendi ratione odit similique nostrum maiores dolor quas! Ipsum reiciendis soluta assumenda nesciunt dolores maiores amet pariatur ullam, culpa et odit molestiae aliquam! Molestiae optio voluptates quo? Nemo fugiat repellat eos vel necessitatibus, sequi, unde eligendi praesentium id officiis quam saepe?</p>
    <h2>I am a</h2>
    <ul>
        <li>Lorem, ipsum.</li>
        <li>Lorem ipsum dolor sit.</li>
        <li>Lorem, ipsum dolor.</li>
        <li>Lorem, ipsum.</li>
    </ul>
    <h1>I am from,</h1>
    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Dignissimos eligendi ratione odit similique nostrum maiores dolor quas! Ipsum reiciendis soluta assumenda nesciunt dolores maiores amet pariatur ullam, culpa et odit molestiae aliquam! Molestiae optio voluptates quo? Nemo fugiat repellat eos vel necessitatibus, sequi, unde eligendi praesentium id officiis quam saepe?</p>
</body>
</body>
</html>
```

Output : 

![screenshot-127.0.0.1_5500-2022.11.13-14_40_11.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668330630982/5hYk98rEt.png align="center")

#### Class and ID selector : 

These selectors are used to select the content we want to style.`ID` and `Class` both are CSS element selectors and are used to identify an element based on its assigned name. CSS id and class selectors are the most used selectors in CSS.

> We can apply a class to various elements so that it could be numerous times on a single page. The class is assigned to an element and its name starts with `.` followed by the name of the class.

> The Id is unique on a page, and we can only apply it to one specific element. The name of the Id starts with the `#` symbol followed by a unique id name.

```css
#idName{
  color:blue;
}

.className{
  color:green;
}
```

Example : 

```html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iwritecode</title>
    <style>
        .heading {
            background-color: blueviolet;
        }

        #green {
            color: green;
        }
    </style>
</head>

<body>
    <h1 class="heading">Hello Smruti Ranjan Nayak,</h1>
    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Dignissimos eligendi ratione odit similique nostrum
        maiores dolor quas! Ipsum reiciendis soluta assumenda nesciunt dolores maiores amet pariatur ullam, culpa et
        odit molestiae aliquam! Molestiae optio voluptates quo? Nemo fugiat repellat eos vel necessitatibus, sequi, unde
        eligendi praesentium id officiis quam saepe?</p>
    <h2>I am a</h2>
    <ul>
        <li>Lorem, ipsum.</li>
        <li>Lorem ipsum dolor sit.</li>
        <li>Lorem, ipsum dolor.</li>
        <li>Lorem, ipsum.</li>
    </ul>
    <h1 id="green">I am from,</h1>
    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Dignissimos eligendi ratione odit similique nostrum
        maiores dolor quas! Ipsum reiciendis soluta assumenda nesciunt dolores maiores amet pariatur ullam, culpa et
        odit molestiae aliquam! Molestiae optio voluptates quo? Nemo fugiat repellat eos vel necessitatibus, sequi, unde
        eligendi praesentium id officiis quam saepe?</p>
</body>
</body>

</html>
```

Output: 

![screenshot-127.0.0.1_5500-2022.11.13-14_49_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668331270910/F1lzYbxf1.png align="left")

Thank you very much for reading. 
Sm8uti