Easy Gradient Buttons for Bootstrap v4

I just started a job that is using Bootstrap v4. I’m happy with the functionality but I miss having a not so plain looking button. Here is the CSS code I am using to add easy gradients on my Bootstrap v4 buttons:

.btn {
    background-image: linear-gradient(to bottom, rgba(255,255,255,0.5), rgba(255,255,255,0.2) 49%, rgba(0,0,0,0.15) 51%, rgba(0,0,0,0.05));
    background-repeat: repeat-x;
}

Here are some example buttons to show you what this change looks like in the wild:

See the Pen Bootstrap v4 Gradient Buttons Examples by Chris West (@cwestify) on CodePen.

As you can tell by the 4 lines of CSS I added, the fix is simple and as seen in the example, the buttons look nice. Let me know what you think and as always, happy coding :cool:.

Responsive YouTube Embed

YouTube URL

Replace the URL below with whatever YouTube video’s URL you want to make responsive embed code for.

Embed Code

You can select and copy the code below to embed this responsive video in your site.

Example Embed

If you resize the window you will see the video change in size according to the width of the page.

How It Works

The reason this all works is due to the fixed aspect ratio of the YouTube video player and how padding percentages work in CSS.

YouTube Video Aspect Ratio

YouTube always shows videos in a 16:9 aspect ratio.

CSS – Padding Percentages

According to MDN, percentages used for padding “refer to the width of the containing block.”

Combining What We Know

Keeping all of this in mind we first create a container element which will maintain the 16:9 aspect ratio.
{{code.replace(/iframe.+iframe/, '!-- EMBED IFRAME CODE --')}}
  • We use position: relative knowing that the <iframe> is later going to need to have position: absolute.
  • Our padding-bottom: 56.25% comes from our aspect ratio because 9 ÷ 16 = 56.25%.
Now let’s add the embed code (<iframe>):

We use position: absolute; top: 0; left: 0; width: 100%; height: 100%; to make sure that the <iframe> fills the entire container (including the padding).

Concluding Thoughts

You can use this same train of thought to make embeddable videos from any site responsive if you want. The one thing you need to know before hand is the aspect ratio. As long as you know the aspect ratio you will be able to calculate the padding-bottom of the containing element. Happy coding! 😎

CSS – Local File System Paths On Windows

When working with Windows you probably have noticed that local file paths use the backslash as the path separator. Although this is the case, even on Windows, in CSS you can use the forward-slash as a path separator to reference an image. If you want to use the forward-slash though, you must escape it with another backslash. Here are two examples:

#elem1 {
  background-image: url("relative-path\\to\\file.ext");
}
#elem2 {
  background-image: url("C:\\path\\to\\file.ext");
}
#elem3 {
  background-image: url("relative-path/to/file.ext");
}
#elem4 {
  background-image: url("C:/path/to/file.ext");
}

As you can see above I have three different ways to add a background image:

  1. url("relative-path\\to\\file.ext")
    For the element of ID “elem1” I am specifying that the background image is found at the relative path of “relative-path\to\file.ext”. It is important to note that I am escaping the backslashes (path separators) with an additional backslash.
  2. url("C:\\path\\to\\file.ext")
    Just as in the previous example, if we need to reference something using an absolute path while maintaining backslashes as path separators, we need to escape all backslashes with a preceeding backslash.
  3. url("relative-path/to/file.ext")
    For the element of ID “elem2” I am specifying that the background image is found at the relative path of “relative-path/to/file.ext”. Even though the normal path separator on Windows is a backslash you can use a forward slash.
  4. url("C:/path/to/file.ext")
    Just as in the previous example, if we need to reference something using an absolute path we can use a forward slash as the path separator.

This may seem like a minor issue but when developing Electron apps it may come in handy. Have fun and as always, happy coding! 😎