Home >Web Front-end >Front-end Q&A >css clear inherited styles
With the development of CSS, web design has become more and more diversified, and style inheritance has become an essential feature. However, in some cases, we sometimes want to clear inherited styles to have more flexible control over the appearance and layout of the page. This article will introduce you to several methods to clear inherited styles.
1. Use reset style sheet
The function of resetting the style sheet is to clear the styles of all elements while retaining the browser's default style. Resetting the style sheet will forcefully reset the styles of all elements and redefine the styles with the basic CSS framework to achieve the purpose of rebuilding the custom style sheet.
The following is the code to reset the style sheet:
/* Reset Styles */ html, body, div, span, applet, object, iframe, /* base elements */ h1, h2, h3, h4, h5, h6, p, blockquote, pre, /* headers */ a, abbr, acronym, address, big, cite, code, /* text markup */ del, dfn, em, img, ins, kbd, q, s, samp, /* text formatting */ small, strike, strong, sub, sup, tt, var, /* text formatting */ b, u, i, center, /* misc inline */ dl, dt, dd, ol, ul, li, /* lists */ fieldset, form, label, legend, /* forms */ table, caption, tbody, tfoot, thead, tr, th, td { /* tables */ border: 0; font-size: 100%; font: inherit; vertical-align: baseline; margin: 0; padding: 0; }
When we use the reset style sheet, the styles of all elements will be cleared, but it should be noted that when using the reset style After the table, we need to redefine all styles and layouts to ensure the correctness of the web page.
2. Use the tag selector
In CSS, the tag selector is the most basic selector and can be easily applied to all elements of the document. By using tag selectors, we can clear inherited styles and redefine the styles we need.
The following is the code to clear the inherited styles using the tag selector:
p { margin: 0; padding: 0; font-size: 16px; line-height: 1.5; font-weight: 400; color: #333; }
In the above example, we can see that we use the p tag selector, clear the inherited styles, and re- Define the style you need. This is a very practical method when we need to clear the inherited styles of a single element.
3. Use the !important keyword
!important keyword is a common CSS style modifier that can be used to force the application of styles under specific circumstances. It can override other style rules and prevent changes.
The following is the code to use !important to clear inherited styles:
p { margin: 0!important; padding: 0!important; font-size: 16px!important; line-height: 1.5!important; font-weight: 400!important; color: #333!important; }
In the above example, we can see that we use the !important keyword to override other style rules and in specific cases Force the application of styles below. This is a very practical method when we need to force a style to be applied.
4. Use selectors
In CSS, selectors refer to patterns used to select HTML elements. We can use selectors to clear inherited styles and redefine the styles we need.
The following is the code for using a selector to clear inherited styles:
div.header p { margin: 0; padding: 0; font-size: 16px; line-height: 1.5; font-weight: 400; color: #333; }
In the above example, we can see that we use a selector to select all p elements in the header element and clear Their inherited styles and redefine the desired styles. This is a very practical method when we need to clear the inherited styles of elements, especially nested elements.
Summary:
This article introduces four methods to clear inherited styles. Use reset stylesheet, use tag selector, use !important keyword and use selector. In actual development, we choose the corresponding method according to our own needs in order to more flexibly control the appearance and layout of the page.
The above is the detailed content of css clear inherited styles. For more information, please follow other related articles on the PHP Chinese website!