简体   繁体   English

如何创建一个在背景上有文字(带有指针/箭头)的div,从而需要自动扩展宽度?

[英]How do I create a div that has text over a background (with a pointer/arrow) creating the need to automatically expand the width?

How do I create a div with an :after selector, with text, that automatically expands to fit the width of the text? 如何创建带有:after选择器和文本的div,该div会自动扩展以适合文本的宽度?

Sound complicated? 听起来复杂吗? Maybe it is. 也许是。

I first solved this with a div id and :after selector however we have encounter too many issues with compatibility and functionality. 我首先使用div id和:after选择器解决了这个问题,但是我们在兼容性和功能方面遇到了太多问题。 I'm able to make this look OK in chrome however whenever I zoom the alignment and placement gets screwy. 我能够在chrome中使它看起来不错,但是每当我缩放对齐方式和放置位置时都会出现螺丝钉。

I have an idea to replace the background-color and :after with .png files but I'm not totally sure how I would do it? 我有一个用.png文件替换background-color和:after的想法,但是我不太确定该怎么做? The code below has a pointer on the left and then a long horizontal body, which I could splice from a .psd into 3 png files -- one for the pointer, a 1px image to repeat and then one to finish off the div. 下面的代码在左侧有一个指针,然后有一个长的水平主体,我可以将其从.psd拼接成3个png文件-一个用于指针,一个1px的图像重复,然后一个完成div。

I hope this is clear. 我希望这很清楚。 See my code below and feel free to ask me any questions! 请参阅下面的代码,随时问我任何问题!

#div1 {
padding:6px 0 0 0;
left:49px;
position:relative;
float:left;
height:35px;
background-repeat:no-repeat;
background-position:left
}

#div1 p {
font-family:sans-serif,Helvetica;
font-size:14px;
font-style:italic;
text-decoration:none;
color:#FFF;
margin:5px 0 0 -2px;
padding-right:10px;
text-shadow:0 1px 0 #000
}

.div2 {
width:auto;
min-height:30px; 
max-width:420px;
background: #fc1e10;
color:white;
padding:5px 10px 0 10px;
position:relative;
word-wrap:break-word;
margin-bottom:2em;
margin-left:20px;
-webkit-box-shadow:3px 3px 0 rgba(0,0,0,.3), inset 0 2px 2px rgba(253,116,84,.9);
box-shadow:3px 3px 0 rgba(0,0,0,.3), inset 0 2px 2px rgba(253,116,84,.9);
border-bottom:solid 1px;
border-bottom-color:#fd7454;
border-top:solid 1px;
border-top-color:#f11d1a;
}

.div2:after { 
content:'';
display:block;
position:absolute;
top:-.05em;
left:-2.16em; 
width:0;
height:0;
border-color: transparent #fc1e10 transparent transparent;
border-style: solid;
border-width: 1.14em;
}

Please help! 请帮忙!

它是否必须为:after,是否可以为div内的跨度?

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

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