Overlay text fields on the image in certain positions

I have a background image that I want to overlay several field messages on it at certain positions. Is there a way to snap boxes to an image so that it scales with the image and maintains its exact position? it is probably also necessary to scale the font size inside the box.

on another note, how can I change the opacity of the background without affecting the boxes on it.

here is the jsfiddle link: http://jsfiddle.net/zd3CA/

UPDATE I want the result to look like this with boxes on certain sections of the path. When the image resizes, my boxes move. It seems that it was incomprehensible.

CSS

.back { height: 85em; margin-bottom: 5em; background: url(http://kpv.s3.amazonaws.com/static/img/film.jpg) no-repeat; background-size: contain; color: #ffffff; text-shadow: 1px 1px 1px #000000; } .box-message { max-width: 15em; min-height: 10em; box-sizing: border-box; box-shadow: 1px 0 3px rgba(0, 0, 0, 0.11), -1px 0 3px rgba(0, 0, 0, 0.11); background: #fff; color:#000; padding: 25px 25px 35px 25px; position: relative; } .flow_three { margin-top: 3em; margin-left: 5em; } .flow_two { margin-top: 3em; margin-left: 10em; } .flow_text h3 { color: #1BB366; font-size: 20px; } .flow_text p { font-size: 18px; line-height: 25px; } .back .container { position: relative; z-index: 2; } .container { width: 940px; } 

HTML

 <div class="back"> <div class="container"> <div class="box-message flow_text flow_three"> <h3>text</h3> <p>text txtegv dsf asd fsda f asdf f as df sadf .</p> </div> <div class="box-message flow_text flow_two"> <h3>text</h3> <p>text txtegv dsf asd fsda f asdf f as df sadf .</p> </div> </div> </div> 
+7
javascript html css css3
source share
9 answers

You simply apply the image as a "background image" to your relative positioned (parent) element of the container and set the " background-size 'to" contain "(as you already did).

Now, if you define your (child) fields with percentage widths and set them absolute also with percentage (relative) values ​​for your positions. everything should scale perfectly up and down.

+1
source share
 .should_work_on_any_browser{ /*For Opacity*/ min-height: 100px; margin: auto; -moz-opacity: 0.52; opacity: 0.52; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha"(Opacity=52); position:relative; } 
+2
source share

If you say you want a background image for your form, just apply the background image via css. There is no reason to create an element behind the form as a background, unless you plan to apply animation via js. That even then I would suggest using AS2 / AS3.

So, as I said, use the background image in your form via css.

+1
source share

Update

Change the width by holding the boxes in place with an opaque background

You can set the background image directly to the element and define the background-width set to cover to proportionally cover the background image.

However, this does not project any child element tied to a specific position.

In addition, you cannot set a separate opacity for the background image. You need to install it on the element to which the image is attached, which means that it will also affect child elements.

To solve this problem, you can:

  • Edit the image and save it with the desired opacity as PNG.
  • You can change the opacity using the canvas (see solution below)
  • Or you can use an image element as a child of that element. This last one is quite effective for this case (and more effective than using the canvas).

Here is a simple but effective solution (there are problems in Firefox, because this browser does not support the required CSS property at the moment, but a possible option is provided).

HTML:

Small restructuring of the html code:

 <div id="container"> <img src="http://kpv.s3.amazonaws.com/static/img/film.jpg" width="794" height="477" /> <div class="box-message flow_one">Ipsum lorem dummy text. <br>Ipsum lorem dummy text. <br>Ipsum lorem dummy text. <br> </div> <div class="box-message flow_two">Ipsum lorem dummy text. <br>Ipsum lorem dummy text. <br>Ipsum lorem dummy text. <br> </div> </div> 

CSS

 html, body { width:100%; height:100%; margin:0; padding:0; } /* Important: use fixed width/height for container */ #container { position:relative; width:794; height:477; } /* Let image follow width 100% and height auto-adjusted */ #container > img { width:100%; height:auto; opacity:0.5; } .box-message { position:absolute; border:2px solid #000; border-radius:7px; background:rgba(255, 255, 255, 0.85); padding:7px; } .flow_one { left:12%; top:10%; } .flow_two { left:50%; top:42%; } 

(The only reason the image is separate is to allow different opacity for the children elements.)

Extra tip: if you need to have different font sizes relative to the device screen, you can override font-size with @media queries.

JavaScript:

We need a small snippet with JavaScript to calculate the ratio of the width compared to the width of the image / background.

This relation is then used for the zoom property for the CSS rule, which we set programmatically, since it will scale the parent element and everything in it (for Firefox we need to use transform:scale(f) , however this has problems, it seems to accumulate with the current width background).

 /// init parent element at load redraw(); /// call everytime we resize window.onresize = redraw; /// calc ratio for zoom function redraw() { /// ratio f = window width / background width (hard-coded for demo) var f = window.innerWidth / 794; //FF: container.style.transform = 'scale(' + f + ')'; container.style.zoom = f.toFixed(2); } 

ONLINE DEMO HERE (for Chrome and other browsers that support CSS zoom )

Update 2

No matter how you twist and turn things, you will encounter compatibility issues with this approach, as well as the CSS approach. One browser supports one thing, another browser - one more thing, but not the first, etc. They are not yet.

A more robust approach (looking away from older IE browsers) is to perform manual calculation of element positions, size, font, etc. using pure JavaScript or a wrapper such as jQuery.

You can check whether the browser is able to use, for example, scaling, for example, as follows:

 if (typeof container.style.zoom === 'undefined') { /// manually calculate relations } else { container.style.zoom = factor; } 

Of course, the calculation part is more tedious than shown here, since you need to iterate over the elements, etc. in a way that suits your final decision.

Opacity

If a single image element or background image (predefined opacity) is an option, then this solution provides an option for this. Please note that this is not very effective in this case with resizing, but may be the last solution.

Here is an example of using canvas:

 var img = document.createElement('img'), canvas = document.createElement('canvas'), ctx = canvas.getContext('2d'), opacity = 0.5; /// when image has loaded and a resize event occured img.onload = window.onresize = draw; /// resize canvas and draw image at given opacity function draw() { /// set canvas = window client size canvas.width = window.innerWidth; canvas.height = window.innerHeight; /// set opacity of canvas ctx.globalAlpha = opacity; /// draw image to canvas size ctx.drawImage(img, 0, 0, canvas.width, canvas.height); /// set background to resized image container.style.background = 'url(' + canvas.toDataURL() + ') no-repeat left top'; } /// request cross-origin sharing (if different domain than page) img.crossOrigin = 'anonymous'; /// set image source and start loading image img.src = 'http://i.imgur.com/Y77lhhL.jpg'; 

ONLINE DEMO HERE

Notes. To do this, you need to comply with the CORS (cross-source sharing) requirements. This means that the image is downloaded from some source (domain, path) - or - if downloaded from another source, that the server allows sharing.

You can see for a demonstration of the script that the source link for the image you provided will not work with this approach, so I moved the image to imgur.com, which allows cross-sharing to work and it works.

+1
source share

A solution is possible using fluid units, such as% and em, and using absolute position.

Styles:

 *,html{ margin:0px; padding:0px; } .back { height: 51em; margin-bottom: 5em; background: url(http://kpv.s3.amazonaws.com/static/img/film.jpg) no-repeat; background-size: contain; color: #ffffff; text-shadow: 1px 1px 1px #000000; width: 60%; } .back .container { position: relative; z-index: 2; } .container { width: 100%; height: 100%; } .box-message { box-sizing: border-box; box-shadow: 1px 0 3px rgba(0, 0, 0, 0.11), -1px 0 3px rgba(0, 0, 0, 0.11); background: #fff; color: #000; padding: 1% 2%; width: 26.66%; } .flow_three { margin-top: 5%; margin-left: 5%; position: absolute; } .flow_two { margin-top: 1%; float: right; right: 2%; position: absolute; } .flow_center{ position: absolute; margin-top: 26%; margin-left: 44%; } .flow_text h3 { color: #1BB366; font-size: 1.25em; } .flow_text p { font-size: 1.125em; line-height: 95%; } 

html

 <div class="back"> <div class="container"> <div class="box-message flow_text flow_three"> <h3>text</h3> <p>text txtegv dsf asd fsda f asdf f as df sadf .</p> </div> <div class="box-message flow_text flow_two"> <h3>text</h3> <p>text txtegv dsf asd fsda f asdf f as df sadf .</p> </div> <div class="box-message flow_text flow_center"> <h3>text</h3> <p>text txtegv dsf asd fsda f asdf f as df sadf .</p> </div> </div> </div> 
+1
source share

I suggest using svg for this.

I made an example:

 <svg width="640" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ --> <g> <title>Layer 1</title> <image x="0" y="0" width="640" height="480" id="svg_1" xlink:href="http://kpv.s3.amazonaws.com/static/img/film.jpg"/> <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="92" y="77" width="130" height="79" id="svg_4"/> <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="91.5" y="213" width="130" height="79" id="svg_6"/> <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="91.5" y="338.5" width="130" height="79" id="svg_7"/> <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="281.5" y="195" width="186" height="113.03077" id="svg_8"/> <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="468.5" y="31" width="161" height="97.83847" id="svg_9"/> <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="474.50001" y="306.99999" width="144.99999" height="88.11538" id="svg_10"/> <text xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" id="svg_2" y="124" x="157" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text> <text id="svg_3" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="262" x="155" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text> <text id="svg_5" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="387" x="157" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text> <text id="svg_11" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="261" x="374" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text> <text id="svg_12" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="88" x="553" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text> <text id="svg_14" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="357" x="548" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text> </g> </svg> 

It was created using this tool: http://svg-edit.googlecode.com/svn/branches/2.6/editor/svg-editor.html

Once your svg is complete, you can simply refer to it in the css and voila property of the property there, you have it, it resizes with the containing element.

Or you could calculate each position and element size in% relative to the containing element and write your css accordingly.

0
source share

To achieve the desired result, the image must first have the same spatial reference (for example, a coordinate system) as .box-message elements. This is achieved by placing the image in the tag and turning it into sibling into .box-message elements. We also need the image to be the only child defining the height and width of the parent. To do this, the .box-message elements .box-message set with absolute positioning relative to the parent, and the image width set to 100%. This allows you to resize the image using the parent container and maintain its aspect ratio.

Now that the absolute positioning of the .box-message elements, we set their coordinates relative to the image using the top and left properties. This will snap the upper left corner of the .box-message elements to a specific point in the image. To bind .box-message elements to their center point, you can add the transform: translate3d(-50%,-50%,0); property transform: translate3d(-50%,-50%,0); . You can set minimum / maximum width / height properties for .box-message elements, as well as change them with respect to the image.

Finally, the font size can be adjusted by specifying the size in viewing units ( vw and / or vh ). Check out this tutorial and example: http://css-tricks.com/examples/ViewportTypography/

Each time you change the viewing area, the font size must change accordingly.

Check out this Fiddle example: http://jsfiddle.net/3wq25/1/

0
source share

So ... no one understands that this is easy?

CSS

 .img { background-image:url('image.jpg'); width: 100%; height: 200px; padding: 200px; margin: 0px; } 

HTML:

 <table class="img"><tr><td> <h1>Text Here</h1> <h3>Text Here</h3> </td></tr></table> 
0
source share

I would do it with lettering.js. http://letteringjs.com/

Set the default text size, and then save the window size by adding an add-on.

Both should scale easily.

0
source share

All Articles