Tuesday, 31 January 2017

Value Conversion Operations and Corresponding instruction

Conversion Operations

Visual Studio .NET 2003
Microsoft Specific
The conversions operations are listed in the Conversion Operations table followed by a description of each intrinsic with the most recent mnemonic naming convention. The alternate name is provided if you have used these intrinsics before.

Conversion Operations
Intrinsic name Corresponding instruction
_mm_cvtss_si32 CVTSS2SI
_mm_cvtps_pi32 CVTPS2PI
_mm_cvttss_si32 CVTTSS2SI
_mm_cvttps_pi32 CVTTPS2PI
_mm_cvtsi32_ss CVTSI2SS
_mm_cvtpi32_ps CVTTPS2PI
_mm_cvtpi16_ps Composite
_mm_cvtpu16_ps Composite
_mm_cvtpi8_ps Composite
_mm_cvtpu8_ps Composite
_mm_cvtpi32x2_ps Composite
_mm_cvtps_pi16 Composite
_mm_cvtps_pi8 Composite

For an explanation of the syntax used in code samples in this topic, see Floating-Point Intrinsics Using Streaming SIMD Extensions.
int _mm_cvtss_si32(__m128 a );

CVTSS2SI

Converts the lower single-precision, floating-point value of a to a 32-bit integer according to the current rounding mode.
r := (int)a0
__m64 _mm_cvtps_pi32(__m128 a );

CVTPS2PI

Converts the two lower single-precision, floating-point values of a to two 32-bit integers according to the current rounding mode, returning the integers in packed form.
r0 := (int)a0
r1 := (int)a1
int _mm_cvttss_si32(__m128 a );

CVTTSS2SI

Converts the lower single-precision, floating-point value of a to a 32-bit integer with truncation.
r := (int)a0
__m64 _mm_cvttps_pi32(__m128 a );

CVTTPS2PI

Converts the two lower single-precision, floating-point values of a to two 32-bit integer with truncation, returning the integers in packed form.
r0 := (int)a0
r1 := (int)a1
__m128 _mm_cvtsi32_ss(__m128 a , int b );

CVTSI2SS

Converts the 32-bit integer value b to an single-precision, floating-point value; the upper three single-precision, floating-point values are passed through from a.
r0 := (float)b
r1 := a1 ; r2 := a2 ; r3 := a3
__m128 _mm_cvtpi32_ps(__m128 a , __m64 b );

CVTPI2PS

Converts the two 32-bit integer values in packed form in b to two single-precision, floating-point values; the upper two single-precision, floating-point values are passed through from a.
r0 := (float)b0
r1 := (float)b1
r2 := a2
r3 := a3
__m128 _mm_cvtpi16_ps(__m64 a );

(composite)

Converts the four 16-bit signed integer values in a to four single-precision, floating-point values.
r0 := (float)a0
r1 := (float)a1
r2 := (float)a2
r3 := (float)a3
__m128 _mm_cvtpu16_ps(__m64 a );

(composite)

Converts the four 16-bit unsigned integer values in a to four single-precision, floating-point values.
r0 := (float)a0
r1 := (float)a1
r2 := (float)a2
r3 := (float)a3
__m128 _mm_cvtpi8_ps(__m64 a );

(composite)

Converts the lower four 8-bit signed integer values in a to four single-precision, floating-point values.
r0 := (float)a0
r1 := (float)a1
r2 := (float)a2
r3 := (float)a3
__m128 _mm_cvtpu8_ps(__m64 a );

(composite)

Converts the lower four 8-bit unsigned integer values in a to four single-precision, floating-point values.
r0 := (float)a0
r1 := (float)a1
r2 := (float)a2
r3 := (float)a3
__m128 _mm_cvtpi32x2_ps(__m64 a, __m64 b );

(composite)

Converts the two 32-bit signed integer values in a and the two 32-bit signed integer values in b to four single-precision, floating-point values.
r0 := (float)a0
r1 := (float)a1
r2 := (float)b0
r3 := (float)b1
__m64 _mm_cvtps_pi16( __m128 a );

(composite)

Converts the four single-precision, floating-point values in a to four signed 16-bit integer values.
r0 := (short)a0
r1 := (short)a1
r2 := (short)a2
r3 := (short)a3
__m64 _mm_cvtps_pi8( __m128 a );

(composite)

Converts the four single-precision, floating-point values in a to the lower four signed 8-bit integer values of the result.
r0 := (char)a0
r1 := (char)a1
r2 := (char)a2
r3 := (char)a3 
 
 
END Microsoft Specific

Assembly Convert Float Value to Integer

CVTTSS2SI--Convert Scalar Single-Precision Floating-Point Value to Doubleword Integer with Truncation

sample: cvttss2si ebx,xmm0

Opcode

Instruction

Description

F3 0F 2C /r
CVTTSS2SI r32, xmm/m32
Convert one single-precision floating-point number from xmm/m32 to one signed doubleword integer r32 using truncation.

Description

Converts a single-precision floating-point value in the source operand (second operand) to a signed doubleword integer in the destination operand (first operand). The source operand can be an XMM register or a 32-bit memory location. The destination operand is a general-purpose register. When the source operand is an XMM register, the single-precision floating-point value is contained in the low doubleword of the register.
When a conversion is inexact, a truncated result is returned. If a converted result is larger than the maximum signed doubleword integer, the floating-point invalid exception is raised, and if this exception is masked, the indefinite integer value (80000000H) is returned.

Operation

DEST[31-0] Convert_Single_Precision_Floating_Point_To_Integer_Truncate(SRC[31-0]);

Intel(R) C++ Compiler Intrinsic Equivalent

int_mm_cvttss_si32(__m128d a)

SIMD Floating-Point Exceptions

Invalid, Precision.

Protected Mode Exceptions

#GP(0) - For an illegal memory operand effective address in the CS, DS, ES, FS or GS segments.
#SS(0) - For an illegal address in the SS segment.
#PF(fault-code) - For a page fault.
#NM - If TS in CR0 is set.
#XM - If an unmasked SIMD floating-point exception and OSXMMEXCPT in CR4 is 1.
#UD - If an unmasked SIMD floating-point exception and OSXMMEXCPT in CR4 is 0. If EM in CR0 is set. If OSFXSR in CR4 is 0. If CPUID feature flag SSE is 0.
#AC - For unaligned memory reference if the current privilege level is 3.

Real-Address Mode Exceptions

Interrupt 13 - If any part of the operand lies outside the effective address space from 0 to 0FFFFH.
#NM - If TS in CR0 is set.
#XM - If an unmasked SIMD floating-point exception and OSXMMEXCPT in CR4 is 1.
#UD - If an unmasked SIMD floating-point exception and OSXMMEXCPT in CR4 is 0. If EM in CR0 is set. If OSFXSR in CR4 is 0. If CPUID feature flag SSE is 0.

Virtual-8086 Mode Exceptions

Same exceptions as in Real Address Mode.
#PF(fault-code) - For a page fault.
#AC - For unaligned memory reference if the current privilege level is 3.


Wednesday, 25 January 2017

Disable Text Selection, Except Specified Enable on Blogger


Enable Partial Text Selection or Disable All Text Selection of Bloger post

Most of the genuine bloggers on Blogspot platform like face the problem of their blogs' content being copied or duplicated by newbies who are just entering in the blogosphere with the goal of making money without making real efforts and understanding the value of original/unique content. Sometimes the content copied by them gets higher rankings in search engines than your original content and this is unfair which lowers the motivation of genuine bloggers. So it is important to protect your blog content from these content thieves and this is the agenda of this post. We will disable the copy function by disabling the mouse right click and CTRL+A. We have two options for doing this i.e., via a JavaScript Code if you want to disable whole text area of blog or via CSS code if you share some codes/text on your blog posts and your readers need to copy them.


Disable Text Selection On Blogger With JavaScript Code

Go to Blogger→Template→Edit Template→Backup Your Template Code First.

Now search for <head> and copy paste the JavaScript Code given below after <head> tag.

(Note: - use Ctrl+F in Windows and Cmd+F in Mac to search for code in template)


<!--Disabe Copy Paste--- Technaij.com-->
<script language='JavaScript1.2'>
function disableselect(e){
return false
}
function reEnable(){
return true
}
document.onselectstart=new Function (&quot;return false&quot;)
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</script>



Now save your template and view your blog to check the results.


Disable Text Selection Except Specified Codes/Text With CSS


This is the magic code we are using on this blog because we are sharing tutorials where readers need to copy some codes we share like CSS/JavaScript/Html etc. So on the same time we do not let readers copy our posts' text area other than codes/Text we want to share. This is possible with this simple CSS code where we enabled the copy paste of text/code we post in"blockquotes" only. Go toBlogger→Template→Edit Template→Backup.

Now search for ]]></b:skin> this code line and copy paste the CSS code given below before this ]]></b:skin> code line.

(Note:- use Ctrl+F in Windows and Cmd+F in Mac to search in template code)


/*----- Disable Text Selection with CSS Code--- Technaij.com----*/
.post blockquote {
-webkit-user-select: text !important;
-moz-user-select: text !important;
-ms-user-select: text !important;
user-select: text !important;
}
body {
-webkit-user-select: none !important;
-moz-user-select: -moz-none !important;
-ms-user-select: none !important;
user-select: none !important;
}




Important Note:- In the above CSS code we have enabled the copy/selection of text/code we post using blockquotes by enabling .post blockquote but it may not work for you if your template is using different CSS class for blockquote for e.g., Our template using .post blockquote where your template may be using .post-body blockquote or blockquote. So in that case you need to replace.post blockquote with .post-body blockquote or blockquote in above CSS code.  If you do not share any codes or content than just copy paste this little piece of CSS code to disable all text area on your blog.


/*----- Disable Text Selection with CSS Code--- Technaij.com----*/
body {
-webkit-user-select: none !important;
-moz-user-select: -moz-none !important;
-ms-user-select: none !important;
user-select: none !important;
}



Note:  You can add CSS codes by simply going to Template Designer >> Add custom CSS and paste your code then save.

Benefit of using CSS Code rather than JavaScript ?

1) When we use JavaScript code, do you know what does a copy cat do? Simply disables JavaScript code inside browser and reload the page. Then everything goes into normal and anyone can copy the whole content.

Using CSS code we eliminate this factor as user usually can not disable the CSS. However, an experienced user can do it simply by inspecting elements and finding our CSS code snippet then delete it in browser but this task is far away from newbies. Experienced users rarely do that.

2) CSS codes are far better than JavaScripts as they do not lower the speed of your blog/website where JavaScript codes consume much time to load and make your blog load speed very low and overall you lose Visitors=Money. Avoid using JavaScripts.

Let us know where these codes worked for you or you are having problems with these codes? We will serve you our best. Thank You


Tuesday, 24 January 2017

Disable Text Selection, Except Specified on Blogger

Disable All Text Selection or Partial Enable

Text Selection


Most of the genuine bloggers on BlogSpot platform face the problem of their blogs' content being copied or duplicated by newbies who are just entering in the blogosphere with the goal of making money without making real efforts and understanding the value of original/unique content. Sometimes the content copied by them gets higher rankings in search engines than your original content and this is unfair which lowers the motivation of genuine bloggers. So it is important to protect your blog content from these content thieves and this is the agenda of this post. We will disable the copy function by disabling the mouse right click and CTRL+A. We have two options for doing this i.e., via a JavaScript Code if you want to disable whole text area of blog or via CSS code if you share some codes/text on your blog posts and your readers need to copy them.


Disable Text Selection On Blogger With JavaScript Code

Go to Blogger→Template→Edit Template→Backup Your Template Code First.
Now search for <head> and copy paste the JavaScript Code given below after <head> tag.
(Note: - use Ctrl+F in Windows and Cmd+F in Mac to search for code in template)

<!--Disabe Copy Paste--- TB-->
<script language='JavaScript1.2'>
function disableselect(e){
return false
}
function reEnable(){
return true
}
document.onselectstart=new Function (&quot;return false&quot;)
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</script>

Now save your template and view your blog to check the results.


Disable Text Selection Except Specified Codes/Text With CSS


This is the magic code we are using on this blog because we are sharing tutorials where readers need to copy some codes we share like CSS/JavaScript/Html etc. So on the same time we do not let readers copy our posts' text area other than codes/Text we want to share. This is possible with this simple CSS code where we enabled the copy paste of text/code we post in "blockquotes" only. Go to Blogger→Template→Edit Template→Backup.
Now search for ]]></b:skin> this code line and copy paste the CSS code given below before this ]]></b:skin> code line.
(Note:- use Ctrl+F in Windows and Cmd+F in Mac to search in template code)

/*----- Disable Text Selection with CSS Code--- TB----*/
.post blockquote {
-webkit-user-select: text !important;
-moz-user-select: text !important;
-ms-user-select: text !important;
user-select: text !important;
}
body {
-webkit-user-select: none !important;
-moz-user-select: -moz-none !important;
-ms-user-select: none !important;
user-select: none !important;
}

Important Note:- In the above CSS code we have enabled the copy/selection of text/code we post using blockquotes by enabling .post blockquote but it may not work for you if your template is using different CSS class for blockquote for e.g., Our template using .post blockquote where your template may be using .post-body blockquote or blockquote. So in that case you need to replace .post blockquote with .post-body blockquote or blockquote in above CSS code.  If you do not share any codes or content than just copy paste this little piece of CSS code to disable all text area on your blog.

/*----- Disable Text Selection with CSS Code--- TB----*/
body {
-webkit-user-select: none !important;
-moz-user-select: -moz-none !important;
-ms-user-select: none !important;
user-select: none !important;
}

Note:  You can add CSS codes by simply going to Template Designer >> Add custom CSS and paste your code then save.

Benefit of using CSS Code rather than JavaScript ?

1) When we use JavaScript code, do you know what does a copy cat do? Simply disables JavaScript code inside browser and reload the page. Then everything goes into normal and anyone can copy the whole content.

Using CSS code we eliminate this factor as user usually can not disable the CSS. However, an experienced user can do it simply by inspecting elements and finding our CSS code snippet then delete it in browser but this task is far away from newbies. Experienced users rarely do that.

2) CSS codes are far better than JavaScripts as they do not lower the speed of your blog/website where JavaScript codes consume much time to load and make your blog load speed very low and overall you lose Visitors=Money. Avoid using JavaScripts.

Let us know where these codes worked for you or you are having problems with these codes? We will serve you our best. Thank You.

https://groovyinn.blogspot.com/feeds/posts/default


Translate

Powered by Google TranslateTranslate





Wednesday, 11 January 2017

YouTube embed Video on mobile

How To Make a Responsive 100% Width YouTube iFrame Embed


The key to creating a responsive YouTube embed is with padding and a container element, which allows you to give it a fixed aspect ratio. You can also use this technique with most other iframe-based embeds, such as slideshows.

Here is what a typical YouTube embed code looks like, with fixed width and height:

<iframe width="560" height="315" src="//www.youtube.com/embed/yCOY82UdFrw"
frameborder="0" allowfullscreen></iframe>

It would be nice if we could just give it a 100% width, but it won't work as the height remains fixed. What you need to do is wrap it in a container like so (note the class names and removal of the width and height):

<div class="container">
<iframe src="//www.youtube.com/embed/yCOY82UdFrw"
frameborder="0" allowfullscreen class="video"></iframe>
</div>

And use the following CSS:

.container {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 56.25%;
}
.video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

How this works: The container element is given a zero height and a percentage bottom padding. The percentage bottom padding is a percentage of the container width, so that gives it a fixed aspect ratio. But in order to get the iframe to show up inside the zero-height container, you need to make the container relative and the iframe absolute, positioned inside the div.

Here is the result:







Alternative Using Media Queries


You can also use media query breakpoints to resize an iframe to specific sizes. It can be a more cumbersome solution, but does avoid the need for a container element.

YouTube videos responsively on any device

Learn how to embed YouTube videos responsively and without increasing the page load time. 

The embeds are light and mobile friendly and the YouTube player is loaded only when the user initiates the video playback. 

It is easy to embed a YouTube video but you’ll be surprised to know how much extra weight that embedded YouTube video can add to your web pages. The browser has to download about half a Mb of extra JavaScript files (see screenshot) for rendering the YouTube video player alone. And these files are downloaded even if the visitor never plays the embedded video.
The embedded video not only increases the byte size of your web pages but the browser has to make multiple HTTP requests to render the video player. This increases the overall loading time of your page thus affecting the page speed score. The other drawback with the default YouTube embed code is that it isn’t responsive. If people view your website on a mobile phone, the video player may not resize properly for the small screen.

Embed YouTube Videos without Increasing Page Size

Google+ uses a clever technique for embedding YouTube videos – it just embeds the thumbnail image of a YouTube video and the actual video player is loaded only when the user manually clicks the thumbnail.
YouTube thumbnail images are about 15 kB in size so we are able to reduce the byte size of web pages by 500+ kb. That’s huge!




  ***  sadly it doesn't work in blogger **

<div data-id="YQHsXMglC9A">


The video above is embedded using the same on-demand technique (demo).
When a visitor clicks the play button, the thumbnail image is replaced with the standard YouTube video player with autoplay set to 1 so the plays the video instantly. The advantage is that the extra YouTube JavaScript gets loaded only when someone decides to watch the embedded video and not otherwise.

Light and Responsive YouTube Embeds

The standard embed code for YouTube uses the IFRAME tag and the width and height of the video player are hard-coded thus making the player non-responsive.
The new on-demand embed code for YouTube is slightly different. You need not specify the player size as we are now embedding the video responsively. Also, the IFRAME is replaced with a DIV tag and the IFRAME is added to the page only when the visitor clicks the play button.
YouTube Embed Code

Embed YouTube Videos Responsively – Tutorial

Copy-paste the following snippet anywhere in your web page where you would like the YouTube video to appear. Remember to replace VIDEO_ID with the actual ID of the YouTube video.
  1. <div class="youtube-player" data-id="VIDEO_ID"></div>
We will not assign height and width since the video player will automatically occupy the width of the parent while the height is auto-calculated. You can paste multiple DIV blocks with different video IDs if you need to embed multiple videos on the same page.
Next, place the JavaScript anywhere in your web template. It finds all embedded videos on a web page and then replaces the DIV elements with the video thumbnails.
  1. <script>
  2.  
  3. /* Light YouTube Embeds by @labnol */
  4. /* Web: http://labnol.org/?p=27941 */
  5.  
  6. document.addEventListener("DOMContentLoaded",
  7. function() {
  8. var div, n,
  9. v = document.getElementsByClassName("youtube-player");
  10. for (n = 0; n < v.length; n++) {
  11. div = document.createElement("div");
  12. div.setAttribute("data-id", v[n].dataset.id);
  13. div.innerHTML = labnolThumb(v[n].dataset.id);
  14. div.onclick = labnolIframe;
  15. v[n].appendChild(div);
  16. }
  17. });
  18.  
  19. function labnolThumb(id) {
  20. var thumb = '<img src="https://i.ytimg.com/vi/ID/hqdefault.jpg">',
  21. play = '<div class="play"></div>';
  22. return thumb.replace("ID", id) + play;
  23. }
  24.  
  25. function labnolIframe() {
  26. var iframe = document.createElement("iframe");
  27. var embed = "https://www.youtube.com/embed/ID?autoplay=1";
  28. iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
  29. iframe.setAttribute("frameborder", "0");
  30. iframe.setAttribute("allowfullscreen", "1");
  31. this.parentNode.replaceChild(iframe, this);
  32. }
  33.  
  34. </script>
Finally, paste the CSS before the closing head tag of your web template.
This method will reduce the size of your web pages by 500 KB while making your site mobile friendly. You may refer to the annotated code to understanding how on-demand embedding works.

  1. <style>
  2. .youtube-player {
  3. position: relative;
  4. padding-bottom: 56.23%;
  5. /* Use 75% for 4:3 videos */
  6. height: 0;
  7. overflow: hidden;
  8. max-width: 100%;
  9. background: #000;
  10. margin: 5px;
  11. }
  12. .youtube-player iframe {
  13. position: absolute;
  14. top: 0;
  15. left: 0;
  16. width: 100%;
  17. height: 100%;
  18. z-index: 100;
  19. background: transparent;
  20. }
  21. .youtube-player img {
  22. bottom: 0;
  23. display: block;
  24. left: 0;
  25. margin: auto;
  26. max-width: 100%;
  27. width: 100%;
  28. position: absolute;
  29. right: 0;
  30. top: 0;
  31. border: none;
  32. height: auto;
  33. cursor: pointer;
  34. -webkit-transition: .4s all;
  35. -moz-transition: .4s all;
  36. transition: .4s all;
  37. }
  38. .youtube-player img:hover {
  39. -webkit-filter: brightness(75%);
  40. }
  41. .youtube-player .play {
  42. height: 72px;
  43. width: 72px;
  44. left: 50%;
  45. top: 50%;
  46. margin-left: -36px;
  47. margin-top: -36px;
  48. position: absolute;
  49. background: url("//i.imgur.com/TxzC70f.png") no-repeat;
  50. cursor: pointer;
  51. }
  52.  
  53. </style>
Please do note that Chrome and Safari browsers on iPhone and Android only allow playback of HTML5 video when initiated by a user interaction. They block embedded media from automatic playback to prevent unsolicited downloads over cellular networks.

YouTube Embed Tutorials

  1. Embed a YouTube Video with Sound Muted
  2. Place YouTube Video as your Webpage Background
  3. Embed Just a Portion of a YouTube Video

Tuesday, 10 January 2017

Building responsive IFrames

To scale a YouTube video to fit any browser or tablet/mobile device for responsive design you can use either a CSS or Javascript solution to accomplish the goal.
This example uses a CSS solution, you need to add a couple of CSS styles in your main CSS file.

The Process

The first thing that you need is the YouTube iframe embed code, grab that in your video share options from the YouTube site.
Place the iframe code on your page, in this example above the iframe's container div (div.youtubevideowrap) has 2 CSS declarations for width, it has a width:80% and a max-width:640px.
This makes the content fluid as a percentage unit is used, it also has a max-width set as I don't want the video displaying wider than 640px
From here we need to add in a HTML container around the video and declare the CSS.

Setting Up the HTML and CSS Styles

Create a container div around the iframe code and give it a class eg:
<div class="video-container"><iframe.......></iframe></div>
Add in the CSS:
.video-container {
 position:relative;
 padding-bottom:56.25%;
 padding-top:30px;
 height:0;
 overflow:hidden;
}

.video-container iframe, .video-container object, .video-container embed {
 position:absolute;
 top:0;
 left:0;
 width:100%;
 height:100%;
}
The first CSS declarations target the video container and the second target what is in the container, in this case it's the iframe, you can also apply this to objects and embed elements.
That's it the video will now scale as the viewport is resized, the magic element is the padding-bottom rule of 56.25%, this figure is reached by using the video's aspect ratio of 16*9, so 9 divided by 16 = 0.5625 or 56.25%, full explanation in alistapart article.
This same process can also be achieved using a javascript technique explained in the Vimeo video guide, which plays just as friendly with YouTube videos.


 Proportionally resizing the site content to another device can be frustrating!


Building responsive IFrames can be frustrating. Don't let an IFrame break your beautifully laid out responsive site. Learn how to build responsive IFrames the right way with just a few lines of CSS.

You’ve spent countless hours designing and building the perfect responsive site. One problem — iframes. Proportionally resizing these pesky little windows to another world can be frustrating. It’s easy enough to make an iframe’s width span 100% of its container. Rather, making the height resize accordingly can be tricky.
So how do you keep from blowing your top trying to make responsive iframes?


Most implementations I’ve seen require the use of JS, like Pym.js. This can cause unexpected behavior depending on the device and browser version — Pym.js only goes back to IE9. Wouldn’t it be nice if there was a way to make responsive iframes without using a dirty little code snippet? There is! It’s name is the CSS intrinsic ratio technique — or what I like to call, ‘Magic iframes!’.

Responsive IFrames Demo

With just a little CSS and HTML, you can build a responsive iframes — even if the dimensions vary! Check out the demo below:

Responsive IFrames Using CSS

In Resize Videos Proportionally, we learned how to use the intrinsic ratio technique to make your embedded videos responsive. We’ll use that same method and apply it to make any iframe, YouTube & Vimeo video or Google Map responsive. Only dependency is you know the aspect ratio (width x height) of the iframe.
When embedding iframes for content such as videos, most services like YouTube and Vimeo will provide you a snippet of code like the one below:
1
<iframe width="560" height="315" src="//www.youtube.com/embed/KMYrIi_Mt8A" allowfullscreen></iframe>
Notice I removed the frameborder attribute. If you’re using HTML5, that attribute is no longer supported.

The Code

First of all, remove the width and height attributes. Keeping those attributes forces the content to stay at that size regardless of the screen size. This causes problems in responsive layouts when the screen size is smaller than the width of the iframe. Though we could use CSS to force the size, why have them if their not being used — less code is beautiful code.
1
<iframe src="//www.youtube.com/embed/KMYrIi_Mt8A" allowfullscreen></iframe>
Next, let’s add a container with a class around the iframe:
1
2
3
<div class="intrinsic-container">
  <iframe src="//www.youtube.com/embed/KMYrIi_Mt8A" allowfullscreen></iframe>
</div>
Now, we add a little touch of CSS magic to make the iframe responsive. Same way we did in the “Resize Videos Proportionally with Intrinsic Ratios”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.intrinsic-container {
  position: relative;
  height: 0;
  overflow: hidden;
}
/* 16x9 Aspect Ratio */
.intrinsic-container-16x9 {
  padding-bottom: 56.25%;
}
/* 4x3 Aspect Ratio */
.intrinsic-container-4x3 {
  padding-bottom: 75%;
}
.intrinsic-container iframe {
  position: absolute;
  top:0;
  left: 0;
  width: 100%;
  height: 100%;
}
IMPORTANT: Don’t forget to apply a aspect ratio class to your iframe. If you don’t, it could cause the iframe to disappear.
That it! Simple, huh? Your iframe should now proportionally resize based on the browser size. Here’s a breakdown of how it works:

Breaking It Down

  • It’s key to specify the container’s position to be relative. This allows us to absolutely position the iframe within it, which is needed to make it responsive.
  • The padding-bottom value is calculated based on the aspect ratio of your content. Instead of adding it to the intrinsic-container class, we added separate classes that can be appended to that element depending on the type of content you’re embedding. I prefer doing this so I’m not duplicating the container code for different aspect ratios. To find the aspect ratio of a container, use this formula: height ÷ width = aspect ratio
  • height is set to 0 because padding-bottom gives the iframe it’s height.
  • Using overflow: hidden is important because it ensures if any content does protrude outside of the container, it will be hidden and avoid screwing up the sites layout.
  • Like with most absolute positioned elements, we need to set the top and left properties so the iframe get’s put in the right place.
  • Finally, width and height are set to 100% so the iframe takes up 100% of the containers space.

Using SASS?

If you’re using SASS, use this function to find the ratio or padding-bottom of the parent container:
1
2
3
4
5
6
7
/**
 * Ratios
 * Returns the ratio for specified dimensions.
 */
@function ratio($width, $height) {
  return percentage( $height / $width);
}
Taking that one step further, you can create a mixin to generate ratio classes:
1
2
3
4
5
6
7
8
9
10
11
@mixin generateRatios($width, $height, $prefix: "ratio-") {
  $class-name: $prefix + $width + "x" + $height;
  .#{$class-name} {
    padding-bottom: ratio($width, $height);
  }
  // Output example: .ratio-16x9 {}
}
@include generateRatios(16,9); // 16x9
@include generateRatios(4,3);  // 4x3
We can use this same technique to make other types of embedded content responsive like Google Maps & Calendars. Basically, anything that uses a iframe using only CSS! If you don’t have access to edit the site stylesheets directly, here’s a nifty tool that will generate responsive embed codes for you.

Responsive IFrames Using JS

What if you don’t know the aspect ratio? Let’s say you have content authors creating interactives with each having different dimensions. Without knowing the aspect ratio of the iframe, it’s not easy to implement the intrinsic ratio technique.
You can overcome this problem by using JS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Find all iframes
var $iframes = $( "iframe" );
// Find &#x26; save the aspect ratio for all iframes
$iframes.each(function () {
  $( this ).data( "ratio", this.height / this.width )
    // Remove the hardcoded width &#x26; height attributes
    .removeAttr( "width" )
    .removeAttr( "height" );
});
// Resize the iframes when the window is resized
$( window ).resize( function () {
  $iframes.each( function() {
    // Get the parent container&#x27;s width
    var width = $( this ).parent().width();
    $( this ).width( width )
      .height( width * $( this ).data( "ratio" ) );
  });
// Resize to fix all iframes on page load.
}).resize();

Let’s Sum It Up!

Say Goodbye to embedded content breaking your layouts with the intrinic ratio technique. We’ve walked through how just a little bit of code can easily make your iframes and other embedded content responsive friendly.
How do you embed third-party content on your responsive website? Do you have a nifty technique or trick to accomplish responsive embedded content? What about your workflow for embedding content like Google Maps, YouTube, etc? I’d love to hear from you. Comment below with your thoughts.

Additional Resources

Check out these other great articles about making embedded content responsive:

Different Type of Youtube video, Play It to see

Each of them act Differently when playing



all = Normal Type
**dd**iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/KiZmmfdUtdg?rel=0" width="560">**dd**/iframe>


no title
**dd**iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/KiZmmfdUtdg?rel=0&showinfo=0" width="560">**dd**/iframe>


no title + no control
**dd**iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/KiZmmfdUtdg?rel=0&controls=0&showinfo=0" width="560">**dd**/iframe>



full https://
www.youtube.com/embed/12dis_5T0_Q?rel=0&&loop=1&list=PLGkukbLMjgMdqnq-k5qAqGqpknBWza0iM


mobile size without css
**dd**div class="separator" style="clear: both; overflow: hidden; position: relative; text-align: center;"> **dd**iframe allowfullscreen="" frameborder="0" height="270" src="https://www.youtube.com/embed/GkoSCpI3RSE?rel=0&&loop=1&list=PLGkukbLMjgMdqnq-k5qAqGqpknBWza0iM" width="480">**dd**/iframe> **dd**/div>




mobile size with css + size
**dd**div class="video-container"> **dd**iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/GkoSCpI3RSE?rel=0&&loop=1&list=PLGkukbLMjgMekF3TfZvdI_an73havrHLN" width="560">**dd**/iframe> **dd**/div>




mobile size with css
**dd**div class="video-container"> **dd**iframe allowfullscreen="" src="https://www.youtube.com/embed/Qkoz_N6qMDk?rel=0&&loop=1&list=PLGkukbLMjgMdqnq-k5qAqGqpknBWza0iM">**dd**/iframe>**dd**/div>



Responsive web content for mobile or any Design


A few HTML elements don’t play nice with responsive layouts. One of these is the good ol’ iframe, which you may need to use when embedding content from external sources such as YouTube.
In this article, we’ll show you how to make embedded content responsive using CSS, so that content such as video and calendars resize with the browser’s viewport. For those occasions when non-coders will be embedding video on your website and you don’t want to rely on them adding extra markup, we’ll also look at a solution that uses JavaScript instead of CSS.
(You can see a demo of these techniques on my blog.)
Note: This technique was originally detailed in Thierry Koblenz’s excellent tutorial ‘Creating Intrinsic Ratios for Video‘. I’ve used techniques I learned from his tutorial and expanded on them here for additional content types such as calendars.

The Markup For Embedded Content

Services such as YouTube provide code that you can copy and paste into your own website to embed content. I tend to recommend to my clients that they host video with YouTube because it will save them server space and, regardless of the user’s browser or device, YouTube will display the video correctly. The two main ways to embed video on a website are the HTML5 video element, which doesn’t work in legacy versions of Internet Explorer, and Flash, which doesn’t work on iOS devices and isn’t standards-compliant.
When you embed content from an external source, the code will include an iframe:
<iframe src="http://www.youtube.com/embed/4aQwT3n2c1Q" height="315" width="560" allowfullscreen="" frameborder="0"></iframe>
This iframe enables external content to be displayed on your website, because it includes a URL that points to the source of the streamed content.
However, you’ll notice that our iframe includes width and height attributes. Remove these and the iframe will disappear because it would have no dimensions. And you can’t fix this in your style sheet, unfortunately.
The width attribute means that, on a screen narrower than 560 pixels, the embedded content will protrude outside of its containing element, breaking the layout. In the example below, I’ve added the code above to a page of my blog. The screenshot is taken from an iPhone in portrait mode (320 pixels wide), and the rest of the page has been shrunk so that the embedded content fits the screen. Far from ideal!
Video breaks the layout in a responsive website on iPhone
Screenshot taken from an iPhone in portrait mode (320px wide). (Larger view)
Fortunately, there is a way around this using CSS. First, I’ll show you how to do this with embedded video, and then with calendars.

Responsive Video Link

The Markup Link

To make embedded content responsive, you need to add a containing wrapper around the iframe. Your markup would be as follows:
<div>
    <iframe src="http://www.youtube.com/embed/4aQwT3n2c1Q" height="315" width="560" allowfullscreen="" frameborder="0">
    </iframe>
</div>
The next step is to add styling to this new wrapper and the iframe within it.

The CSS Link

First, we style the containing wrapper with the .video-container class. As proposed by Thierry Koblentz in his ALA article “Creating Intrinsic Ratios For Video”, we can use the following snippet in our style sheet:
.video-container {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 35px;
    height: 0;
    overflow: hidden;
}
This does a few things:
  • Setting the position to relative lets us use absolute positioning for the iframe itself, which we’ll get to shortly.
  • The padding-bottom value is calculated out of the aspect ratio of the video. In this case, the aspect ratio is 16:9, which means that the height will be 56.25% of the width. For a video with a 4:3 aspect ratio, we set padding-bottom to 75%.
  • The padding-top value is set to 30 pixels to allow space for the chrome — this is specific to YouTube videos.
  • The height is set to 0 because padding-bottom gives the element the height it needs. We do not set the width because it will automatically resize with the responsive element that contains this div.
  • Setting overflow to hidden ensures that any content protruding outside of this element will be hidden from view.
We also need to style the iframe itself. Add the following to your style sheet:
.video-container iframe {
    position: absolute;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
}
This targets iframes inside containers with the .video-container class. Let’s work through the code:
  • Absolute positioning is used because the containing element has a height of 0. If the iframe were positioned normally, we would have given it a height of 0 as well.
  • The top and left properties position the iframe correctly in the containing element.
  • The width and height properties ensure that the video takes up 100% of the space used by the containing element (which is actually set with padding).
Having done this, the video will now resize with the screen’s width. Here’s how it will look on a desktop:
Responsive video as seen on the desktop (in the flow of the content, fitting nicely)
Desktop screenshot of the video resizing the screen’s width. (Larger view)
And here’s how it will look on a screen that is 320 pixels wide:
Responsive video fits into the layout on a 320px wide screen
The video on a 320 pixels wide screen. (Larger view)
Let’s move on to other sources of embedded content — specifically, Google calendars.
Today, too many websites are still inaccessible. In our new book Inclusive Design Patterns, we explore how to craft flexible front-end design patterns and make future-proof and accessible interfaces without extra effort. Hardcover, 312 pages. Get the book now!

Responsive Calendar Link

The Markup Link

The CSS to make any form of embedded content responsive is essentially the same, but different content will have different aspect ratios, which means you’ll need to set the padding-bottom value accordingly.
Below is a screenshot of a website that I manage for a primary school, a website that embeds a Google calendar. As you can see, the calendar breaks the layout on a small screen. In this case, the website is displayed at the correct width, but the calendar goes beyond the screen’s width.
A celandar as seen on a responsive website on an iPhone - not all of the calendar is visible
The calendar breaks the layout on a small screen. (Larger view)
The markup for the embedded calendar is as follows:
<iframe src="https://www.google.com/calendar/embed?height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=60aqhusbghf7v0qvvbfu1ml22k%40group.calendar.google.com&amp;color=%232952A3&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>
To make a calendar responsive, add a div with a class of .calendar-container to contain the iframe:
<div>
    <iframe src="https://www.google.com/calendar/embed?height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=60aqhusbghf7v0qvvbfu1ml22k%40group.calendar.google.com&amp;color=%232952A3&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no">
    </iframe>
</div>
The next step is to style this div.

The CSS Link

The CSS for a calendar is almost identical to the CSS for a video, with two exceptions: The aspect ratio will be different, and padding-top isn’t needed.
Add the following to your style sheet:
.calendar-container {
    position: relative;
    padding-bottom: 75%;
    height: 0;
    overflow: hidden;
}
In this case, the iframe is 800 pixels wide and 600 pixels high, which gives us an aspect ratio of 4:3. So, set padding-bottom to be 75%.
Having done this, we need to apply the same styling to the iframe element in this new container:
.calendar-container iframe {
    position: absolute;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
}
This is exactly the same styling that we applied to the video.
Now, the calendar will resize with the browser window, as shown here in Opera Mobile on an Android phone:
A responsive calendar fits into the width of a 320px wide screen
As long as you remember to wrap your embedded calendars and videos with the appropriate containing element, then this CSS will work for any new videos and calendars that you add to your website.
The problem, however, is that although you can fit the whole calendar on a page, it’s still almost unusable because click targets are so small and no information is visible. If you absolutely have to display Google Calendar, you can, but if you can use more usable responsive calendar solutions like simple CSS (setting display: block on table rows, for example), or w3widgets Responsive calendar or Calendario for your own calendars, your users might appreciate it.

Responsive Video With CSS or JavaScript Link

If you’re developing a responsive website using a content management system, then one or more of the website’s editors will probably have to embed video at some point. You can point your editors to EmbedResponsively.com which generates responsive <embed> codes for embedding rich third-party media with one click, with CSS alone. Alternatively, you could use a JavaScript solution, to relieve nervous editors from having to add extra CSS and markup. However, as long as you can avoid this path, the better, of course.
Until recently, most solutions were plugins, which are OK to an extent but can have performance issues. A popular plugin is FitVids.js, developed by Chris Coyier and Paravel.
A more current solution is to use just a script — such as FluidVids.js, developed by Todd Motto. FluidVids.js is simple to use:
  1. Download the script (ZIP) from GitHub and upload it to your server with the same folder structure that the downloaded files come in. This will place the script itself in a folder named dist.
  2. Call the script in each page’s <head> section with the following code:
<script src="dist/fluidvids.js></script>
That’s all you need to do to make videos resize on all devices that support JavaScript. It works not only for YouTube, but for Vimeo, too. The problem, however, is that if you users don’t have JavaScript support or the JavaScript hasn’t loaded yet or JavaScript hasn’t loaded correctly, the only fallback you can use is to add the following to your style sheet:
iframe {
    max-width: 100%; 
}
This will ensure that video resizes to the width of the browser’s window. But it won’t resize the video’s height; unfortunately, iframe just doesn’t work this way. So, the video won’t break your layout, but it won’t look very good either. This is not really a good option, so if you can avoid using JavaScript for videos, it’s a good idea to do so.

Responsive Google Maps Link

Apart from videos and calendars, another common issue is the embedding of Google maps, responsively. Basically, we again use the same intrinsic ratio technique, and when setting padding-bottom for the wrapper, we just divide the height by width and add the aspect ratio in CSS.
Usually, the code generated by Google Maps would look like this:
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d3022.260812859955!2d-73.990184!3d40.756288!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xb134c693ac14a04c!2sThe+TimesCenter!5e0!3m2!1sen!2suk!4v1393485725496" width="500" height="450" frameborder="0" style="border:0"></iframe>
We just wrap a div around the iframe and apply the familiar CSS styling to it:
.google-maps {
    position: relative;
    padding-bottom: 90%; // (450 ÷ 500 = 0.9 = 90%)
    height: 0;
    overflow: hidden;
}
.google-maps iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
And this is what the markup will look like:
<div class="google-maps">
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m12!1m3!1d7098.94326104394!2d78.0430654485247!3d27.172909818538997!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!5e0!3m2!1sen!2s!4v1385710909804" width="500" height="450" frameborder="0" style="border:0"></iframe>
</div>
Voilá! Again, we can just use EmbedResponsively to generate the copy-paste-code with one click.

Summary

Embedded content has a habit of breaking responsive layouts, because it’s contained in an iframe with a fixed width. In this article, we’ve seen how to add a single containing wrapper, and some CSS, to ensure that all embedded content contained in an iframe resizes with the browser’s window.
Sometimes it’s good enough, but sometimes you might need to come up with more advanced solutions, since resizing isn’t always a solution. We’ve also looked at embed code generators and alternative solutions, using JavaScript, which might be necessary in some cases, especially if editors have to deal with many videos and don’t have technical skills required, or CMS doesn’t allow for inline styling.
How do you embed third-party content on your responsive websites? Have you discovered any particular tricks or techniques? What does your workflow for embedding such content look like?

Making Embedded Content Work In Responsive Design

Get paid to share your links!