I'm trying to get a better understanding of using mixins in Polymer 2: Here is my sample:
<dom-module id="x-test">
<template>
<custom-style>
<style is="custom-style">
html {
--center-on-screen: {
left: 50%;
top: 50%;
position: absolute;
border: solid 1px red;
};
}
</style>
</custom-style>
<style>
.signal {
border-radius: 30px;
height: 30px;
width: 30px;
#apply --center-on-screen;
}
</style>
<div class="signal"></div>
</template>
<script>
'use strict'
class XTest extends Polymer.Element {
static get is() {
return 'x-test';
}
static get properties() {
return {
}
}
static get observers() {
return [];
}
constructor() {
super();
}
ready() {
super.ready();
}
connectedCallback() {
super.connectedCallback();
}
connectedCallback() {
super.connectedCallback();
}
}
customElements.define(XTest.is, XTest);
</script>
</dom-module>
when the code #apply --center-on-screen; in the class, I would expect the div to have the color red and be centered on the screen. I have verified it because I had all the code in --center-on-screen in the class .signal. I moved it into --center-on-screen just for testing purposes. If anyone can advise me on what i'm doing incorrectly.
**Update **
When I move --center-on-screen into :host then it works. So it looks like this
<style>
:host {
--center-on-screen: {
left: 50%;
top: 50%;
position: absolute;
border: solid 1px red;
}
}
.signal {
border-radius: 30px;
height: 30px;
width: 30px;
border: solid 1px red;
#apply --center-on-screen;
}
</style>
Try to include cd shady css mixin:
<link rel="import" href="../../bower_components/shadycss/apply-shim.html">
CSS custom properties are becoming widely supported, CSS mixins remain a proposal. So support for CSS mixins has been moved to a separate shim that is optional for 2.0 class-style elements. For backwards compatibility, the polymer.html import includes the CSS mixin shim. Class-style elements must explicitly import the mixin shim.
Ref: https://www.polymer-project.org/2.0/docs/upgrade#class-based-elements-import-the-css-mixin-shim
Thanks for posting this query. I was searching some credible resource on how to use css mixins in polymer 2.0 .
I had this mixin -
--calendarbox-mixin: {
display:flex;
position:relative;
flex-direction: column;
border-radius: 5px;
--webkit-border-radius:5px;
--moz-border-radius:5px;
width:11vw;
margin:10px 5px;
text-align: center;
height:18vh;
justify-content: space-around;
}
I tried adding it just abover another class where I wanted to use the mixin -
.dayfare_box {
#apply(--calendarbox-mixin);
background: #fbfcfc;
border:2px solid #e2e2e2;
}
The output came without the mixin applied. Tried adding in :host and it worked!!
Just stumbled upon this link and it confirmed my doubt whether I was doing it right. Thanks for posting :)
Related
I need to change the default primary Bootstrap 4 color (after the Bootstrap stylesheet has been loaded) to a custom color (choosed by user) for a dynamic Bootstrap component with an internal CSS stylesheet.
I could do, for example, .btn-primary { background-color: red; } but this works just for buttons and, however, it doesn't change the other btn-primary states like ":hover", ":active" and "disabled". It also doesn't change the "primary" color throughout the entire CSS for .alert-primary, .text-primary, .bg-primary, .btn-outline-primary, .badge-primary, etc...
What's the possible solution?
You need to download the Bootstrap Sass files. You can do so from this link.
Once you have them you can open the main bootstrap .scss file and search for:
$theme-colors: (
"primary": #0074d9,
"danger": #ff4136
);
Change "primary" to what you need and then recompile to CSS. If you don't have Sass installed on your machine you can use various online tools to accomplish this. Example.
Source: https://getbootstrap.com/docs/4.3/getting-started/theming/
You can do so by using the variables concept(https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties)
Bootstrap 4 also works on the variables
You can try changing the variable values at run time as mentioned below:
:root {
--main-bg-color: brown;
}
.one {
color: white;
background-color: var(--main-bg-color);
margin: 10px;
width: 50px;
height: 50px;
display: inline-block;
}
.two {
color: white;
background-color: black;
margin: 10px;
width: 150px;
height: 70px;
display: inline-block;
}
.three {
color: white;
background-color: var(--main-bg-color);
margin: 10px;
width: 75px;
}
.four {
color: white;
background-color: var(--main-bg-color);
margin: 10px;
width: 100px;
}
.five {
background-color: var(--main-bg-color);
}
// get variable from inline style
element.style.getPropertyValue("--main-bg-color");
// get variable from wherever
getComputedStyle(element).getPropertyValue("--main-bg-color");
// set variable on inline style
element.style.setProperty("--main-bg-color", "red");
There are different ways do it. I am describing one of them.
Import below lines to your scss file.
#import '~bootstrap/scss/bootstrap.scss';
After that write below code to override it.
$primary = 'red';
$gray = 'gray';
.btn {&.btn-primary {
#include button-variant($primary, $primary, $primary, $primary);
&.disabled,
&:disabled {
border: $gray;
background-color: $gray;
}
&:hover {
opacity: 0.85;
}
}
}
Check the link to use sass mixins to override bootsrap classes.
I am currently building an application in Angular 6.
I am using ngx-smart-modal to handle all of my modals.
I have 20+ modals in my application.
How do I apply CSS to each one uniquely.
I have tried using the [customClass] parameter in their documentation, but I am relatively new to Angular/HTML/CSS/etc, and I could not get it working.
I can change the sizes of my modals globally using
/deep/ .nsm-dialog{ -insert style- }
But could not replicate for individual modals
HTML
<ngx-smart-modal #Create identifier="Create" customClass="'modal'">
CSS
.nsm-dialog.modal {
width: 50vw;
height: 50vh;
}
I would like to have each modal with unique CSS.
Ex.
* Modal1 size is 50vw x 50vh
* Modal2 size is 20vw x 40vh
* etc.
For using customClass directive with brackets, as [customClass], you must pass a string to the directive, like :
<ngx-smart-modal [customClass]="'my-custom-class'"></ngx-smart-modal>
Finally, you can style this and only this modal with my-custom-class.
I was having the same issue.
My resolution was making a ngx-modal.scss file and including it in style.scss after #import "~ngx-smart-modal/ngx-smart-modal";
I had to add !important to a few styles. Didnt love that but it worked.
HTML - in component
<ngx-smart-modal #modalName
identifier="modalName"
[customClass]="'confirmation-modal'">
</ngx-smart-modal>
scss - ngx-modal.scss
.confirmation-modal {
background-color: black !important;
border: 1px solid grey;
.modal-body {
color: white;
display: flex;
align-items: center;
justify-content: center;
}
.modal-footer {
display: flex;
align-items: center;
justify-content: center;
padding: 1rem;
border-top: 1px solid transparent;
border-bottom-right-radius: 0.3rem;
border-bottom-left-radius: 0.3rem;
top: 60px;
position: relative;
}
}
For normal way above told examples will work, but if you are creating modals in dynamic way you should do
const optionModel: INgxSmartModalOptions = {
closable: true,
escapable: false,
dismissable: false,
customClass: "signSowClass" };
let modal;
modal = this.ngxSmartModalService.create('signSow', SignSowComponent, optionModel).open()
You can pass the custom class through options as mentioned above
I'm using <paper-progress-button> in a project, and I want to style it from my main stylesheet but can't get it to work.
The styling code for <paper-progress-button> looks like this:
<dom-module id="paper-progress-button">
<template>
<style>
:host {
display: inline-block;
}
.button {
#apply --paper-progress-button-button;
}
.button:not([disabled]) {
#apply --paper-progress-button-button-active;
}
.spinner {
margin-left: 10px;
#apply --paper-progress-button-spinner;
}
[hidden] {
display: none;
}
</style>
...
I've tried all sorts of ways to get my styles from a main site-level stylesheet to affect the button, but none seem to work:
main.css
--paper-progress-button-button {
background-color: red;
}
main.css
* {
--paper-progress-button-button {
background-color: red;
};
}
custom_style.html
<custom-style>
<style is="custom-style">
--paper-progress-button-button {
background-color: red;
}
</style>
</custom-style>
custom_style.html
<custom-style>
<style is="custom-style">
:root {
--paper-progress-button-button {
background-color: red;
};
}
</style>
</custom-style>
The documentation for styling Polymer 2 is huge, but doesn't even mention #apply once! So how do I really style that button from my site-level stylesheet?
Polymer currently only shims CSS properties within a custom-style or a Polymer element's style; and not from an external stylesheet.
Also note the style usage is incorrect, as the CSS property name must be followed by a colon:
.my-div {
--paper-progress-button-button: {
background-color: red;
};
}
demo
Is there a way where I could dynamically add the styles in css/less file by just passing in class name ?
For example:
<div class="xyz_20"></div>
<div class="xyz_40"></div>
Instead of writing:
.xyz_20 {width:20px;} .xyz_40 {width:40px;}
Is there a way where i could write a single class .xyz_i and width be automatically added based on the i value, like .xyz_i {width: i px;}` without involving javascript.
If so, Please suggest.
This is not possible, as far as I know, however this is a great use case for inline styling:
<div class="xyz" style="width:20px"></div>
If you wanted to support a finite number of widths, then you can use recursion to generate classes:
.widthgen(#count) when (#count > 0) {
.widthgen((#count - 10));
.xyz_#{count} {
background-color: red;
width: #count * 1px;
}
}
.widthgen(50);
Output:
.xyz_10 {
background-color: red;
width: 10px;
}
.xyz_20 {
background-color: red;
width: 20px;
}
.xyz_30 {
background-color: red;
width: 30px;
}
.xyz_40 {
background-color: red;
width: 40px;
}
.xyz_50 {
background-color: red;
width: 50px;
}
Lists Plugin
You could use the lists plugin (to install: npm install -g less-plugin-lists) if your widths you want to support are not easily captured in a linear pattern:
#widths: 10, 20, 40, 50;
.for-each(#i in #widths) {
.xyz_#{i} {
background-color: red;
width: #i * 1px;
}
}
You would compile that with:
lessc --lists in.less out.css
And you would get:
.xyz_10 {
background-color: red;
width: 10px;
}
.xyz_20 {
background-color: red;
width: 20px;
}
.xyz_40 {
background-color: red;
width: 40px;
}
.xyz_50 {
background-color: red;
width: 50px;
}
No.
There is no way to write classes and expect the browser to infer meaning from them.
The only way to accomplish something similar to this would be with javascript (which OP said they did now want to use).
I want to apply custom CSS to the title and content of a popover in Bootstrap, however, it seems that my CSS is being ignored.
How can I apply specific CSS to the title and the content respectively?
$("#poplink").popover({
html: true,
placement: "right",
trigger: "hover",
title: function () {
return $(".pop-title").html();
},
content: function () {
return $(".pop-content").html();
}
});
html, body {
width: 100%;
height: 100%;
}
.pop-div {
font-size: 13px;
margin-top: 100px;
}
.pop-title {
display: none;
color: blue;
font-size: 15px;
}
.pop-content {
display: none;
color: red;
font-size: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="pop-div">
<a id="poplink" href="javascript:void(0);">Pop</a>
<div class="pop-title">Title here</div>
<div class="pop-content">Content here</div>
</div>
For example: http://jsfiddle.net/Mx4Ez/
The reason appears to be that the javascript is creating brand new elements to display the popover itself. These new elements have different css class names than the original.
Try adding this to your css:
.popover-title {
color: blue;
font-size: 15px;
}
.popover-content {
color: red;
font-size: 10px;
}
Update
Depending on the library version you're using, the names may be different. If the above does not work, try using .popover-header and .popover-body instead.
If you have multiple popovers on your page and only want to style one of them, you can leverage the popover's template option to add another class:
$("#myElement").popover({
template: '<div class="popover my-specific-popover" role="tooltip">...'
});
I started by just using the default value for template from the docs, and added my-specific-popover to the class attribute.
The newly created elements have the following hierarchy:
.popover
|_ .popover-title
|_ .popover-content
Which is injected after the element that triggers the popover (you can specify a specific container for the injected popover by setting the container option, in which case you will set the styles using the element that you passed as container). So to style a popover you can use css like the following example:
<div id="my-container">
Popover This!
</div>
<style>
.popover-title { color: green; } /* default title color for all popovers */
#my-container .popover-title { color: red; } /* specific popover title color */
</style>
As Matt said before, it may depend on the bootstraps version, for v4.6 you should do:
.popover{
background-color: red;
}
.popover-header {
color: red;
}
.popover-body {
color: blue;
}
Bootstrap >= 5.0
You can override Bootstrap popover styles.
.popover {
background-color: var(--main-bg-color);
border: 1px solid var(--border-color);
}
.popover-body {
color: var(--font-color);
}
.bs-popover-top {
> .popover-arrow {
&::before {
border-top-color: var(--border-color);
}
&::after {
border-top-color: var(--main-bg-color);
}
}
}
.bs-popover-end {
> .popover-arrow {
&::before {
border-right-color: var(--border-color);
}
&::after {
border-right-color: var(--main-bg-color);
}
}
}
.bs-popover-bottom {
> .popover-arrow {
&::before {
border-bottom-color: var(--border-color);
}
&::after {
border-bottom-color: var(--main-bg-color);
}
}
}
.bs-popover-start {
> .popover-arrow {
&::before {
border-left-color: var(--border-color);
}
&::after {
border-left-color: var(--main-bg-color);
}
}
}
And replace --main-bg-color, --border-color, and --font-color with yours.