透明形状,上角有箭头
transparent shape with arrow in upper corner
请看下图
我想在 div
的右上角添加一个箭头,我将其视为可编辑的输入框。
请帮助我如何使用 CSS 实现此目的。
我不能使用 SVG,因为我需要它作为 div
以在其上显示表情符号作为图像。
<div placeholder="Your message" id="Message">
...
</div>
您可以按照下面的代码片段进行操作。实现形状的方法如下:
- 主要
div
元素只有顶部、底部和左边框。右边框无效,因为元素及其箭头需要透明。使用透明箭头时,如果存在右边框,也会显示该边框。
- 右边的箭头是使用相对于形状右边缘放置的
skew
ed 元素实现的。
- 形状的右边框是通过使用另一个大小与整个容器的高度相同的伪元素——箭头伪元素的高度来实现的。此元素相对于形状的右下角定位。
您可以根据需要调整高度和边框半径。我已经设置了定位,即使 height/width 父级的变化也不会影响它。
div.shape {
position: relative;
width: 300px;
height: 100px;
padding: 4px;
margin-top: 20px;
border: 2px solid gray;
border-right: none; /* not required as the shape needs to be transparent */
border-radius: 8px; /* not required as the right border is done through pseudo element */
border-top-right-radius: 0px;
border-bottom-right-radius: 0px;
}
div.shape:before {
position: absolute;
content: '';
top: -2px; /* equal to border top of parent - no need to change*/
right: -6px; /* for positioning - no need to change*/
height: 15%; /* should be changed depending on height of arrow */
width: 10%; /* should be changed depending on width of arrow */
border-top: 2px solid gray;
border-right: 3px solid gray; /* thicker border because skew makes it thin */
/* to achieve the arrow like shape */
-webkit-transform-origin: bottom right;
-webkit-transform: skew(-45deg);
-moz-transform: skew(-45deg);
transform: skew(-45deg);
}
div.shape:after {
position: absolute;
content: '';
right: -6px; /* for positioning - no need to change*/
height: 85%; /* height of parent - height of arrow */
width: 2%; /* no need to change */
bottom: -2px; /* equal to border bottom of parent - no need to change*/
border-right: 2px solid gray;
border-bottom: 2px solid gray;
border-bottom-right-radius: 8px; /* for producing curve on bottom right */
}
/* Just for demo */
body {
background: -webkit-linear-gradient(0deg, crimson, indianred, purple);
background: -moz-linear-gradient(90deg, crimson, indianred, purple);
background: linear-gradient(90deg, crimson, indianred, purple);
}
<div class="shape">
Lorem Ipsum Dolor Sit Amet...
</div>
可以通过更改定位属性和倾斜方向(从正角到负角)将箭头添加到左侧,如下面的代码片段所示。
div.shape {
position: relative;
width: 300px;
height: 100px;
padding: 4px;
margin-top: 20px;
margin-left: 20px;
border: 2px solid gray;
border-left: none; /* not required as the shape needs to be transparent */
border-radius: 8px; /* not required as the right border is done through pseudo element */
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
}
div.shape:before {
position: absolute;
content: '';
top: -2px; /* equal to border top of parent - no need to change*/
left: -6px; /* for positioning - no need to change*/
height: 15%; /* should be changed depending on height of arrow */
width: 10%; /* should be changed depending on width of arrow */
border-top: 2px solid gray;
border-left: 3px solid gray; /* thicker border because skew makes it thin */
/* to achieve the arrow like shape */
-webkit-transform-origin: bottom right;
-webkit-transform: skew(45deg);
-moz-transform: skew(45deg);
transform: skew(45deg);
}
div.shape:after {
position: absolute;
content: '';
left: -6px; /* for positioning - no need to change*/
height: 85%; /* height of parent - height of arrow */
width: 2%; /* no need to change */
bottom: -2px; /* equal to border bottom of parent - no need to change*/
border-left: 2px solid gray;
border-bottom: 2px solid gray;
border-bottom-left-radius: 8px; /* for producing curve on bottom right */
}
/* Just for demo */
body {
background: -webkit-linear-gradient(0deg, crimson, indianred, purple);
background: -moz-linear-gradient(90deg, crimson, indianred, purple);
background: linear-gradient(90deg, crimson, indianred, purple);
}
<div class="shape">
Lorem Ipsum Dolor Sit Amet...
</div>
滤镜:阴影()
兼容性是limited
不过效果还不错:P
.inputcontainer {
display: inline-block;
position: relative;
filter: drop-shadow(0px 0px 5px black);
-webkit-filter: drop-shadow(0px 0px 1px black);
}
.input {
display: inline-block;
border: none;
border-radius: 10px;
border-top-right-radius: 0px;
width: 280px;
height: 50px;
background-color: white;
padding-left: 20px;
font-size: 20px;
}
.input:focus {
outline: none;
}
.arrow {
position: absolute;
display: inline-block;
top: 0;
right: -5px;
width: 20px;
height: 20px;
background-color: white;
transform: skew(-45deg);
z-index: -1;
}
<div class="inputcontainer">
<input type="text" class="input" placeholder="Your message"/>
<div class="arrow"></div>
</div>
方框阴影:
这里的兼容性是a lot better
.inputcontainer {
display: inline-block;
position: relative;
filter: drop-shadow(0px 0px 5px black);
}
.input {
display: inline-block;
border: none;
border-radius: 10px;
border-top-right-radius: 0px;
width: 280px;
height: 50px;
background-color: white;
padding-left: 20px;
font-size: 20px;
box-shadow: 0px 0px 0px 2px gray;
}
.input:focus {
outline: none;
}
.arrow {
position: absolute;
display: inline-block;
top: 0;
right: -8px;
width: 20px;
height: 20px;
background-color: white;
transform: skew(-45deg);
box-shadow: 2px -2px 0px 0px gray;
}
<div class="inputcontainer">
<input type="text" class="input" placeholder="Your message"/>
<div class="arrow"></div>
</div>
请看下图
我想在 div
的右上角添加一个箭头,我将其视为可编辑的输入框。
请帮助我如何使用 CSS 实现此目的。
我不能使用 SVG,因为我需要它作为 div
以在其上显示表情符号作为图像。
<div placeholder="Your message" id="Message">
...
</div>
您可以按照下面的代码片段进行操作。实现形状的方法如下:
- 主要
div
元素只有顶部、底部和左边框。右边框无效,因为元素及其箭头需要透明。使用透明箭头时,如果存在右边框,也会显示该边框。 - 右边的箭头是使用相对于形状右边缘放置的
skew
ed 元素实现的。 - 形状的右边框是通过使用另一个大小与整个容器的高度相同的伪元素——箭头伪元素的高度来实现的。此元素相对于形状的右下角定位。
您可以根据需要调整高度和边框半径。我已经设置了定位,即使 height/width 父级的变化也不会影响它。
div.shape {
position: relative;
width: 300px;
height: 100px;
padding: 4px;
margin-top: 20px;
border: 2px solid gray;
border-right: none; /* not required as the shape needs to be transparent */
border-radius: 8px; /* not required as the right border is done through pseudo element */
border-top-right-radius: 0px;
border-bottom-right-radius: 0px;
}
div.shape:before {
position: absolute;
content: '';
top: -2px; /* equal to border top of parent - no need to change*/
right: -6px; /* for positioning - no need to change*/
height: 15%; /* should be changed depending on height of arrow */
width: 10%; /* should be changed depending on width of arrow */
border-top: 2px solid gray;
border-right: 3px solid gray; /* thicker border because skew makes it thin */
/* to achieve the arrow like shape */
-webkit-transform-origin: bottom right;
-webkit-transform: skew(-45deg);
-moz-transform: skew(-45deg);
transform: skew(-45deg);
}
div.shape:after {
position: absolute;
content: '';
right: -6px; /* for positioning - no need to change*/
height: 85%; /* height of parent - height of arrow */
width: 2%; /* no need to change */
bottom: -2px; /* equal to border bottom of parent - no need to change*/
border-right: 2px solid gray;
border-bottom: 2px solid gray;
border-bottom-right-radius: 8px; /* for producing curve on bottom right */
}
/* Just for demo */
body {
background: -webkit-linear-gradient(0deg, crimson, indianred, purple);
background: -moz-linear-gradient(90deg, crimson, indianred, purple);
background: linear-gradient(90deg, crimson, indianred, purple);
}
<div class="shape">
Lorem Ipsum Dolor Sit Amet...
</div>
可以通过更改定位属性和倾斜方向(从正角到负角)将箭头添加到左侧,如下面的代码片段所示。
div.shape {
position: relative;
width: 300px;
height: 100px;
padding: 4px;
margin-top: 20px;
margin-left: 20px;
border: 2px solid gray;
border-left: none; /* not required as the shape needs to be transparent */
border-radius: 8px; /* not required as the right border is done through pseudo element */
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
}
div.shape:before {
position: absolute;
content: '';
top: -2px; /* equal to border top of parent - no need to change*/
left: -6px; /* for positioning - no need to change*/
height: 15%; /* should be changed depending on height of arrow */
width: 10%; /* should be changed depending on width of arrow */
border-top: 2px solid gray;
border-left: 3px solid gray; /* thicker border because skew makes it thin */
/* to achieve the arrow like shape */
-webkit-transform-origin: bottom right;
-webkit-transform: skew(45deg);
-moz-transform: skew(45deg);
transform: skew(45deg);
}
div.shape:after {
position: absolute;
content: '';
left: -6px; /* for positioning - no need to change*/
height: 85%; /* height of parent - height of arrow */
width: 2%; /* no need to change */
bottom: -2px; /* equal to border bottom of parent - no need to change*/
border-left: 2px solid gray;
border-bottom: 2px solid gray;
border-bottom-left-radius: 8px; /* for producing curve on bottom right */
}
/* Just for demo */
body {
background: -webkit-linear-gradient(0deg, crimson, indianred, purple);
background: -moz-linear-gradient(90deg, crimson, indianred, purple);
background: linear-gradient(90deg, crimson, indianred, purple);
}
<div class="shape">
Lorem Ipsum Dolor Sit Amet...
</div>
滤镜:阴影()
兼容性是limited
不过效果还不错:P
.inputcontainer {
display: inline-block;
position: relative;
filter: drop-shadow(0px 0px 5px black);
-webkit-filter: drop-shadow(0px 0px 1px black);
}
.input {
display: inline-block;
border: none;
border-radius: 10px;
border-top-right-radius: 0px;
width: 280px;
height: 50px;
background-color: white;
padding-left: 20px;
font-size: 20px;
}
.input:focus {
outline: none;
}
.arrow {
position: absolute;
display: inline-block;
top: 0;
right: -5px;
width: 20px;
height: 20px;
background-color: white;
transform: skew(-45deg);
z-index: -1;
}
<div class="inputcontainer">
<input type="text" class="input" placeholder="Your message"/>
<div class="arrow"></div>
</div>
方框阴影:
这里的兼容性是a lot better
.inputcontainer {
display: inline-block;
position: relative;
filter: drop-shadow(0px 0px 5px black);
}
.input {
display: inline-block;
border: none;
border-radius: 10px;
border-top-right-radius: 0px;
width: 280px;
height: 50px;
background-color: white;
padding-left: 20px;
font-size: 20px;
box-shadow: 0px 0px 0px 2px gray;
}
.input:focus {
outline: none;
}
.arrow {
position: absolute;
display: inline-block;
top: 0;
right: -8px;
width: 20px;
height: 20px;
background-color: white;
transform: skew(-45deg);
box-shadow: 2px -2px 0px 0px gray;
}
<div class="inputcontainer">
<input type="text" class="input" placeholder="Your message"/>
<div class="arrow"></div>
</div>