Home >Web Front-end >HTML Tutorial >Div CSS rules organization_html/css_WEB-ITnose
1. Make good use of CSS abbreviation rules
/*Pay attention to the writing order of top, right, bottom and left*/
1. About margins (4 sides):
1px 2px 3px 4px (top, right, bottom, left)
1px 2px 3px (the omitted left is equal to right)
1px 2px (the omitted top is equal to bottom)
1px (all four sides are the same)
2. Simplify everything:
*/ body{margin:0}------ -----Indicates that the margin of all elements in the web page is 0
#menu{ margin:0}------------Indicates that the margin of all elements under the menu box is 0
3. Abbreviation (border) specific style:
Border:1px solid #ffffff;
Border-width:0 1px 2px 3px;
4. About the abbreviation rules of text:
Font-style:italic; italic form
Font-variant:small-caps/normal; change Font style: small caps/normal
Font-weight:bold;
Font-size:12px;
Line-height:1.2em(120%)/1.5 em(150%);
Font-family:arrial,sans-serif,verdana;
abbreviated to:
Font:italic small-caps bold 12px/1.5em arrival,sans-serif;
Note: Font-size and Line-height are combined with slashes and cannot be written separately.
5. About the background image:
Background:#FFF url(log.gif) no-repeat fixed top left;
6. About the list:
List-style-type:square/none;
List-style-position:inside;
List-style-image :url(filename.gif);
is abbreviated to:
List-style:none inside url(filename.gif);
2. Use 4 methods to introduce CSS styles
1.link
rel relationship
type data type, there are many
href path
Some browsers support candidate styles ,Keywords: alternate:
2. Internal style block
3.@import
@import url{a.css}
Note: This directive must be placed in < ;style> in the container, and before all styles
is recommended to be placed in an html comment, The browser will not display the content in the comment, but css codes such as import can work normally
4. Inline style
Selector is a basic concept of CSS, the basic rules are as follows:
1. Rule structure:
h1 {color:red;}
Selector {Attribute: value;}
This type is an element selector, Basically can include all html elements
attribute value can include multiple elements, such as: border: 1px solid red;
Common syntax
1) Grouping:
Both selectors and declarations can be grouped:
h1,h2,h3{color:red;background:#fff;}, selectors are separated by "," and attributes are separated by ";"
2) Class selector, that is, a declaration applied through class="stylename"
Definition:
.stylename{color:red;}
Note:
You can use multiple categories of selection in html: such as class="cn1 cn2 cn3"
3) ID selector, that is, the style corresponding to the id attribute
definition:
#a{color:red;} ->This definition applies to elements with id="a"
2. This part is our common css syntax. Let’s talk about it below Uncommon selector syntax
1) Parent-child structure, corresponding to the document structure diagram
For example, p span{border:1px solid red;} corresponds to
below< li> tag, this is very useful and can be accurately positioned.
Some special applications (supported by IE7):
(1) p > span{}, matches all spans under all p
(2) p span{}, matches the first span tag that appears immediately after the p element. The two must have the same parent tag
2) Attribute selector: (note :The attribute selector is only supported by IE7, and the following versions do not support it. Other browsers can basically support it)
Syntax: img[alt]{border:1px solid;}
means that the corresponding The img tag with alt attribute can of course support multiple attribute correspondences, such as img[alt][title]{}; indicating that the img tag has both attributes, and can also correspond to specific values: such as: img[alt=” Photography"]{};
Advanced application in attribute selector, special matching:
(1)img[class~=”b”], ~= : corresponds to a value in the attribute, that is, corresponds to
(2) [class^=”a”],
(3) starting with a, [class$=”a”],
(4) ending with a [class*=”a ”],
(5)[class|=”a”] including a,
equal to a or starting with a 3) Pseudo-class and pseudo-element
In daily use, there are mainly several pseudo-classes of the tag: link:hover:active:visited
and: first-child:first:before:left:right:lang:focus:fist-line Wait
Note: Dynamic pseudo-classes can be applied to any element, such as input:focus{background:red;} When the input tag gets focus, the background turns red
Used in combination with the above syntax, You can achieve accurate positioning, simple and indirect style.
3. Selector classification integration
Priority level follows: Inline style>ID > Class >Mark
| Tag selector (eg:) | |||||||||
Category selector (eg:class) | ||||||||||
ID selector | ||||||||||
Compound selector | "Intersection" compound selector (eg:p.menu{color:red}) must be a tag category/ID combination | |||||||||
"Union" compound selector (eg:h1,h2,h3{color:red}) | ||||||||||
"descendant" compound selector (eg: #menu .menulist{ ... }) | ||||||||||
"child ” Compound selector (eg: #menu .menulist .selectit { ... }) |
四、 使用子选择器减少id和class的定义
示例结构:
Example CSS:
#menu { ... }
#menu .menulist { ... }
#menu .menulist .selectit { ... }
5. Use the group selector to apply the same style to different elements
Such as h1,h2,h3,div{font-size:16px ;font-weight:bold}
Then the styles of h1, h2, h3, and div elements are all 16 pixels in font, bold font
6. Pseudo-classes and Use selectors together
By combining pseudo-classes and classes, you can create several groups of different link effects on the same page. For example, we define a group of links to be red and blue after access. color; the other group is green and yellow after visiting:
a.red:link {color: #FF0000}
a.red:visited {color: #0000FF}
a.blue:link {color: #00FF00}
a.blue:visited {color: #FF00FF}
now applies to different links:
This is the first set of links
This is the second set of links
7. CSS closest-first principle
/*If multiple styles are defined for an element, Then the most recent level will take precedence, and the most recent level of style will overwrite other Inline Styles >ID > Class > Tags */
The following is a reference fragment:
CSS:
p{color:red}
.blue{color:blue}
.yellow{color:yellow}
HTML:
This is red
This is blue
This is green
This is yellow
Note:
(1) Pay attention to several priorities of styles (the priorities decrease from top to bottom, and the styles below cover the styles above):
<<>
-Element style setting
-Head area & lt; style & gt; & lt;/style & gt; 🎜>
- Externally referenced css files
(2) The priority is not set by the access order, but by the declaration order in css of.
As in the above example,
is displayed as yellow here
is also displayed as yellow, because in the css definition, .yellow is in. behind blue.
8. Write the correct link style
When using CSS to define the various states of the link, pay attention to the order of writing: :link :visited :hover : active uses the initials: L V H A. You can remember the order by memorizing LoVe, Hate, two words.
:link --------Color of link
:visited -----Color after mouse click
:hover ----- --The color when the mouse is placed but not clicked (hover)
:active-------The color when the mouse is clicked
9. :hover Flexible use of
IE6 does not support the :hover attribute other than the a tag. We understand that the :hover attribute is the mouse hover effect. In IE7 and FF, you can set the :hover attribute effect on almost any element. This works great for us doing different visits.
Such as:
p {
width : 360px;
height : 80px;
padding : 20px;
margin : 50px auto 0 auto;
border : 1px solid #ccc;
line-height : 25px;
background : #fff;
}
p:hover {
border : 1px solid #000;
background : #ddd;
}
- ---------------This effect is for IE7 and FF
p a {
color: #00f;
text-decoration: none;
font-size : 13px;
}
p a:hover {
color : #036;
text- decoration: underline;
}
-----------------This effect is for IE6
10. Small issues to pay attention to when defining the A tag
When we define a{color:red;}, it represents the styles of the four states of A. If you want to define a state where the mouse is placed, just define a:hover. The other three states It is the style defined in A.
When only one a:link is defined, be sure to remember to define the other three states!
11. Prohibiting content from wrapping and forcing content to wrap
In tables or layers we may want the content not to wrap or to force wrapping. We can do this through some css attributes meet these requirements.
Prohibit line breaks: white-space:nowrap
Force line breaks: word-break: break-all; white-space: normal;
十2. The difference between relative and absolute
Absolute---The writing method in CSS is: position:absolute; It means absolute positioning. It refers to the upper left corner of the browser and cooperates with TOP, RIGHT, BOTTOM, and LEFT (hereinafter referred to as TRBL) for positioning. If TRBL is not set, the original point will be the original point of the parent by default. If TRBL is set and the parent does not set the position attribute, then the current absolute will be positioned with the upper left corner of the browser as the original point, and the position will be determined by TRBL.
Relative---The writing method in CSS is: position:relative; It means absolute relative positioning. It refers to the original point of the parent as the original point. If there is no parent, the original point of BODY is used as the original point. The original point is positioned with TRBL. When there are CSS attributes such as padding in the parent, the original point of the current level is positioned with reference to the original point of the parent content area.
13. Difference between block-level elements block and inline elements inline
Block level---definable width and height, exclusive from a separate One line (such as: div ul)
Inline---the width and height cannot be defined, such as text elements (such as a span)
14. The difference between display and visibility
Both display:none and visibility:hidden can hide an element, but visibility:hidden only hides the content of the element, but the position space used is still retained. Display:none is equivalent to removing the element from the page, and its occupied position will also be deleted.
15. Some syntax of background
background-image:url(background pattern.jpg, gif, bmp);
background- color:#FFFFFF; (Background color)
background-color : transparent; Settings
Description
repeat Background images are side by side
repeat-x Background images are side by side in the X direction
repeat-y Background images are side by side in the Y direction
no-repeat The background image is not processed side by side
Background-attachment Whether the image position is fixed
Description
Scroll When the scroll is pulled, the background image will Move along (default value)
fixed When the scroll is pulled, the background image will not move along
Position background-position by length: x y
Use percentage to position background-position : x% y%
Description
x% Move right
y% Move down
backgroud-position: 0% 0%; left Above
backgroud-position: 0% 50%; Middle left
backgroud-position: 50% 0%; Above middle
backgroud-position: 50% 50%; Center
backgroud-position: 100% 0%; Upper right
backgroud-position: 0% 100%; Lower left
backgroud-position: 100% 50% ; Right middle
backgroud-position: 50% 100%; Middle lower
backgroud-position: 100% 100%; Right lower
with Keyword positioning
Keyword Description
top top ( y = 0 )
center middle ( x = 50, y = 50 )
bottom bottom ( y = 100 )
left Left ( x= 0 )
Exp:
background-position:center;
The image is at X=50% Y=50% position in the center of the specified background
background-position: 200px 30px
16. How to write comments
In Html:
<-- footer -->
content<-- end footer -->
in CSS Center:
/* ---------- header ------------------ */
style
Seventeen, CSS naming convention
1. Naming of id
(1) Page structure
Container: container
Header: header
Content: content/container
Page body: main
Footer: footer
Navigation: nav
Sidebar: sidebar
Column: column
Page peripheral control overall layout width: wrapper
left right center: left right
(2) Navigation
Navigation: nav
Main navigation: mainnav
Sub-navigation: subnav
Top navigation: topnav
Side navigation: sidebar
Left navigation: leftsidebar
Right navigation: rightsidebar
Menu: menu
Submenu: submenu
Title: title
Summary: summary
(3) Function
Logo: logo
Advertisement: banner
Login: login
Login bar: loginbar
Registration: regsiter
Search: search
Ribbon: shop
Title: title
Join: joinus
Status: status
Button: btn
Scroll: scroll
Tab: tab
Article list: list
Prompt message: msg
Current: current
Tips: tips
Icon: icon
Note: note
Guide: guild
Service: service
Hotspot: hot
News: news
Download: download
Vote: vote
Partner: partner
Friendly links: link
Copyright: copyright
2. Class naming
(1) Color: use the name of the color or 16 characters Custom code, such as
.red { color: red; }
.f60 { color: #f60; }
.ff8600 { color: #ff8600; }
(2) Font size, directly use "font font size" as the name, such as
.font12px { font-size: 12px; }
.font9pt {font-size: 9pt; }
(3) Alignment style, use the English name of the alignment target, such as
.left { float:left; }
.bottom { float:bottom; }
(4) Title bar style, named using "category function", such as
.barnews { }
.barproduct { }
Notes::
u Always lowercase;
u Try to use English;
u Do not add dashes or underlines;
u 2 combinations The first letter of the second word can be capitalized (eg: mainContent) without a dash or underline;
u Try not to abbreviate the word unless it is a word that is easy to understand at a glance.
3. Main Site css file
Main master.css
Module module.css
Basic shared base.css (root.css)
Layout, layout layout.css
Themes.css
Columns.css
Text font.css
Forms.css
Patch mend.css
Print print.css
As for the spacing, leave it to the margin attribute of the label located inside instead of defining the padding of the label located outside
19. Perfect single-pixel outline table
table{border-collapse:collapse;}
td{border:1px solid #000;}
Twenty, If the text is too long , then the overly long part will be displayed as ellipsis