简体   繁体   English

放置float属性后按钮不起作用

[英]Button not working after putting float property

I have a media query where my two buttons will go on opposite sides of the screen, so clearly I had to use the float property on the buttons. 我有一个媒体查询,其中我的两个按钮将位于屏幕的相对两侧,因此很明显,我不得不在按钮上使用float属性。

At first I just put a left float on one button but that didn't centered the two buttons, so I went ahead and put a right float on the other. 起初,我只是在一个按钮上放了一个左浮子,但没有使两个按钮居中,所以我继续在另一个按钮上放了一个右浮子。 After doing so, the buttons didn't work. 这样做之后,按钮不起作用。

I made a fiddle, and everything is in there... could someone help me? 我摆弄了个小提琴,一切都在那里……有人可以帮我吗?

Basically, what I want is that the two buttons are side by side and centered in the media query. 基本上,我想要的是两个按钮并排放置,并在媒体查询中居中。

@media screen and (min-width: 600px) and (max-width: 1199px){
  #mainButtons{
    display: inline;
    button{
      width: 45%;
    }
  }

  #oneButton{
    margin-right: 10px !important;
    .random-button{
      float: left;
    }
  }

 #twoButton{
   .picky-eater-btn{
     float: right;
     margin-top: 0;
   }
 }
}

Fiddle: here 小提琴: 这里

At first glance, I notice that your CSS syntax is invalid. 乍一看,我注意到您的CSS语法无效。 You cannot put element styles inside another element style definition. 您不能将元素样式放入另一个元素样式定义中。

for example, you have button defined inside of #mainButtons . 例如,你有button的内部定义#mainButtons That is invalid. 那是无效的。

Is this basically the result you want? 这基本上是您想要的结果吗?

Fiddle - Fiddle link! 小提琴- 小提琴链接!

HTML 的HTML

<div id="mainButtons">
    <button class="btn btn-hg btn-warning one-button">BUTTON 1</button>
    <button class="btn btn-hg btn-danger two-button">BUTTON 2</button>
</div>

CSS 的CSS

#mainButtons {
    min-width: 200px;
    max-width: 800px;
    margin: 0 auto;
}

#mainButtons button {
    width: 46%;
    float: left;
    margin: 0 2%;
}

.one-button {
    color: #FFFFFF;
    text-align: center;
    border-radius: 10px;
    height: 150px;
    box-shadow: 0px 10px #FF841A;
}
.two-button {
    font-family:'Lato', sans-serif;
    height:150px;
    box-shadow: 0px 10px #D70A00;
}
.btn:active {
    -webkit-transform: translateY(5px);
    -ms-transform: translateY(5px);
    transform: translateY(5px);
}

There were a lot of issues with your code. 您的代码有很多问题。 I will try to explain all of them to you, but first, here is a working fiddle: 我将尝试向您解释所有这些内容,但首先,这是一个有效的小提琴:

JSFiddle JSFiddle


1. You cannot have nested CSS elements: 1.您不能嵌套CSS元素:

This is invalid: 这是无效的:

#buttons{
    button{
        width: 100%;
    }
}

Instead, use concatenation: 而是使用串联:

#buttons button{
    width: 100%;
}

2. Floating width:100% elements is essentially useless in your case 2.浮动width:100%元素在您的情况下基本上没有用

You were attempting to float #oneButton and #twoButton , which is correct, but as divs, which are block elements, they innately have 100% width, so setting the width of the buttons inside them to 45% will still make them appear on different lines. 您试图浮动#oneButton#twoButton ,这是正确的,但作为div元素(它们是块元素),它们本来就具有100%的宽度,因此将它们内部的按钮宽度设置为45%仍会使它们在不同的位置上显示线。

#oneButton, #twoButton
{
    width: 45%;
}

.one-button, .two-button
{
    width: 100%
}

3. Apply margins to the parent divs, not the buttons 3.将边距应用于父div,而不是按钮

Since you want the floated divs to appear on the same line, you should apply all CSS affecting margin, width, padding, etc. to the parent, not the child. 由于您希望浮动的div显示在同一行上,因此应将所有影响边距,宽度,填充等的CSS应用于父级,而不是子级。


Hopefully this works for you. 希望这对您有用。 Remember that clean CSS is good CSS and it makes it easier for us to help you. 请记住,干净的CSS是好的CSS,它使我们更容易为您提供帮助。

Once again, here is the fiddle. 再次,这是小提琴。

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

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