简体   繁体   English

使背景图像与动态文本对齐

[英]aligning background image with dynamic text

I have a quote generator and I am simply trying to place a background image behind the dynamic text. 我有一个报价生成器,我只是想在动态文本后面放置一个背景图像。 That way the text looks likes it is inside the quotes. 这样,文本看起来就像在引号内一样。 I cannot get this to properly align no matter what I do. 无论我做什么,我都无法正确对齐。 I know its a matter of having different parts in divs and adjusting the css, but its not working. 我知道在div中有不同的部分并调整CSS的问题,但是它不起作用。 Is there a different approach. 是否有其他方法。

 <style> #allTogether{ margin-top:60px; padding-left:-100px; width:400px; } #quote{ margin-top:-40px; width:380px; height:100px; font-size:18px; } #bunch{ margin-left:200px; } </style> 
 <div id="bunch"> <div id="allTogether"> <div id="quote" > <script type="text/javascript" src="https://www.brainyquote.com/link/quotebr.js"></script> </div> </bunch> <br> <img src="https://www.thesenaseproject.org/wp-content/uploads/2015/10/quotation_marks.png"/> 

Here is one way of doing it setting the parent element to the size of the background image, then using absolute positioning to center the quote over the image. 这是一种将父元素设置为背景图像大小的方法,然后使用绝对定位将报价单置于图像的中心。 This will ensure the text overlays the image, and the background image is never stretched. 这将确保文本覆盖图像,并且背景图像不会拉伸。

 #bunch { width: 700px; height: 197px; background: url('https://www.thesenaseproject.org/wp-content/uploads/2015/10/quotation_marks.png') top center no-repeat; position: relative; } #quote { position: absolute; left: 150px; right: 150px; top: 50%; transform: translateY(-50%); text-align: center; } 
 <div id="bunch"> <div id="quote" > <script type="text/javascript" src="https://www.brainyquote.com/link/quotebr.js"></script> </div> </div> 

And here's a more adaptive way to do it without a fixed width/height, and using a padding to make sure the parent's aspect ratio is proportional to the background image, and stretching the background image to the size of the parent using background-size: 100% 100%; 这是一种更灵活的方法,无需固定宽度/高度,并使用填充来确保父母的长宽比与背景图像成比例,并使用background-size: 100% 100%;将背景图像拉伸到父母background-size: 100% 100%; . This is better because the quote box can be whatever size you want it to be, but comes with the downside of the image appearing stretched if you stretch the quote box beyond the size of the image. 这样会更好,因为引号框可以是您想要的任何大小,但是如果将引号框拉伸到超出图像尺寸,则图像的下方会被拉伸。

 #bunch { height: 0; padding-bottom: 28.14%; background: url('https://www.thesenaseproject.org/wp-content/uploads/2015/10/quotation_marks.png') top center no-repeat; background-size: 100% 100%; position: relative; } #quote { position: absolute; left: 20%; right: 20%; top: 50%; transform: translateY(-50%); text-align: center; } 
 <div id="bunch"> <div id="allTogether"> <div id="quote" > <script type="text/javascript" src="https://www.brainyquote.com/link/quotebr.js"></script> </div> </bunch> <br> <img src=""/> 

 .allTogether { display: flex; height: 400px; width: 500px; } .allTogether div { align-self: auto; margin: auto; } .quote { padding-top: 40px; } 
 <div class="allTogether"> <div><img src="http://seanrawles.work/scoopzilla/left_quote.png" /></div> <div> <script type="text/javascript" src="https://www.brainyquote.com/link/quotebr.js"></script> </div> <div><img src="http://seanrawles.work/scoopzilla/right_quote.png" /></div> </div> 

I would cut the image apart (as I have done here), use FLEXBOX and play with the paddings/margins/etc. 我将图像切开(如我在这里所做的),使用FLEXBOX并使用paddings / margins / FLEXBOX

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

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