Can CSS Be Applied to <Track> Element

Can CSS be applied to track element

According to the WebVTT, The <track> element cannot be rendered with css, thus there is no way to add css to it. You can change traits and the given cue, but nothing beyond that. Here's a nice tutorial for creative use of these. The closest you can get to styling it is surrouding it with a div and applying the style to the div itself. Sorry, no nasty hax. :/

EDIT: There is one forbidden nasty trick, but it's highly dangerous for the mortal. Beware, for the worst bugs and demons you've ever seen if you ever get close to these technics.

Protect the track

A given track element can be embeded inside an Audio element, protected by it, since they both share the unliked race of HTML5 media players. The mozilla docs have a nice scroll which may help you in your quest; it includes the following:

<audio src="foo.ogg">
<track kind="captions" src="foo.en.vtt" srclang="en" label="English">
<track kind="captions" src="foo.sv.vtt" srclang="sv" label="Svenska">
</audio>

Listen to no rules

As a forbidden prophecy once mentioned, there's no implemented audio player which uses CSS - however, the chosen one could make their own. He or she shall leave the controls attribute off, and implement the controls from the scratch using javascript. They might get a few followers to assist them in the form of existing audio players partial implementation, such as your childhood friend.

Attack from the inside out

Once the chosen one has mastered the various arts of styling controls with js, they may use the secret inner-trait-css-styling-non-justu and add styling to the inner elements from the audio controls - one of which, is the track.

Good luck, adventurer. You'll be the first to ever come back alive, if you will. And if you will, please tell us the tale of your mighty battles.

Find all CSS rules that apply to an element

EDIT: This answer is now deprecated and no longer works in Chrome 64+. Leaving for historical context. In fact that bug report links back to this question for alternative solutions to using this.


Seems I managed to answer my own question after another hour of research.

It's as simple as this:

window.getMatchedCSSRules(document.getElementById("description"))

(Works in WebKit/Chrome, possibly others too)

Cannot find which css style is being applied to an element

This is build-in, HTML5, native validation style of Firefox browser. It is done by applying required="required" HTML5 tag. You can remove this styling by appying :invalid CSS pseudo element.

textarea:invalid,
input:invalid {
box-shadow: none;
}

Find out all elements a given CSS class is applied to?

Are you looking for this: https://developer.mozilla.org/en-US/docs/Web/API/document.querySelectorAll ?

Basically you may find elements matching a certain selector like this: document.querySelectorAll('.firstclass.secondclass')

Proper way to apply CSS to HTML5 custom elements

You can apply CSS to custom elements just like you can to standard HTML elements.

There's nothing wrong with scroll-content { ... }, as written in your code.


A Little Background

The browser, at a basic level, has no clue what elements exist. It doesn't recognize anything... until it is exposed to the default style sheet (sample).

The default style sheet introduces the browser to HTML elements.

Custom elements can therefore be defined as elements that are not included in the default style sheet. (Elements that exist, but are unsupported by the browser, could share this definition.)

Custom elements could, however, be introduced to the browser in author styles.

Here's something important to consider:

If the browser doesn't recognize an element (i.e., it's not in the default style sheet), it will apply CSS initial values.

6.1.1 Specified
values

User agents must first assign a specified value to each property based
on the following mechanisms (in order of precedence):

  1. If the cascade results in a value, use it.

  2. Otherwise, if the property is inherited and the element is not the root of the document tree, use the computed value of the parent
    element.

  3. Otherwise use the property's initial value. The initial value of each property is indicated in the property's definition.

As outlined above, if an element is unrecognized (#1 & #2 don't apply), use the initial value from the property definition (#3 applies).

So in your case:

  • Your custom element is: <scroll-content>

  • Your CSS is: scroll-content { overflow: hidden; }

  • You say in your question that this code does what it's supposed to do. But unless the framework you mention provides additional styles for custom elements, it cannot work (demo).

Here's why:

  • Since <scroll-element> is not in the default style sheet it will use CSS initial values.

  • Well, the initial value of the display property is inline.

  • But the overflow property only works on block elements.

So there's no way this HTML/CSS combination could work – the overflow property would be ignored, as would height, width and any other properties that don't apply to inline elements.

A custom element would need to have display: block applied for overflow to work (demo).

Similarly, the only reason body, div, h1, p, ul exist as block elements is because they are defined this way in the default style sheet (sample).

So, putting aside the arguments for and against custom elements, here's the bottom line:

Add display: block to your custom elements and you're good-to-go.

How to programmatically check CSS styles for particular elements?

You can use window.getComputedStyle(). The returned value is a CSSStyleDeclaration, and you can access the properties directly using the dot notation or use .getPropertyValue('property name').

var p = document.querySelector('.demo');var style = window.getComputedStyle(p);
console.log(style);
console.log('style.color ', style.color);
// or
console.log('getPropertyValue(\'color\')', style.getPropertyValue('color'));
.demo {  color: red; }
<p class="demo">This is red</p>

Is there a way to check which CSS styles are being used or not used on a web page?

Install the CSS Usage add-on for Firebug and run it on that page. It will tell you which styles are being used and not used by that page.

Can you use if/else conditions in CSS?

Not in the traditional sense, but you can use classes for this, if you have access to the HTML. Consider this:

<p class="normal">Text</p>

<p class="active">Text</p>

and in your CSS file:

p.normal {
background-position : 150px 8px;
}
p.active {
background-position : 4px 8px;
}

That's the CSS way to do it.


Then there are CSS preprocessors like Sass. You can use conditionals there, which'd look like this:

$type: monster;
p {
@if $type == ocean {
color: blue;
} @else if $type == matador {
color: red;
} @else if $type == monster {
color: green;
} @else {
color: black;
}
}

Disadvantages are, that you're bound to pre-process your stylesheets, and that the condition is evaluated at compile time, not run time.


A newer feature of CSS proper are custom properties (a.k.a. CSS variables). They are evaluated at run time (in browsers supporting them).

With them you could do something along the line:

:root {
--main-bg-color: brown;
}

.one {
background-color: var(--main-bg-color);
}

.two {
background-color: black;
}

Finally, you can preprocess your stylesheet with your favourite server-side language. If you're using PHP, serve a style.css.php file, that looks something like this:

p {
background-position: <?php echo (@$_GET['foo'] == 'bar')? "150" : "4"; ?>px 8px;
}

In this case, you will however have a performance impact, since caching such a stylesheet will be difficult.

How to find all possible CSS Styles and Attributes of an Element?

You can check if the found elements are inside of other elements using jQuery $.contains()

More info here how to do this here: http://api.jquery.com/jQuery.contains/

So for your example to find if some found classes are inside set you can do:

$.each(foundElement, function(index, domElement){
if($.contains(domElement, $('.set'))) {
// domElement is inside the container with 'set' class
} else {
// domElement is not inside the container with 'set' class
}
});


Related Topics



Leave a reply



Submit