1

So I am creating a custom component based on <ul><li> but looking more like a <select><option> and when clicking on it I want its foreground/background to be set to whatever is the default selection colour on the users system, by adding something to css for that particular component

Currently it is just hardcoded to a version of blue which would only be right for a specific browser on particular OS

 .delete_criteria li.selected {
      background-color: #3399FF;
      color: white;
    }

If I don't add the above CSS then there is no visible change when you select it. What I want to do is set to to default as this pseudo code will demonstrate

.delete_criteria li.selected {
          background-color: default-selection-background-colour;
          color: default-selection-foreground-colour;
        }

but I don't know what to replace default-selection-background-colour and default-selection-foreground-colour with

Fuller example showing the html and javscript:

   <div class="delete_criteria">
<ul>
    <li>
        <div>
            <label>
                Audio Format
            </label>
            <input name="AUDIOFORMAT" id="AUDIOFORMAT" value="AUDIOFORMAT" hidden="hidden" type="text">
        </div>
    </li>
    <li>
        <div>
            <label>
                Bitrate
            </label>
            <input name="BITRATE" id="BITRATE" value="BITRATE" hidden="hidden" type="text">
        </div>
    </li>
    <li>
        <div>
            <label>
                Track Length
            </label>
            <input name="TRACKLENGTH" id="TRACKLENGTH" value="TRACKLENGTH" hidden="hidden" type="text">
        </div>
    </li>
    <li>
        <div>
            <label>
                Filename
            </label>
            <input name="FILENAME" id="FILENAME" value="FILENAME" hidden="hidden" type="text">
        </div>
    </li>
    <li>
        <div>
            <label>
                File Creation Date
            </label>
            <input name="FILE_CREATION_DATE" id="FILE_CREATION_DATE" value="FILE_CREATION_DATE" hidden="hidden" type="text">
        </div>
    </li>
    <li>
        <div>
            <label>
                File Modified Date
            </label>
            <input name="FILE_MODIFIED_DATE" id="FILE_MODIFIED_DATE" value="FILE_MODIFIED_DATE" hidden="hidden" type="text">
        </div>
    </li>
</ul>
<button id="up" type="button">
    Up
</button>
<button id="down" type="button">
    Down
</button>
// enable moving an element up
document.getElementById('up').addEventListener('click', () => {
  var li = document.querySelector('li.selected');
  if (li.previousElementSibling !== null) {
    li.parentNode.insertBefore(li, li.previousElementSibling);
  }
});

// enable moving an element down
document.getElementById('down').addEventListener('click', () => {
  var li = document.querySelector('li.selected');
  if (li.nextElementSibling !== null) {
    li.parentNode.insertBefore(li, li.nextElementSibling.nextElementSibling);
  }
});
                                    </script>
                                </div>
                            </td>
                        </tr>
                    </table>
                    <div>
.delete_criteria ul {
  padding-left: 0;
  width: 20em;
  list-style-type: none;
  border: 1px solid darkgray;
  vertical-align:top;
}

.delete_criteria li {
  padding: 2px 4px;
}

.delete_criteria li.selected {
  background-color: #3399FF;
  color: white;
}

I have read the question below but it only shows you how to set the value to a hardcoded value irrespective of users situation.

What is the browser-default background color when selecting text?

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • 2
    No idea why two have marked as closed it is clearly a css programming question, and one that is very specific – Paul Taylor Jan 31 '18 at 07:52
  • You know you can change the background selection colour to whatever you want right? – DreamTeK Feb 01 '18 at 15:07
  • 1
    If you know the default colours and you 'must' use the browsers default. Just detect the browser and then set the colour based on the result from your list of colours. – DreamTeK Feb 01 '18 at 15:11
  • @PaulTaylor The two people who voted to close it did so because you are missing a [mcve] from your post, which is required. – TylerH Feb 01 '18 at 16:10
  • @TylerH you don't have to give such a thing, and anyway I have shown what Im currently doing (using a hardcoded value to set colour) – Paul Taylor Feb 01 '18 at 20:07
  • Yes, you do, when one is needed. The text specifically says "Questions seeking debugging help must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]." – TylerH Feb 01 '18 at 20:11
  • @Obsidian I dont know the default colours and I dont want to amass a big list of different colours for different browsers, surely there is a way to just ask for the colour of the current browser. And yes I must use the default to create a coherant web application that will look as user on any given browser expects (within reason) – Paul Taylor Feb 01 '18 at 20:11
  • In this question you are asking how to create something in JavaScript and modify it in CSS. We don't know what the element is, or what the JavaScript looks like. Further, you've tagged the question with [tag:html' but not provided any. – TylerH Feb 01 '18 at 20:12
  • @TylerH its not a debugging question, its a question asking how to do something that I don't know how to do. The problem is totally clear, I dont see how anyone with even minimal computer knowledge could not understand the question. – Paul Taylor Feb 01 '18 at 20:13
  • @TylerH The CSS shows it is the selected element of an li element and I want to set the colour to the standard colour shown when you select items in a select. – Paul Taylor Feb 01 '18 at 20:15
  • @TylerH Ive added a fuller example although it seems to me to detract from a quite straightforward question – Paul Taylor Feb 02 '18 at 07:57

5 Answers5

1

Do you mean the CSS initial value?

.customcomponent li.selected {
  background-color: initial;
  color: initial;
}

Reference: https://www.w3schools.com/cssref/css_initial.asp

Anne Douwe
  • 681
  • 1
  • 5
  • 19
  • Sort of and I didnt know about initial value, but I want the initial colour of a select option element rather than list element initial value since Im trying to mimic a sort of select box. – Paul Taylor Feb 01 '18 at 20:09
1

Well, depending on what exactly you mean, and how you want to go about this, there are a number of potential solutions. CSS has the keyword initial, which resets a CSS property to its "default" value. Something else that might work in certain contexts is inherit. The table provided in the linked question, provides browser-specific default color codes.

Safari 6.0.3 Mac*: #B4D5FE

Chrome 26.0.1410.65 Mac*: #ACCEF7

Firefox 19.0 Mac*: #B4D5FF

Chrome 26.0.1410.64 m Windows 8+: #3297FD

Firefox 20.0.1 Windows 8+: #3399FF

Safari 5.1.7 Windows 8+: #3298FD

Internet Explorer 10.0.4 Windows 8+: #3399FF

Another reference that might help you, is this CSS Default Browser Values table from W3 Schools.

If you're looking to change or modify the "selection" color, when the user selects text on the page, use the ::selection pseudo-selector.

.customcomponent li::selected {
    background-color:#3399ff;
    color:white;
}

/*  or...  */

/*  note that the pseudo-selector is not the class name */
.customcomponent li.selected::selected {
    background-color:#3399ff;
    color:white;
}

I hope any of this helps.

Community
  • 1
  • 1
Tanner Babcock
  • 3,232
  • 6
  • 21
  • 23
  • 1
    Thanks that doesnt reeally help. I dont understand why none seems to understand my question but i have added a fuller example. To restate all I want is to be able to set li::selected to a default for selection not hard code a value – Paul Taylor Feb 02 '18 at 07:56
1

You can try using System Colors in CSS2, but its Deprecated already with CSS3 appearance.

System colors refer to Operating system colors and not User Agent colors.

.highlight {
   color: HighlightText;
   background-color: Highlight;
   }
<div class="highlight">Highlight</div>

Discussing appearance in editors draft

Here are some pointers into the appearance spec and its issues: Note: This specification intentionally refrains from making the appearance of all possible form controls and sub-controls available as values, as had previously been attempted by earlier proposals for this property and by several UA vendors in experimental implementations. Experience has shown that such a list would be very long and not practical to maintain, and UAs would need to add non-standard values to account for the behavior of non-standard pseudo-elements sometimes used to implement form controls. Moreover, many values of such an enumeration only make sense on a single element or pseudo-element, and are never used outside of the UA stylesheet. Instead, this specification only provides auto, none. UAs cannot therefore use the appearance property in the UA stylesheet to give each control its native look and feel, and must use appearance: auto instead.

Authors desiring to make certain elements in their document look and behave like native form controls should use the correct element in the markup language of the document rather than attempt to use this property or its experimental variants.

Future version of this specification may add a few more values for commonly desired appearances if compability with content written for experimental implementations proves problematic. So far, experience indicates that this is not the case. Even if this were to happen, it is not anticipated that the property would grow to cover all possible controls and sub-controls.

CSS3 dropped features

Thoughts from svgeesus

System colors were introduced from Java, which wanted to make native-looking dialogs matching the then-current windowing systems (Windows 95 and MacOS 7). It was unable to do even that, and the introduction of titlebar gradients in Windows 98 made it clear that chasing platform look would always be an elusive, moving target. In addition, the security implications of being able to accurately fake native UI were becoming apparent. appearance was an attempt to match native appearance without specifying exactly what that was like. The timeframe there was the appearance of brightly-colored iMacs whose UI theming reflected the case color (there was a flavor keyword for that, too). these proposals were too vaguely worded to really be implementable, saw little developer interest and thus were also dropped.

How do we proceed with scenarios where we have to style elements to look as native as possible?

With great caution and a full understanding of the spoofing risks involved; by deciding exactly what UI on what platform at what point in time you want to emulate; and with the realization that your styling will be fragile and need re-doing every few years.

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • HI, thats work perfectly and was what i was looking for. The only question mark is that it is deprecated and not clear what is therefore now the preferred way to do things. – Paul Taylor Feb 02 '18 at 09:23
  • Well I would say that its deprecated and there is nothing similar in current specs. You can use `appearance` and style your `ul` as `menulist` or `popupmenu`. You can basically tell that "This `li` should appear like a `menuitem`". – sabithpocker Feb 02 '18 at 09:52
  • can you give me example to do this. – Paul Taylor Feb 02 '18 at 10:10
  • and then I will award you the bounty – Paul Taylor Feb 02 '18 at 11:19
  • Hey, As I said I don't personally think `appearance` specs and implementation has yet reached the position where it can be used in this scenario. But the idea of deprecation is probably pointing to this direction is what is mentioned in the above comment. – sabithpocker Feb 05 '18 at 07:50
  • ok I thought you were saying I could style my ul to use same colours as menulistm but if not fine. – Paul Taylor Feb 05 '18 at 07:54
  • I just updated the answer with the problems with using `appearance`. – sabithpocker Feb 05 '18 at 08:07
  • Also be sure to read the line I added in bold letters, that makes your original question somewhat unanswered – sabithpocker Feb 05 '18 at 10:48
  • @PaulTaylor Check the comment on [this issue](https://github.com/w3c/csswg-drafts/issues/2302) that I raised at w3c github – sabithpocker Feb 13 '18 at 07:22
  • Not sure why you closed it so rapidly, Im not trying to spoof native dialogs anything just trying create a select list without using select, and use the same colour – Paul Taylor Feb 13 '18 at 10:24
  • It can be reopened if you feel so. I closed it because I got convinced that having System Colors is only a historical requirement that failed. But yes, the solution or rather non-solution provided by @svgeesus is not that satisfactory. I ended the discussion for now as I dont have enough time to investigate and come up with something constructive to discuss. – sabithpocker Feb 13 '18 at 11:16
1

Here, I'm doing it with click event, you can hook it on onLoad, or in any jQuery/Javascript events you want, and once you have the color code, you can use it to set to any field.

    
 function knowMyColor(){
 var element = document.getElementById('myId'),
    style = window.getComputedStyle(element),
    color = style.getPropertyValue('color');
    //alert("I'm of" + color + " color");
    if(color.startsWith("#")){
      console.log(color);
    }
    else{
      convertRGBtoHex(color);
    }
 }
 
function convertRGBtoHex(RGBcolor){
  var res = RGBcolor.substring(4).slice(0, -1);
  var rgb = [];
 var nRes = res.split(/\s*,\s*/).forEach(function(myString) {
    rgb.push(parseInt(myString));
});

var hexColor = rgbToHex(rgb[0],rgb[1],rgb[2]);
console.log(hexColor)
}

function componentToHex(c) {
    var hex = c.toString(16);
    return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
.myClass {
   color: #502147;
   background-color: yellow;
   }
<div id="myId" class="myClass" onClick="knowMyColor()">What is my color? Click Me..!!</div>

Let me know if this is you want, if this is not u were looking for, I would not mind deleting my answer. :)

miiiii
  • 1,580
  • 1
  • 16
  • 29
  • Thanks please keep your answer since the knowMyColour() function is useful and provides a solution without having to hardcode values. However the answer by sabithpocker does provide a simpler solution which I am tempted to go with, – Paul Taylor Feb 02 '18 at 09:25
  • I'm glad you found the solution and yes, I'll keep mine too. :) – miiiii Feb 02 '18 at 09:44
0

There is a way to get the selection color, however it always returns transparent.

getComputedStyle(element, '::selection').backgroundColor;    
getComputedStyle(element, '::-moz-selection').backgroundColor; 

//rgba(0, 0, 0, 0);

Both will return transparent as value. Browser won't override OS default or won't let the user access the OS defaults.

Its best to maintain a map of such default values as already mentioned above.

pixlboy
  • 1,452
  • 13
  • 30