Then define it like this in the style sheet: #menubar {MARGIN: 0px;BACKGROUND: #FEFEFE;COLOR: #666;} where "menubar" is the id name you defined. Note the "#" sign in front. The id selector also supports derivation, for example: #menubar p { text-align : right; margin-top : 10px; } This method is mainly used to define layers and those that are more complex and have many derived elements. 6. Category selector Use a dot in CSS to indicate the category selector definition, for example: .14px {color : #f60 ;font-size:14px ;} in the page , use the class="category name" method to call: 14px size font This method is relatively simple and flexible, and you can create and create new ones at any time according to the needs of the page. delete. 7. Define the link style Four pseudo-classes are used in CSS to define the link style, namely: a:link, a:visited, a:hover and a: active, for example: a :link{font-weight : bold ;text-decoration : none ;color : #c00 ;} a:visited {font-weight : bold ;text-decoration : none ;color : #c30 ;} a :hover {font-weight : bold ;text-decoration : underline ;color : #f60 ;} a:active {font-weight : bold ;text-decoration : none ;color : #F90 ;} above The statements respectively define the styles of "links, visited links, when the mouse is over, and when the mouse is clicked". Note that you must write in the above order, otherwise the display may be different from what you expected. Remember they are in order "LVHA". Haha, I feel a little dizzy after reading so much. In fact, there are many more grammatical specifications for CSS. Here are just some commonly used ones. After all, we are taking it step by step, and we cannot become fat in one bite:) 2. Introduction to CSS layout
The biggest difference between CSS layout and traditional table layout is that the original positioning uses tables, through the spacing of tables or using colorless and transparent GIF images. Control the spacing of text layout sections; now, layers (divs) are used for positioning, and the spacing of sections is controlled through the margin, padding, border and other attributes of the layer.
1. Define DIV
Analyze a typical definition div example: #sample{ MARGIN: 10px 10px 10px 10px; PADDING: 20px 10px 10px 20px; BORDER-TOP: #CCC 2px solid; BORDER-RIGHT: #CCC 2px solid; BORDER-BOTTOM: #CCC 2px solid; BORDER-LEFT: #CCC 2px solid; BACKGROUND: url(images/bg_poem.jpg) #FEFEFE no-repeat right bottom; COLOR: #666; TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDTH:60%; } The description is as follows:
The name of the layer is sample. You can call this style by using
on the page. MARGIN refers to the blank space left outside the border of the layer, used for page margins or to create a gap with other layers. "10px 10px 10px 10px" respectively represents the four margins of "top, right, bottom and left" (clockwise). If they are all the same, they can be abbreviated to "MARGIN: 10px;". If the margin is zero, write "MARGIN: 0px;". Note: When the value is zero, except for the RGB color value 0% which must be followed by a percent sign, in other cases the unit "px" does not need to be followed. MARGIN is a transparent element and cannot define a color. PADDING refers to the space between the layer's border and the layer's content. Like margin, specify the distance from the top, right, bottom and left borders to the content respectively. If they are all the same, they can be abbreviated to "PADDING:0px;". To specify the left side individually, you can write "PADDING-LEFT: 0px;". PADDING is a transparent element and cannot define color. BORDER refers to the border of the layer. "BORDER-RIGHT: #CCC 2px solid;" defines the right border color of the layer as "#CCC", the width as "2px", and the style as "solid" straight line. If you want a dotted line style, you can use "dotted". BACKGROUND defines the background of the layer. It is defined in two levels. First define the image background and use "url(../images/bg_logo.gif)" to specify the background image path; secondly define the background color "#FEFEFE". "no-repeat" means that the background image does not need to be repeated. If you need to repeat it horizontally, use "repeat-x", to repeat it vertically, use "repeat-y", and to repeat it to cover the entire background, use "repeat". The following "right bottom;" means that the background image starts from the lower right corner. If there is no background image, you can only define the background color BACKGROUND: #FEFEFE. COLOR is used to define font color, which has been introduced in the previous section. TEXT-ALIGN is used to define the arrangement of content in the layer, center is in the middle, left is in the left, and right is in the right. LINE-HEIGHT defines the line height. 150% means that the height is 150% of the standard height. It can also be written as: LINE-HEIGHT:1.5 or LINE-HEIGHT:1.5em, which have the same meaning. WIDTH is the width of the defined layer, which can be a fixed value, such as 500px, or a percentage, like "60%" here. It should be noted that this width only refers to the width of your content, and does not include margin, border and padding. However, it is not defined this way in some browsers, so you need to try more.
2. CSS2 box model Since the launch of CSS1 in 1996, the W3C organization has recommended that all objects on the web page be placed in a box (box). Designers can control this box by creating definitions. Properties, these objects include paragraphs, lists, titles, images, and layer
. The box model mainly defines four areas: content, padding, border and margin. The sample layer we talked about above is a typical box. For beginners, they are often confused about the levels, relationships, and mutual influences among margin, background-color, background-image, padding, content, and border. Here is a 3D schematic diagram of the box model, I hope it will be easier for you to understand and remember.
3. All auxiliary images should use background processing Using XHTML CSS layout, there is a technology that you are not used to at first. It should be said that it is a way of thinking and tradition. The table layout is different, that is: all auxiliary images are implemented with backgrounds. Something like this:
BACKGROUND: url(images/bg_poem.jpg) #FEFEFE no-repeat right bottom; Although it is possible to insert directly into the content, this is not recommended. The "auxiliary pictures" here refer to pictures that are not part of the content to be expressed on the page, but are only used for decoration, interval, and reminder. For example: pictures in photo albums, pictures in picture news, and the 3D box model pictures above are all part of the content. They can be directly inserted into the page using the element, while others are similar to logos, title pictures, and list prefixes. Images must be displayed using background or other CSS methods.
There are two reasons for this:
Completely separate the performance from the structure, and use CSS to control all appearance and performance to facilitate revision. Make the page more usable and friendly. For example, if a blind person uses a screen reader, pictures implemented using background technology will not be read aloud. 3. The first CSS layout example
The next step is to actually design the layout. As with the traditional method, you first have to have a rough outline idea in your mind, and then draw it out using photoshop. You may see that most websites related to web standards are very simple, because web standards pay more attention to structure and content. In fact, it has no fundamental conflict with the beauty of web pages. You can design it however you want. The layout is implemented using the traditional table method. DIV can also be implemented. Technology has a maturation process. Think of DIV as the same tool as TABLE. How to use it depends on your imagination.
Note: In actual application, DIV is indeed not as convenient as a table in some places, such as the definition of background color. But everything has gains and losses, and the choice depends on your value judgment. Okay, without further ado, let’s start:
1. Determine the layout The initial design sketch of w3cn is as follows:
用表格的设计方法的话,一般都是上中下三行布局。用DIV的话,我考虑使用三列来布局,成为这样。
2、定义body样式 先定义整个页面的body的样式,代码如下:
body { MARGIN: 0px; PADDING: 0px; BACKGROUND: url(../images/bg_logo.gif) #FEFEFE no-repeat right bottom; FONT-FAMILY: 'Lucida Grande','Lucida Sans Unicode','宋体','新宋体',arial,verdana,sans-serif; COLOR: #666; FONT-SIZE:12px; LINE-HEIGHT:150%; } 以上代码的作用在上一天的教程有详细说明,大家应该一看就明白。定义了边框边距为0;背景颜色为#FEFEFE,背景图片为bg_logo.gif,图片位于页面右下角,不重复;定义了字体尺寸为12px;字体颜色为#666;行高150%。
The specific style sheets are written in the corresponding sections. The key point is that the #mainbox layer is nested in three layers: #menu, #sidebar and #content. When the content of #content increases, the height of #content will increase, and the height of #mainbox will also expand, and the #footer layer will automatically move down. This achieves a high degree of adaptability.
It is also worth noting that #menu and #content are floating on the right side of the page "FLOAT: right;", #sidebar is floating on the left side of the #menu layer "FLOAT: left;", this is floating Method positioning, you can also use absolute positioning to achieve such an effect.
There is another problem with this method, that is, the background of the sidebar #sidebar cannot be 100%. The general solution is to fill it with the body's background color. (The background color of #mainbox cannot be used because the background color of #mainbox is invalid in browsers such as Mozilla.) 4. 3D diagram of the CSS2 box model
in CSS2 The box model is key to our layout positioning. For example,
follows the box model specification. The margin, background-color, background-image, padding, content, and border defined by the box model are often confused by beginners’ levels, relationships, and interactions. Here is a 3D schematic diagram of the box model, I hope it will be easier for you to understand and remember.
Original image from: hicksdesign
It should be noted that IE and Mozilla browsers have inconsistent interpretations of the box model, resulting in The difficulty in our positioning is that the main difference is:
When IE calculates the distance between two divs, it will include the 1px border, but Mozilla does not; after setting the width of the div, if you add a value to padding , IE will subtract this value from the width, and Mozilla will add this value to the width.
5. Introduction to w3c technical architecture Original text: http://www.w3.org/Consortium/technology Original author: w3c Translation: A Hong
Legend W3C Technical Architecture Diagram depicts a two-layer model: World Wide Web Architecture (labeled "One Web") Built on the Internet architecture. The rich Web layer in the figure shows the areas of concern and developing technologies of W3C.
Web architecture is depicted as a series of layers, each layer built on top of the other. From bottom to top:
URI/IRI, HTTP Web Architectural Principles XML Infosets; RDF(S) Graphs XML, Namespaces, Schemas, XQuery/XPath, XSLT, DOM, XML Base, XPointer , RDF/XML, SPARQL
Contains six boxes at the top level, corresponding to the main W3C activity groups: Web Applications, Mobile, Voice, Web Services, Semantic Web, and Privacy.
The "Interaction" box lists: XHTML, SVG, CDF, SMIL, XForms, CSS, and WCID. The "Mobile" box lists XHTML Basic, Mobile SVG, SMIL Mobile, XForms Basic, CSS Mobile, MWI BP. "Voice" box lists VoiceXML, SRGS, SSML, CCXML, and EMMA "Web Services" box The "Semantic Web" box lists SOAP, XOP, WSDL, WS-CDL, and WS-A. The "Semantic Web" box lists OWL, SKOS, and RIF. The "Privacy" box lists Listing P3P, APPEL, XML Encryption, XML Signature, and XKMS
An orange horizontal bar connects these fields: Web Accessibility (Web accessibility), Internationalization (Internationalization), Mobile Access ( Mobile Access), Device Independence, and Quality Assurance.
This example diagram shows the basic framework of the World Wide Web and the focus of W3C's work.
The foundation of URI, HTTP, XML and RDF supports five areas of work. Topics such as web accessibility, internationalization, device independence, and quality management are integrated into W3C technologies.
The W3C is working to transform the World Wide Web from its original design (basic HTML, URIs, and HTTP) into the model it will need in the future. W3C's technology will help the future World Wide Web become a highly stable, scalable and adaptable basic framework in the information world. 6. Analysis of CSS compatibility points IE vs FF
CSS compatibility points:
DOCTYPE affects CSS processing
FF: div setting margin- When left and margin-right are set to auto, they are already centered. IE cannot >
FF: After setting padding, the div will increase the height and width, but IE will not, so you need to use !important to set an additional height and width
FF: Support !important, but IE will ignore it, available !important Specially set the style for FF
The vertical centering problem of div: vertical-align:middle; Increase the line spacing to the same height as the entire DIV line-height:200px; Then insert text and it will be vertically centered. The disadvantage is that you need to control the content without wrapping
cursor: pointer can display the cursor finger shape in IE FF at the same time, hand can only be displayed in IE
FF: To add borders and background colors to links, you need to set display: block and float: left to ensure no line breaks. Referring to the menubar, setting the height of a and menubar is to avoid dislocation of the bottom edge display. If the height is not set, you can insert a space in the menubar. XHTML CSS Compatibility Solution Collection
There are many benefits to using the XHTML+CSS architecture. But there are indeed some problems, whether it is due to unskilled use or unclear thinking, I will first write down some of the problems I encountered below to save everyone from looking around^^
1. In mozilla firefox The inconsistent explanation with the BOX model in IE leads to a difference of 2px. Solution:
div{margin:30px!important;margin:28px;}
Note that the order of these two margins must not be reversed. According to Ajie, the !important attribute cannot be recognized by IE, but other browsers can. So it is actually interpreted like this under IE:
div{maring:30px;margin:28px}
If you repeat the definition, it will be executed according to the last one, so you cannot just write margin:XXpx!important ;
2. The BOX interpretation of IE5 and IE6 is inconsistent. The width of div{width:300px;margin:0 10px 0 10px;}under IE5 will be interpreted as 300px-10px (right padding)-10px (left Padding) The final width of the div is 280px, while on IE6 and other browsers the width is calculated as 300px 10px (right padding) 10px (left padding) = 320px. At this time we can make the following modifications
, about this/**/. I don’t quite understand what it is. I only know that IE5 and firefox support it but IE6 does not. If anyone understands, please let me know, thanks! :)
3. The ul tag has a padding value by default in Mozilla, but in IE only margin has a value, so define it first
ul{margin:0;padding:0;}
can solve most problems
4. Regarding scripts, the language attribute is not supported in xhtml1.1. You only need to change the code to
That's it 7. 10 CSS skills you may not know
1. CSS font attribute abbreviation rules
This is generally how to set font attributes using CSS:
Great! Just one caveat: this shorthand method only works when both the font-size and font-family properties are specified. Also, if you don't set font-weight, font-style, and font-variant, they will use default values, so keep this in mind.
2. Use two classes at the same time
Generally, only one class (Class) can be set for an element, but this does not mean that two cannot be used. In fact, you can do this:
...
Give the P element two classes at the same time, with spaces in between Turn on, so that all attributes of the text and side classes will be added to the P element. If there is a conflict between the attributes in the two classes, the one set later will take effect, that is, the attributes of the class placed later in the CSS file will take effect.
Supplement: For an ID, you cannot write
...
nor can you write it like this
3. Lack of CSS border The default value
can usually set the color, width and style of the border, such as:
border: 3px solid #000
This will display the border as 3 pixels wide, Black, solid line. But in fact, you only need to specify the style here.
If only style is specified, other attributes will use default values. Generally, the default width of Border is medium, which is generally equal to 3 to 4 pixels; the default color is the color of the text. If this value is just right, there is no need to set so many settings.
4. CSS for document printing
Many websites have a version for printing, but in fact this is not needed because CSS can be used to set the printing style.
In other words, you can specify two CSS files for the page, one for screen display and one for printing:
The first line is for display, the second line is for printing, pay attention to the media attribute.
But what should be written in the printing CSS? You can set it up the same way you would design normal CSS. While designing, you can set this CSS to display CSS to check its effect. Maybe you will use the display: none command to turn off some decorative images and turn off some navigation buttons. To learn more, see the "Printing Differences" article.
5. Image replacement skills
It is generally recommended to use standard HTML to display text instead of images. This is not only faster, but also more readable. But if you want to use some special fonts, you can only use pictures.
For example, if you want to create an icon for selling things, you would use this image:
Of course it is possible, but for search engines, compared with normal text, they have little interest in the replacement text in alt. This is because many designers put many keywords here To trick search engines. So the method should be like this:
Buy widgets
But this way there is no special font. To achieve the same effect, you can design CSS like this:
Pay attention to replacing the image height with the height of the real image. Here, the image will be displayed as the background, and because the indentation of -2000 pixels is set for the real text, they will appear 2000 points on the left side of the screen and will be invisible. But for people who turn off the picture, they may not be able to see it at all, so be careful. 8. Eighteen skills of CSS
Translation: Ajie Original author: Roger Johansson
About the author: Living in Sweden Gothenburg, started to get involved in web design in 1994. 456 Berea Street is his address, so he adopted this name as the domain name of his personal homepage
Recently, friends often ask me about some CSS problems I encountered at work. . They always cannot control CSS well, which affects the efficiency of CSS. Let me analyze and summarize the errors to help everyone use CSS more easily.
This article summarizes all the techniques and compatibility solutions I have learned since I started using CSS layout methods. I am willing to share these with you. I will focus on explaining some mistakes that novices often make (including those I have made myself). , if you are already a CSS master, you may already know these experience and skills. If you have more, I hope you can help me add them.
1. Use CSS abbreviations
Using abbreviations can help reduce the size of your CSS file and make it easier to read. For the main rules of CSS abbreviation, please refer to "Common CSS Abbreviation Syntax Summary", which will not be described here.
2. Define the unit clearly, unless the value is 0
Forgetting to define the unit of the size is a common mistake among CSS newbies. In HTML you can just write width="100", but in CSS you have to give an exact unit, such as: width:100px width:100em. There are only two exceptions for not defining units: row height and 0 value. In addition, other values must be followed by the unit. Be careful not to add spaces between the value and the unit.
3. Case-sensitive
When using CSS in XHTML, the element names defined in CSS are case-sensitive. To avoid this error, I recommend using lowercase for all definition names.
The values of class and id are also case-sensitive in HTML and XHTML. If you must use mixed case, please carefully confirm that your definition in CSS is consistent with the tags in XHTML.
4. Cancel the element qualification before class and id
When you write to define class or id for an element, you can omit the previous element qualification, because the ID is unique in a page , and clas s can be used multiple times in the page. It makes no sense for you to qualify an element. For example:
div#content { /* declarations */ }
fieldset.details { /* declarations */ }
can be written as
#content { /* declarations */ }
.details { /* declarations */ }
This can save some bytes.
5. Default value
Usually the default value of padding is 0, and the default value of background-color is transparent. But the default value may be different in different browsers. If you are afraid of conflicts, you can define the margin and padding values of all elements as 0 at the beginning of the style sheet, like this:
* {
margin:0;
padding:0;
}
6. There is no need to repeatedly define inheritable values
In CSS, child elements automatically inherit the attribute values of the parent element, such as color. , fonts, etc., which have been defined in the parent element, can be directly inherited in the child element without repeated definition. But be aware that the browser may override your definition with some default values.
7. Closest first principle
If there are multiple definitions of the same element, the closest (minimum level) definition will take precedence. For example, there is this piece of code
Update: Lorem ipsum dolor set
In the CSS file, you have defined the element p and a class "update"
p {
margin :1em 0;
font-size:1em;
color:#333;
}
.update {
font -weight:bold;
color:#600;
}
In these two definitions, class="update" will be used because class is closer than p . You can check out W3C’s “Calculating a selector’s specificity” to learn more.
8. Multiple class definitions
A tag can define multiple classes at the same time. For example: We first define two styles, the first style has a background of #666; the second style has a 10 px border.
Note that the order of the 4 numbers is: top, right, bottom, left. Of course, sometimes positioning rather than margins is better.
10. The background color that goes straight to the bottom of the screen
cannot be controlled by CSS in the vertical direction. If you want the navigation bar to go straight to the bottom of the page like the content bar, it is very convenient to use a table, but if you only use CSS like this:
#navigation { background: blue; width: 150px }
A shorter navigation bar will not go straight to the bottom, it will end when the content ends halfway. What to do?
Unfortunately, the only way to cheat is to add a background image to the shorter column, with the same width as the column width, and make it the same color as the set background color.
body { background: url(blue-image.gif) 0 0 repeat-y }
You cannot use em as the unit at this time, because in that case, once the reader changes the font size, this The trick will be revealed, you can only use px.
10. There is no need to add quotes to the background image path
In order to save bytes, I recommend not to add quotes to the background image path, because the quotes are not necessary. For example:
background:url("images/***.gif") #333;
can be written as
background:url(images/***.gif) #333;
If you add quotation marks, On the contrary, it will cause some browser errors.
11. Group selectors
When some element types, classes or ids have common attributes, you can use groups. Selector to avoid multiple repeated definitions. This can save a lot of bytes.
For example: to define the font, color and margin of all titles, you can write:
If there are individual elements that need to define independent styles when using them, you can add new definitions or overwrite the old definitions, for example:
h1 { font-size:2em; }
h2 { font-size:1.6em; } 12. Specify link styles in the correct order
When When you use CSS to define multiple state styles of a link, pay attention to the order in which they are written. The correct order is::link :visited :hover :active. The first letter extracted is "LVHA", which you can remember as "LoVe". HAte" (like to hate). Why is it so defined? You can refer to Eric Meyer's "Link Specificity".
If your users need to use the keyboard to control and need to know the focus of the current link, you can also define: focus Property. The effect of the :focus attribute also depends on the position where you write it. If you want the focused element to display the :hover effect, you write :focus in front of the :hover; if you want the focus effect to replace the :hover effect, you put: Focus is placed after:hover
13. Clear floats
A very common CSS problem is that when floating is used for positioning, the underlying layer is covered by the floating layer, or is embedded in the layer. The nested sublayer exceeds the scope of the outer layer.
The usual solution is to add an extra element after the floating layer, such as a div or a br, and define its style as clear: both. It's a bit far-fetched, but fortunately there is a good way to solve it. Please refer to this article "How To Clear Floats Without Structural Markup" (Note: This site will translate this article as soon as possible).
The above two methods can solve the problem of floating overflow very well, but what if you really need to clear the layer or the objects in the layer? A simple method is to use the overflow attribute. This method was originally published in "Simple Clearing of Floats" and has been widely discussed in "Clearance" and "Super simple clearing floats".
Which clear method above is more suitable for you? It depends on the specific situation and will not be discussed here. In addition, some excellent articles have made it very clear about the application of float. It is recommended that you read: "Floatutorial", "Containing Floats" and "Float Layouts"
14. Horizontal centering (centering)
This is a simple trick, but it’s worth saying again because I see too many newbie questions asking this: How to horizontally center CSS? You need to define the width of the element and define the horizontal margin. If your layout contains In a layer (container), like this:
You can define it to be horizontally centered:
#wrap {
width:760px; /* Modify to the width of your layer*/
margin:0 auto;
}
But IE5/Win This definition cannot be displayed correctly, so we use a very useful trick to solve it: use the text-align attribute, like this:
body {
text-align:center;
<.>}
#wrap {
width:760px; /* Change to the width of your layer*/
margin:0 auto;
text-align:left;
}
text-align:center; rule of the first body defines that all elements of the body in IE5/Win are centered (other browsers just center the text) , the second text-align:left; is to center the text in #warp
.
15. Import and hide CSS
Because older browsers do not support CSS, a common approach is to use the @import technique to hide CSS. For example:
@import url("main.css");
However, this method does not work for IE4, which gave me a headache for a while. Later, I wrote it like this:
@import "main.css";
This way I can hide the CSS in IE4, haha, it also saved 5 bytes. If you want to know the detailed explanation of @import syntax, you can see here "centricle's css filter chart"
16. Optimization for IE
Sometimes, you need to define some special bugs in IE browser There are too many CSS techniques (hacks) here, and I only use two of them. Regardless of whether Microsoft supports CSS better in the upcoming IE7 beta version, these two methods are the safest.
1. Annotation method
(a) To hide a CSS definition in IE, you can use a child selector:
html>body p {
/* Definition content*/
}
(b) The following writing method can only be understood by IE browser (hidden from other browsers)
* html p {
/* declarations */
}
(c) There are also times when you want IE/Win to be active and IE/Mac to be hidden, you can Use the "backslash" trick:
/* */
* html p {
declarations
}
/* */
2. Method of conditional comments
Another method, which I think is more testable than CSSHacks, is to use Microsoft’s private attribute conditional comments (conditional comments) . Using this method you can define some styles separately for IE without affecting the definition of the main style sheet. Like this:
17. Debugging skills: How big is the layer? Worker that analyzes CSS code line by line. I usually define a background color on the layer in question so it's obvious how much space the layer takes up. Some people suggest using border, which is generally possible, but the problem is that sometimes border will increase the size of elements, and border-top and boeder-bottom will destroy the vertical margin value, so it is safer to use background.
Another attribute that often causes problems is outline. Outline looks like boeder, but does not affect the size or position of the element. Only a few browsers support the outline attribute, the only ones I know of are Safari, OmniWeb, and Opera.
18. CSS code writing style
When writing CSS code, everyone has their own writing habits for indentation, line breaks, and spaces. After constant practice, I decided to use the following writing style:
selector1,
selector2 {
property:value;
}
When using union definitions, I usually write each selector on its own line so they are easier to find in the CSS file. Add a space between the last selector and the curly braces {. Also write each definition on its own line. The semicolon should be placed directly after the attribute value. Do not add spaces.
I am used to adding a semicolon after each attribute value. Although the rules allow you to leave out the semicolon after the last attribute value, it is easy to forget to add a semicolon if you want to add a new style. Error, so it’s better to add them all.
Finally, the closing brace} is written on a line by itself.
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