close
The Wayback Machine - https://web.archive.org/web/20240926155525/https://www.geeksforgeeks.org/inline-css/
Open In App

Inline CSS

Last Updated : 22 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Inline CSS is a method that applies CSS styling directly to HTML elements using the ‘style’ attribute. This approach allows developers to define styles for individual elements, making it an effective tool for applying unique styles to specific HTML elements.

Syntax:

<tag style = " "></tag>

When to Use Inline CSS ?

When Should You Use Inline CSS? Inline styles are particularly useful in web development when you need to apply specific, one-off styles to an element. This approach ensures that global styles remain unaffected. Additionally, inline styles are ideal when you need to generate dynamic styles programmatically, based on data or user interactions.

How to use inline CSS ?

To use inline CSS, add a “style” attribute to an HTML element and define CSS properties and values within double quotes, Inline CSS has the highest priority out of external, internal CSS, and inline CSS.

Examples of Inline CSS

This example demonstrates the styling like color, font and text alignment using inline css

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Inline CSS</title>
</head>

<body>
    <h1 style="color: green; 
               font-size: 60px; 
               text-align: center;">
        GeeksforGeeks
    </h1>
    <p style="color: gray; 
              font-size: 25px; 
              text-align: center;">
        A computer science portal..!
    </p>
</body>

</html>

Output:

Inline CSS Example output

In this example, we use inline CSS to provide colors to your given p tag.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Inline CSS</title>
</head>

<body>
    <p style="color: green;">
        HTML
    </p>
    <P style="color: red;">
        CSS
    </P>
    <p style="color: blue;">
        JavaScript
    </p>
</body>

</html>

Output:

Inline CSS example

Advantages

  • Using style attributes we can provide styles directly to our HTML elements.
  • Inline CSS Overrides external and internal styles with higher specificity.
  • No need to create and upload a separate document as in the external style.
  • Inline styles have high specificity, allowing precise control over individual elements.
  • Enables dynamic style changes using JavaScript or server-side logic.
  • Inline styles don’t require separate CSS files, potentially reducing HTTP requests.

Disadvantages

  • Adding style attributes to every HTML element is time-consuming.
  • Styling multiple elements can increase your page’s size and download time, impacting overall page performance.
  • Reduced separation of concerns between HTML structure and CSS.
  • Inline styles cannot be used to style pseudo-elements and pseudo-classes.
  • It can be difficult to maintain consistency and make global style updates.

Conclusion

In conclusion, Inline CSS is useful for applying unique styles to individual HTML elements. However, it’s important to use it judiciously to maintain clean and manageable code. Remember, while Inline CSS offers high specificity, it should be used sparingly to avoid bloating your HTML and complicating style updates.



Similar Reads

What is the difference between display: inline and display: inline-block in CSS?
The display property in CSS is a very useful and commonly used property that contains many values. The display property defines how an HTML element should be displayed on the webpage. The property also specifies the type of box used for an HTML element and how it should be laid out on the page. If we need to display the elements that are laid out a
4 min read
How display: inline is different from display: inline-block in CSS ?
In this article, we will know about the display property in CSS, along with understanding the 2 different property values for display property, i.e., display: inline & display: inline-block properties, & will understand their basic differences & implementation through the examples. The display property facilitates setting the element by
3 min read
What is the difference between inline-flex and inline-block in CSS?
The display property specifies how an element should be displayed on a webpage. There can be many values, related to this property in CSS. Inline-block and inline-flex are two such properties. Although there are several values that this property can have, to understand the aforementioned, let us first look into three other values: inline, block, an
3 min read
CSS | border-inline-start-style Property
The border-inline-start-style property is an inbuilt property in CSS it is used to define the individual logical inline-end border property values in a single place in the style sheet. This property sets the border-style at the left of the defining element border. Syntax: border-inline-start-style: border-style; Property values: border-style: This
1 min read
CSS | margin-inline Property
The margin-inline property is used to define the logical inline start and end margin of an element. This property helps to place margin depending on the element's writing mode, directionality, and text orientation. Syntax: margin-inline: length | auto | initial | inherit | unset; Property values: length: It sets a fixed value defined in px, cm, pt.
2 min read
CSS | margin-inline-start Property
The margin-inline-start property in CSS is used to define the logical inline start margin of an element. This property helps to place margin depending on the element's writing mode, directionality, and text orientation. Syntax: margin-inline-start: length | auto | initial | inherit | unset; Property values: length: It sets a fixed value defined in
2 min read
CSS | border-inline-style Property
The border-inline-style property is an inbuilt property in CSS which is used to set the individual logical block inline-border-style property values in a single place in the style sheet. It sets the inline border-style top(left), and bottom(right) of the defining border element. Syntax: border-inline-style: style; Property values: style: This prope
1 min read
CSS | border-inline Property
The border-inline property in CSS is used to set the individual logical block inline border property values in a single place in the style sheet. It sets the inline border-top(left) and bottom(right) of the defining element. Syntax: border-inline: border-width| border-style| border-color; Property values: border-width: This property value holds the
2 min read
CSS | border-inline-end-color Property
The border-inline-end-color property in CSS is used to define the individual logical inline-end border-color property values in a single place in the style sheet. This property sets the border-color at the bottom of the defining element border. Syntax: border-inline-end-color: border-color; Property values: border-color: This property holds the col
1 min read
CSS | border-inline-end Property
The border-inline-end property in CSS is used to define the individual logical inline-end border property values in a single place in the style sheet. This property place the border at the bottom of the defining element. Syntax: border-inline-end: border-width|border-style|border-color; Property values: border-width: This property value holds the w
2 min read
CSS | padding-inline-start Property
The padding-inline-start property in CSS is used to define the logical block start padding of an element. This property helps to place padding depending on the element’s writing mode, directionality, and text orientation. Syntax: padding-inline-start: length|percentage|auto|inherit|initial|unset; Property values: length: It sets a fixed value defin
2 min read
CSS | padding-inline Property
The padding-inline property in CSS is used to define the logical inline start and end padding of an element. This property helps to place padding depending on the element’s writing mode, directionality, and text orientation. Syntax: padding-inline: length|percentage|auto|inherit|initial|unset; Property values: length: It sets a fixed value defined
2 min read
CSS | inset-inline-end Property
The inset-inline-end property is an inbuilt property in CSS. It is used to define logical inline end offset, not for the block offset or logical block. This property can apply to any writing-mode property. Syntax: inset-inline-end: length|percentage|auto|inherit|initial|unset; Property values: length: It sets a fixed value defined in px, cm, pt etc
2 min read
CSS | inset-inline-start Property
The inset-inline-start property in CSS is used to define logical inline start offset, not for the block offset or logical block. This property can apply to any writing-mode property. Syntax: inset-inline-start: length|percentage|auto|inherit|initial|unset; Property values: length: It sets a fixed value defined in px, cm, pt etc. Negative values are
2 min read
CSS | inset-inline Property
The inset-inline property in CSS is used to define the logical inline start and end offset, not for the block offset or logical block. This property can apply to any writing-mode property. Syntax: inset-inline: length|percentage|auto|inherit|initial|unset; Property values: length: It sets a fixed value defined in px, cm, pt etc. Negative values are
2 min read
CSS | border-inline-start-width Property
The border-inline-start-width property in CSS is used to define the individual logical inline-end border property values in a single place in the style sheet. This property sets the border-width at the left of the defining element border. Syntax: border-inline-start-width: border-width; Property values: border-width: This property holds the width o
1 min read
CSS | border-inline-start Property
The border-inline-start property in CSS is used to define the individual logical inline-end border property values in a single place in the style sheet. This property place the border at the left of the defining element. Syntax: border-inline-start: border-width| border-style| border-color; Property values: border-width: This property value holds t
2 min read
CSS | border-inline-start-color Property
The border-inline-start-color property in CSS is used to define the individual logical inline-end border property values in a single place in the style sheet. This property sets the border-color at the left of the defining element border. Syntax: border-inline-start-color: border-color; Property values: border-color: This property holds the color o
1 min read
CSS | border-inline-end-style Property
The border-inline-end-style property in CSS is used to define the individual logical inline-end border property values in a single place in the style sheet. This property sets the border-style at the bottom of the defining element border. Syntax: border-inline-end-style: border-style; Property values: border-style: This property holds the style of
1 min read
CSS | border-inline-end-width Property
The border-inline-end-width property in CSS is used to define the individual logical inline-end border property values in a single place in the style sheet. This property sets the border-width at the bottom of the defining element border. Syntax: border-inline-end-width: border-width; Property values: border-width: This property holds the width of
1 min read
CSS | border-inline-color Property
The border-inline-color property in CSS is used to set the individual logical inline border-color property values in a single place in the style sheet. It sets the border color top, and bottom of the defining border element. Syntax: border-inline-color: color; Property values: color: This property holds the color of the border. Below examples illus
1 min read
CSS | inline-size Property
The inline-size property in CSS is used to define the horizontal or vertical size of an element's block. It coincides with the width or the height property, depending on the value of the writing-mode property. It leaves the space around the element text. Syntax: inline-size: length | percentage | auto | inherit | initial | unset Property values: le
2 min read
CSS | padding-inline-end Property
The padding-inline-end property in CSS is used to define the logical block end padding of an element. This property helps to place padding depending on the element’s writing mode, directionality, and text orientation. Syntax: padding-inline-end: length|percentage|auto|inherit|initial|unset; Property values: length: It sets the fixed value defined i
2 min read
How to Shift Inline Elements When Text Bold on Hover using CSS ?
When we will use a:hover pseudo-class to add a bold effect to the inline elements then we observe that whenever we hover over elements with the mouse, the elements to the right side of the mouse gets shifted to the right. This is not a good user experience thus needs to be removed. We can use the letter-spacing CSS property to fix this issue and ma
2 min read
CSS | min-inline-size Property
The CSS min-inline-size property is used to create the minimum size of an element in the direction opposite that of the writing direction. Like if the writing direction is horizontal then min-inline-size is equivalent to min-height, and if it is in vertical mode then equal to min-width. Syntax: min-inline-size: length | percentage | auto | none | m
2 min read
How to display paragraph elements as inline using CSS ?
The purpose of this article is to display paragraph elements as inline elements using CSS. The display property in CSS is used for placing the components ("div", "hyperlink", "heading", etc) on the web page. The display property is set to inline. It has the default property of "anchor" tags. It is used to place the "div" inline i.e. in a horizontal
1 min read
How to apply inline CSS ?
In this article we will learn how to apply inline CSS, Inline CSS contains the CSS property in the body section attached to the element is known as inline CSS. This kind of style is specified within an HTML tag using the style attribute. It is used to apply a unique style to a single HTML element. Syntax: <tag style = " "></tag> Example
1 min read
Foundation CSS Forms Inline Labels and Buttons
Foundation CSS is an open-source & responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing & can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
3 min read
Primer CSS Inline Styles
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
2 min read
Primer CSS Inline Progress
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by object-oriented CSS principles, functional CSS, an
2 min read