{"id":1954,"date":"2012-05-18T11:48:56","date_gmt":"2012-05-18T09:48:56","guid":{"rendered":"http:\/\/www.nyamsprod.com\/blog\/?p=1954"},"modified":"2012-05-18T15:41:10","modified_gmt":"2012-05-18T13:41:10","slug":"background-image-and-textarea","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/background-image-and-textarea\/","title":{"rendered":"Background Image and Textarea"},"content":{"rendered":"<div class=\"message warning\">\n<p><strong>Attention:<\/strong> Les informations de ce billet sont susceptibles d'&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans.<\/p>\n<p><strong>Warning: <\/strong> The information you are reading may be obsolete, this post was published more than 2 years ago.<\/p>\n<\/div><p>Using a single background image on a <code>textarea<\/code> to change its presentation is no so trivial after all. Modern browser engine (Gecko and Webkit, for now) allow <code>textarea<\/code> to be resize by the user. This resize that I&#8217;ve already talk about in another post can easily break your design if you don&#8217;t adapt your CSS . Here&#8217;s a technique that I use to mitigate the new behaviour in modern browser.<\/p>\n<p>To start with, here is a simple rule that should work on any browser (IE8+).<\/p>\n<pre lang=\"css\">textarea {\r\n\t-moz-box-sizing:border-box;\r\n\tbox-sizing:border-box;\r\n\twidth:276px;\r\n\theight:172px;\r\n\tpadding:20px;\r\n\toverflow:auto;\r\n\tborder:none;\r\n\tbackground:#fff url(background.png) no-repeat center scroll;\r\n}<\/pre>\n<p>The problem with this rule is that in modern browsers you can resize the <code>textarea<\/code> tag as you wish, which means that on resize my image design will break since the background image will not adapt to the resized <code>textarea<\/code>.<!--more--><\/p>\n<h2>using CSS3 resize property<\/h2>\n<p>If you don&#8217;t want your user to break your design by resizing the <code>textarea<\/code> the simplest solution it&#8217;s to disallow the resize through CSS. this solution is simple, effective and backward compatible. The only problem with this solution is that you remove a very handy functionality from your website only because you don&#8217;t know how to work with it \ud83d\ude41 .<\/p>\n<pre lang=\"css\">textarea {\r\n\t-moz-box-sizing:border-box;\r\n\tbox-sizing:border-box;\r\n\twidth:276px;\r\n\theight:172px;\r\n\tpadding:20px;\r\n\toverflow:auto;\r\n\tborder:none;\r\n\tbackground:#fff url(background.png) no-repeat center scroll;\r\n\t<strong><em>-moz-resize:none; resize:none;<\/em><\/strong>\r\n}<\/pre>\n<h2>using CSS3 Background module<\/h2>\n<p>My first attempt to find a simple solution to my problem was to use the CSS3 property: <a title=\"Background-size on the MDN website\" href=\"https:\/\/developer.mozilla.org\/en\/CSS\/background-size\" target=\"_blank\">background-size<\/a>. Which means that I just had to add a new property to my rule.<\/p>\n<pre lang=\"css\">textarea {\r\n\t-moz-box-sizing:border-box;\r\n\tbox-sizing:border-box;\r\n\twidth:276px;\r\n\theight:172px;\r\n\tpadding:20px;\r\n\toverflow:auto;\r\n\tborder:none;\r\n\tbackground:#fff url(background.png) no-repeat center scroll;\r\n\t<strong><em>background-size:100% 100%;<\/em><\/strong>\r\n}<\/pre>\n<p>In doing so, I know that my image will <em>automagically<\/em> adapt to the <code>textarea<\/code> resize. But I&#8217;ve introduced a new problem, the image ratio is never preserved which means that on resize, the image can be deformed and the result is not quite what I wanted. So even if this solution works and is backward compatible, the result is not great. So my suggestion is to avoid this technique. You may try to work around the problem using the min\/max-height properties as well as the min\/max-width property to further <a title=\"Re-styling the textarea tag in recent browsers\" href=\"http:\/\/www.nyamsprod.com\/blog\/2011\/styling-textarea-tag\/\">style the textarea in modern browser.<\/a> But the loss of ratio is too important to be overlooked.<\/p>\n<h2>Using CSS3 Border module<\/h2>\n<p>Since I was not happy with this method, I tried another approach by using the CSS3 border module property: <a title=\"Border-image on the MDN website\" href=\"https:\/\/developer.mozilla.org\/en\/CSS\/border-image\" target=\"_blank\">border-image<\/a>. With this property, the image is sliced and repeated on the tag border <a title=\"Border-image explained\" href=\"http:\/\/css-tricks.com\/understanding-border-image\/\" target=\"_blank\">following some specific rules<\/a>. So whenever you resize the <code>textarea<\/code>, the image ratio remains intact and you finally get the resulting effect you wanted. You may think that everything is fine &#8230; but wait , this is CSS3 we&#8217;re talking about.<\/p>\n<pre lang=\"css\">textarea {\r\n\twidth:276px;\r\n\theight:172px;\r\n\toverflow:auto;\r\n\tborder-width:5x;\r\n\tborder-type:solid;\r\n\tborder-color:transparent;\r\n\t<strong><em>-webkit-border-image:url(textarea.png) 5 repeat;<\/em><\/strong>\r\n\t<strong><em>-moz-border-image:url(textarea.png) 5 repeat;<\/em><\/strong>\r\n\t<strong><em>-ms-border-image:url(textarea.png) 5 repeat;<\/em><\/strong>\r\n\t<strong><em>-o-border-image:url(textarea.png) 5 repeat;<\/em><\/strong>\r\n\t<strong><em>border-image:url(textarea.png) 5 repeat;<\/em><\/strong>\r\n\t<strong><em>background-color:#fff;<\/em><\/strong>\r\n}<\/pre>\n<p>Some points to consider:<\/p>\n<ul>\n<li>I used the border module not the background module so we do not use the <code>background-image<\/code> property.<\/li>\n<li>Since the border-image is relatively new <a title=\"When can I use border-image\" href=\"http:\/\/caniuse.com\/#search=border-image\" target=\"_blank\">it is not supported on IE browsers<\/a>. Which means that on old browser and in IE, you&#8217;ll see no background image and no border either, so we need to create a fallback solution.<\/li>\n<\/ul>\n<h2><del>Modernizr to the rescue<\/del><\/h2>\n<p><del>To try to mitigate these issues, I had to rely on the excellent <a title=\"is an open-source JavaScript library that helps you build the next generation of HTML5 and CSS3-powered websites.\" href=\"http:\/\/www.modernizr.com\" target=\"_blank\">javascript framework modernizr<\/a> which can detect browser support for the <code>border-image<\/code> property. With modernizr help, I only switch to the modern border module way only when possible. So When IE support the <code>border-image<\/code> property it will <em>automagically<\/em> switch to the border image module way.<\/del><\/p>\n<h2>Always read the Spec<\/h2>\n<p>After reading again the specification for the border-image property, in turns out that you can achieve a backward compatibility rendering without Javascript \ud83d\ude42 . The border-image has a value called fill which specify the following :<\/p>\n<blockquote>\n<dl>\n<dt>fill<small> (optional)<\/small><\/dt>\n<dd>If the &#8220;fill&#8221; keyword is present, the middle part of the border-image is preserved, and drawn as the background-image of the image.<\/dd>\n<\/dl>\n<\/blockquote>\n<p>So by using this <code>fill<\/code> value in border-image supporting browser we draw the image middle part above the declared <code>background-image<\/code>. By doing so we can specify both rules and gain a fallback behaviour \ud83d\ude42 .<\/p>\n<pre lang=\"css\">.full {\r\n\t-moz-box-sizing:border-box;\r\n\tbox-sizing:border-box;\r\n\twidth:276px; height:172px; overflow:auto;\r\n\tbackground:#fff url(pattern.png) no-repeat center scroll;\r\n\tborder:20px solid transparent;\r\n    \/* Early Implementations the default behaviour was fill and\/or fill was not supported *\/\r\n    -webkit-border-image:url(pattern.png) 20 repeat;\r\n       -moz-border-image:url(pattern.png) 20 repeat;\r\n         -o-border-image:url(pattern.png) 20 repeat;\r\n    \/* new implementation: fill must be specified *\/\r\n    -webkit-border-image:url(pattern.png) 20 fill repeat;\r\n       -moz-border-image:url(pattern.png) 20 fill repeat;\r\n         -o-border-image:url(pattern.png) 20 fill repeat;\r\n    \/* standard specification *\/\r\n            border-image:url(pattern.png) 20 fill repeat;\r\n}<\/pre>\n<h2>Caveats<\/h2>\n<ul>\n<li><del>First, you need to have your JavaScript activated to make this solution works. Always keep this in mind.<\/del><\/li>\n<li><del>Second,<\/del> if IE start supporting the <code>textarea<\/code> resize attribute without supporting the <code>border-image<\/code> CSS property, my Solution won&#8217;t work since the two could be implemented separatly in a browser.<\/li>\n<li>Because we&#8217;re using 2 different css modules, the scrollbar position changes depending on the solution used. You may view theses changes when comparing my test page rendering in Opera.<\/li>\n<\/ul>\n<p>To summarize my findings I&#8217;ve created a <a title=\"Styling Textarea with a backgroudn Image\" href=\"http:\/\/www.nyamsprod.com\/test\/textarea\/\">test page with all the techniques explained<\/a> here with some comments to explain what I did. Feel free to use and abuse this page \ud83d\ude42 .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to style the textarea tag with a background image in modern browser that support texteara resize<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[58,723,722,140,220,273,286,404,721,521,567],"class_list":["post-1954","post","type-post","status-publish","format-standard","hentry","category-web","tag-background","tag-background-size","tag-border-image","tag-css3","tag-gecko","tag-image","tag-internet-explorer","tag-opera","tag-resize","tag-textarea","tag-webkit"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/1954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/comments?post=1954"}],"version-history":[{"count":5,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/1954\/revisions"}],"predecessor-version":[{"id":1981,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/1954\/revisions\/1981"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=1954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=1954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=1954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}