目录
本文目标:
问题:
现在来具体操作
总结:
首页 web前端 css教程 CSS结构性伪类选择器—nth-of-type实现自定义导航菜单案例解析(代码实例)

CSS结构性伪类选择器—nth-of-type实现自定义导航菜单案例解析(代码实例)

Jun 18, 2020 pm 12:47 PM
css

本文目标:

1、掌握CSS中结构性伪类选择器—nth-of-type的用法

问题:

1、实现以下自定义导航菜单,且使用纯DIV+CSS,必须使用结构性伪类选择器—nth-of-type

实现效果.png

附加说明:

1、导航宽800px,高90px,居中显示

2、雪花背景图片宽高都是50px,酒瓶图片大小也是一样

现在来具体操作

1、准备素材:结合目标效果我们可以做一张酒瓶的图片,背景是透明的,这样背景颜色更改了,它里面透明的部分也可以随之变化,还有左右两片雪花,也是所需的素材

jptm.png

xh2.png

2、创建好index.html,写好架构,架构如何分析呢

思路分析:

1、目标导航分为6个子项,所以我们可以使用常用的li来实现它,li是水平排列,所以肯定需要浮动起来,所以最后一个li我们可以清除浮动,达到ul依然可以有效包裹住里面所有的浮动起来的li

2、1,3,5导航背景是蓝色,2,4,6的导航背景是黄色,所以li的颜色都是呈现规律性的变化,所以此时我们可使用nth-of-type

3、每个导航都是上下两部分,上部分是一张图片,下部分是文字

好,先按照分析,写好思路,暂时不管css的实现

<!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="images/jptm.png" />
                </div>
                <div class="title">
                    导航一
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航二
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航三
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航四
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航五
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航六
                </div>
            </li>
            <li class="clear"> </li>
        </ul>
    </div>
</body>

</html>
登录后复制

3、写样式 ,创建css文件夹,里面新建index.css,里面的样式怎么写了,以下是分析思路

思路分析:

.container * 公共样式

1、写了这么多案例,这一步基本上是必不可少的,也是为了减少代码冗余性,所以在这里我们可以定义公共的样式

所以index.css中添加代码如下:

.container *{
    padding:0;
    margin:0;
}
登录后复制

.container 外层容器

1、根据说明得知,宽600,高90,左右填充间隔为100,背景色土黄,带圆角,要居中,背景图片是多个,第一个背景图片水平居左,第二个背景图片水平居右,垂直方向上都是居中,背景图片大小为50px

所以index.css中添加代码如下:

.container{
    width: 600px;
    height: 90px;
    background-color: burlywood;
    color: white;
    margin: 0 auto;
    border-radius: 15px;
    padding:0 100px;
    background-image: url(../images/xh2.png),url(../images/xh2.png);
    background-size: 50px 50px;
    background-position-x: left,right;
    background-repeat: no-repeat;
    background-position-y: center;
}
登录后复制

li 列

1、不带默认黑点,所以list-style:none,水平排列所以float:left,宽度都一样,所以width=600/6=100px,字体居中text-align: center;

所以index.css中添加代码如下:

li{
    list-style: none;
    float: left;
    width:100px;
    text-align: center;
}
登录后复制

img图片

1、根据要求得知宽高都是50,且要居中,所以里面的图片的宽高正好等于图片容器的大小,所以宽100%,高100%

所以index.css中添加代码如下:

.img{
    width: 50px;
    height: 50px;
    margin:0 auto;
    
}
.img img{
    width:100%;
    height: 100%;
}
登录后复制

clear清除浮动列

1、因为该列的目的是清除浮动

所以index.css中添加代码如下:

li.clear{
    width:0;
    height: 0;
    clear: both;
    float: none;
}
登录后复制

title文字

1、上下存有填充距离,所以index.css中添加代码如下:

.title{
    padding:10px;
}
登录后复制

li的单独设置:

1、1,3,5导航背景是蓝色,2,4,6的导航背景是黄色,所以说明奇数列背景是蓝色,偶数列背景是黄色,正好nth-of-type可以带表达式,所以index.css中添加代码如下:

li:nth-of-type(2n){
    background-color:lightgoldenrodyellow;
    color:peru;
}
li:nth-of-type(2n+1){
    background-color:lightblue;
}
登录后复制

到此为止,index.css代码如下:

.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/xh2.png),url(../images/xh2.png);
    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;
}
登录后复制

然后将index.css引入index.html中

<!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="images/jptm.png" />
                </div>
                <div class="title">
                    导航一
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航二
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航三
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航四
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航五
                </div>
            </li>
            <li>
                <div class="img">
                    <img src="images/jptm.png" />
                </div>
                <div class="title">
                    导航六
                </div>
            </li>
            <li class="clear"> </li>
        </ul>
    </div>
</body>

</html>
登录后复制

最终运行效果如下:

1.png

总结:

1、学习了结构性伪类选择器—nth-of-type用法,这里的难点也是在于表达式,所以写代码的时候需要多花点耐心去总结规律

以上是CSS结构性伪类选择器—nth-of-type实现自定义导航菜单案例解析(代码实例)的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

vue中怎么用bootstrap vue中怎么用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分为五个步骤:安装 Bootstrap。在 main.js 中导入 Bootstrap。直接在模板中使用 Bootstrap 组件。可选:自定义样式。可选:使用插件。

HTML,CSS和JavaScript的角色:核心职责 HTML,CSS和JavaScript的角色:核心职责 Apr 08, 2025 pm 07:05 PM

HTML定义网页结构,CSS负责样式和布局,JavaScript赋予动态交互。三者在网页开发中各司其职,共同构建丰富多彩的网站。

bootstrap怎么写分割线 bootstrap怎么写分割线 Apr 07, 2025 pm 03:12 PM

创建 Bootstrap 分割线有两种方法:使用 标签,可创建水平分割线。使用 CSS border 属性,可创建自定义样式的分割线。

了解HTML,CSS和JavaScript:初学者指南 了解HTML,CSS和JavaScript:初学者指南 Apr 12, 2025 am 12:02 AM

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

bootstrap怎么插入图片 bootstrap怎么插入图片 Apr 07, 2025 pm 03:30 PM

在 Bootstrap 中插入图片有以下几种方法:直接插入图片,使用 HTML 的 img 标签。使用 Bootstrap 图像组件,可以提供响应式图片和更多样式。设置图片大小,使用 img-fluid 类可以使图片自适应。设置边框,使用 img-bordered 类。设置圆角,使用 img-rounded 类。设置阴影,使用 shadow 类。调整图片大小和位置,使用 CSS 样式。使用背景图片,使用 background-image CSS 属性。

bootstrap按钮怎么用 bootstrap按钮怎么用 Apr 07, 2025 pm 03:09 PM

如何使用 Bootstrap 按钮?引入 Bootstrap CSS创建按钮元素并添加 Bootstrap 按钮类添加按钮文本

bootstrap怎么设置框架 bootstrap怎么设置框架 Apr 07, 2025 pm 03:27 PM

要设置 Bootstrap 框架,需要按照以下步骤:1. 通过 CDN 引用 Bootstrap 文件;2. 下载文件并将其托管在自己的服务器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根据需要编译 Sass/Less;5. 导入定制文件(可选)。设置完成后,即可使用 Bootstrap 的网格系统、组件和样式创建响应式网站和应用程序。

bootstrap怎么调整大小 bootstrap怎么调整大小 Apr 07, 2025 pm 03:18 PM

要调整 Bootstrap 中元素大小,可以使用尺寸类,具体包括:调整宽度:.col-、.w-、.mw-调整高度:.h-、.min-h-、.max-h-

See all articles