是否可以用带边框的透明容器创建箭头?

时间:2015-11-27 08:39:20

标签: javascript html css

是否可以创建这样的东西:

enter image description here

因为现在我有这样的事情: https://jsfiddle.net/38kjb5us/

<div class="dropdown-content"></div>


body{
  background-image: url('//ssl.gstatic.com/gb/images/v1_76783e20.png');
}
.dropdown-content{
 display: inline-block;
 padding: 18px 14px;
 border: 1px solid #ffffff;
 min-width: 160px;
 position: relative;
 box-shadow: 0 0 10px #000000;
 margin: 15px;
}
.dropdown-content:before{
    position: absolute;
    content: "";
    border: 1px solid #ffffff;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 12px 12px 12px;
    border-color: transparent transparent #ffffff transparent;
    top: -12px;
 }

我不知道如何将带边框的箭头添加到透明容器中((

有可能吗?如果是,那怎么办?

13 个答案:

答案 0 :(得分:10)

仅使用DIV和Css检查此解决方案

下面的解决方案可以更改箭头容器的宽度和高度和位置

https://plnkr.co/edit/nXRNr7bbdMSaxBKa6h4J?p=preview

和此codepen添加阴影 http://codepen.io/tawfiqin/pen/JKYyMK

<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <h1>Hello Plunker!</h1>
    <div class="image-cont">
        <div class="arrow-container">
          <div class="arrow"></div>
          <div class="top-border"></div>
          <div class="bottom-section"></div>
          <div><h2 style="margin-left:10px;">your content</h2></div>
        </div>

         <div class="arrow-container cont2">
          <div class="arrow"></div>
          <div class="top-border"></div>
          <div class="bottom-section"></div>
          <div class="arrow-content">
            <h2 style="margin-left:10px;">your content</h2>
            <p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
          </div>
        </div>

        <div class="arrow-container cont3">
          <div class="arrow"></div>
          <div class="top-border"></div>
          <div class="bottom-section"></div>
          <div class="arrow-content">
            <h2 style="margin-left:10px;">Dynamic Width</h2>
            <p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
          </div>
        </div>
    </div>

  </body>

</html>

和css代码

/* Styles go here */

.image-cont{
  height:400px;
  background:red;
  background:url('https://mir-s3-cdn-cf.behance.net/project_modules/hd/94430a33978280.56bf480f1ad36.jpg');
}

.arrow-container{
  position:absolute;
  width:360px;
  height:170px;
  background:rgba(255,255,255,0.0);
  top:140px;
  left:160px;
  color:white;
}

.arrow-content{
  height:100%;
  overflow:auto;
  background:rgba(255,255,255,0.0);
}
.arrow{
  position:absolute;
  width:30px;
  height:30px;
  border-left:1px solid white;
  border-top:1px solid white;
  transform:rotate(45deg);
  top:-15px;
  left:40px;
}

.top-border{
  position:absolute;
  width:calc(100% + 2px) ;
  height:1px;
  /*Thanks to Ahmad Shadeed @shadeed user at codepen*/
  background: linear-gradient(to right, #fff 34px, transparent 0, transparent 76px, #fff 0, #fff 100%);

}
.bottom-section{
  width:100%;
  border:1px solid white;
  border-top:none;
  height:100%;
  top:1px;
  position:absolute;
  pointer-events:none;
}

.cont2{
  width:200px;
  height:100px;
  top:350px;

}

.cont3{
  width:auto;
  max-width:300px;
  height:100px;
  top:350px;
  left:400px;

}

你也可以使用SVG

答案 1 :(得分:5)

这是一个超简单的纯CSS解决方案,只有一个div。使用伪元素:before:after创建箭头。

body {
  background: url('https://farm8.staticflickr.com/7187/6895047173_d4b1a0d798.jpg') top center no-repeat;
  background-size: cover;
  padding: 50px;
  overflow: visible;
}
.box {
  padding: 15px;
  color: red;
  position: relative;
  width: 300px;
  height:auto;
  max-width: 100%;
  margin: 0 auto 30px auto;
  background-color: transparent;
  border: 1px solid #FFFFFF;
  border-top: none;
  overflow: visible;
}

.box.second {
  width: 100%;
}

.box:before, .box:after {
  content:'';
  position: absolute;
  top: -10px;
  background-color: inherit;
  padding-top: 10px;
}
.box:before {
  width: calc(30% - 12px);;
  left: 0px;
  -ms-transform-origin: 100% 100%;
  -webkit-transform-origin: 100% 100%;
  transform-origin: 100% 100%;
  -ms-transform: skewX(-45deg);
  -webkit-transform: skewX(-45deg);
  transform: skewX(-45deg);
  border-bottom: 1px solid #FFFFFF;
  border-right: 3px solid #FFFFFF;
}
.box:after {
  width: calc(70% - 12px);;
  right: 0px;
  -ms-transform-origin: 0 100%;
  -webkit-transform-origin: 0 100%;
  transform-origin: 0 100%;
  -ms-transform: skewX(45deg);
  -webkit-transform: skewX(45deg);
  transform: skewX(45deg);
  border-bottom: 1px solid #FFFFFF;
  border-left: 3px solid #FFFFFF;
}
<div class="box">
    This is a transparent box (fixed width) with an transparent arrow (with borders)
</div>

<div class="box second">
    This is a transparent (responsive width) box with an transparent arrow (with borders)
</div>

请随时根据您的需求改进和编辑快速演示。

答案 2 :(得分:5)

另一种可能性是使用:beforeborderbackgroundlinear-gradient或一个像素图片)。

p {
  margin: 40px 5% auto;
  padding: 1em;
  border: solid;
  color: white;
  border-top: none;
  position: relative;
  background: linear-gradient(white, white) top left no-repeat, linear-gradient(white, white) 100px 0 no-repeat;
  background-size: 50px 3px, 100% 3px;
  /* background-position and background size are used to set length or position to leave a gap */
}
p:before {
  /* the arrow*/
  content: '';
  position: absolute;
  height: 35px;
  width: 35px;
  border-top: solid;
  border-right: solid;
  top: 0;
  left: 48px;
  transform: rotate(-45deg);
  transform-origin: 0 0;
}
p+p {
  width: 20%;
  float:left;
}
p+p+p {
  width: 50%;
  border-radius: 5px;
}
p+p+p:before {
  border-radius: 0 5px;
}
html {
  height: 100%;
  background: url(http://lorempixel.com/640/480/nature/6) fixed;
  background-size: cover
}
body {
  min-height: 100%;
  background: rgba(0, 0, 0, 0.15);
  margin: 0;
  padding:5px;
  display:table;
}
* {
  box-sizing: border-box
}


p:first-of-type {
  background-color:rgba(250,250,0,0.25);
  box-shadow:-5px 5px 5px -5px ,5px 5px 5px -5px , 50px -50px 5px -50px
}
p:first-of-type:before {
  background: linear-gradient(45deg, rgba(0, 0, 0, 0)  50% , rgba(250, 250, 0, 0.25)  50% ) 0 0 no-repeat;
  box-shadow: 5px 0 5px -5px black, 0 -5px 5px -5px black;
  background-size:38px 32px

}
p:first-of-type:after {
  content:'';
  position:absolute;
  top:0;
  left:-3px;
  width:60px;
  height:20px;
  box-shadow:-5px -5px 5px  -5px ;
}
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris
  placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus
  enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis
  luctus, metus</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris
  luctus, metus</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris
  placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus
  enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis
  luctus, metus</p>

阴影可以添加到主容器和伪,第二个伪需要用于绘制最后一点阴影。 甚至可以通过代码段(or 3rd paragraph in demo)

中的假第一段上的rgba()颜色和gradient添加半透明背景

答案 3 :(得分:3)

此解决方案允许弹出式容器上的小边框半径

对于箭头,我使用了png图像,但我非常确定你可以在css中轻松创建完整的弹出窗口。

&#13;
&#13;
.popover {
  position: relative;
  border-left: 2px solid white;
  border-bottom: 2px solid white;
  border-right: 2px solid white;
  margin-top: 12px;
  padding: 12px 18px;
  display: block;
  border-radius: 4px;
}

.popover--fixed {
  width: 300px;
}

.popover--responsive {
  width: 75%;
}

.popover:before {
  content: " ";
  position: absolute;
  top: 0; left: 0;
  width: 20px;
  height: 2px;
  background-color: #fff;
}

.popover:after {
  content: " ";
  position: absolute;
  top: 0; right: 0;
  width: calc(100% - 32px);
  height: 2px;
  background-color: #fff;
}

.popover__arrow {
  width: 16px;
  height: 12px;
  background: url(http://i.imgur.com/oOMecP3.png) no-repeat;
  position: absolute;
  top: -6px; left: 18px;
}

* {
  box-sizing: border-box;
}

body {
  background: black;
  padding: 36px;
  background-image: url(https://farm8.staticflickr.com/7187/6895047173_d4b1a0d798.jpg);
  background-size: cover;
  color: white;
}

h1 {
  margin-top: 3px;
  margin-bottom: 9px;
}

p {
  margin-top: 3px;
  margin-bottom: 0;
}
&#13;
<div class="popover popover--fixed">
  <div class="popover__arrow"></div>
  <h1>Fixed width</h1>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit?</p>
</div>


<div class="popover popover--responsive">
  <div class="popover__arrow"></div>
  <h1>Responsive width</h1>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit? Lorem ipsum dolor sit amet, consectetur adipisicing elit?</p>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:2)

看看网站。形状示例 https://css-tricks.com/examples/ShapesOfCSS/

答案 5 :(得分:1)

我想 - 姗姗来迟 - 提供另一种方法,它只使用一个元素及其两个伪元素,::before::after

body {
  background-color: #000;
}
.quote {
  border: 1px solid #fff;
  /* Hiding the top-border by making it transparent, to
     prevent a line from showing beneath the transparency
     of the pseudo-elements */
  border-top-color: transparent;
  
  /* adjustable to taste: */
  width: 50vw;
  padding: 0.4em;
  color: #fff;
  font-weight: bold;
  /* to allow the pseudo-elements to be placed relative
     to this element, rather than any other ancestors */
  position: relative;
  /* to ensure that the 'arrow' portion is entirely
     visible on screen and between any element
     vertically (on-screen) 'above' the current
     .quote element */
  margin-top: 2em;
}

/* defining the common settings of the arrow portions: */
.quote::before,
.quote::after {
  position: absolute;
  /* moving the arrows to the top of their parent */
  top: 0;
  content: '';
  border-bottom: 1px solid #fff;
  height: 1em;
  /* the negative of the pseudo-element's height
     to align the bottom-border of the pseudo-element
     over the top-border (transparent) of the parent
     element */
  margin-top: -1em;
}
.quote::before,
.quote.quote-center::before {
  /* the right border of the ::before
     pseudo-elements will form the left
     slope of the 'arrow': */
  border-right: 1px solid #fff;
  /* defining a width in which the 'arrow' base is
     equal to ten percent of the width of the element;
     adjust to taste */
  left: 0;
  right: 55%;
  /* skewing the rectangular pseudo-element into
     a parallelogram; the -45deg skews the rectangle
     to rightwards */
  transform: skew(-45deg);
  /* we want the bottom left corner of the pseudo
     element to stay in place relative to the top
     top-left corner of the parent, so we transform
     from that point */
  transform-origin: 0 100%;
  /* to position the left-most point of the pseudo
     element over the left-border of the parent */
  margin-left: -1px;
}
.quote::after,
.quote.quote-center::after {
  border-left: 1px solid #fff;
  left: 55%;
  right: 0;
  transform: skew(45deg);
  transform-origin: 100% 100%;
  margin-right: -1px;
}

/* repositioning the 'arrow' using another
   class-name to override the default placing;
   all other details remain the same */
.quote.quote-left::before {
  left: 0;
  right: 80%;
}
.quote.quote-left::after {
  right: 0;
  left: 30%;
}
.quote.quote-right::before {
  left: 0;
  right: 30%;
}
.quote.quote-right::after {
  right: 0;
  left: 80%;
}

/* defining a 'bold' arrow, as the
   picture in the question shows
   (though I'm unsure if that was a
   deliberate or incidental part of
   the image */
.quote.quote-bold::before {
  /* increasing the thickness of the arrow's
     left slope */
  border-right-width: 3px;
  /* adjusting the skew to a higher number to
     adjust for the changed thickness; this is
     somewhat arbitrary unfortunately and requires
     adjustment to values that work (so far as I
     can work out) */
  transform: skew(-48deg);
}
.quote.quote-bold::after {
  border-left-width: 3px;
  transform: skew(48deg);
}

/* entirely irrelevant to the demo; just to show
   the classes used in each of the elements which
   influence each of the pseudo-elements */
pre {
  color: limegreen;
}
pre::before {
  content: "Class: ";
}
<div class="quote">
  <!-- the <pre> elements are included only to show the
       classes used for styling the quotes, they have
       no part to play in the CSS which creates the
       'arrows' or 'quotes' -->
  <pre>"quote"</pre>
  <p>Lorem ipsum dolor sit amet, nibh patrioque nam ne, graeco consequat reprehendunt ut mei, ex enim labitur vis. Graeci causae adversarium his no. Pro enim causae nostrum eu. Ius unum ornatus liberavisse et, mei dolore menandri mandamus no.</p>
</div>

<div class="quote quote-center">
  <pre>"quote quote-center"</pre>
  <p>Leverage agile frameworks to provide a robust synopsis for high level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition. Organically grow the holistic world view of disruptive
    innovation via workplace diversity and empowerment.

  </p>
</div>

<div class="quote quote-left">
  <pre>"quote quote-left"</pre>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.

  </p>
</div>

<div class="quote quote-right">
  <pre>"quote quote-right</pre>
  <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
    content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
    versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

  </p>
</div>

<div class="quote quote-left quote-bold">
  <pre>"quote quote-left quote-bold"</pre>
  <p>And with a emboldened arrow</p>
</div>

JS Fiddle demo

答案 6 :(得分:1)

这是一个不使用calc并且在视网膜显示器上看起来很酷的解决方案:

body,
.content {
  height: 100%;
  width: 100%;
  background-color: blue;
}
.box {
  color: white;
  font-family: "Lucida Console", Monaco, monospace;
}
<div class='content'>
<pre class='box'>
  __/\__________
 |              |
 |   DOST THOU  |
 |   LOVE ME?   |
 |______________|</pre>
<pre class='box'>
                    __________
                   |          |
                   |   NO.    |
                   |______  __|
                          \/</pre>
<pre class='box'>
  __/\__________
 |              |
 |   BUT THOU   |
 |   MUST!      |
 |______________|</pre>

</div>

答案 7 :(得分:0)

试试这个:

这是https://jsfiddle.net/desqxw38/1/

<div class="arrow_box">
            Lorem Ipsum dummy set.
</div>

    body {
      margin:0;
  }
.arrow_box {
    position: relative;
    background: #88b7d5;
    border: 4px solid #c2e1f5;
    height:150px;
    margin-top:40px;
}
.arrow_box:after, .arrow_box:before {
    bottom: 100%;
    left: 15%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
}

.arrow_box:after {
    border-color: rgba(136, 183, 213, 0);
    border-bottom-color: #88b7d5;
    border-width: 30px;
    margin-left: -30px;
}
.arrow_box:before {
    border-color: rgba(194, 225, 245, 0);
    border-bottom-color: #c2e1f5;
    border-width: 36px;
    margin-left: -36px;
}

答案 8 :(得分:0)

您可以使用以下代码段使用HTML创建不同的箭头。

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
    </head>
<body>
    <table border=1 width=100% style="background-color:yellow">
        <tr>
            <td>
                <center>
                    <div style="width:0px;border:25px solid;border-color:Black yellow yellow yellow"><div>
                </center>
            </td>
            <td>
                <center>
                    <div style="width:0px;border:25px solid;border-color:yellow yellow black yellow"><div>
                </center>
            </td>
        </tr>
        <tr>
            <td>
                <div style="width:0px;border:25px solid;border-color: yellow yellow yellow Black"><div>
            </td>
            <td>
                <div style="width:0px;border:25px solid;float:right;border-color:yellow Black yellow yellow"><div>
            </td>
        </tr>
    </table>    
</body>
</html>

答案 9 :(得分:0)

这应该可以解决问题:

.dropdown-content::before {
    position: absolute;
    content: "";
    width: 16px;
    height: 16px;
    border-style: solid;
    border-width: 1px;
    border-color: white white transparent transparent;
    top: -10px;
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
 }

答案 10 :(得分:0)

是的,这是可能的。

透明的工具提示,它是最干净的形式(我认为)

Transparent tooltip

&#13;
&#13;
body {
  background-image: -webkit-linear-gradient(315deg, #fff, #aaa);
  background-image: linear-gradient(135deg, #fff, #aaa);
  background-size: 50px 50px;
}

.tooltip {
  border-color: #000;
  border-style: solid;
  border-width: 0 2px 2px;
  left: 20%;
  padding: 15px;
  position: absolute;
  top: 20%;
}
.tooltip::after, .tooltip::before {
  border-color: inherit;
  border-style: inherit;
  content: '';
  display: block;
  height: 10px;
  position: absolute;
  top: -10px;
  -webkit-transform-origin: left bottom;
          transform-origin: left bottom;
}
.tooltip::after {
  border-width: 0 2px 2px 0;
  left: -3px;
  -webkit-transform: skewX(135deg);
          transform: skewX(135deg);
  width: calc(30% - 10px);
}
.tooltip::before {
  border-width: 0 0 2px 2px;
  right: -2px;
  position: absolute;
  -webkit-transform: skewX(45deg);
          transform: skewX(45deg);
  -webkit-transform-origin: left bottom;
          transform-origin: left bottom;
  width: calc(70% - 10px);
}
&#13;
<div class="tooltip">
  tooltip adf asd afdasdf asdfas fasdfas
</div>
&#13;
&#13;
&#13;

答案 11 :(得分:0)

我们可以这样做

&#13;
&#13;
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Model{
public function Insert()
    {
        $this->load->view('Insertion_view');
    }
&#13;
&#13;
&#13;

答案 12 :(得分:-2)

在这里尝试这个演示我使用了一些代码,我希望它有所帮助 HTML

<div class="dropdown-content">
 <div class="triangle"></div>
</div>

CSS

.triangle {
position: absolute;
margin: auto;
top: -10px;
left: -160px;
right: 0;
width: 15px;
height: 15px;
transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
-moz-transform: rotate(-135deg);
-o-transform: rotate(-135deg);
-ms-transform: rotate(-1355deg);
border-right: 2px solid #fff;
border-bottom: 2px solid #fff;

}

https://jsfiddle.net/38kjb5us/1/