简体   繁体   English

如何使用CSS实现复杂的按钮样式

[英]How to implement complicated button style using CSS

I faced a little problem. 我遇到了一个小问题。 I'm implementing the web-site design and need to implement a bit complicated button style (attached here). 我正在实现网站设计,需要实现一些复杂的按钮样式(附在此处)。 I have no idea how to make that using only CSS. 我不知道如何只使用CSS。

在此输入图像描述

It's gonna be a few button styles and one of its must be with transparent background and must get background color from element where this button is placed. 这将是一些按钮样式,其中一个必须具有透明背景,并且必须从放置此按钮的元素获得背景颜色。 I implemented button with custom background that can be set using CSS. 我实现了可以使用CSS设置的自定义背景按钮。 But it's not flexibly to use. 但它不灵活使用。 If I want to use it on the different backgrounds you should add new style like "btn-customColor" etc. Now I have styles for transparent background and it looks: 如果我想在不同的背景上使用它,你应该添加新的样式,如“btn-customColor”等。现在我有透明背景的样式,它看起来:

在此输入图像描述

The point is that I can't cut or hide the part of bottom block under top block with transparent background. 关键是我无法在透明背景的顶部块下切割或隐藏底部块的部分。 I can set color and it will be like first image. 我可以设置颜色,它就像第一张图片。 But it restricts usages of buttons. 但它限制了按钮的使用。 I could use btn-transparent instead btn-blue, btn-green, btn-white, etc. 我可以用btn-transparent代替btn-blue,btn-green,btn-white等。

I had another idea to draw buttons in photoshop, but it's not really good approach and there are a lot of "no" here. 我有另一个想法在Photoshop中绘制按钮,但这不是很好的方法,这里有很多“不”。 Maybe is it possible to implement using canvases or smth. 也许可以使用画布或smth来实现。 like that? 像那样? If it's, would be great if you shared a few links for articles and so on. 如果是的话,如果您共享一些文章链接等等,那就太好了。

There are staff that is available to use: HTML, CSS, JS (jQuery). 有工作人员可以使用:HTML,CSS,JS(jQuery)。

I hope I explained what the problem is. 我希望我解释了问题所在。 Any ideas and help is appreciated. 任何想法和帮助表示赞赏。

Thank you for your time. 感谢您的时间。

 .btn-base { padding: 0; margin: 0; height: 30px; outline: none; border-radius: 3px; background: transparent; border: 2px solid #fff; font-size: 12px; transition: 0.5s; } .btn-base>div { position: relative; width: 101%; left: 3px; bottom: 8px; padding: 5px 15px; outline: none; border-radius: 3px; background: #e4645d; /* hardcoded code, must be transparent */ border: 2px solid #fff; font-size: 12px; } 
 <button type="submit" class="btn-base btn-transparent"> <div>Button example</div> </button> 

Consider using pseudo-elements as an alternative solution. 考虑使用伪元素作为替代解决方案。

This method is demonstrated against x3 varying background colours in the code snippet embedded below. 此方法在下面嵌入的代码段中针对x3不同的背景颜色进行了演示。

Code Snippet Demonstration: 代码片段演示:

 .row { padding: 20px; } .row:nth-child(1) { background: #e4645d; } .row:nth-child(2) { background: #5dace4; } .row:nth-child(3) { background: #5fe45d; } .btn-base { padding: 0; margin: 0; height: 30px; outline: none; border-radius: 3px; background: transparent; border: 2px solid #fff; font-size: 12px; transition: 0.5s; /* added */ position: relative; /* required for absolutely positioned pseudo-elements */ padding: 0px 10px; } /* Additional */ .btn-base:before { content: ""; position: absolute; width: 7px; height: 30px; border: 2px solid #fff; border-right: 0; right: 100%; bottom: -5px; top: 5px; border-top-left-radius: 3px; border-bottom-left-radius: 3px; box-sizing: border-box; } .btn-base:after { content: ""; position: absolute; border: 2px solid #fff; height: 9px; border-top: 0; border-left: 0; right: 5px; left: 0; bottom: -9px; border-bottom-right-radius: 3px; box-sizing: border-box; } 
 <div class="row"> <button type="submit" class="btn-base btn-transparent">Button example</button> </div> <div class="row"> <button type="submit" class="btn-base btn-transparent">Button example</button> </div> <div class="row"> <button type="submit" class="btn-base btn-transparent">Button example</button> </div> 

You could do that with only, box shadow property 你只能使用box shadow属性来做到这一点

 body{ background:#e4645d; text-align:center; } .btn-base { padding: 10px; margin: 0; outline: none; border-radius: 3px; background: transparent; border: 2px solid #fff; font-size: 12px; transition: 0.5s color:white; box-shadow: -10px 10px 0 -2px #e4645d, -10px 10px 0 0px white; } 
 <button type="submit" class="btn-base btn-transparent"> Button example </button> 

Here's how I'd do it. 这是我怎么做的。 Please note I changed the markup, making them siblings of a common wrapper. 请注意我更改了标记,使它们成为常见包装的兄弟姐妹。 I used background-color: inerhit . 我使用了background-color: inerhit Proof of concept: 概念证明:

 .btn-base { padding: 0; margin: 0; height: 30px; outline: none; border-radius: 3px; background: transparent; border: 2px solid #fff; font-size: 12px; transition: 0.5s; color: white; position: absolute; width: 100%; } .button-holder>div { color: white; position: relative; width: calc(100% + 2px); left: 5px; bottom: 8px; padding: 5px 15px; outline: none; border-radius: 3px; background-color: inherit; border: 2px solid #fff; font-size: 12px; box-sizing: border-box; } body { background-color: teal; } .button-holder { background-color: inherit; display: inline-block; position: relative; } 
 <div class="button-holder"> <button type="submit" class="btn-base btn-transparent"></button> <div>Button example</div> </div> 

For modern browsers ( except IE/Edge ) you can use the clip-path css property and create a polygon to clip the element on the back to only show the parts you want. 对于现代浏览器( IE / Edge除外 ),您可以使用clip-path css属性并创建一个多边形来剪切背面的元素,以仅显示所需的部分。

This will make it truly transparent, in the sense that it can appear even over images. 这将使其真正透明,即使它甚至可以出现在图像上。

 body{background:url('https://placeimg.com/640/480/animals') no-repeat;} .btn-base { margin: 0; height: 30px; outline: none; border-radius: 3px; border: 2px solid currentColor; font-size: 12px; transition: 0.5s; padding: 5px 15px; font-size: 12px; position:relative; background:transparent; color:#fff; cursor:pointer; } .btn-base:before { content:''; position: absolute; width: 100%; height:100%; left: -7px; bottom: -10px; border-radius: 3px; /* hardcoded code, must be transparent */ border: 2px solid currentColor; cursor:pointer; -webkit-clip-path: polygon(0 0, 5% 0, 5% 88%, 100% 88%, 100% 100%, 0 100%); clip-path: polygon(0 0, 5% 0, 5% 70%, 100% 70%, 100% 100%, 0 100%); } 
 <button type="submit" class="btn-base"> Button example </button> 

You can imitate borders using box-shadow to create a multi-border look. 您可以使用box-shadow模仿边框以创建多边框外观。

Example: 例:

 button { border: 0; background: #666; box-shadow: -8px 8px 0 -4px #FFF, -8px 8px 0 0px #666; } 
 <button>Sample</button> 

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

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