【问题标题】:Angular 6 define global scss heading styles - BootstrapAngular 6 定义全局 scss 标题样式 - Bootstrap
【发布时间】:2019-03-25 05:20:04
【问题描述】:

我正在尝试为我的 Angular 6 应用程序覆盖和定义全局标题样式。如果我在 styles.scss 文件中做这样的事情,它会起作用!

$h1-font-size: 50px;

但是当我尝试根据屏幕大小(媒体查询)定义 $h1-font-size 时,它​​没有显示正确的字体大小,我有这样的 ATM:

// 屏幕大小断点

$grid-breakpoints: (
  xs: 0,
  sm: 576px,
  md: 768px,
  lg: 992px,
  xl: 1200px
);

// media queries xs, md and lg
@media only screen and (min-width: map-get($grid-breakpoints, xs)) {
    $h1-font-size: 12px;
}

@media only screen and (min-width: map-get($grid-breakpoints, md)) {
    $h1-font-size: 18px;
}

@media only screen and (min-width: map-get($grid-breakpoints, lg)) {
    $h1-font-size: 50px;
}

angular.json:

  "styles": [
              "node_modules/bootstrap/dist/css/bootstrap-reboot.css",
              "node_modules/bootstrap/dist/css/bootstrap-grid.css",
              "src/styles.scss",
              ...
            ],

bootstrap-scss._variables.scss 文件:scss file

$h1-font-size:                $font-size-base * 2.5 !default;
$h2-font-size:                $font-size-base * 2 !default;
$h3-font-size:                $font-size-base * 1.75 !default;
$h4-font-size:                $font-size-base * 1.5 !default;
$h5-font-size:                $font-size-base * 1.25 !default;
$h6-font-size:                $font-size-base !default;

styles.scss 文件:

/* You can add global styles to this file, and also import other style files */

@import '~bootstrap/scss/bootstrap-reboot';
@import '~bootstrap/scss/bootstrap-grid';
// Required
@import "~node_modules/bootstrap/scss/bootstrap";

/* Set global default font family */
$font-family-base: 'Source Sans Pro',
sans-serif;
body,
html {
    font-family: $font-family-base
}

$grid-breakpoints: ( xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px);
@media only screen and (min-width: map-get($grid-breakpoints, xs)) {
    $h1-font-size: 12px;
}

@media only screen and (min-width: map-get($grid-breakpoints, md)) {
    $h1-font-size: 12px;
}

@media only screen and (min-width: map-get($grid-breakpoints, lg)) {
    $h1-font-size: 50px;
}

有可能吗?根据屏幕大小覆盖 boostrap-scss 标题的最佳做法是什么?

stackblitz 示例的链接 - https://stackblitz.com/edit/angular-scss-global-headings

【问题讨论】:

  • 我没有收到任何错误。
  • @Roy 我的错,对不起,我使用的不是 Material,而是 Bootstrap
  • 能否在您的起点上添加一个关于 StackBlitz 的最小、完整和可验证的示例? stackoverflow.com/help/mcve 我认为这与更具特异性的 Bootstrap 类有关。
  • 我已经添加了 Stackblitz 示例,它甚至没有接近完美的示例,但至少它是一些东西,ATM 我试图了解如何在 angular.json 文件中包含 bootstrap-scss @罗伊

标签: angular sass media-queries


【解决方案1】:

问题是您为变量分配新值取决于屏幕大小,但您从不使用它。只需选择 h1 并设置一个值。它适用于您的 stackblitz 示例

@media only screen and (min-width: map-get($grid-breakpoints, xs)) {
    $h1-font-size: 10px;
    h1 {
      font-size: $h1-font-size
      }
}

@media only screen and (min-width: map-get($grid-breakpoints, md)) {
    $h1-font-size: 30px;
    h1 {
      font-size: $h1-font-size
      }
}

@media only screen and (min-width: map-get($grid-breakpoints, lg)) {
    $h1-font-size: 50px;
    h1 {
      font-size: $h1-font-size
      } 
}

【讨论】:

  • 哦,这是我的一个愚蠢的错误,感谢@Vladislav Guleaev 的帮助,我还需要在我的 style.scss 文件中更改 scss 导入的顺序。祝你有美好的一天!
猜你喜欢
  • 2018-11-13
  • 2018-10-16
  • 2018-12-04
  • 2020-09-02
  • 2019-03-03
  • 2023-03-18
  • 1970-01-01
  • 2021-12-03
  • 2019-04-21
相关资源
最近更新 更多