复制营养标签 - 专家CSS级别

时间:2017-02-07 19:35:05

标签: html css css3

我试图在CSS中模仿这种营养标签格式,但我无法正确获取形状。我能想到的最好的是摆弄边缘半径,但这给了我更多的药丸形状,但仍然无法在底部获得黑色切割形状。有没有人在CSS中复制这样的营养标签?有人愿意尝试吗?任何帮助将不胜感激。

以下是我目前所拥有的链接:jsfiddle.net/f5jczunf /

#block {
        border-radius:50%/10px;
        background: #ccc;
        padding: 20px; 
        width: 50px;
        height: 100px; 
        border: 1px solid #000;
        background-color:#FFF;
        text-align:center;
    }



.number {
          font-weight:bold;
          font-size:18pt;
          text-align:center;
        }

<div id="block">
<span class="number">150</span>
<br/>Calories
</div>

Front of Package Nutrition Label

4 个答案:

答案 0 :(得分:5)

也许这个小例子可以提供帮助。

&#13;
&#13;
.label {
  position: relative;
  width: 100px;
  height: 140px;
  text-align: center;
  border: 1px solid #000;
  border-radius: 100px/50px;
  overflow: hidden;
}

.title {
  display: inline-block;
  margin-top: 30px;
}

.bottom {
  position: absolute;
  bottom: -10px;
  left: 0;
  right: 0;
  height: 50px;
  color: #fff;
  line-height: 40px;
  border-top: 1px solid #000;
  border-radius: 100px/50px;
  background-color: #000;
}
&#13;
<div class="label">
  <span class="title">Title</span>
  <span class="bottom">Bottom</span>
</div>
&#13;
&#13;
&#13;

https://jsfiddle.net/9xs2wcbL/1/

答案 1 :(得分:3)

这是我的看法。但它确实需要一些先进的,前沿的CSS。

$('body').on('click', 'li.dropdown', function(){
  if($(this).hasClass('active')){
    $(this).find('a.dropdown-toggle').attr('aria-expanded', true);
  }
});
@import url('https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300');
body {
  padding: 3em;
  font-size: 16px;
  font-family: 'Open Sans Condensed', sans-serif;
}
.label-list {
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  display: inline-flex;
}
.label-list .label-item {
  text-align: center;
  border: 1px solid;
  position: relative;
  border-radius: 2em / 0.65em;
  padding: 0.2em 0.25em 1.5em;
  min-width: 3.5em;
  overflow: hidden;
  margin: 0.1em;
  z-index: 1;
  background: white;
  color: black;
}
.label-list .label-item h1 {
  font-size: 3em;
  line-height: 1em;
  font-weight: 900;
  margin: 0;
}
.label-list .label-item h1.smaller {
  font-size: 1.75em;
  margin-top: 0.5em;
}
.label-list .label-item h1 small {
  font-size: 0.4em;
  text-transform: none;
}
.label-list .label-item small {
  font-size: 1em;
  line-height: 1em;
  font-weight: 900;
  text-transform: uppercase;
}
.label-list .label-item span {
  position: absolute;
  bottom: 0.5em;
  left: 0;
  right: 0;
  color: white;
  font-size: 0.8em;
  line-height: 1em;
}
.label-list .label-item span:before {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  top: 0;
  background: black;
  z-index: -1;
  border-radius: 40%;
  transform-origin: center;
  width: 100%;
  height: 0;
  padding-top: 100%;
  margin: auto;
  transform: rotate(45deg) translate(20%, 20%);
}

答案 2 :(得分:0)

我相信在纯CSS中使用这种形状的唯一方法是使用一些重叠的形状,类似于下面的代码:

&#13;
&#13;
->
&#13;
.wrapper {
  position: relative;
  height: 112px;
  width: 80px;
  overflow: hidden;
}

.rectangle,
.circle {
  position: absolute;
  box-sizing: border-box;
}

.rectangle {
  height: 96px;
  width: 80px;
  top: 8px;
  border-left: 1px solid black;
  border-right: 1px solid black;
}

.circle {
  width: 200px;
  height: 200px;
  left: -60px;
  border-radius: 200px;
  border: 1px solid black;
}

.top {
  top: 0;
}

.bottom {
  bottom: 0;
}
&#13;
&#13;
&#13;

https://jsfiddle.net/dylanstark/01hck5dv/

答案 3 :(得分:0)

这是我的方法。我使用beforeafter伪元素。 before包含带有border-radius的黑色bg,并且溢出了#block的主overflow: hidden;after包含来自data-text

#block属性的文字

&#13;
&#13;
#block {
  border-radius: 50%/10px;
  background: #ccc;
  padding: 20px;
  width: 50px;
  height: 100px;
  border: 1px solid #000;
  background-color: #FFF;
  text-align: center;
  position: relative;
  overflow: hidden;
}
#block:before {
  display: block;
  content: " ";
  position: absolute;
  bottom: -15px;
  left: 0;
  width: 100%;
  height: 50px;
  border-radius: 50%;
  background: black;
  z-index: 0;
}
#block:after {
  display: block;
  content: attr(data-label);
  position: absolute;
  bottom: 5px;
  color: white;
  text-align: center;
  z-index: 1;
}
.number {
  font-weight: bold;
  font-size: 18pt;
  text-align: center;
}
&#13;
<div id="block" data-label="5% DY">
  <span class="number">150</span>
  <br/>Calories
</div>
&#13;
&#13;
&#13;