Table of Contents
Goal of this article:
Question:
Now let’s do the specific operation
Summary:
Home Web Front-end CSS Tutorial CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)

CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)

Jun 18, 2020 pm 12:47 PM
css

Goal of this article:

1. Master the usage of nth-of-type, a structural pseudo-class selector in CSS.

Question:

1. Implement the following customization To define a navigation menu and use pure DIV CSS, you must use the structural pseudo-class selector—nth-of-type

CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)

Additional notes:

1, The navigation width is 800px, the height is 90px, and the display is centered

2. The width and height of the snowflake background image are 50px, and the size of the wine bottle image is also the same

Now let’s do the specific operation

1. Prepare materials: Combined with the target effect, we can make a picture of a wine bottle. The background is transparent. In this way, the background color changes, and the transparent part inside it can also change accordingly. There are also two snowflakes on the left and right, which are also the required materials.

CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)

CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)

2. Create index.html, write the architecture, how to analyze the architecture

Idea analysis:

1. The target navigation is divided into 6 sub-items, so we can use the commonly used li to implement it. The li is arranged horizontally, so it definitely needs to be floated, so we can clear the float of the last li, and it will still work if it reaches ul Effectively wraps all the floating li

2, 1, 3, 5 navigation background is blue, 2, 4, 6 navigation background is yellow, so the colors of li are regular. Change, so at this time we can use nth-of-type

3. Each navigation has two parts, the upper part is a picture, and the lower part is text

Okay, first According to the analysis, write a good idea and ignore the implementation of css for the time being

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

<!DOCTYPE html>

<html>

 

<head>

    <meta charset="utf-8">

    <title>CSS结构性伪类选择器—nth-of-type实现自定义导航菜单案例解析</title>

</head>

 

<body>

    <div class="container">

        <ul>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航一

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航二

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航三

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航四

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航五

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航六

                </div>

            </li>

            <li class="clear"> </li>

        </ul>

    </div>

</body>

 

</html>

Copy after login

3. Write the style, create a css folder, create a new index.css in it, how to write the style inside, the following is the analysis idea

Idea analysis:

.container *Public style

1. After writing so many cases, this step is basically indispensable, and it is also to reduce code redundancy. , so here we can define the public style

So add the following code to index.css:

1

2

3

4

.container *{

    padding:0;

    margin:0;

}

Copy after login

.container outer container

1. According to the instructions, The width is 600, the height is 90, the left and right padding intervals are 100, the background color is earthy, with rounded corners, and it must be centered. There are multiple background images. The first background image is horizontally on the left, the second background image is horizontally on the right, and both vertically It is centered, and the background image size is 50px

So add the following code to index.css:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

.container{

    width: 600px;

    height: 90px;

    background-color: burlywood;

    color: white;

    margin: 0 auto;

    border-radius: 15px;

    padding:0 100px;

    background-image: url(../images/CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)),url(../images/CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example));

    background-size: 50px 50px;

    background-position-x: left,right;

    background-repeat: no-repeat;

    background-position-y: center;

}

Copy after login

li Column

1. There is no default black point, so list-style :none, horizontal arrangement, so float:left, width is the same, so width=600/6=100px, font is centered text-align: center;

So add the following code to index.css:

1

2

3

4

5

6

li{

    list-style: none;

    float: left;

    width:100px;

    text-align: center;

}

Copy after login

imgimage

1. According to the requirements, the width and height are both 50 and must be centered, so the width and height of the image inside are exactly equal to the size of the image container, so the width is 100% and the height is 100%. 100%

So add the following code to index.css:

1

2

3

4

5

6

7

8

9

10

.img{

    width: 50px;

    height: 50px;

    margin:0 auto;

     

}

.img img{

    width:100%;

    height: 100%;

}

Copy after login

clear clear floating column

1. Because the purpose of this column is to clear floating column

So add the following code to index.css:

1

2

3

4

5

6

li.clear{

    width:0;

    height: 0;

    clear: both;

    float: none;

}

Copy after login

title text

1. There is a padding distance between the top and bottom, so add the following code to index.css:

1

2

3

.title{

    padding:10px;

}

Copy after login

li Separate settings:

1, 1, 3, 5 navigation background is blue, 2, 4, 6 navigation background is yellow, so the background of odd-numbered columns is blue, and the background of even-numbered columns is yellow, exactly nth -of-type can bring expressions, so add the following code to index.css:

1

2

3

4

5

6

7

li:nth-of-type(2n){

    background-color:lightgoldenrodyellow;

    color:peru;

}

li:nth-of-type(2n+1){

    background-color:lightblue;

}

Copy after login

So far, the index.css code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

.container *{

    padding:0;

    margin:0;

}

.container{

    width: 600px;

    height: 90px;

    background-color: burlywood;

    color: white;

    margin: 0 auto;

    border-radius: 15px;

    padding:0 100px;

    background-image: url(../images/CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)),url(../images/CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example));

    background-size: 50px 50px;

    background-position-x: left,right;

    background-repeat: no-repeat;

    background-position-y: center;

}

 

li{

    list-style: none;

    float: left;

    width:100px;

    text-align: center;

}

.img{

    width: 50px;

    height: 50px;

    margin:0 auto;

     

}

.img img{

    width:100%;

    height: 100%;

}

li.clear{

    width:0;

    height: 0;

    clear: both;

    float: none;

}

.title{

    padding:10px;

}

 

li:nth-of-type(2n){

    background-color:lightgoldenrodyellow;

    color:peru;

}

li:nth-of-type(2n+1){

    background-color:lightblue;

}

Copy after login

Then introduce index.css into index. The final running effect of

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

<!DOCTYPE html>

<html>

 

<head>

    <meta charset="utf-8">

    <title>CSS结构性伪类选择器—nth-of-type实现自定义导航菜单案例解析</title>

    <link href="css/index.css" rel="stylesheet" type="text/css">

</head>

 

<body>

    <div class="container">

        <ul>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航一

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航二

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航三

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航四

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航五

                </div>

            </li>

            <li>

                <div class="img">

                    <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/image/535/279/708/1592455276504427.png"  class="lazy"   / alt="CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)" >

                </div>

                <div class="title">

                    导航六

                </div>

            </li>

            <li class="clear"> </li>

        </ul>

    </div>

</body>

 

</html>

Copy after login

in html is as follows:

CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example)

Summary:

1. Learned the structural pseudo-class selector—nth- Of-type usage, the difficulty here also lies in expressions, so you need to spend more patience to summarize the rules when writing code

The above is the detailed content of CSS structural pseudo-class selector—nth-of-type implements custom navigation menu case analysis (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to verify bootstrap date How to verify bootstrap date Apr 07, 2025 pm 03:06 PM

To verify dates in Bootstrap, follow these steps: Introduce the required scripts and styles; initialize the date selector component; set the data-bv-date attribute to enable verification; configure verification rules (such as date formats, error messages, etc.); integrate the Bootstrap verification framework and automatically verify date input when form is submitted.

How to set the bootstrap navigation bar How to set the bootstrap navigation bar Apr 07, 2025 pm 01:51 PM

Bootstrap provides a simple guide to setting up navigation bars: Introducing the Bootstrap library to create navigation bar containers Add brand identity Create navigation links Add other elements (optional) Adjust styles (optional)

See all articles