跳转到主要内容
你行网

Main navigation

  • 首页
  • Drupal
  • 编程与开发
  • 数据库
  • 操作系统与应用
  • 服务器与运维
  • 社区
User account menu
  • 登录

面包屑

  1. 首页

CSS 基础学习-195 CSS把导航菜单固定在顶部或底部

由 hrs, 7 四月, 2023

使用position: fixed; 和   top: 0;  把导航菜单固定在顶部

实例代码:

<!doctype html>
<html>                                                        
    <head>                                                    
        <meta charset="utf-8">                                
        <style>                                               
            body{                                             
            margin:0;                                         
            }                                                 
            div{                                              
                background-color:green;                       
                padding:20px;                                 
                margin-top:30px;                              
                height:1500px;                                
            }                                                 
                                                              
            ul{                                               
                background-color:black;                       
                position:fixed;   /*固定ul位置*/              
                top:0;  /*顶部*/                              
                width:100%;                                   
                padding:0;                                    
                margin:0;                                     
                list-style-type:none;                         
                overflow:hidden;                              
            }                                                 
            li{                                               
                float:left;                                   
            }                                                 
            li a{                                             
                display:block;                                
                text-align:center;                            
                color:white;                                  
                text-decoration:none;                         
                padding:14px 16px;                            
            }                                                 
            li a:hover:not(.home){                            
                    background-color:red;                     
            }                                                 
            .home{                                            
                 background-color:#4CAF50;                                       
             }                                                                   
         </style>                                                                
     </head>                                                                     
     <body>                                                                      
         <ul>                                                                    
             <li><a class="home" href="https://www.dwoke.com" >Home</a></li>     
             <li><a href="https://www.dwoke.com" >News</a></li>                  
             <li><a href="https://www.dwoke.com" >Contact</a></li>               
             <li><a href="https://www.dwoke.com" >About</a></li>                 
         </ul>                                                                   
         <div>                                                                   
         <h3>固定的顶部导航栏</h3>                                               
         <h4>请滚动页面以查看效果。</h4>                                         
         <h4>页面滚动时导航栏将位于页面顶部。</h4>                               
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         </div>                                                                  
     </body>                                                                     
                                                                                 
 </html>                                                                         

运行效果:

css-195.png

使用position: fixed; 和  bottom: 0;  把导航菜单固定在底部

实例代码:

<!doctype html>
<html>                                                        
    <head>                                                    
        <meta charset="utf-8">                                
        <style>                                               
            body{                                             
            margin:0;                                         
            }                                                 
            div{                                              
                background-color:green;                       
                padding:20px;                                 
                margin-top:30px;                              
                height:1500px;                                
            }                                                 
                                                              
            ul{                                               
                background-color:black;                       
                position:fixed;   /*固定ul位置*/              
                bottom:0;  /*底部*/                              
                width:100%;                                   
                padding:0;                                    
                margin:0;                                     
                list-style-type:none;                         
                overflow:hidden;                              
            }                                                 
            li{                                               
                float:left;                                   
            }                                                 
            li a{                                             
                display:block;                                
                text-align:center;                            
                color:white;                                  
                text-decoration:none;                         
                padding:14px 16px;                            
            }                                                 
            li a:hover:not(.home){                            
                    background-color:red;                     
            }                                                 
            .home{                                            
                 background-color:#4CAF50;                                       
             }                                                                   
         </style>                                                                
     </head>                                                                     
     <body>                                                                      
         <ul>                                                                    
             <li><a class="home" href="https://www.dwoke.com" >Home</a></li>     
             <li><a href="https://www.dwoke.com" >News</a></li>                  
             <li><a href="https://www.dwoke.com" >Contact</a></li>               
             <li><a href="https://www.dwoke.com" >About</a></li>                 
         </ul>                                                                   
         <div>                                                                   
         <h3>固定的顶部导航栏</h3>                                               
         <h4>请滚动页面以查看效果。</h4>                                         
         <h4>页面滚动时导航栏将位于页面顶部。</h4>                               
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         <p>some text some text some text some text..</p>                        
         </div>                                                                  
     </body>                                                                     
                                                                                 
 </html>                                                                         

实例效果:

css-196.png

  • 登录或注册以发表评论
内容类型分类
编程与开发
文章归类
前端开发

文章标签

  • CSS

评论

文章标签

Excel
CSS
ubuntu
php
vim
五笔
VPN
Linux
command
HTML
drupal 8
drupal 7
centos
composer
Mysql
模块(module)
drush
Drupal
JavaSrcipt
apache
英语
drupal 9
nerdtree
荔壳风
network
果树种植
surround
drupal10
SEO
主题(theme)
更多

相关文章

  • 匿名函数(Anonymous Functions)详解
  • 如何去掉input和button标签中间有缝隙
  • PHP 中的四种标量数据类型
  • php 语言的四种标记风格
  • date_default_timezone_set 设置php服务器的默认时区
  • php8.3 的扩展模块简略说明
  • <input>` 类型的定义和用途的简要说明
  • 什么是cookie
  • php字符串运算符
  • HTML空格符简介
RSS源

友情链接 网站地图 版权信息 联系我们 网址导航 社区

Copyright © 2019 - 2021 你行网 版权所有  粤ICP备19072650号