6

I was using simple textarea element and then replaced it with iframe with designMode='on' to give user the possibility to mark some text and make it italic. But I still want an iframe to look like textarea, so I need a border around it similar to that which appears in Chrome and Safari when textarea is active. How can I achieve such an effect?

Danylo Mysak
  • 1,514
  • 2
  • 16
  • 22

4 Answers4

18

You can get the rounded outline in webkit like this:

outline: 2px auto red;

Notice that the width of the outline will not obey the specified width, and the color isn't completely accurate either.

To use the normal focus color, you can do this:

outline: 2px auto -webkit-focus-ring-color;

In moz, you can use -moz-outline-radius (works just like border-radius) to get a rounded outline.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • -webkit-focus-ring-color is just what was needed. Unfortunately the constants like -webkit-focus-ring-width and -webkit-focus-ring-offset seem to be missing. – Danylo Mysak Sep 27 '10 at 10:18
  • As you can see, Sarari renders 'outline: 2px auto -webkit-focus-ring-color' pretty differently: http://matholymp.org.ua/_temp/outline.png (the upper one is original 5px width -2px offset, the lower is 2px width). – Danylo Mysak Sep 27 '10 at 10:30
3

You can use the :focus psuedo-selector and the outline property:

`.elementClass:focus {
    outline: 1px solid #ffa;
}

This will add a yellow outline to the element, I'm not sure what colour Chrome and Safari uses, but just add your preferred colour-to-taste.


Edited in response to OP's comment:

Well, unfortunately this kind of border is different in Chrome and Safari (and, perhaps, in other browsers which support or will support it). So it would be perfect if I could simulate exactly that kind of border that each individual user is used to.

There are some platform/OS-specific colours available in CSS (though browser implementation, obviously, varies):

+----------------------+------------------------------------------------------------------+
|  ActiveBorder        |                                            Active window border  |
|  ActiveCaption       |                                           Active window caption  |
|  AppWorkspace        |                 Background color of multiple document interface  |
|  Background          |                                              Desktop background  |
|  ButtonFace          |                              Face color for 3D display elements  |
|  ButtonHighlight     |    Dark shadow for 3D display elements (facing away from light)  |
|  ButtonShadow        |                            Shadow color for 3D display elements  |
|  ButtonText          |                                            Text on push buttons  |
|  CaptionText         |              Text in caption, size box, and scrollbar arrow box  |
|  GrayText            |            Grayed (disabled) text (#000 if not supported by OS)  |
|  Highlight           |                                   Item(s) selected in a control  |
|  HighlightText       |                           Text of item(s) selected in a control  |
|  InactiveBorder      |                                          Inactive window border  |
|  InactiveCaption     |                                         Inactive window caption  |
|  InactiveCaptionText |                            Color of text in an inactive caption  |
|  InfoBackground      |                           Background color for tooltip controls  |
|  InfoText            |                                 Text color for tooltip controls  |
|  Menu                |                                                 Menu background  |
|  MenuText            |                                                   Text in menus  |
|  Scrollbar           |                                            Scroll bar gray area  |
|  ThreeDDarkShadow    |                             Dark shadow for 3D display elements  |
|  ThreeDFace          |                              Face color for 3D display elements  |
|  ThreeDHighlight     |                         Highlight color for 3D display elements  |
|  ThreeDLightShadow   |          Light color for 3D display elements (facing the light)  |
|  ThreeDShadow        |                             Dark shadow for 3D display elements  |
|  Window              |                                               Window background  |
|  WindowFrame         |                                                    Window frame  |
|  WindowText          |                                                 Text in windows  |
+----------------------+------------------------------------------------------------------+

Source: http://blogs.sitepoint.com/2009/08/11/css-system-styles/

I'm not aware, though, of any browser-specific options that could be applied. You could, perhaps, useJavaScript to find the colour from a particular browser, but I'm not convinced that would work, due to the difficulty of accessing the pseudo-selectors.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • Well, unfortunately this kind of border is different in Chrome and Safari (and, perhaps, in other browsers which support or will support it). So it would be perfect if I could simulate exactly that kind of border that each individual user is used to. – Danylo Mysak Sep 26 '10 at 17:27
  • Thank you for the idea to use JavaScript to find the color of the border. I used it and solved my problem. And that’s quite interesting that some OS-specific colors are available in CSS although it’s hard to think of application. – Danylo Mysak Sep 27 '10 at 07:56
  • @Danylo, I can think of a few reasons people might want to imitate platform-specific alerts...possibly it'd be a good idea for web-apps too, to make them look more at-home? What JS did you use to find the browser-specific colours? – David Thomas Sep 27 '10 at 09:28
  • I used the code similar to this: **[var textarea=document.createElement('textarea'); document.body.appendChild(textarea); textarea.focus(); var color=window.getComputedStyle(textarea,null).getPropertyValue('outline-color');]** And I described the approach in more details in my answer below. – Danylo Mysak Sep 27 '10 at 10:01
2

I found an article that mentions browser specific colours for FireFox and for Safari/Chrome.

I was trying to read the focus ring colour in javascript, so I could soften it and use it within our UI, but I gave up. Here was the test code I was playing with:

<!DOCTYPE HTML>
<html>
    <head></head>
    <body>
        <div id="hellowebkit" style="outline: 5px auto -webkit-focus-ring-color;">outline: 5px auto -webkit-focus-ring-color</div>
        <div style="outline: 5px auto green;">outline: 5px auto green</div>
        <div style="outline: 5px auto -moz-mac-focusring;">outline: 5px auto -moz-mac-focusring</div>
        <div style="background-color:-webkit-focus-ring-color;">-webkit-focus-ring-color</div>
        <div style="background-color:-moz-mac-focusring;">-moz-mac-focusring</div>
        <div id="hello" style="color:highlight;">hello</div>
        <button id="btn" onclick="readval()">readval()</button>
        <button id="btn" onclick="readPropertyVal()">readPropertyVal()</button>
        <input id="inp" value="input" />
        <div id="test">test</div>
        <script>
            function readval() {
                    var hello = document.getElementById('hello');
                    var style = hello.currentStyle || getComputedStyle(hello, null);
                    var color = style.color;
                    var currentColor = style.currentColor;
                    var hellowebkit = document.getElementById('hellowebkit');
                    var hellowebkitStyle = hellowebkit.currentStyle || getComputedStyle(hello, null);
                    var outlineColor = hellowebkitStyle.outlineColor;
                    alert('color:' + color + ' currentColor:' + currentColor + ' outlineColor:' + outlineColor + ' color.match: ' + ('' + color).match(/rgb[(](\d+)[,]\s*(\d+)[,]\s*(\d+)[)]/));
                    var test = document.getElementById('test');
                    test.style.backgroundColor = '' + outlineColor;
            }   
            function readPropertyVal() {
                    //var inp = document.getElementById('hello');
                    //var inp = document.getElementById('btn');
                    var inp = document.getElementById('inp');
                    var color = getComputedStyle(inp, null).getPropertyValue('outline-color');
                    alert('color:' + color);
                    var test = document.getElementById('test');
                    test.style.backgroundColor = '' + color;
            }   
        </script>
    </body>
</html>
allicarn
  • 2,859
  • 2
  • 28
  • 47
robocat
  • 5,293
  • 48
  • 65
1

I solved my problem in a following way.

When I need to highlight iframe for the first time I’m creating textarea with negative 'left' coordinate (so that it’s invisible to user), give it a focus and get its CSS properties via window.getComputedStyle. Then I’m applying four of these properties to focused iframe: 'outline-color', 'outline-style', 'outline-width' and 'outline-offset'.

For some reason Safari 5 wouldn’t give you correct value for 'outline-offset'. So for the time being I hardcoded it to be '-2px'.

Danylo Mysak
  • 1,514
  • 2
  • 16
  • 22
  • 1
    Watch out, `window.getComputedStyle` doesn't work in IE... It gives dom nodes a `currentStyle` property instead. http://msdn.microsoft.com/en-us/library/ms535231.aspx – Dagg Nabbit Sep 29 '10 at 01:20
  • IE9 implements getComputedStyle and previous versions don’t draw outline. So 'if(window.getComputedStyle)' check is sufficient to avoid this problem. – Danylo Mysak Sep 30 '10 at 17:02