• Articles
  • Tutorials
  • Interview Questions

How to Use Internal CSS in HTML?

How to Use Internal CSS in HTML?

Before delving into Internal CSS, it is essential to have a foundational understanding of HTML (Hypertext Markup Language), which is used to structure the content of webpages. Familiarity with CSS (Cascading Style Sheets) is also beneficial, as Internal CSS builds upon the principles of CSS to define the visual presentation of HTML elements within a single HTML file.

Table of Contents:

Watch this video below to understand Full Stack Web Development in detail

Video Thumbnail

What is Internal CSS?

Internal CSS, also known as embedded CSS, is a valuable technique in web development that allows developers to define and apply styles directly within an HTML file. By incorporating style declarations within the <style> tags, web designers can streamline the process of styling individual web pages without the need for external CSS files. 

Internal CSS involves the inclusion of style rules within the HTML document itself, typically within the <head> section. The styles defined within the <style> tags are specific to the current webpage and override any external stylesheets. This approach grants web developers greater control over the visual presentation of their web pages.

Example 1: Applying Styles to HTML Elements

<!DOCTYPE html>
<html>
<head>
  <title>Internal CSS Example</title>
  <style>
    h1 {
      color: blue;
      font-size: 24px;
    }
    p {
      color: red;
      font-size: 16px;
    }
  </style>
</head>
<body>
  <h1>Welcome to Internal CSS!</h1>
  <p>This paragraph is styled using internal CSS.</p>
</body>
</html>

Within this illustration, the <style> tags encompass CSS rules employed to format the <h1> and <p> elements. The color property establishes the text color, while the font-size property is utilized to determine the font size for the corresponding elements.

Example 2: Targeting Classes and IDs

<!DOCTYPE html>
<html>
<head>
  <title>Internal CSS Example</title>
  <style>
    .red-text {
      color: red;
      font-weight: bold;
    }
    #special-heading {
      font-size: 24px;
    }
  </style>
</head>
<body>
  <h1 id="special-heading">Internal CSS Example</h1>
  <p class="red-text">This paragraph has red text.</p>
</body>
</html>

In this example, the CSS rules target classes and IDs. The .red-text class is used to set the text color to red and applies bold font-weight, while the #special-heading ID sets a larger font size for the heading.

  • Simplified Structure: With Internal CSS, developers can consolidate their styles within a single HTML file, simplifying the file structure and making it easier to manage and maintain.
  • Specificity and Priority: Internal CSS takes precedence over external stylesheets, allowing developers to override global styles and customize the appearance of specific elements within a webpage.
  • Code Portability: Since Internal CSS resides within the HTML file, it eliminates the need to manage separate CSS files. This makes it convenient to share or transfer HTML files along with their associated styles.

Acquire fundamental and advanced skills in Web Development through enrollment in one of the top-rated online web development courses.

Get 100% Hike!

Master Most in Demand Skills Now!

How Does Internal CSS Work?

How Does Internal CSS Work?

Internal CSS (Cascading Style Sheets) is a powerful tool used to define the visual presentation and layout of HTML documents. Internal CSS allows web developers to control the appearance of web pages by specifying style rules within the HTML document itself. Here are the following steps:

Step 1: Defining the Internal Style Sheet To begin, open the HTML document using any text editor. Inside the <head> tag, create a <style> element. This element acts as a container for the internal CSS rules.

Step 2: Selecting HTML Elements To apply styles to specific HTML elements, you must select them using CSS selectors. Selectors can target elements based on their tag name, class, or ID. For instance, to select all paragraphs, use the selector “p”, while to select an element with a specific class, use “.classname”.

Step 3: Defining Style Rules Within the <style> element, write CSS rules to define the desired styles for the selected elements. Each rule consists of a selector followed by one or more declarations enclosed in curly braces. Declarations consist of a property and a value. For example, to set the font size of all paragraphs to 16 pixels, use the rule “p { font-size: 16px; }”.

Step 4: Applying Styles to HTML Elements Once the style rules are defined, they are automatically applied to the selected HTML elements. For instance, if you set the background color of all paragraphs to yellow, all paragraphs in the HTML document will have a yellow background.

Step 5: Saving and Linking the Document Save the HTML document with a .html extension. To view the styled document in a web browser, simply open it by double-clicking the file. The styles defined in the internal CSS will be applied.

Internal CSS offers a convenient way to style HTML documents by keeping the styles within the document itself. By following the steps outlined above, you can effectively utilize internal CSS to control the presentation and layout of your web pages. Experimenting with different styles and selectors will allow you to achieve the desired visual effects, creating visually appealing and engaging websites.

<!DOCTYPE html>
<html>
<head>
  <title>Internal CSS Example</title>
  <style>
    /* Internal CSS rules */
    p {
      font-size: 16px;
      color: blue;
    }
    h1 {
      color: red;
    }
  </style>
</head>
<body>
  <h1>Welcome to My Website</h1>
  <p>This is a paragraph of text.</p>
  <p>This is another paragraph.</p>
</body>
</html>

In the above example, the internal CSS is used to set the font size and color of paragraphs and the color of the heading. When the HTML document is opened in a web browser, the paragraphs will appear in blue with a font size of 16 pixels, and the heading will be red.

Gain a solid understanding of web development fundamentals by exploring our tutorial on Web Development.

Inline CSS Vs. Internal CSS Vs. External CSS

Inline CSS Vs. Internal CSS Vs. External CSS

Each method has its own use case based on the scope and requirements of your web project. Let’s take a closer look at each of these methods and their vital differences:

ParameterInline CSSInternal CSSExternal CSS
LocationThe CSS code is placed directly within the HTML tags using the “style” attribute.The CSS code resides within the <style> tags within the HTML document.The CSS code is stored in a separate CSS file, which is linked to the HTML document.
ScopeApplies only to the specific HTML element it is defined in.Applies to the entire HTML document, affecting all elements within it.Applies to the entire HTML document, impacting all linked elements across multiple HTML pages.
Code ReusabilityNot reusable. Each element requires its own inline CSS declaration.Reusable within the same HTML document but not across multiple documents.Highly reusable. CSS rules can be applied to multiple HTML documents by linking to the external CSS file.
ReadabilityDecreases the readability of HTML due to the mixing of style and content.Improves the readability of HTML by separating the style rules from the content.Improves the readability of HTML by maintaining a clear separation between style and content.
MaintenanceDifficult to maintain as each element’s style is defined individually.Easier to maintain as style rules can be updated centrally within the HTML document.Easier to maintain as changes can be made in a separate CSS file, affecting all linked HTML documents
Loading SpeedThere is no impact on the loading speed as the styles are applied directly within the HTML.It slightly impacts the loading speed as the styles are parsed along with the HTML document.Offers faster loading speed as the CSS file is cached by the browser and can be reused across multiple HTML pages.
FlexibilityProvides limited flexibility and makes it difficult to implement complex styles.Offers moderate flexibility, allowing more complex styles to be defined within the HTML document.Provides high flexibility, allowing comprehensive and complex styles to be defined in a separate CSS file.

Explore the Tooltip in CSS blog and incorporate its concepts into your project.

Conclusion

In today’s fast-paced digital world, users expect web pages to load within just a few seconds. To meet these expectations, businesses need to prioritize speed. CSS plays a critical role in achieving a fast and seamless online experience.

You might have noticed many visually appealing and user-friendly websites during your online browsing. One common characteristic among these websites is the consistent design elements they exhibit. This consistency is made possible through the use of CSS by developers. By utilizing CSS, developers can ensure that style components are applied consistently across multiple web pages, resulting in a cohesive and visually pleasing user experience.

Course Schedule

Name Date Details
Python Course 30 Nov 2024(Sat-Sun) Weekend Batch View Details
07 Dec 2024(Sat-Sun) Weekend Batch
14 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.