Customizing Component Styles

Learn how to customize Element UI component styles, override defaults, handle common pitfalls, and style frequently used components.

🎯 Introduction

By the end of this article, you will be able to customize Element UI components to match your designs and improve the user experience. We will cover style overrides, deep selectors, and scoped style limitations, with practical examples using Input, Button, and Dialog.


🎨 Customizing Component Styles

✨ Overriding Element UI Styles

Element UI is an established Vue 2 UI framework with a wide range of components and default styles. When those defaults do not meet your application requirements, you can override them in several ways:

1. Scoped Style Limitations

Using <style scoped> in a Vue component limits styles to that component’s template, preventing them from affecting other components. It also limits how you can style elements inside Element UI components.

For example, a selector in a scoped style block may not affect an Element UI component’s internal elements because its scope is limited to the current component.

2. Deep Selectors

Vue supports deep selectors such as :deep to reach beyond scoped styles and target elements inside child components or third-party libraries.

Example:

<template>
    <el-button>Button</el-button>
</template>

<style scoped>
:deep(.el-button) {
    background-color: #409eff;
    color: white;
}
</style>

Using :deep allows the styles to reach elements inside the Element UI .el-button component.

3. Overriding Styles with Wrapper Classes

Another common approach is to add a custom class to a wrapper and use a more specific selector to override the default styles.

Example:

<template>
    <div class="custom-button">
        <el-button>Button</el-button>
    </div>
</template>

<style scoped>
.custom-button >>> .el-button {
    border-radius: 20px;
}
</style>

This approach offers flexibility and helps avoid style conflicts on complex pages.

4. Global Styles

To override shared styles or many Element UI components, use a global stylesheet such as main.css, or a style block without scoped in App.vue. Scoped style restrictions do not apply, but selector specificity still matters.

.el-button {
    font-weight: bold;
}
Use selectors that are as specific as necessary to avoid affecting unrelated components.

⚠️ Things to Watch for When Overriding Styles

Why Do Style Overrides Fail for Dialogs and Drawers Mounted on body?

When components such as el-dialog and el-drawer are mounted on body through append-to-body, they sit outside the current component’s DOM tree. This has two implications:

  • Their DOM elements are outside the parent component’s scope, so scoped selectors may not reach their contents.
  • Deep selectors such as >>> and :deep may also fail when they depend on an ancestor in the current component’s DOM tree.

Solutions:

  • Override dialog styles in a global stylesheet.
  • Add a custom class to the dialog and target that class with global styles.

Example:

<el-dialog title="Example dialog" :visible.sync="dialogVisible" custom-class="custom-dialog">
    <p>Dialog content</p>
</el-dialog>
.custom-dialog .el-dialog__header {
    background-color: #f56c6c;
    color: white;
}

🎯 Styling Common Components

🔍 Customizing Input Height, Borders, and Colors

The default <el-input> styles are straightforward. Use deep selectors or global styles to customize properties such as height and border color.

Example code:

<template>
    <el-input placeholder="Enter text" v-model="inputValue" />
</template>

<script>
export default {
    data() {
        return {
            inputValue: '',
        };
    },
};
</script>

<style lang="scss" scoped>
.el-input {
    :deep(.el-input__inner) {
        padding: 0 20px;
        color: red;
        border: 1px solid blue;
        background-color: transparent;
        border-radius: 0;
        height: 30px;
        line-height: 30px;
    }
}
</style>

🎨 Customizing Button Styles

Hover and disabled states provide important interaction feedback. Customizing these states helps buttons match your brand.

Example:

<template>
    <el-button>Click me</el-button>
</template>

<style lang="scss" scoped>
.el-button {
    width: 120px;
    height: 40px;
    padding: 0;
    background: #f0f9ff;
    border: 1px solid #c6e2ff;
    color: #409eff;
    font-size: 12px;
    border-radius: 6px;
}
</style>

🖼️ Customizing Dialog Margins and Width

Dialogs are common interactive components. Adjusting their width and margins helps them fit different page layouts.

Example:

<template>
    <el-dialog title="Custom dialog" :visible.sync="dialogVisible" custom-class="custom-dialog">
        <p>Example dialog content</p>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            dialogVisible: true,
        };
    },
};
</script>

<style lang="scss">
body {
    .custom-dialog {
        width: 600px;

        .el-dialog__header {
            background-color: #f5f7fa;
        }

        .el-dialog__body {
            padding: 20px;
        }
    }
}
</style>
When applying global overrides, add a class such as custom-dialog to avoid affecting other dialogs.

🧾 Summary

  • When scoped styles cannot reach Element UI internals, use a deep selector such as /deep/.
  • For dialogs mounted on body, use global styles and the custom-class attribute.
  • Combine custom wrapper classes and global styles to adjust heights, colors, and state styles.

❓ Questions and Answers

Q1: Why do Element UI selectors sometimes have no effect in scoped styles?

Scoped styles apply within the current component’s scope and do not automatically reach deeply nested elements in child components. Use /deep/ or global styles.

Q2: How can I change a dialog’s width and margins?

Add custom-class to the dialog, then use more specific global selectors (often with !important) to override its defaults.


🧪 Exercises

Exercise 1

Customize an el-select dropdown’s background and text colors.

<template>
    <el-select v-model="value" placeholder="Select an option" class="custom-select">
        <el-option label="Option 1" value="1" />
        <el-option label="Option 2" value="2" />
    </el-select>
</template>

<script>
export default {
    data() {
        return {
            value: '',
        };
    },
};
</script>

<style lang="scss" scoped>
/* Write your styles here */
</style>

Exercise 2

Customize the color and size of an el-checkbox check box.

<template>
    <el-checkbox v-model="checked" class="custom-checkbox">Check me</el-checkbox>
</template>

<script>
export default {
    data() {
        return {
            checked: false,
        };
    },
};
</script>

<style lang="scss" scoped>
/* Write your styles here */
</style>

🎉 You now know how to override Element UI styles, use deep selectors, and customize common components!