CSS如何使菜单居中 [英] CSS how to center a menu

查看:42
本文介绍了CSS如何使菜单居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让菜单居中.我有图像等,居中就好,但我无法将菜单居中.

以下是该网站的一些与菜单相关的 CSS:

@charset "UTF-8";/* CSS 文档 */#导航{宽度:975px;/* 六个主菜单按钮,每个 150px */保证金:1 自动;/* 基于来自 stackoverflow 的建议 */列表样式类型:无;文字对齐:居中;位置:固定;/* 用于在顶部渲染菜单栏 */顶部:0;/* 用于在顶部渲染菜单栏 */z-索引:1000;/* 使用 z-index 在所有内容的顶部渲染菜单栏,甚至是幻灯片 */显示:内联块;}#导航李{显示:内联块;/* 用于包装文本 */位置:相对;向左飘浮;背景颜色:#BDB76B;/* 菜单背景颜色 */;字体系列:Arial、Helvetica、sans-serif;字体大小:14px;}#nav li:hover {/* 悬停在父菜单上时突出显示当前悬停的列表项 */背景色:#BDB76B;字体粗细:500;颜色:蓝色;}#nav li a {显示:内联块;/* 用于包装文本 */边距:5px 0;/*菜单元素之间的空间*/文字装饰:无;宽度:150px;/* 这是菜单项的宽度 */行高:15px;/* 这是菜单项的文本高度 */颜色:#000000;/* 列表项字体颜色 000=black, FFF=white*/}#nav li a:hover {/* 突出显示父菜单的悬停项目 */背景色:#BDB76B;字体粗细:500;颜色:蓝色;字体大小:18px;宽度:225px;}#nav li li a {/* 子菜单项的字体变小 */字体大小:90%;背景颜色:#BDB76B;/* 子菜单的菜单背景颜色 */}#nav li li a:hover {/* 悬停在子菜单上时突出显示当前悬停的列表 */背景色:#BDB76B;字体粗细:500;颜色:蓝色;字体大小:120%;宽度:200px;}/*--- 子列表样式 ---*/#nav ul {位置:绝对;填充:0;左:0;显示:无;/* 隐藏子列表 */}#nav li:hover ul ul {显示:无;}/* 隐藏子子列表 */#nav li:hover ul {显示:块;}/* 悬停时显示子列表 */#nav li:hover ul ul {显示:无;}/* 隐藏子子子列表 */#nav li li:hover ul {显示:块;/* 悬停时显示子子列表 */左边距:150px;/* 这应该与父列表项的宽度相同 */边距顶部:-35px;/* 将子菜单顶部与列表项顶部对齐 */}#nav li li li:hover ul {显示:块;/* 在悬停时显示子子子列表 */左边距:150px;/* 这应该与父列表项的宽度相同 */边距顶部:-35px;/* 将子菜单顶部与列表项顶部对齐 */宽度:200px;}.upcoming_events_list li {/* 即将到来的事件列表级别的属性 *//* 改变包含列表中的链接在 div 中的 CSS */宽度:380px;文本对齐:左;列表样式类型:圆形;颜色:蓝色;}.upcoming_events_list li a:link {/* 未访问的即将发生的事件的属性 */font-family: "Times New Roman", Times, serif;颜色:黑色;字体大小:18px;字体粗细:300;列表样式类型:圆形;}.upcoming_events_list li a:visited {/* 访问即将到来的事件的属性 */font-family: "Times New Roman", Times, serif;颜色:蓝色;字体大小:18px;字体粗细:100;列表样式类型:圆形;}.upcoming_events_list li a:hover {/* 即将到来的事件悬停属性 */font-family: "Times New Roman", Times, serif;字体大小:20px;字体粗细:500;}.upcoming_events_list li a:active {/* 选定的即将发生的事件的属性 */font-family: "Times New Roman", Times, serif;字体大小:20px;字体粗细:500;文字装饰:下划线;}

这是网站 index.shtml 的一个子集:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/ss_template_1.dwt" codeOutsideHTMLIsLocked="false" --><头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><!-- InstanceBeginEditable name="Page_Specific_Title" --><title>我的网站主页 SHTML</title><!-- InstanceEndEditable --><link href="CSS/MySite.css" rel="stylesheet" type="text/css"/><style type="text/css"><!--身体 {背景颜色:#EEE8AA;}--></风格><!-- InstanceBeginEditable name="Page_Specific_Head" --><!-- 包含 jQuery 库--><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script><!-- 包括 Cycle 插件 --><script type="text/javascript" src="http://malsup.github.com/jquery.cycle.all.js"></script><script type="text/javascript">$(document).ready(function() {$('.幻灯片').cycle({fx: 'shuffle',//选择你的过渡类型,例如:fade、scrollUp、shuffle 等...speed: 1500,//发生转换的速度(以毫秒为单位)delay: 1000,//开始幻灯片之前在第一张幻灯片上花费的时间连续: 0,//true (1) 在当前转换完成后立即开始下一个转换timeout: 9000//在每张幻灯片上花费的时间});});<style type="text/css"><!--p {边距:0;填充:0}--></风格><meta name="description" content="热烈欢迎大家.我们邀请大家参与我们的方方面面教区生活."/><meta name="keywords" content="教区、教堂、欢迎、欢迎所有人、精神、和平、正义"/><!-- InstanceEndEditable --><身体><!-- InstanceBeginEditable name="Page_Specific_Body" --><br/><div><?php include("menu_primary1.shtml");?>

<div class="block_center_1000_text_left"><!-- 标题是使用 Pristina 常规 36 磅字体创建的.背景颜色代码为 D3AC25--><p align="center"><img src="images/front_page_title.jpg" alt="教区站点"/></p>

<div class="outer_width_1000" >

这里是包含文件 menu_primary1.shtml 的一个子集:

<li><a href="#">教区团体</a><ul><li><a href="Attachments/ChurchGroupsListing.pdf" target="_blank">团体和事工列表</a></li><li><a href="good_art_series.shtml">Good Art Series</a></li><li><a href="contact_us.shtml#peace">和平与正义外展</a></li><li><a href="svdp.shtml">Saint Vincent de Paul</a></li><li><a href="#">即将举行的活动</a><?php include("menu_upcoming_events1.shtml");?>... 其他三个顶级
  • ;菜单项
  • 感谢您的帮助.

    解决方案

    如果您将任何元素或菜单项居中,您必须遵循 2 个步骤:

    setp1:

    ul{宽度:100%;显示:表;文本对齐:居中;}

    步骤 2:

    ul li{显示:内联块;浮动:无;}

    完成.就是这样,现在自己尝试一下.

    I can't get a menu to be centered. I have images, etc. that center just fine, but I can't center the menu.

    Here's some of the site's CSS that pertains to the menu:

    @charset "UTF-8";
    
        /* CSS Document */
    
    #nav {
        width:975px; /* six main menu buttons at 150px per */
        margin:1 auto; /* Based on a suggestion from stackoverflow */ 
        list-style-type:none;
        text-align:center;
        position: fixed; /* used to render menu bar on top */
        top: 0; /* used to render menu bar on top */
        z-index: 1000; /* use z-index to render menu bar on top of everything, even the slideshow */
        display:inline-block;
        }
    
    #nav li {
        display:inline-block; /* used to wrap text */
        position:relative;
        float:left;
        background-color: #BDB76B; /* menu background color */;
        font-family: Arial, Helvetica, sans-serif;
        font-size:14px;
        }
    
    #nav li:hover {  /* highlights current hovered list item when hovering over the parent menu */
        background-color:#BDB76B;
        font-weight:500;
        color:blue;
    }
    
    #nav li a {
        display:inline-block; /* used to wrap text */
        margin:5px 0; /*space between menu elements */
        text-decoration:none;
        width:150px; /* this is the width of the menu items */
        line-height:15px; /* this is the text height of the menu items */
        color:#000000; /* list item font color 000=black, FFF=white*/
        }
    
    #nav li a:hover {  /* highlights hovered item of the parent menu */
        background-color:#BDB76B;
        font-weight:500;
        color:blue;
        font-size:18px;
        width:225px;
    }
    
    #nav li li a { /* smaller font size for sub menu items */
        font-size:90%;
        background-color: #BDB76B; /* menu background color for submenues */
    } 
    
    #nav li li a:hover {  /* highlights current hovered list when hovering over sub menues */
        background-color:#BDB76B;
        font-weight:500;
        color:blue;
        font-size:120%;
        width:200px;
    }
    
    /*--- Sublist Styles ---*/
    #nav ul {
        position:absolute;
        padding:0;
        left:0;
        display:none; /* hides sublists */
        }
    
    #nav li:hover ul ul {
        display:none;
    } /* hides sub-sublists */
    
    #nav li:hover ul {
        display:block;
    } /* shows sublist on hover */
    
    #nav li:hover ul ul ul {
        display:none;
    } /* hides sub-sub-sublists */
    
    #nav li li:hover ul {
        display:block; /* shows sub-sublist on hover */
        margin-left:150px; /* this should be the same width as the parent list item */
        margin-top:-35px; /* aligns top of sub menu with top of list item */
        }
    
    #nav li li li:hover ul {
        display:block; /* shows sub-sub-sublist on hover */
        margin-left:150px; /* this should be the same width as the parent list item */
        margin-top:-35px; /* aligns top of sub menu with top of list item */
        width:200px;
        }
    
    .upcoming_events_list li { /* attributes for the upcoming_events list level */
        /* CSS to alter the links in the include list when inside the div */
        width: 380px;
        text-align: left;
        list-style-type:circle;
        color:blue;
    }
    
    .upcoming_events_list li a:link { /* attributes for the upcoming_events unvisited */
        font-family: "Times New Roman", Times, serif;
        color: black;
        font-size: 18px;
        font-weight: 300;
        list-style-type: circle;
    }
    
    .upcoming_events_list li a:visited { /* attributes for the upcoming_events visited */
        font-family: "Times New Roman", Times, serif;
        color: blue;
        font-size: 18px;
        font-weight: 100;
        list-style-type: circle;
    
    }
    
    .upcoming_events_list li a:hover { /* attributes for the upcoming_events hover */
        font-family: "Times New Roman", Times, serif;
        font-size: 20px;
        font-weight: 500;
    }
    
    .upcoming_events_list li a:active { /* attributes for the upcoming_events selected */
        font-family: "Times New Roman", Times, serif;
        font-size: 20px;
        font-weight: 500;
        text-decoration: underline;
    }
    

    Here's a subset of the site's index.shtml:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/ss_template_1.dwt" codeOutsideHTMLIsLocked="false" -->
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <!-- InstanceBeginEditable name="Page_Specific_Title" -->
        <title>My Site Home Page SHTML</title>
        <!-- InstanceEndEditable -->
        <link href="CSS/MySite.css" rel="stylesheet" type="text/css" />
        <style type="text/css">
        <!--
        body {
            background-color: #EEE8AA;
             }
        -->
        </style>
        <!-- InstanceBeginEditable name="Page_Specific_Head" -->
    <!-- include jQuery library -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <!-- include Cycle plugin -->
        <script type="text/javascript" src="http://malsup.github.com/jquery.cycle.all.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('.slideshow').cycle({
                    fx:         'shuffle',  // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                    speed:      1500,       // speed at which the transition occurs (in miliseconds)
                    delay:      1000,       // time spent on the first slide before starting the slideshow
                    continuous: 0,          // true (1) to start next transition immediately after current one completes
                    timeout:    9000        // time spent on each slide
                });
            });
        </script>
        <style type="text/css">
        <!--
        p {margin:0; padding:0}
        -->
        </style>
        <meta name="description" content="A warm welcome to all. We invite everyone to participate in all aspects of our
        parish life."/>
        <meta name="keywords" content="parish,church,welcome,all are welcome,spiritual,peace,justice"/>
    <!-- InstanceEndEditable -->
    </head>
    <body>
        <!-- InstanceBeginEditable name="Page_Specific_Body" -->
    <br />
    <div>
    <?php include("menu_primary1.shtml");
    ?>
    </div>
    <div class="block_center_1000_text_left">
        <!-- The title was created using the Pristina Regular 36 point font.  The background color code is D3AC25-->
        <p align="center">
            <img src="images/front_page_title.jpg" alt="Parish Site"/>
        </p>
    </div>
    <div class="outer_width_1000" >
    </div>
    </body>
    

    and here's a subset of the included file menu_primary1.shtml:

    <ul id="nav">
      <li><a href="#">Faith Formation</a>
        <ul>
          <li><a href="adult_faith_formation.shtml">Adult Faith Formation</a></li>
          <li><a href="Attachments/RelEdGrades1to5Schedule.pdf" target="_blank">Youth Faith Formation Grades 1 to 5</a></li>
          <li><a href="Attachments/RelEdGrades6to8Schedule.pdf" target="_blank">Youth Faith Formation Grades 6 to 8</a></li>
          <li><a href="Attachments/ConfirmationPreparation.pdf" target="_blank">Confirmation Preparation</a></li>
        </ul>
      </li>
      <li><a href="#">Parish Groups</a>
        <ul>
          <li><a href="Attachments/ChurchGroupsListing.pdf" target="_blank">Listing of Groups and Ministries</a></li>
          <li><a href="good_art_series.shtml">Good Art Series</a></li>
          <li><a href="contact_us.shtml#peace">Peace and Justice Outreach</a></li>
          <li><a href="svdp.shtml">Saint Vincent de Paul</a></li>
        </ul>
      </li>
      <li><a href="#">Upcoming Events</a>
          <?php include("menu_upcoming_events1.shtml");
          ?>
          </li>
    ... Three other top-level <li> menu items
    </ul>
    

    Thanks for your help.

    解决方案

    if you center any element or menu item you have to follow 2 steps:

    setp1:

    ul{
       width: 100%;
       display: table;
       text-align: center;
    }
    

    step2:

    ul li{
         display: inline-block;
         float: none;
    }
    

    and done. That's it now try it yourself.

    这篇关于CSS如何使菜单居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    前端开发最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆