如何在 WordPress 中从左到右显示菜单对齐方式?

How to display menu alignmet from left to right in WordPress?

我是 WordPress 主题开发的新手,对菜单项的对齐有疑问。它从上到下出现,我想从左到右更改项目,并添加一些功能,例如悬停在项目上时,它将以粗体显示并显示子页面。

我无法更改任何内容:颜色、尺寸等

我该如何解决这个问题?

我的菜单是这样的:

我想要的样子:

我的header.php文件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="profile" href="http://gmpg.org/xfn/11">
        <?php wp_head(); ?>
    </head>
    <body <?php body_class(); ?>>
        <div id="page">
            <a href="#content" class="skip-link screen-reader-text">
                <?php esc_html_e( 'Skip to content', 'wphierarchy' ) ?>
            </a>
            <header id="masthead" class="site-header" role="banner">
                <div class="site-branding">
                    <p class="site-title">
                        <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
                            <?php bloginfo( 'name' ); ?>
                        </a>
                    </p>
                    <p class="site-description">
                        <?php bloginfo( 'description' ); ?>
                    </p>
                </div>

                <nav id="site-navigation" class="main navigation" role="navigation">
                    <?php
                        $args = [
                            'theme_location' => 'main-menu'
                        ];
                        wp_nav_menu( $args );
                    ?>
                </nav>
            </header>
            <div id="content" class="site-content">

style.css 文件

body {
  background-color: #fff091;
  font-family: "Open Sans", sans-serif;
}

body.tag-50,
body.category-9 {
  background-color: #a7ff91;
}

h1, h2, h3, h4, h5, h6,
.site-title {
  font-family: "Varela Round", sans-serif;
}

h1 {
  font-size: 2rem;
}

h2 {
  margin:  2rem 0 1rem;
}

h2.search-title {
  text-transform: capitalize;
}

a {
  color: #494329;
  text-decoration: none;
}

pre {
  background: #222;
  padding: 10px;
  border: 1px #777 solid;
  color: #ededed;
  font-family: monospace;
  font-size: 1rem;
}

video {
  max-width: 100%;
}

ul {
  padding-left: 0;
  margin-left: 20px;
  list-style-position: outside;
}

.site-title {
  font-size: 2rem;
  font-weight: bold;
  margin: 0 0 1rem 0;
}

#masthead,
footer.site-footer {
  margin: 2rem auto 1rem;
  text-align: center;
  width: 80%;
}

.main-navigation li {
  list-style-type: none;
  display: inline-block;
}

.main-navigation li a {
  padding: 5px;
}

.main-navigation li:hover>a {
  color: #000;
}

.main-navigation li.current-menu-item>a,
.main-navigation li.current-page-parent>a {
  font-weight: bold;
  color: #494329;
}

.main-navigation li ul {
  display: none;
}

.main-navigation li:hover ul {
  display: block;
  position: absolute;
  margin-left: -10px;
  z-index: 100;
  background: #fff091;
  padding: 5px 10px;
}

.main-navigation li:hover ul li {
  display: block;
  margin: .5rem 0;
}

#content {
  background: #fff;
  margin: 20px auto;
  padding: 20px;
  width: 80%;
  max-width: 1020px;
  border-radius: 5px;
  position: relative;
  overflow: auto;
  -ms-word-wrap: break-word;
  word-wrap: break-word;
}

.single-portfolio #content,
.page-template-template-splash #content,
.page-id-1096 #content,
.category-excerpt #content,
.tag-wordpress #content {
  width: 50%;
  max-width: 720px;
}

#content article,
#content article div,
#content img {
  max-width: 100%;
}

#content img {
  height: auto;
}

#primary {
  width: 70%;
  float: left;
}


#primary.extended,
#primary.excerpt,
#primary.wordpress {
  width: 100%;
  float: none;
}

article.post {
  margin-bottom: 4rem;
}

.author article.post {
  margin-bottom: 2rem;
}

.prev-posts,
.next-posts,
.archive article.portfolio {
  width: 45%;
  float: left;
}

.next-posts {
  text-align: right;
  float: right;
}

.archive article.portfolio:nth-child(even) {
  margin-right: 5%;
}

.form-submit input#submit,
.button {
  border: 1px #ccc solid;
  border-radius: 3px;
  background: #fff;
  padding: 1rem 2rem;
  margin: 1rem 0;
  display: inline-block;
}

.form-submit input#submit:hover,
.button:hover {
  cursor: pointer;
  border-color: #777;
}


#secondary {
  width: 25%;
  float: right;
}

#secondary h2 {
  font-size: 1.2rem;
}

.byline {
  color: #777;
  font-size: .8rem;
}

.byline a {
  color: #777;
  text-decoration: underline;
  display: inline-block;
}

.byline a:hover {
  color: #564A71;
}

.author-bio {
  border-bottom: 1px #ccc solid;
  padding-bottom: 1rem;
  margin-bottom: 2rem;
}

.author-bio img {
  float: left;
}


/*--------------------------------------------------------------
# Default WP Classes
--------------------------------------------------------------*/


.alignleft, img.alignleft {
    margin-right: 1.5em;
    display: inline;
    float: left;
}
.alignright, img.alignright {
    margin-left: 1.5em;
    display: inline;
    float: right;
}
.aligncenter, img.aligncenter {
    margin-right: auto;
    margin-left: auto;
    display: block;
    clear: both;
}
.wp-caption {
    margin-bottom: 1.5em;
    text-align: center;
    padding-top: 5px;
}
.wp-caption img {
    border: 0 none;
    padding: 0;
    margin: 0;
}
.wp-caption p.wp-caption-text {
    line-height: 1.5;
    font-size: 10px;
    margin: 0;
}
.wp-smiley {
    margin: 0 !important;
    max-height: 1em;
}
blockquote.left {
    margin-right: 20px;
    text-align: right;
    margin-left: 0;
    width: 33%;
    float: left;
}
blockquote.right {
    margin-left: 20px;
    text-align: left;
    margin-right: 0;
    width: 33%;
    float: right;
}
.gallery dl {}
.gallery dt {}
.gallery dd {}
.gallery dl a {}
.gallery dl img {}
.gallery-caption {}

.size-full {}
.size-large {}
.size-medium {}
.size-thumbnail {}


  /**
   * 11.4 - Comments
   */

  .comments-area {
    margin: 3rem 0 3.5em;
  }

  .comment-list + .comment-respond,
  .comment-navigation + .comment-respond {
    padding-top: 1.75em;
  }

  .comments-title,
  .comment-reply-title {
    border-top: 1px solid #ccc;
    font-size: 23px;
    font-size: 1.4375rem;
    font-weight: 700;
    line-height: 1.3125;
    padding-top: 1.217391304em;
  }

  .comments-title {
    margin-bottom: 1.217391304em;
  }

  .comment-list {
    list-style: none;
    margin: 0;
  }

  .comment-list article,
  .comment-list .pingback,
  .comment-list .trackback {
    border-top: 1px solid #d1d1d1;
    padding: 1.75em 0;
  }

  .comment-list .children {
    list-style: none;
    margin: 0;
  }

  .comment-list .children > li {
    padding-left: 0.875em;
  }

  .comment-author {
    color: #1a1a1a;
    margin-bottom: 0.4375em;
  }

  .comment-author .avatar {
    float: left;
    height: 28px;
    margin-right: 0.875em;
    position: relative;
    width: 28px;
  }

  .comment-form textarea {
    width: 90%;
    font-size: 1rem;
    padding: .5rem;
  }

  .comment-form input {
    padding: .5rem 1rem;
    width: 100%;
  }


  /*--------------------------------------------------------------
  # Twenty Sixteen Comments CSS
  --------------------------------------------------------------*/

  .comment-metadata,
  .pingback .edit-link {
    font-size: 13px;
    font-size: 0.8125rem;
    line-height: 1.6153846154;
  }

  .comment-metadata {
    margin-bottom: 2.1538461538em;
  }

  .comment-metadata a,
  .pingback .comment-edit-link {

  }

  .comment-metadata a:hover,
  .comment-metadata a:focus,
  .pingback .comment-edit-link:hover,
  .pingback .comment-edit-link:focus {

  }

  .comment-metadata .edit-link,
  .pingback .edit-link {
    display: inline-block;
  }

  .comment-metadata .edit-link:before,
  .pingback .edit-link:before {
    content: "[=11=]2f";
    display: inline-block;
    opacity: 0.7;
    padding: 0 0.538461538em;
  }

  .comment-content ul,
  .comment-content ol {
    margin: 0 0 1.5em 1.25em;
  }

  .comment-content li > ul,
  .comment-content li > ol {
    margin-bottom: 0;
  }

  .comment-reply-link {
    border: 1px solid #d1d1d1;
    border-radius: 2px;
    color: #007acc;
    display: inline-block;
    font-size: 13px;
    font-size: 0.8125rem;
    line-height: 1;
    margin-top: 2.1538461538em;
    padding: 0.5384615385em 0.5384615385em 0.4615384615em;
  }

  .comment-reply-link:hover,
  .comment-reply-link:focus {
    outline: 0;
  }

  .comment-form {
  }

  .comment-form label {
    display: block;
    font-size: 13px;
    font-size: 0.8125rem;
    letter-spacing: 0.076923077em;
    line-height: 1.6153846154;
    margin-bottom: 0.5384615385em;
    text-transform: uppercase;
  }

  .comment-list .comment-form {
    padding-bottom: 1.75em;
  }

  .comment-notes,
  .comment-awaiting-moderation,
  .logged-in-as,
  .form-allowed-tags {
    color: #686868;
    font-size: 13px;
    font-size: 0.8125rem;
    line-height: 1.6153846154;
    margin-bottom: 2.1538461538em;
  }

  .no-comments {
    border-top: 1px solid #d1d1d1;
    font-family: Montserrat, "Helvetica Neue", sans-serif;
    font-weight: 700;
    margin: 0;
    padding-top: 1.75em;
  }

  .comment-navigation + .no-comments {
    border-top: 0;
    padding-top: 0;
  }

  .form-allowed-tags code {
    font-family: Inconsolata, monospace;
  }

  .form-submit {
    margin-bottom: 0;
  }

  .required {
    color: #007acc;
    font-family: Merriweather, Georgia, serif;
  }

  .comment-reply-title small {
    font-size: 100%;
  }

  .comment-reply-title small a {
    border: 0;
    float: right;
    height: 32px;
    overflow: hidden;
    width: 26px;
  }

  .comment-reply-title small a:hover,
  .comment-reply-title small a:focus {
    color: #1a1a1a;
  }

  .comment-reply-title small a:before {
    content: "\f405";
    font-size: 32px;
    position: relative;
    top: -5px;
  }

/*--------------------------------------------------------------
# Accessibility
--------------------------------------------------------------*/
/* Text meant only for screen readers. */
.screen-reader-text {
    clip: rect(1px, 1px, 1px, 1px);
    position: absolute !important;
    height: 1px;
    width: 1px;
    overflow: hidden;
    word-wrap: normal !important; /* Many screen reader and browser combinations announce broken words as they would appear visually. */
}

.screen-reader-text:focus {
    background-color: #f1f1f1;
    border-radius: 3px;
    box-shadow: 0 0 2px 2px rgba(0, 0, 0, 0.6);
    clip: auto !important;
    color: #21759b;
    display: block;
    font-size: 14px;
    font-size: 0.875rem;
    font-weight: bold;
    height: auto;
    left: 5px;
    line-height: normal;
    padding: 15px 23px 14px;
    text-decoration: none;
    top: 5px;
    width: auto;
    z-index: 100000; /* Above WP toolbar. */
}

/* Do not show the outline on the skip link target. */
#content[tabindex="-1"]:focus {
    outline: 0;
}

/*--------------------------------------------------------------
# Media Queries
--------------------------------------------------------------*/
@media only screen and ( max-width: 720px ) {
  #content {
    width: 90% !important;
  }
}

要从左到右对齐项目,您可以在具有 id menu-main-menuul 上使用 flex,因为它是所有菜单元素之间的第一个公共父元素。

试试下面的代码,这将有助于实现您的需要,但它会在移动设备上中断。



#site-navigation #menu-main-menu{
  display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-evenly;
    align-items: stretch;
    align-content: stretch;
}
#site-navigation #menu-main-menu  li {
  padding: 15px;
  display: block;
  position: relative;
}
#site-navigation #menu-main-menu li   ul {
  display: none;
} 

#site-navigation #menu-main-menu li:hover ul {
  display: block;
  margin: 0;
  position: absolute;
  top: 100%;
  width: 100%;
  background: red;
}
#site-navigation #menu-main-menu li:hover ul a {
  width: 100%;
  display: block
}