简体   繁体   English

如何将文本与导航栏中心的徽标对齐?

[英]How can I align text with my logo in the center of my nav bar?

In my nav bar I want my logo text to be centered vertically with the logo.在我的导航栏中,我希望我的徽标文本与徽标垂直居中。 Here's what it looks like right now .是它现在的样子

HTML: HTML:

<header>
            <ul class = "logo">
                <img class = "logo-image" src = "images/logo.png" alt = "logo">
                <a href="../index.html">Ultimate Tennis Team</a>
            </ul>
            <nav>
                <ul class = "nav-links">
                    <li><a href="#">Leaderboard</a></li>
                    <li><a href="#">Scoring</a></li>
                    <li><a href="#">Sign in</a></li>
                </ul>
            </nav>

CSS: CSS:


.logo {
    margin-right: auto;
    list-style: none;
    display: inline-block;
}

.logo li {
    padding: 0px 20px;
}

.logo-image {
    cursor: pointer;
    height: 50px;
}

I'm new to web development.我是 web 开发的新手。 I've tried a variety of things like spans and table but I couldn't figure out this issue.我尝试了各种方法,例如 span 和 table,但我无法弄清楚这个问题。

It seems that you could just use flex layout and set align-items: center for .logo .看来您可以只使用flex布局并设置align-items: center.logo

More about Flex layout 有关Flex布局的更多信息

Exmaple:例子:

.logo {
  margin-right: auto;
  list-style: none;
  /* 👇 Add these */
  display: flex;
  align-items: center;
  /* 👇 (Optional) set a gap between the two */
  gap: 1em;
}

Assign the .logo container as a flexbox and then, with a combination of margin and align-items you should be able to align centrally vertically..logo容器指定为 flexbox,然后结合使用 margin 和align-items ,您应该能够垂直居中对齐。 The image has been changed to a publicly accessible one and in the css a border has been applied for ease of identification.该图像已更改为可公开访问的图像,并且在 css 中应用了边框以便于识别。

 .logo { margin-right: auto; list-style: none; /* position things within flex container */ display: inline-flex; flex-direction:row; align-items:center; }.logo li { padding: 0px 20px; }.logo-image { cursor: pointer; height: 50px; border:1px solid red }.logo a{ margin:0 0 0 2rem; }
 <header> <ul class="logo"> <img class="logo-image" src="//img.freepik.com/free-vector/branding-identity-corporate-vector-logo-design_460848-8717.jpg?w=300" alt="logo"> <a href="../index.html">Ultimate Tennis Team</a> </ul> <nav> <ul class="nav-links"> <li><a href="#">Leaderboard</a></li> <li><a href="#">Scoring</a></li> <li><a href="#">Sign in</a></li> </ul> </nav> </header>

You can use flexbox, to align your items.您可以使用 flexbox 来对齐您的物品。 You can use it to align it as you want.您可以根据需要使用它来对齐它。 Add this to you `.logo´ class to make it align.将此添加到您的“.logo”class 中以使其对齐。

  display: flex;
  align-items: center;

 .logo { display: flex; align-items: center; }.logo li { padding: 0px 20px; }.logo-image { cursor: pointer; height: 50px; } header { display: flex; align-items: center; }.nav-links { display: flex; list-style: none; }.nav-links li { padding: 0px 5px; }
 <header> <ul class="logo"> <img class="logo-image" src="https://png.pngtree.com/png-clipart/20190524/ourmid/pngtree-tennis-ball-clipart-png-png-image_1079029.jpg" alt="logo"> <a href="../index.html">Ultimate Tennis Team</a> </ul> <nav> <ul class="nav-links"> <li><a href="#">Leaderboard</a></li> <li><a href="#">Scoring</a></li> <li><a href="#">Sign in</a></li> </ul> </nav> </header>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM