简体   繁体   English

自定义引导 Sass 组件

[英]Customizing Bootstrap Sass Components

I'm someone who is very new to Bootstrap 5 and Sass, and I've been really enjoying my time learning them both.我是一个对 Bootstrap 5 和 Sass 非常陌生的人,我真的很享受学习它们的时间。 I just have a quick couple of questions...我只是有几个快速的问题...

When it comes to customizing Bootstrap Sass, I know you can do variable overrides (to change certain colors, fonts, etc.), but when it comes to making changes to various components (such as Bootstraps Nav component), is it good practice to override some of the prewritten classes with my own changes - or should I rewrite new classes and just reference them in my HTML? When it comes to customizing Bootstrap Sass, I know you can do variable overrides (to change certain colors, fonts, etc.), but when it comes to making changes to various components (such as Bootstraps Nav component), is it good practice to用我自己的更改覆盖一些预先编写的类 - 或者我应该重写新类并在我的 HTML 中引用它们?

For example, I want to have a specific nav for my header with a certain colored background, hover, font, and border radius;例如,我想为我的 header 有一个特定的导航,具有一定的彩色背景、hover、字体和边框半径; however, I don't want to maintain that style for the other navs used throughout my page.但是,我不想为整个页面中使用的其他导航保持这种风格。 What would I do in this instance?在这种情况下我会怎么做?

My current approach is to just create some more custom variables, and then create a custom class for my nav header where certain things will be applied.我目前的方法是创建更多自定义变量,然后为我的导航 header 创建自定义 class,其中将应用某些内容。

Here is how my main.scss file is set up as well:这也是我的 main.scss 文件的设置方式:

// Custom.scss
// Option B: Include parts of Bootstrap

// 1. Include functions first (so you can manipulate colors, SVGs, calc, etc)
@import "../node_modules/bootstrap/scss/functions";

// 2. Include any default variable overrides here

// 3. Include remainder of required Bootstrap stylesheets
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

// 4. Include any optional Bootstrap components as you like
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
@import "../node_modules/bootstrap/scss/images";
@import "../node_modules/bootstrap/scss/containers";
@import "../node_modules/bootstrap/scss/grid";

// 5. Add additional custom code here

Thanks for your help!谢谢你的帮助!

Edit: Here's an example of a customization .编辑:这是一个自定义示例 I wanted to change the nav hover option for only one use case (in my header), so I created a new file called _custom-nav.scss where I put this class:我只想为一个用例(在我的标题中)更改导航 hover 选项,所以我创建了一个名为_custom-nav.scss的新文件,我在其中放置了这个 class:

  .hover-light {
      &:hover {
        background-color: $nav-pills-hover-color;
      }
  }

I would then reference this class in my HTML so that my page is properly styled.然后我会在我的 HTML 中引用这个 class 以便我的页面样式正确。

Is this bad practice?这是不好的做法吗? I would just manually change the variables and hover options within Bootstraps nav;我只需在 Bootstraps 导航中手动更改变量和 hover 选项; however, I only needed this hover option for the nav in my header.但是,我的 header 中的导航只需要这个 hover 选项。

This is an opinion based question and not really suited to this kind of forum - I'm sure a dozen ppl will reply with a dozen different views.这是一个基于意见的问题,并不真正适合这种论坛——我敢肯定,十几个人会用十几个不同的观点来回答。

The basics of what you've done so far is not wrong, and you can just stick with that.到目前为止,您所做的基本工作并没有错,您可以坚持下去。 The thing to remember about frameworks like Bootstrap is that it is just that - a framework.关于像 Bootstrap 这样的框架要记住的是,它就是一个框架。 Akin it to a car chassis - that's all you're "buying" > the chassis and the motor.类似于汽车底盘 - 这就是您“购买”>底盘和电机的全部内容。 You can certainly drive it... but to get the most of it you should add more to it.你当然可以驾驶它......但要充分利用它,你应该添加更多。

How that's done is many ways - from loading the whole BS5 core and then creating a separate CSS to override the default - call that a "plugin" - to compiling ones own CSS from BS5 SASS (like you're doing).如何做到这一点有很多方法 - 从加载整个 BS5 内核,然后创建一个单独的 CSS 以覆盖默认值 - 称之为“插件” - 到从 BS5 Z1AC21585AAF0B5A916B0B5 编译自己的 CSS。

If you're asking for "best practice" that too is a misnomer - as that too will vary.. from purists who will tell you that you should not use BS5 at all and that you should create your own CSS from code to those who say don't customise anything and just use "themes" like you can get from bootswatch.com... there is no correct answer here - just opinions.如果您要求“最佳实践”,那也是用词不当-因为这也会有所不同..纯粹主义者会告诉您根本不应该使用 BS5,并且您应该从代码创建自己的 CSS说不要自定义任何东西,只需使用从 bootswatch.com 获得的“主题”......这里没有正确的答案 - 只是意见。

Your answer is somewhere in the middle - and for you, you're on the right track.你的答案在中间——对你来说,你走在正确的轨道上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM