Open In App

What is a User Agent Stylesheet ?

A user agent stylesheet is a pre-defined set of CSS rules that are built into a web browser and applied to web pages automatically. The purpose of the user agent stylesheet is to provide a consistent and standardized visual rendering of HTML elements across different web pages and websites. The rules in the user agent stylesheet define how HTML elements should be displayed by default, including things like font size, color, spacing, and alignment.

How does the user agent stylesheet work?



Syntax:

selector {
    property: value;
    property: value;
    property: value;
}

The syntax may vary depending on the browser being used. The syntax used in the current version of chrome is as follows:



 /*Syntax*/
html {
    font-family: sans-serif;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    margin: 8px;
    background-color: white;
    color: black;
    font-family: -apple-system, BlinkMacSystemFont, 
                 "Segoe UI", Roboto, Oxygen-Sans, 
                 Ubuntu, Cantarell, "Helvetica Neue", 
                 sans-serif;
    font-size: 16px;
    line-height: 1.5;
}

h1, h2, h3, h4, h5, h6 {
    margin-top: 0;
    margin-bottom: 0.5em;
    font-weight: bold;
    line-height: 1.2;
}

a {
    color: #1a0dab;
    background-color: transparent;
    text-decoration: none;
}

Example 1: In this example, we will give you a basic understanding that what an actual user-agent stylesheet is:




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="UTF-8">
    <title>User Agent Stylesheet Example</title>
</head>
  
<body>
    <h1>Welcome To GeeksforGeeks</h1>
</body>
  
</html>

Output:

What is a user agent stylesheet?

In this example, we have a very basic HTML document with just an h1 element inside the body section. We have not defined any styles for the h1 element in our own stylesheet. When we load this page in a web browser and inspect the h1 element with the developer tools, we will see that it has some default styles applied to it. These styles are coming from the user agent stylesheet. The specific styles that are applied will depend on the browser that we are using. For example, in Google Chrome, the h1 element will have a font size of 2em, a margin-top of 0.67em, and a margin-bottom of 0.67em, among other styles.

Example 2: Another example to get more clarity about the user-agent stylesheet:-




<!DOCTYPE html>
<html>
  
<head>
    <title>User Agent Stylesheet Example</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            text-align: center;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <p>A computer science portal for Geeks</p>
</body>
  
</html>

Output:

What is a user agent stylesheet?

In this example, we have an HTML document with a head section that includes a style tag. Inside the style tag, we define some basic styles for the body and h1 elements. We set the margin and padding of the body element to 0 and text-align as the center, and we set the color of the h1 element to green. We have also added a p element to the body section. We have not defined any styles for the p element in our own stylesheet. When we load this page in a web browser and inspect the p element with the developer tools, we will see that it has some default styles applied to it. These styles are coming from the user agent stylesheet.


Article Tags :