简体   繁体   English

如何使这些按钮响应?

[英]How do I make these buttons responsive?

I have an HTMl and a CSS instance. 我有一个HTMl和一个CSS实例。

I'm trying to make these buttons get smaller based on the screen size and to correlate into a column. 我试图使这些按钮根据屏幕尺寸变小,并关联到一列中。 Right now when I change the window size the buttons just disappear. 现在,当我更改窗口大小时,按钮消失了。

 /* grid */ .col-1of1 { width: 100%; margin-left: auto; margin-right: auto; } .col-1of2 { width: 50%; float: left; } .col-1of3 { width: 33.33%; float: left; } .col-1of4 { width:25%; float: left; } .col-1of5 { width: 20%; float: left; } #p-button { font-size: 1.5vmin; color: white; background: #F0AB00; position: relative; width: 200px; min-width:20px; height: 50px; border-radius: 6px; margin-top: 180px; margin-right: 25px; margin-left: 25px; line-height: 50px; text-align: center; -webkit-transition-duration: 0.3s; /* Safari */ transition-duration: 0.3s; } #p-button: hover { background: #970A82; } 
 <p id="player-text">Please select the number of players</p> <div class="col-1of1" id="options"> <a href="form1.html?player=1&form=1"><p class="col-1of4" id="p- button">1 Player</p></a> <!--button--> <a href="form1.html?player=2&form=1"><p class="col-1of4" id="p- button">2 Players</p></a> <!--button--> <a href="form1.html?player=3&form=1"><p class="col-1of4" id="p- button">3 Players</p></a> <!--button--> <a href="form1.html?player=4&form=1"><p class="col-1of4" id="p- button">4 Players</p></a> <!--button--> </div> 

You can use Media Queries. 您可以使用媒体查询。 For Example. 例如。

@media(max-width: 767px){
    #p-button{
        font-size: 1vmin;
        width: 100px;
    }
}

For starters, you have all your p elements with the same id and the only reason you are using that id is for CSS styling. 对于初学者来说,所有p元素都具有相同的id并且使用该id的唯一原因是CSS样式。 id s must be unique. id必须是唯一的。 p-button should be a class, not an id . p-button应该是一个类,而不是id

Next, you have your column class set to be responsive because you are using a percentage for the width (that's good), but then you are hard coding the p elements to a width of 200px , which is overriding that percentage because you had the p elements using an id based selector (the most specific type of selector). 接下来,您将列类设置为响应式的,因为您正在使用百分比的width (这很好),但是随后您将p元素硬编码为200pxwidth ,因为您拥有p ,所以将覆盖该百分比元素使用基于id的选择器(最具体的选择器类型)。 Changing to classes and removing the static 200px width allows the buttons to grow/shrink. 更改为类并删除静态的200px宽度可使按钮增大/缩小。

After that, using min-width and max-width is an essential part of responsive design because people may have tiny or huge viewports and you need to set reasonable limits. 此后,使用min-width max-width是响应式设计的重要组成部分,因为人们的视口可能很小或很大,并且您需要设置合理的限制。

Also, (while technically valid), it's better to put a elements inside of the p elements for better semantics. 另外,(虽然在技术上是有效的),最好是把a元素的内部p更好的语义元素。

Here's a cleaned up version. 这是一个清理的版本。 Run the code snippet and then click the "full page" link at the top, right of the window to expand it and you will see the buttons get bigger and go from a grid layout to a row layout. 运行代码片段,然后单击窗口顶部顶部的“整页”链接以将其展开,您将看到按钮变大,并从网格布局变为行布局。 Then, click the "close" link at the very top, right of that window and you'll see the buttons shrink and change their layout again. 然后,单击该窗口右侧最顶部的“关闭”链接,您将看到按钮缩小并再次更改其布局。

 /* grid */ .col-1of1 { width: 100%; margin-left: auto; margin-right: auto; } .col-1of2 { width: 50%; float: left; } .col-1of3 { width: 33.33%; float: left; } .col-1of4 { width:25%; float: left; } .col-1of5 { width: 20%; float: left; } .p-button { font-size: 1.25vw; background: #F0AB00; position: relative; min-width:75px; max-width:125px; height: 50px; border-radius: 6px; margin: 2em 2em 0 2em; line-height: 50px; text-align: center; transition-duration: 0.3s; } .p-button a { color: white; } .p-button:hover { background: #970A82; } 
 <p id="player-text">Please select the number of players</p> <div class="col-1of1" id="options"> <p class="col-1of4 p-button"> <a href="form1.html?player=1&form=1">1 Player</a> </p> <p class="col-1of4 p-button"> <a href="form1.html?player=2&form=1">2 Players</a> </p> <p class="col-1of4 p-button"> <a href="form1.html?player=3&form=1">3 Players</a> </p> <p class="col-1of4 p-button"> <a href="form1.html?player=4&form=1">4 Players</a> </p> </div> 

CSS media queries is a great feature to do it. CSS媒体查询是一个很棒的功能。 You can do it by media queries 您可以通过媒体查询来做到这一点

For example: 例如:

@media(max-width: 767px){
  .col-1of2,.col-1of3,.col-1of4,.col-1of5 {
    width: 100%;
    float: none;
  }
  #p-button{
    font-size: 1vmin;
    width: 100px;
  }
}

NB: I noticed that you use the same ID much time.Remember ID is a unique identifier. 注意:我注意到您经常使用相同的ID。记住ID是唯一的标识符。 if you need to give the same style more than one element you have to use Class. 如果您需要给同一样式多个元素,则必须使用Class。

You have a paragraph inside the hyperlink and the hyperlinks do not have a float:left. 您在超链接内有一个段落,并且超链接没有float:left。 Make the following changes: 进行以下更改:

.col-1of4 {
  width: 100%;
  float: left;
  white-space: nowrap;
}
a {
  float: left;
  width: 25%;
  text-align: center;
}

First of all is that you have a lot of redundant code. 首先是您有很多冗余代码。 I made it work with only one id="options" All you needed was reffering to the width of buttons in % . 我仅用一个id="options"使其起作用。您所需要的只是恢复%的按钮宽度。 I also included a cool calc() function which allows using both percentages and pixels when reffering to size of an element 我还包括一个很酷的calc()函数,该函数允许在引用元素大小时同时使用百分比和像素

 #options { width: 100%; } #options a { display: block; float: left; color: white; background: #F0AB00; position: relative; width: calc( 25% - 30px ); margin: 15px; border-radius: 6px; line-height: 50px; font-size: 1.5vmin; text-align: center; -webkit-transition-duration: 0.3s; /* Safari */ transition-duration: 0.3s; } 
 <p id="player-text">Please select the number of players</p> <div class="col-1of1" id="options"> <a href="#"> 1 Player </a> <!--button--> <a href="#"> 2 Players </a> <!--button--> <a href="#"> 3 Players </a> <!--button--> <a href="#"> 4 Players </a> <!--button--> </div> 

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

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