简体   繁体   English

已复制 css 样式在两个元素上的行为不同?

[英]Copied css styling behaves differently on two elements?

I'm still new to HTML & CSS and can't figure this one out.我还是 HTML 和 CSS 的新手,无法弄清楚这个。 I have two <ul> lists with <li> elements.我有两个包含<li>元素的<ul>列表。 I created a button out of one of the li elements in the first ul list, and then copied the html and css but on hover, they behave differently.我用第一个 ul 列表中的一个 li 元素创建了一个按钮,然后复制了 html 和 css,但在 hover 上,它们的行为不同。 I've split them into two classes to try identify the problem but I don't know how to fix it.我已将它们分成两类以尝试找出问题所在,但我不知道如何解决。

Button 1 (in nav) - the whole button acts as the element and changes colour on hover按钮 1(在导航中)- 整个按钮充当元素并在 hover 上改变颜色

<nav>
        <ul>
            <li><img src="Women in Tech-logos_transparent.png" width="150px"><a href="Homepage.html" class="Logo"></a></li>
            <li><a href="Homepage.html">Home</a></li>
            <li><a href="">About Us</a></li>
            <li class="SignUp"><a href="">Sign Up</a></li>
        </ul>
      </nav>

Button 2 - only the text inside the button acts as the element and only the text background colour changes on hover.按钮 2 - 只有按钮内的文本充当元素,并且只有文本背景颜色在 hover 上发生变化。

<div class="IntroCTA">
        <ul>
            <li class="SecondaryButton"><a href="">Learn More</a></li>
            <li class="SignUp2"><a href="">Sign Up</a></li>
        </ul>
      </div>

And the CSS styling I was using on both before splitting into two在分成两部分之前,我在两者上都使用了 CSS 样式

    .SignUp {
  background-color: #F3ED01;
  text-decoration: none;
  text-align: center;
  font-size: 16px;
  border-radius: 50px;
  height: 40px;
  line-height: 40px;
  width: 150px;
  margin-right: 40px;
  margin-left: 20px;
  padding: 0 0 0 0;
}

.SignUp :hover {
  text-decoration: none;
  background-color: white;
  border-radius: 50px;
}

.SignUp a {
  
  text-decoration: none;
}

I've created a jsfiddle of the code.我已经创建了代码的jsfiddle It's probably something really simple and I'm just missing it!这可能真的很简单,我只是想念它!

Really appreciate any input or advice!非常感谢任何意见或建议!

.SignUp2 :hover

You don't need to put space there.你不需要在那里放空间。

.SignUp2:hover

You should use that like this.你应该像这样使用它。

You are styling in your list the "list" and not the "a" tag.您在列表中设置的样式是“列表”而不是“a”标签。 To fix this add a to your selector:要解决此问题,请将 a 添加到您的选择器:

From

li.SignUp2 a

to

li.SignUp2 a

And add display: block;并添加显示:块; or display: inline-block;或显示:内联块;

li.SignUp2 a {
    display: block;


    background-color: #F3ED01;
    text-decoration: none;
    text-align: center;
    font-size: 16px;
    border-radius: 50px;
    height: 40px;
    line-height: 40px;
    width: 150px;
    margin: 10px 10px 10px 10px;
}

(edited) (编辑)

The difference (in yours JS fiddle) is in the li parent elements of the a tags and the second a tag itself: In the first case (nav menu) the li has display: inline-block , in the second case you applied float: left to it, which makes the applied display: block invalid and changes it to block .区别(在你的 JS 小提琴中)在于a标签的li父元素和第二个a标签本身:在第一种情况下(导航菜单) li具有display: inline-block ,在第二种情况下你应用了float: left它,这使得应用的display: block无效并将其更改为block This changes the size of the a tag inside it, and therefore the size of the area that changes background-color on hover.这改变了其中a标签的大小,因此改变了 hover 上背景颜色的区域的大小。

If you set both li s to inline-block , remove the float and add display: block;如果将两个li都设置为inline-block ,请删除浮动并添加display: block; to the a tag inside it, they should behave identical.对于其中的a标签,它们的行为应该相同。

please using the same class for applying same css请使用相同的 class 申请相同的 css

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

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