search
HomeWeb Front-endCSS TutorialHow CSS handles browser default styles

This time I will show you how CSS handles the default style of the browser and how CSS handles the default style of the browser. What are the precautions?The following is a practical case, let’s take a look.

Discovered

Recently when adjusting the format of the web page, I found that there is always a space of about 10 pixels at the bottom of the web page.

By using the Chrome browser to inspect elements, I found that there is a margin-botton called user agent stylesheet that sets the entire form's margin-botton:1em;

After searching for information on the Internet, I found that this user agent stylesheet is some of the default styles of the browser. If you are not satisfied with this default style, the simplest solution is to reset the attributes of the style. Because the user agent stylesheet has a very low priority and will be overwritten, it will not affect our style. For example, here I will talk about form. The margin-bottom is set to 0px.

User Agent Stylesheet Introduction

Different browsers have different default styles for the same elements, which is why we need to Write * {padding:0;marging:0};

But that’s not all I’m talking about now. Basically, two browsers with different kernels will have differences in the performance of certain elements, such as indent size, font selection, character style, etc. Maybe a beautiful CSS style sheet performs well in one browser, but becomes cluttered in another browser even without CSS bugs. I think it's the browser's default style that's to blame.

Therefore, when we generate CSS style rules, one of the necessary steps is to reset the browser's default style, that is, to overwrite the browser's default style. Different from using * {padding:0;margin:0}, not all elements have differences in padding and margin (the difference between element lists and ordered lists in Internet Xplorer and Firefox is due to their indentation. margin indentation and padding indentation).

For example, the following paragraph:

The font used by this code in Internet Explorer is Times New Roman, while the font used in Firefox and Opera is System default font. Therefore, we need to set a unified style in CSS for

.

However, if we use the wildcard "*" to simply set the global style like using * {padding:0;margin:0}, then an obvious problem will arise, such as form Elements, input elements, textareas, etc. will ignore their reset in some browsers. More importantly, this will seriously damage the appearance of these elements, so you have to manually reset them one by one. Set a padding value and margin value. So we should give up simply using "*" and reset the first element with inconsistent performance, such as body, p, dl, dt, dd, ul, ol, li, h1, h2, h3, h4 ,h5,h6,pre etc.

At the same time, the default styling of elements may ruin the appearance of the page. For example, the element will bold the text, the

will indent the text, and the will tilt the text. If you want the text on the page to have a consistent appearance, you should also change the appearance of these elements in CSS. reset. At the same time, sometimes we require the appearance of these elements to be the same as the parent element. We can directly use inherit to inherit from the parent element.

Processing method

As for which elements should be reset? Yahoo! has made a comparative summary for us. According to Yahoo's suggestion, you need to put these rules into a file called Reset.css and reference them separately (this is recommended):

html{color:#000;background:#FFF;} 
body,p,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form, 
fieldset,input,textarea,p,blockquote,th,td { 
margin:0; 
padding:0; 
} 
table { 
border-collapse:collapse; 
border-spacing:0; 
} 
fieldset,img { 
border:0; 
} 
address,caption,cite,code,dfn,em,strong,th,var { 
font-style:normal; 
font-weight:normal; 
} 
ol,ul { 
list-style:none; 
} 
caption,th { 
text-align:left; 
} 
h1,h2,h3,h4,h5,h6 { 
font-size:100%; 
font-weight:normal; 
} 
q:before,q:after { 
content:''; 
} 
abbr,acronym { border:0; 
}

All you have to do is simply save these rules to reset .css and then use it in the page. When you need to add new styles to these elements, it is no different from the settings of other elements.

Note: Input, textarea, select{*font-size:100%;} in reset.css above can only be recognized by Internet Explorer. Such settings This is to enable the font size of form controls to be scaled in Internet Explorer.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Use HTML+CSS to implement drop-down menu

Use CSS3 to create a progress bar

The above is the detailed content of How CSS handles browser default styles. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
A Little Reminder That Pseudo Elements are Children, Kinda.A Little Reminder That Pseudo Elements are Children, Kinda.Apr 19, 2025 am 11:39 AM

Here's a container with some child elements:

Menus with 'Dynamic Hit Areas'Menus with 'Dynamic Hit Areas'Apr 19, 2025 am 11:37 AM

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

Improving Video Accessibility with WebVTTImproving Video Accessibility with WebVTTApr 19, 2025 am 11:27 AM

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

Weekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteWeekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteApr 19, 2025 am 11:25 AM

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

Making width and flexible items play nice togetherMaking width and flexible items play nice togetherApr 19, 2025 am 11:23 AM

The short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.

Position Sticky and Table HeadersPosition Sticky and Table HeadersApr 19, 2025 am 11:21 AM

You can't position: sticky; a

Weekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryWeekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryApr 19, 2025 am 11:18 AM

In this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties

IndieWeb and WebmentionsIndieWeb and WebmentionsApr 19, 2025 am 11:16 AM

The IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!