Learning Internal CSS| CSS Selectors | Shortcuts with Emmets | Intro to HTML5
Drop your notes and learning here.
Learning Internal CSS| CSS Selectors | Shortcuts with Emmets | Intro to HTML5
Drop your notes and learning here.
Post the emmet for
<div>
<div>
<h1></h1>
</div>
<div>
<div>
<pre></pre>
</div>
</div>
</div>
Emmet: div>(div>h1)+div>div>pre
Today’s class notes:
CSS In HTML
Types of CSS
Inline CSS
Internal CSS
External CSS
CSS Selectors
id(#) and class(.)
The HTML class attribute is used to specify a class for an HTML element.
The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name.
Here is an example for class:
.city { background-color: tomato; color: white; border: 2px solid black; margin: 20px; padding: 20px; }London is the capital of England.
Paris is the capital of France.
Tokyo is the capital of Japan.
id
The HTML id attribute is used to specify a unique id for an HTML element
The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document.
The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.
The syntax for id is: write a hash character (#), followed by an id name. Then, define the CSS properties within curly braces {}.
Note: The id name is case sensitive!
Note: The id name must contain at least one character, cannot start with a number, and must not contain whitespaces (spaces, tabs, etc.).
Example for id:
#myHeader { background-color: lightblue; color: black; padding: 40px; text-align: center; }Difference Between Class and ID
A class name can be used by multiple HTML elements, while an id name must only be used by one HTML element within the page.
Example:
/* Style the element with the id "myHeader" */ #myHeader { background-color: lightblue; color: black; padding: 40px; text-align: center; } /* Style all elements with the class name "city" */ .city { background-color: tomato; color: white; padding: 10px; }London is the capital of England.
Paris is the capital of France.
Tokyo is the capital of Japan.
order of priority
Inline>Internal>External
Element Selector- The element selector selects HTML elements based on the element name.
ID Selector- The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique element!
Can be selected using the “#” symbol.
Class Selector- The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
<div>
and target it through element selector.<div class="div-class" id="div-id"></div>
<style>
div{
background-color: red;
width: 100px;
height:100px;
}
</style>
Output-
<div class="div-class" id="div-id"></div>
<style>
div{
background-color: red;
width: 100px;
height:100px;
}
.div-class{
background-color: green;
}
</style>
Output-
Red color overridden by Green color.
<div class="div-class" id="div-id"></div>
<style>
div{
background-color: red;
width: 100px;
height:100px;
}
.div-class{
background-color: green;
}
#div-id{
background-color: magenta;
}
</style>
Output-
Now again, the green color is overridden by magenta.
Through the above example, we can see that id selector
has the highest priority, while element selector
has the lowest. Priority is fine, again why ID should be unique? Because id has a specificity score of 100, the class has 10, and the element has 1. So, if two elements has the same id, it would be nearly impossible to make them have a different style. The only way to do make them have different styles is by adding inline styles which has a specificity score of 1000(highest), which is mostly not recommended.
Reference- CSS Specificity
we use for: div>(div>h1)+div>div>pre
<h1 style="color: red;background-color: yellow; font-size: 70px;">Harshit Raj</h1>
@nikhileshyadav281199 . Thanks for putting my name. Try with your name It would look even better Good going