css 徽标浮动在基本响应式导航上

r6hnlfcb  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(142)
    • 问题**

" float: right"在响应类中工作不佳
基本上,我试图把我的图像标志放在右上角的一个半语言的情况下,但每一个.logo的操纵迫使导航栏改变其值。

    • 问题**

如何将徽标与右上角对齐?
"我所尝试的"

let mainNav = document.getElementById('js-menu');
let navBarToggle = document.getElementById('js-navbar-toggle');

navBarToggle.addEventListener('click', function () {
mainNav.classList.toggle('active');
});
* {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
}
body {
    font-family: 'Josefin Sans', sans-serif;
}
.navbar {
    background-image: url("bg-mob.png");
    background-size: 100vw;
    font-size: 18px;
    /*background-image: linear-gradient(260deg, #2376ae 0%, #c16ecf   100%);*/
    border: 1px solid rgba(0, 0, 0, 0.2);
    padding-bottom: 10px;
}
.main-nav {
    list-style-type: none;
    display: none;
}
.nav-links, .logo {
    text-decoration: none;
    color: rgba(255, 255, 255, 0.7);

}

.main-nav li {
    text-align: center;
    margin: 15px auto;

}
.logo {
    display: inline-block;
    font-size: 22px;
    margin-top: 10px;
    margin-left: 20px;
    /*margin-right: auto;*/

}
.logo img {
    width: 150px;
/*background-color: white;*/
}
.navbar-toggle {
    position: absolute;
    top: 15px;
    left: 20px;
    cursor: pointer;
    color: rgba(255,255,255,0.8);
    font-size: 24px;
}
.active {
    display: block;
}
<nav class="navbar">
        <span class="navbar-toggle" id="js-navbar-toggle">
            <img src="https://via.placeholder.com/50" alt="">
        </span>
    <a href="#" class="logo"><img src="https://via.placeholder.com/150/0000FF"></a>
    <ul class="main-nav" id="js-menu">
        <li>
            <a href="#" class="nav-links">Home</a>
        </li>
        <li>
            <a href="#" class="nav-links">Products</a>
        </li>
        <li>
            <a href="#" class="nav-links">About Us</a>
        </li>
        <li>
            <a href="#" class="nav-links">Contact Us</a>
        </li>
        <li>
            <a href="#" class="nav-links">Blog</a>
        </li>
    </ul>
</nav>
bvn4nwqk

bvn4nwqk1#

绝对位置和浮点数不能很好地配合使用,当您试图将它们与其他元素对齐时,它们本身就是一个令人头痛的问题。谢天谢地,您不必对它们进行干预,因为display:flex是一个东西
我要做的是在你的toggle和logo周围添加一个 Package 器div,使其弯曲,并在边缘对齐这两个项目,如下所示:

<nav class="navbar"> 
  <div style="display:flex; justify-content: space-between;">
      <span class="navbar-toggle" id="js-navbar-toggle">
          <img src="menuicon.png.png" alt="">
      </span>
      <a href="#" class="logo"><img src="logo-blue.png"></a>
  </div>
  
  <ul class="main-nav" id="js-menu">
  [...]

这样你就可以从切换器中移除绝对位置

busg9geu

busg9geu2#

您可以在.navbar中使用text-align: right;

let mainNav = document.getElementById('js-menu');
let navBarToggle = document.getElementById('js-navbar-toggle');

navBarToggle.addEventListener('click', function () {
mainNav.classList.toggle('active');
});
* {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
}
body {
    font-family: 'Josefin Sans', sans-serif;
}
.navbar {
    background-image: url("bg-mob.png");
    background-image: url("https://lh3.googleusercontent.com/IlhDxQVsR17dwwER5xYZJej867KrdSx0K5eyRP2RFP4eQJMD2pi0ZGBhrMOcajBUP9M54lpmIr90JecPUFGPaRe3sDZ82RvHBSw1rw-YJvQs7J8K3g=w1024-h683-n-l50-sg-rj");
    background-size: 100vw;
    font-size: 18px;
    /*background-image: linear-gradient(260deg, #2376ae 0%, #c16ecf   100%);*/
    border: 1px solid rgba(0, 0, 0, 0.2);
    padding-bottom: 10px;
    text-align: right; /* added */
}
.main-nav {
    list-style-type: none;
    display: none;
}
.nav-links, .logo {
    text-decoration: none;
    color: rgba(255, 255, 255, 0.7);

}

.main-nav li {
    text-align: center;
    margin: 15px auto;

}
.logo {
    display: inline-block;
    font-size: 22px;
    margin-top: 10px;
    margin-left: 20px;
    /*margin-right: auto;*/

}
.logo img {
    width: 150px;
/*background-color: white;*/
}
.navbar-toggle {
    position: absolute;
    top: 15px;
    left: 20px;
    cursor: pointer;
    color: rgba(255,255,255,0.8);
    font-size: 24px;
}
.active {
    display: block;
}
<nav class="navbar">
        <span class="navbar-toggle" id="js-navbar-toggle">
            <img src="https://lh3.googleusercontent.com/taykG37GWDgY-FGkdogDvsHSJMUGRMvkuVRT6yR-5UNkKvGRKeRlpGYXlslocOcS0txlfUdGW59JGtzADknxbMqnh6AtVCv9EXyB8nHp80YsRNA0Yw=w24-h16-n-l50-sg-rj" alt="">
        </span>
    <a href="#" class="logo"><img src="https://lh3.googleusercontent.com/taykG37GWDgY-FGkdogDvsHSJMUGRMvkuVRT6yR-5UNkKvGRKeRlpGYXlslocOcS0txlfUdGW59JGtzADknxbMqnh6AtVCv9EXyB8nHp80YsRNA0Yw=w150-h100-n-l50-sg-rj"></a>
    <ul class="main-nav" id="js-menu">
        <li>
            <a href="#" class="nav-links">Home</a>
        </li>
        <li>
            <a href="#" class="nav-links">Products</a>
        </li>
        <li>
            <a href="#" class="nav-links">About Us</a>
        </li>
        <li>
            <a href="#" class="nav-links">Contact Us</a>
        </li>
        <li>
            <a href="#" class="nav-links">Blog</a>
        </li>
    </ul>
</nav>

相关问题