From: http://www.javaeye.com/topic/52036
Recently I have been studying CSS. I just made a flexible tab page in combination with the project and implemented it using pure HTML + CSS. I just want to summarize it.
First, let's take a look at the preview interface:
Sample HTML accessible: http://www.demo2do.com/htmldemo/school/attendance/AttendanceGlobal.html
The following describes how to complete the preceding page.
1. Construct html
Building HTML is the most basic part of the process. A key principle for building HTML is "returning the original meaning of HTML tags ". So here, we should analyze the expected HTML structure reasonably, analyze it, and select the appropriate HTML Tag, instead of using non-standard table layout or layout with a large number of divs and classes. In fact, there is a misunderstanding that the Web standard is used for page programming through Div + CSS in all things. In fact, this is totally wrong, it is easy to cause divitus or classitis ).
Back to the topic, let's analyze the page style. We can divide the entire tab into two parts: level-1 menu and level-2 menu. They have similar features and are arranged horizontally. The unordered list in the HTML Tag reflects this logical relationship. Therefore, we use two Unordered Lists to represent the level-1 menu and level-2 menu. The Code is as follows:
HTML code
- <Div class = "navg">
- <Div id = "Attendance" class = "mainnavg">
- <Ul>
- <Li id = "attendancenavg"> <a href = "#"> attendance management </a> </LI>
- <Li id = "teachnavg"> <a href = "#"> teaching management </a> </LI>
- <Li id = "communicationnavg"> <a href = "#"> Home-School interconnection </a> </LI>
- <Li id = "systemnavg"> <a href = "#"> system management </a> </LI>
- </Ul>
- </Div>
- <Div id = "dailyattendance" class = "secondarynavg">
- <Ul>
- <Li id = "dailyattendancenavg"> <a href = "#"> day attendance </a> </LI>
- <Li id = "leaveapprovenavg"> <a href = "#"> Leave for approval </a> </LI>
- <Li id = "attendancestatisticsnavg"> <a href = "#"> attendance statistics </a> </LI>
- <Li id = "attendancecollectnavg"> <a href = "#"> attendance summary </a> </LI>
- </Ul>
- </Div>
- </Div>
The two divs divide the menu level. In fact, there will be other functions in the future. Now, let's take a look at this page. We can be pleasantly surprised to find that this page is just like a Word document and is readable. We can verify it again after the entire process is completed.
2. Construct basic CSS
First, let the UL be arranged horizontally. Pay attention to the cleaning after the element float.
Then, the main menu style is implemented by applying the background on Li and a respectively. The most important thing here is that the element a becomes a block-level element (display: block ), this makes it easy for us to do some processing below, and also applies the entire menu to the link style.
The line-height can center the text in a vertically. Text-align center the words in a horizontally.
CSS code
- . Navg. mainnavg ul {
- Margin: 0;
- Padding: 0;
- List-style: none;
- }
- . Navg. mainnavg ul Li {
- Float: left;
- Background-color: # e1e9f8;
- Background: URL (../images/tab_right.gif) No-repeat right top;
- Margin: 10px 3px;
- Height: 25px;
- }
- . Navg. mainnavg ul Li {
- Display: block;
- Height: 25px;
- Padding: 0 25px;
- Line-Height: 24px;
- Background-color: # e1e9f8;
- Background: URL (../images/tab_left.gif) No-repeat left top;
- Text-Decoration: none;
- Float: left;
- Text-align: center;
- Color: # FFF;
- Font-weight: bold;
- }
3. Make width adaptive
Here we use the sliding door technology for width adaptive. The following describes the sliding door technology.
Simply put, it is to apply a large image background on Li and place the background on the right.
Then apply a small image background on a and place the background on the left side to cover the big image edge.
In this way, no matter how long the menu text content is changed, the original structure will not be damaged.
4. highlight the current menu
There are many ways to highlight the current page. the most rigid one is to explicitly define classes on each page.
However, for web projects, most pages are dynamic, so this is not the best method.
The method I use here is the flexible use of the CSS selector.
Java code
- # Attendance # attendancenavg,
- # Teach # teachnavg,
- # Communication # communicationnavg,
- # System # systemnavg {
- Background: URL (../images/tab_right_on.gif) No-repeat right top;
- }
- # Attendance # attendancenavg,
- # Teach # teachnavg,
- # Communication # communicationnavg,
- # System # systemnavg {
- Background: URL (../images/tab_left_on.gif) No-repeat left top;
- Color: # 0000ff;
- }
In the <Div id = "Attendance" class = "mainnavg"> code, we can use different IDS as selectors, because the selector ID in CSS has a higher priority than the class, therefore, you only need to match the ID above Li according to the ID to achieve the dynamic selection and highlighting purpose.
In fact, because our pages are dynamic, IDS can be generated by the background, so that we can achieve our needs through the different combinations of IDs.
5. Tips
Finally, there may be another question: How do you implement it? How can the highlighted tab hide the following horizontal line?
It's easy. Tips on pictures. Set the height of the highlighted image to 25px, while that of the ordinary image to 24px. Then, you can use padding to hide the horizontal line.
We can use a similar method to make the level-2 menu, which is not detailed here. You can try it with the source code.
Source code can be analyzed link page get: http://www.demo2do.com/htmldemo/school/attendance/AttendanceGlobal.html