Mục lục bài viết

Mẹo Hướng dẫn Stop video with CSS Mới Nhất

Cập Nhật: 2022-02-10 12:46:04,Quý khách Cần biết về Stop video with CSS. Bạn trọn vẹn có thể lại Comment ở phía dưới để Admin đc tương hỗ.

731

Video player styling basics

In the previous Cross browser video player article we described how to build a cross-browser HTML5 video player using the Media and Fullscreen APIs. This follow-up article looks at how to style this custom player, including making it responsive.

Tóm lược đại ý quan trọng trong bài

  • Video player styling basics
  • The example in action
  • Preliminary modifications from the original example
  • HTML Markup
  • Related CSS alteration
  • Basic styling
  • Progress bar
  • Control visibility
  • Progress bar tư vấn
  • Button functionality
  • Responsive styling

The example in action

You can find the code for the updated, styled example on Github, and view it live.

Preliminary modifications from the original example

This section summarizes the modifications that were made to the original video player example to make the styling task easier, before the bulk of the work was started.

HTML Markup

There are a few changes that were made to the HTML markup shown in the previous article. The custom video controls and element are now contained within

elements, rather than residing inside unordered list items.

The markup for the custom controls now looks as follows:

<div id=video-controls class=controls data-state=hidden>
<button id=playpause type=button data-state=play>Play/Pause</button>
<button id=stop type=button data-state=stop>Stop</button>
<div class=progress>
<progress id=progress value=0 min=0>
<span id=progress-bar></span>
</progress>
</div>
<button id=mute type=button data-state=mute>Mute/Unmute</button>
<button id=volinc type=button data-state=volup>Vol+</button>
<button id=voldec type=button data-state=voldown>Vol-</button>
<button id=fs type=button data-state=go-fullscreen>Fullscreen</button>
</div>

The previous article set the display property of the video controls to block in order to display them. This has now been changed to use a data-state attribute, which this code already uses to handle its fullscreen implementation.

This “data-state” idea is also used for setting the current state of buttons within the video control set, which allows specific state styling.

JavaScript

As mentioned above, a data-state attribute is used in various places for styling purposes and these are set using JavaScript. Specific implementations will be mentioned at appropriate places below.

Styling

The resultant video player style used here is rather basic this is intentional, as the purpose is to show how such a video player could be styled and be made responsive.

Note: in some cases some basic CSS is omitted from the code examples here as its use is either obvious or not specifically relevant to styling the video player.

Basic styling

The HTML video and its controls are all contained within a

element, which is given a maximum width and height (based on the dimensions of the video used) and centered within the page:

figure
max-width:64rem;
width:100%;
max-height:30.875rem;
height:100%;
margin:1.25rem auto;
padding:1.051%;
background-color:#666;

The video controls container itself also needs some styling so that it is set up the correct way:

.controls
width:100%;
height:8.0971659919028340080971659919028%; /* of figure’s height */
position:relative;

The height of the .controls class is set to be (a very precise!) percentage of the enclosing

element (this was worked out with experimentation based on the required button height). Its position is also specifically set to relative, which is required for its responsiveness (more on that later).

As mentioned earlier, a data-state attribute is now used to indicate whether the video controls are visible or not and these also need to be styled:

.controls[data-state=hidden]
display:none;

.controls[data-state=visible]
display:block;

There are a number of properties that also need to be set for all elements within the video controls:

.controls > *
float:left;
width:3.90625%;
height:100%;
margin-left:0.1953125%;
display:block;

.controls > *:first-child
margin-left:0;

All elements are floated left, as they are to be aligned next to one another, and each element is set to have a width of nearly 4% (again the actual value was calculated based on the required dimensions of the buttons), and a height of 100%. A value for margin-left is also set, but the first element (in this case the play/pause button) has this property overridden by the value 0.

The

container for the element also requires some specific settings; it is set to be much wider than the other child elements and its cursor value is set to be pointer:

.controls .progress
cursor:pointer;
width:75.390625%;

Buttons

The first major styling task to tackle is to make the video control’s buttons actually look like and act like real buttons.

Each button has some basic styling:

.controls button
border:none;
cursor:pointer;
background:transparent;
background-size:contain;
background-repeat:no-repeat;

By default, all