div css上的右角色带

时间:2015-05-28 10:26:11

标签: css css-shapes

嗨我正在尝试在div中制作一个角落色带,它到处都是我希望它看起来整洁而且很好看它越过div并且不会很好看

/* The ribbons */

.corner-ribbon {
  width: 100px;
  background: #e43;
  position: absolute;
  top: 25px;
  left: -50px;
  text-align: center;
  line-height: 50px;
  letter-spacing: 1px;
  color: #f0f0f0;
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  overflow: hidden;
}
.corner-ribbon.shadow {
  box-shadow: 0 0 3px rgba(0, 0, 0, .3);
}
/* Different positions */

.corner-ribbon.top-right {
  /* top: 18px; */
  right: -4px;
  left: auto;
  transform: rotate(45deg);
  -webkit-transform: rotate(46deg);
  overflow: hidden;
}
.corner-ribbon.blue {
  background: #39d;
}
<div class="large-4 columns">

  <div class="corner-ribbon top-right sticky blue">Hello</div>
</div>

有人能告诉我如何在右上方放置一个角落色带,看起来聪明又漂亮,可以处理大约3个单词。

4 个答案:

答案 0 :(得分:5)

修改:我一直看到人们赞同我的快速且不灵活的建议 - 但如果您不知道功能区的内容(长度/大小),请务必查看{{ 3}}!他更容易调整,因为功能区&#34;调整&#34;,取决于功能区内的文字长度。 我会为min-width / :before伪元素建议:after,因为如果内容只是&#34; new&#34;,则只会得到非常短的块示例

你真的只是在寻找更好的定位吗?

使色带更长并移动它,使其整齐地定位在角落,确保您给出CONTAINER元素overflow: hidden;

Paulie_D's solution

&#13;
&#13;
.corner-ribbon {
  width: 250px;
  background: #e43;
  position: absolute;
  top: 25px;
  left: -50px;
  text-align: center;
  line-height: 50px;
  letter-spacing: 1px;
  color: #f0f0f0;
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  overflow: hidden;
}
.corner-ribbon.shadow {
  box-shadow: 0 0 3px rgba(0, 0, 0, .3);
}
/* Different positions */

.corner-ribbon.top-right {
  /* top: 18px; */
    top:30px;
  right: -70px;
  left: auto;
  transform: rotate(45deg);
  -webkit-transform: rotate(46deg);
  overflow: hidden;
}
.corner-ribbon.blue {
  background: #39d;
}

.large-4, html{
    height:100%;
    overflow:hidden;
    width:100%;
}

body{
    margin:0;
    padding:0;
    position:relative;
    width:100%;
}
&#13;
<div class="large-4 columns">

  <div class="corner-ribbon top-right sticky blue shadow">Hello</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:4)

目前尚不清楚这应该是什么样的但是如果这只是一个角度,在div / body顶部45度处则此选项为一个(到目前为止)不需要特别调整。

我会根据字体大小/填充等的变化自动更改“位置”。

Codepen Demo

.parent {
  overflow: hidden; /* required */
  width: 50%; /* for demo only */
  height: 250px /* some non-zero number */;
  margin: 25px auto; /* for demo only */
  border:1px solid grey; /* for demo only */
  position: relative; /* required  for demo*/
}

.ribbon {
  margin: 0;
  padding: 0;
  background: rebeccapurple;
  color:white;
  padding:1em 0;
  position: absolute;
  top:0;
  right:0;
  transform: translateX(30%) translateY(0%) rotate(45deg);
  transform-origin: top left;
}
.ribbon:before,
.ribbon:after {
  content: '';
  position: absolute;
  top:0;
  margin: 0 -1px; /* tweak */
  width: 100%;
  height: 100%;
  background: rebeccapurple;
}
.ribbon:before {
  right:100%;
}

.ribbon:after {
  left:100%;
}
<div class="parent">
<h4 class="ribbon">Special Sale Today</h4>
  </div>

答案 2 :(得分:-1)

试试这个Demo

.corner - ribbon {
    width: 100 px;
    background: #e43;
    position: absolute;
    text - align: center;
    line - height: 50 px;
    letter - spacing: 1 px;
    color: #f0f0f0;
    transform: rotate(45 deg); - webkit - transform: rotate(45 deg);
    overflow: hidden;
    right: 0;
    top: 1.7e m;
}

.corner - ribbon.shadow {
    box - shadow: 0 0 3 px rgba(0, 0, 0, .3);
}

/* Different positions */

.corner - ribbon.blue {
        background: #39d;}
.large-4{
    position: relative;    
}

答案 3 :(得分:-1)

你可以尝试这段代码, 在HTML中,

<div class="large-4 columns top-right">
  <div class="corner-ribbon sticky blue">Hello</div>
</div>

在CSS中,

.columns {
 font-size: 16px !important;
 /* This ribbon is based on a 16px font side and a 24px vertical rhythm. I've used em's to position each element for scalability. If you want to use a different font size you may have to play with the position of the ribbon elements */

 width: 50%;

 position: relative;
 background: #ba89b6;
 color: #fff;
 text-align: center;
 padding: 1em 2em; /* Adjust to suit */
 margin: 2em auto 3em; /* Based on 24px vertical rhythm. 48px bottom margin - normally 24 but the ribbon 'graphics' take up 24px themselves so we double it. */
}
.columns:before, .columns:after {
 content: "";
 position: absolute;
 display: block;
 bottom: -1em;
 border: 1.5em solid #986794;
 z-index: -1;
}
.columns:before {
 left: -2em;
 border-right-width: 1.5em;
 border-left-color: transparent;
}
.columns:after {
 right: -2em;
 border-left-width: 1.5em;
 border-right-color: transparent;
}
.columns .corner-ribbon:before, .columns .corner-ribbon:after {
 content: "";
 position: absolute;
 display: block;
 border-style: solid;
 border-color: #804f7c transparent transparent transparent;
 bottom: -1em;
}
.columns .corner-ribbon:before {
 left: 0;
 border-width: 1em 0 0 1em;
}
.columns .corner-ribbon:after {
 right: 0;
 border-width: 1em 1em 0 0;
}
.columns.top-right {
  top: 75px;
  right: -75px;
  left: auto;
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  /* overflow: hidden; */
  z-index: 10;
}

作为参考,请阅读这篇精彩的文章https://css-tricks.com/snippets/css/ribbon/