在bootstrap 3中使用mixins以避免布局结构的非语义标记

Kei*_*ant 19 css less twitter-bootstrap twitter-bootstrap-3

我对bootstrap和less都比较新,这是一个测试运行.目标是使用较少的引导类并将它们转换为简单和语义类.我不断收到错误消息,说明.col-sm-12和.col-md-8未定义.

我该如何解决此问题.

    /*less styles file*/

@import "bootstrap.min.css";

@boom: #ea7000;

@screen-sm: ~"(min-width:768px)";
@screen-md: ~"(min-width:992px)";
@screen-lg: ~"(min-width:1200px)";

.tim    {
    height: 200px;
        @media screen, @screen-sm   {
            background-color: black;
            .col-sm-12;
        }
        @media screen, @screen-md{
            background-color: @boom;
            .col-md-8;
        }
}
Run Code Online (Sandbox Code Playgroud)

AJ *_*ani 35

You should import the bootstrap.lessfile. So, download the full bootstrap project, and copy out the less folder. It contains everything. And then put the less folder in your project. Also make sure to add the less.js file to your project if you want to get your less compiled while your working. Look at lesscss.org for more information. And also make sure that you have a local server like mamp or xamp, because you can't see the results if you are just serving static html from file:// ....

在此输入图像描述

In your custom less file do something like this:

custom.less

@import "../less/bootstrap.less";

 section {
    .make-row();
}
.left-navigation {
    .make-sm-column(3);
}
.main-content {
    .make-sm-column(9);
}
Run Code Online (Sandbox Code Playgroud)

html

<head>
    <link rel="stylesheet/less" href="css/custom.less">
    <script src="//cdnjs.cloudflare.com/ajax/libs/less.js/1.4.1/less.min.js"></script>
</head>
<section>
<div class="left-navigation">
</div>
<div class="main-content">
</div>
</section>
Run Code Online (Sandbox Code Playgroud)


ATL*_*DEV 9

如果您的目标是使用Bootstrap制作漂亮的语义HTML,我建议您现在放弃或面临精神错乱.我非常痛苦地发现尝试从BS 2中获取语义标记是不可能的.它需要复杂的嵌套而不是混合架构才能实现您想要的效果,即使您使用的更少或更少.此外,类和ID由其JavaScript组件引用,因此无法更改.您可以创建自己的样式,但它违背了使用像BS这样的框架的目的.我无法评论第3版,并希望听到其他人的经历.

  • 有趣的评论.我刚刚开始使用bootstrap,我认为第一件事是"这个非语义CSS类命名通常被认为是_BAD_练习吗?".我正在考虑使用LESS mixins来创建从bootstrap继承的语义类,但是考虑到它可能会花费更多时间不使用bootstrap并从头开始编写所有CSS.您的回答有助于验证我的方法,谢谢! (2认同)
  • 框架的要点是,您有一组可重用的组件,可以随着时间的推移轻松维护.当您对其源代码进行修改时,您已从框架更新版本流中删除了源代码.您将不得不手动合并可能非常困难的更改.非平凡的网站将需要更新HTML和CSS. (2认同)