简体   繁体   English

HTML / CSS:获取链接样式类似于按钮的链接

[英]HTML/CSS: Getting links styled like buttons to stack

I'm currently working on a simple project in HTML/CSS (Bootstrap). 我目前正在使用HTML / CSS(引导程序)进行一个简单的项目。 Very simply, I have the task of styling links to look like buttons, and getting them to stack in a mobile view. 很简单,我的任务是设计链接的样式以使其看起来像按钮,并使它们堆叠在移动视图中。

Requirements: buttons should be side-by-side on regular/desktop view, and should stack on top of each other in iPhone/Android devices. 要求:按钮应在常规/桌面视图上并排放置,并且在iPhone / Android设备中应堆叠在一起。

CodePen: https://codepen.io/anfperez/pen/joqoXG CodePen: https ://codepen.io/anfperez/pen/joqoXG

Here's the code I have so far: 这是我到目前为止的代码:

html html

<a class="button-a" href="www.google">Link that looks like button</a>
<a class="button-b" href="www.amazon.com">Another link that looks like a button</a>

css (Bootstrap can be included) CSS(可以包含引导程序)

.button-a, .button-b {
  background-color: blue;
  color: white;
  text-decoration: none;
  padding: 10px;
}

.button-a {
  background-color: blue
}

.button-b {
  background-color: red;
}

When I try viewing the code in a mobile view, the red button button ends up overlapping on top of the blue button since they're both links. 当我尝试在移动视图中查看代码时,红色按钮按钮最终重叠在蓝色按钮上,因为它们都是链接。 How can I get the red button to clear the blue button? 如何获得红色按钮以清除蓝色按钮? I can't use Bootstrap's btn-group in this case. 在这种情况下,我无法使用Bootstrap的btn-group。

You can use Flexbox inside a media query to stack the elements using flex-direction: column . 您可以在媒体查询中使用Flexbox使用flex-direction: column来堆叠元素。

 .button-a, .button-b { background-color: blue; color: white; text-decoration: none; padding: 10px; } .button-a { background-color: blue } .button-b { background-color: red; } @media (max-width: 767px) { .wrapper { display: flex; flex-direction: column; align-items: flex-start; } } 
 <div class="wrapper"> <a class="button-a" href="www.google">Link that looks like button</a> <a class="button-b" href="www.amazon.com">Another link that looks like a button</a> </div> 

....really? ....真? you can making a have behaviour of div, if you want them to stack, just make the button class having inline-block 您可以使div具有行为,如果要它们堆叠,只需使按钮类具有inline-block

.button-a, .button-b {
  background-color: blue;
  color: white;
  text-decoration: none;
  padding: 10px;
  margin: 20px;
  display:inline-block;
}

Edited Codepen This make the a link has the margin and padding behaviour like div. Edited Codepen这使链接具有诸如div之类的边距和填充行为。 You just have to setting the width.. So if the media canvas shorter, the link will automatically stacking 您只需要设置宽度即可。因此,如果媒体画布较短,则链接将自动堆叠

.button-a, .button-b {
  background-color: blue;
  color: white;
  text-decoration: none;
  padding: 10px;
  margin: 20px;
  display:block;
}

.button-a {
  background-color: blue
}

.button-b {
  background-color: red;
}

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

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