首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在悬停时阻止单个物品向左移动

如何在悬停时阻止单个物品向左移动
EN

Stack Overflow用户
提问于 2022-09-14 15:21:08
回答 2查看 28关注 0票数 0

我正在尝试创建一个带有下拉列表的反应性导航条。为了让事情变得更简单,我把我的css放在一个样式标签中。我已经尝试了我所能想到的一切,把“我们的故事”从左移到“我们的故事”。此外,为了使这更多的挑战,我只是允许自己使用HTML和CSS。

非常感谢你的帮助。

代码语言:javascript
复制
body {
  background-image: url(http://www.getuwired.com/devtest/Death_to_stock_photography_Vibrant.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  font-size: 22px;
  line-height: 32px;
  color: #000;
  margin: 0;
  padding: 0;
  word-wrap: break-word !important;
  font-family: 'Roboto', sans-serif;
}

h1 {
  font-size: 60px;
  text-align: center;
  color: #FFF;
}

h3 {
  font-size: 30px;
  line-height: 34px;
  text-align: center;
  color: #FFF;
}

h3 a {
  color: #FFF;
}

a {
  color: #FFF;
}

h1 {
  margin-top: 100px;
  text-align: center;
  font-size: 60px;
  line-height: 70px;
  font-family: 'Bree Serif', 'serif';
}

#title {
  color: #000;
  text-shadow: none;
}

#container {
  margin: 0 auto;
  max-width: 890px;
}

p {
  text-align: center;
}

.toggle,
[id^=drop] {
  display: none;
}


/* Giving a background-color to the nav container. */

nav {
  margin: 0;
  padding: 1em;
  background-color: #FFF;
}

#logo {
  display: block;
  padding: 0 30px;
  float: left;
  font-size: 40px;
  font-weight: 700;
  line-height: 60px;
}


/* Since we'll have the "ul li" "float:left"
* we need to add a clear after the container. */

nav:after {
  content: "";
  display: table;
  clear: both;
}


/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */

nav ul {
  float: right;
  padding: 0;
  margin: 0;
  list-style: none;
  position: relative;
  background-color: #34cc99;
}


/* Positioning the navigation items inline */

nav ul li {
  margin: 0px;
  display: inline-block;
  float: left;
  background-color: #FFF;
}


/* Styling the links */

nav a {
  display: inline-block;
  padding: 14px 20px;
  color: #fff;
  font-size: 20px;
  text-decoration: none;
  white-space: nowrap;
  text-align: center;
}

nav ul li ul li:hover,
nav ul li:hover {
  background: #34cc99;
  color: #FFF;
  width: max-content;
  padding: 0%;
  text-align: center;
}

nav:hover {
  border-bottom: #34cc99 5px solid;
}


/* Background color change on Hover */

nav a:hover {
  background-color: #019966;
  color: #ffffff;
  text-shadow: 1px 1px lightgray;
  width: auto;
}


/* Hide Dropdowns by Default
* and giving it a position of absolute */

nav ul ul {
  display: none;
  position: absolute;
  /* has to be the same number as the "line-height" of "nav a" */
  top: 60px;
}


/* Display Dropdowns on Hover */

nav ul li:hover>ul {
  display: inherit;
  background-color: #019966;
  color: #FFF;
  text-align: center;
  width: max-content;
}


/* Fisrt Tier Dropdown */

nav ul ul li {
  width: auto;
  float: none;
  display: list-item;
  position: relative;
  background-color: #34cc99;
  color: #FFF;
}


/* Second, Third and more Tiers 
* We move the 2nd and 3rd etc tier dropdowns to the left
* by the amount of the width of the first tier.
*/

nav ul ul ul li {
  position: relative;
  top: -60px;
  /* has to be the same number as the "width" of "nav ul ul li" */
  left: 170px;
}


/* Change ' +' in order to change the Dropdown symbol */

li>a:after {
  content: ' ';
}

li>a:only-child:after {
  content: '';
}


/* Media Queries
--------------------------------------------- */

@media all and (max-width: 768px) {
  #logo {
    display: block;
    padding: 0;
    width: 100%;
    text-align: center;
    float: none;
  }
  nav {
    margin: 0;
  }
  /* Hide the navigation menu by default */
  /* Also hide the  */
  .toggle+a,
  .menu {
    display: none;
  }
  /* Styling the toggle lable */
  .toggle {
    display: block;
    background-color: #254441;
    padding: 14px 20px;
    color: #FFF;
    font-size: 17px;
    text-decoration: none;
    border: none;
  }
  .toggle:hover {
    background-color: #000000;
  }
  /* Display Dropdown when clicked on Parent Lable */
  [id^=drop]:checked+ul {
    display: block;
  }
  /* Change menu item's width to 100% */
  nav ul li {
    display: block;
    width: 100%;
  }
  nav ul ul .toggle,
  nav ul ul a {
    padding: 0 40px;
  }
  nav ul ul ul a {
    padding: 0 80px;
  }
  nav a:hover,
  nav ul ul ul a {
    background-color: #000000;
    color: #FFF;
  }
  nav ul li ul li .toggle,
  nav ul ul a,
  nav ul ul ul a {
    padding: 14px 20px;
    color: #FFF;
    font-size: 12px;
  }
  nav ul li ul li .toggle,
  nav ul ul a {
    background-color: #212121;
  }
  /* Hide Dropdowns by Default */
  nav ul ul {
    float: none;
    position: static;
    color: #ffffff;
    /* has to be the same number as the "line-height" of "nav a" */
  }
  /* Hide menus on hover */
  nav ul ul li:hover>ul,
  nav ul li:hover>ul {
    display: none;
  }
  /* Fisrt Tier Dropdown */
  nav ul ul li {
    display: block;
    width: 100%;
  }
  nav ul ul ul li {
    position: static;
    /* has to be the same number as the "width" of "nav ul ul li" */
  }
}

@media all and (max-width: 330px) {
  nav ul li {
    display: block;
    width: 94%;
  }
}
代码语言:javascript
复制
<nav>
  <div id="logo">GETUWIRED</div>

  <label for="drop" class="toggle">Menu</label>
  <input type="checkbox" id="drop" />
  <ul class="menu">
    <li>
      <!-- First Tier Drop Down -->
      <label for="drop-1" class="toggle">WordPress +</label>
      <a href="#" id="title">ABOUT</a>
      <input type="checkbox" id="drop-1" />
      <ul>
        <li><a href="#">OUR STORY</a></li>
        <li><a href="#">OUR COMMITMENT TO YOU</a></li>
      </ul>
    </li>
    <li>

      <!-- First Tier Drop Down -->
      <label for="drop-2" class="toggle">Web Design +</label>
      <a href="#" id="title">LOCATIONS</a>
      <input type="checkbox" id="drop-2" />
      <ul>
        <li><a href="#">AUSTIN, TX</a></li>
        <li><a href="#">ATLANTA, GA</a></li>
        <li><a href="#">SEATTLE, WA</a></li>
        <li><a href="#">PORTLAND, OR</a></li>
      </ul>
    </li>
    <li>
      <!-- First Tier Drop Down -->
      <label for="drop-1" class="toggle">WordPress +</label>
      <a href="#" id="title">PRODUCTS</a>
      <input type="checkbox" id="drop-3" />
      <ul>
        <li><a href="#">NEW ARRIVALS</a></li>
        <li><a href="#">FEATURED</a></li>
        <li><a href="#">TOP RATED</a></li>
        <li><a href="#">HIS</a></li>
        <li><a href="#">HERS</a></li>
        <li><a href="#">KIDS</a></li>
        <li><a href="#">ACCESSORIES</a></li>
        <li><a href="#">SPRING CATALOG</a></li>
        <li><a href="#">THE ESSENTIALS</a></li>
        <li><a href="#">SALE</a>
      </ul>
      </li>
      <li><a href="#" id="title">BLOG</a></li>
      <li><a href="#" id="title">CONTACT</a></li>
  </ul>
</nav>

EN

回答 2

Stack Overflow用户

发布于 2022-09-14 15:52:06

我在这里做了测试,这个成功了:

代码语言:javascript
复制
    /* I modified that part */
    nav ul li ul li:hover,
    nav ul li:hover {
        background: #34cc99;
        color: #FFF;
        padding: 0%;
        text-align: center;
    }
    
    /* I added this part */
    nav ul li ul li {
        width: 100%;
    }

    /* I added this part */
    nav ul li ul li:hover a {
        max-width: 100%;
        display: block;
    }
票数 0
EN

Stack Overflow用户

发布于 2022-09-14 17:24:32

试试这个解决方案

从以下位置删除width: max-content;

代码语言:javascript
复制
nav ul li ul li:hover, nav ul li:hover { 
    background: #34cc99; 
    color: #FFF; 
    /* width: max-content;  */
    padding: 0%;  
    text-align: center;
}

另外,将display: block;添加到:

代码语言:javascript
复制
nav a:hover { 
    display: block;
    background-color: #019966; 
    color: #ffffff;
    text-shadow: 1px 1px lightgray;
    width: auto;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73719410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档