Home >Web Front-end >HTML Tutorial >Div CSS rules organization_html/css_WEB-ITnose

Div CSS rules organization_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:32:061400browse

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

基本选择器

标记选择器(eg:

类别选择器(eg:class)

ID选择器

复合选择器

“交集”复合选择器(eg:p.menu{color:red}) 必须是标记 类别/ID组合

“并集”复合选择器(eg:h1,h2,h3{color:red})

“后代”复合选择器(eg: #menu .menulist{ ... })

“子”  复合选择器(eg: #menu .menulist .selectit { ... })

Basic selector
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

21. Not all styles need to be abbreviated

When the style sheet is defined such as p{padding:1px 2px 3px 4px}, it will be used in subsequent projects. Added a style with 5px upper padding and 6px lower padding. We don't necessarily have to write p.style1{padding:5px 6px 3px 4px}. It can be written as p.style1{padding-top:5px;padding-right:6px;}. You may feel that writing it this way is not as good as the original one, but have you ever thought about it? Your writing method repeatedly defines the style. In addition, You don’t have to find out what the original lower and left padding values ​​are! If the previous style P changes in the future, the style of p.style1 you defined will also change. (This method is very important for later modification of styles)

Twenty-two, Several commonly used CSS details processing styles

1) Both ends of Chinese characters Alignment: text-align:justify; text-justify:inter-ideograph;

2) Fixed-width Chinese character truncation: overflow:hidden; text-overflow:ellipsis; white-space:nowrap; (Do not let it wrap , but it can only handle text truncation on one line, not multiple lines) (IE5 and above) FF cannot, it only hides.

***Universal forced line break: white-space:normal;word-break:break-all;

Forbidden line break: white- space:nowrap

Force line break: word-wrap: break-word; word-break: normal;

.AutoNewline

{

/*word-break: break-all ; Method one must*/

/*word-wrap:break-word;overflow:hidden; Method two */

/*word-wrap:break-word; word-break: normal; Method 3*/

word-wrap:break-word; word-break:break-all;

}

.NoNewline

{

/*word-break: keep-all; Method 1 Required*/

white-space:nowrap;

}

3) Fixed-width Chinese character (word) line break: table-layout:fixed; word-break:break-all; (IE5 and above) FF cannot.

4)TextUse the mouse to place the previous text See the effect. This effect can be seen on many foreign websites, but very few domestic ones.

5) Set the image to semi-transparent: .halfalpha {background-color:#000000;filter:Alpha(Opacity=50)} passed the test in IE6 and IE5, but failed in FF. This is because this style is private to IE;

6) FLASH transparency: select swf, open the original code window, and enter

overflow:hidden;

}

23. Issues to note when using elements with float attributes

1. Use the border attribute to determine the layout characteristics of the error element

Use float Property layout can go wrong if you're not careful. At this time, add the border attribute to the element to determine the element boundary, and the cause of the error will be revealed.

2. The parent element of a float element cannot specify the clear attribute

If you use the clear attribute on the parent element of a float element under MacIE, the surrounding float elements will be laid out confusion. This is a famous bug of MacIE. If you don't know it, you will take detours.

3. Float elements must specify the width attribute

Many browsers have bugs when displaying float elements without specified width. So regardless of the content of the float element, the width attribute must be specified for it.

In addition, try to use em instead of px as the unit when specifying elements.

4. Float elements cannot specify attributes such as margin and padding.

IE has a bug when displaying float elements with margin and padding specified. Therefore, do not specify margin and padding attributes on float elements (you can nest a div inside the float element to set margin and padding). You can also use hacks to specify special values ​​for IE.

5. The sum of the widths of float elements must be less than 100%

If the sum of the widths of float elements is exactly 100%, some ancient browsers will not display it properly. Therefore, please ensure that the sum of the widths is less than 99%.

Twenty-four, browser compatibility issues (for FF/IE6/IE7)

1. CSS hack: distinguish IE6, IE7, firefox

Difference between FF, IE7 and IE6:

background:green !important; background:orange; *background:blue;

IE6 can recognize *, but not !important,

IE7 can recognize *, and it can also recognize !important;

FF can’t recognize *, but it can recognize !important;

Also add one more, underline "_",

IE6 supports underline, but neither IE7 nor firefox supports underline.

So you can also distinguish firefox, IE7, IE6 in this way

background:green!important; *background:orange; _background:blue;

Note: No matter what it is Methods are written in the order that Firefox is written first, IE7 is written in the middle, and IE6 is written at the end.

2. The BOX model interpretation in firefox and IE is inconsistent, resulting in a 2px difference. Solution

div{margin:30px!important;margin:28px;}

Note that the order of these two margins must not be reversed. The !important attribute is not recognized by IE, but other browsers can. So it is actually interpreted like this under IE: If div{maring:30px;margin:28px} is repeatedly defined, it will be executed according to the last one, so you cannot just write margin:XXpx!important;

3. Conditional comments to select different browsers (more concise than CSS hack)

4 .Differentiate IE8

.color{

background-color: #CC00FF; /*All browsers will display purple*/

background-color: #FF00009; / *IE6, IE7 and IE8 will display red*/

*background-color: #0066FF; /*IE6 and IE7 will turn blue*/

_background-color: #009933; /*IE6 will turn green*/

Twenty-five, Standard principles followed by W3C

1. Before arranging the table, Please think carefully about the best solution. Try to control the nesting of tables within three levels, and try to avoid the two tags " ". Experience shows that these two tags will bring many problems. trouble.

2. A webpage should try to avoid using an entire large table. All content is nested within this large table, because when the browser interprets the elements of the page, it displays them one by one in units of tables. If a web page is nested within a large table, the likely consequence is that when the visitor types in the URL, he will first face a blank space for a long time, and then all the web content will appear at the same time. If you must do this, use the tag to enable the large table to be displayed in chunks.


3. In typesetting, we often encounter the need to indent the first line. Do not use or full-width spaces to achieve the effect. The standard approach is to define p { text-indent in the style sheet : 2em; } Then add the

mark to each paragraph. Note that under normal circumstances, please do not omit the

end mark.


4. In principle, we prohibit using to artificially interfere with the size of the image display, and it is recommended not to include width and height in the tag Two attributes. This is because during the production process, pictures often need to be modified repeatedly. This can avoid human intervention in the size of the picture display and maximize the browser's own functions. However, a side effect of this is that when the web page has not loaded the picture, When the image is inserted, the site size of the image will not be left out, which may cause the web page to jitter during the loading process (if the image is inserted into a fixed-size table, this phenomenon will not occur), especially when the image size is large This phenomenon will be very obvious, so when it is expected that this situation will obviously cause the web page to jitter, please be sure to attach the width and height attributes to at the end.


5. In order to maximize the automatic layout function of the browser, please try not to use
to manually intervene in the segmentation of a complete text.


6. There should be a half-width space between words in different languages, except before the header avoidance symbol and after the tail avoidance symbol. The punctuation between Chinese characters should be full-width punctuation, and around English letters and numbers. The parentheses should use half-angle brackets.


7. All font sizes should be implemented using style sheets, and the tag is prohibited from appearing on the page.


8. Please do not appear more than one consecutively in the web page and use full-width spaces as little as possible (under the English character set, full-width spaces will become garbled characters). Use text-indent as much as possible for white spaces. Padding, margin, hspace, vspace and transparent gif images are used to achieve this.


9. When mixing Chinese and English, we try our best to define English and numbers as verdana and arial fonts.


10. Line spacing is recommended to be defined by percentage. The commonly used two line spacing values ​​are line-height:120%/150%.


11. In the website All paths use relative paths. Generally, the link path to a default file in a certain directory does not need to be written in the full name. For example, we do not need to do this: Instead, it should be like this: < a href="aboutus/">


12. Use larger fonts to embed text in graphics. It is recommended not to include text in graphics.


13. "Web page size" is defined as the sum of the file sizes of all web pages, including HTML files and all embedded objects. Users prefer sites that are fast rather than novel. For modem users, it is appropriate to keep the page size below 34K.

14. Float elements must specify the width attribute
Many browsers have bugs when displaying float elements without specified width. So regardless of the content of the float element, the width attribute must be specified for it.
In addition, when specifying elements, try to use em instead of px as the unit.

15. Float elements cannot specify attributes such as margin and padding
IE has a bug when displaying float elements with margin and padding specified. Therefore, do not specify margin and padding attributes on float elements (you can nest a div inside the float element to set margin and padding). You can also use hacks to specify special values ​​for IE.

 16. The sum of the widths of float elements must be less than 100%
If the sum of the widths of float elements is exactly 100%, some ancient browsers will not display properly. Therefore, please ensure that the sum of the widths is less than 99%.

Twenty-six. List elements ul ol li dl dt dd interpretation

;Content 1

  • Content 2
  • Title

    Content description 1

    Content description 2

    l You can add ol ul li and p to dt and dd

    Twenty-seven ,                                                                                              Clear floating

    clearfix:after {content: "."; display:block; height:0; clear:both; visibility:hidden;}

    In Firefox, when the children are all When it is floating, the height of the parent cannot completely cover the entire child. Then use this HACK to clear the floating to define the parent once, which can solve this problem.

    .clearfix {

    display:inline-block;

    }

    /* Hides from IE-mac */

    * html .clearfix {

    height:1%;

    }

    .clearfix {

    display:block;

    }

    /* End hide from IE-mac */

    ** This kind of usage is more common when mixing graphics and text, but it is not easy to control. Use margin to match clear{clear:both} controls directly.

    Twenty-eight. Text processing

    1. General fonts:

    font-family: "Lucida Grande", Verdana, Lucida, Arial, Helvetica, "宋体",sans-serif;

    Title font (h1/h2): font-family: Cambria, Georgia, "Times New Roman", Times, serif;

    2. Drop cap:

    P:first-letter{padding:10px,fontsize:32pt;float:left}

    3 . Pinyin Chinese characters:

    bruce wolfbu lu si lang

    Twenty-nine, Min-height multi-browser compatibility issues

    Div{

    min-height:450px;

    height:auto!important;

    height:450px;

    overflow:visible;????

    }

    Thirty, CSS layout tips - CSS BUG jingle

    · If the IE border is obvious, it must be noted, and the height settings must be forgotten;

    · The floating is caused by the cause, if the parent layer is included, the container should be cleared, and the container naturally shows it; · Three pixel texts do not have to panic, set the height settings to help you;

    · Pay attention to each browse, the default setting is high may be the killer; It must be remembered that the row height is set to zero, the height is set to zero, the design effect is combined with browsing; , friendly engines like to greet.

    ·                                                                                                                                                                                                                                                                                The number is all back to the original, the layering pattern requires more practice, everything is regular.

    · Be careful with the layout of image links. If the image links and text links are aligned, padding and vertical-align:middle must be set. It doesn’t matter if the difference is subtle.

    · IE floating double margins, please use display: inline.

    · The list should be typed horizontally, the list codes must be close together, and the gaps must be remembered.

    31. Font applications in the Web

    Summarize several sets of practical and simple font-family

    font-family: Tahoma, Helvetica , Arial, sans-serif;

    Tahoma-based unisex font. It is recommended to use an environment above 13px.

    font-family: Trebuchet MS, Verdana, Helvetica, Arial, sans-serif;

    Verdana wide flat font. It is recommended to use it in an environment below 11px.

    font-family: Geogia, Times New Roman, Times, serif;

    The perfect serif font. Mostly used for large title fonts above 16px.

    font-family: Lucida Console, Monaco, Courier New, mono, monospace;

    A series of monospaced fonts. Writing code is very useful. In addition, if you feel that Lucida Console is too wide, you can switch to the narrower Lucida Sans Typewriter. By the way, the code blocks on Lao Zhao’s blog use Lucida Sans Typewriter~

    If the visibility is set to hidden in the style of the div, the div will be hidden, but It will occupy the blank space, but if it is set to display:none, it will not occupy the blank space;

    and visible="false" will not return the div to the html;