css - 减少变亮、变暗和旋转有时只起作用

标签 css less less-mixins

问题

我是 LESS 的新手,我们刚刚在我的中级 CSS 和预处理器类(class)中学习了一些关于参数混合的基础知识。我正在我的元素网站上工作,并且喜欢 LESS 可以做的事情,但它并不总是像我想要的那样工作。

例如,在第 143 行附近,我正在设置导航背景颜色,如下所示:

background-color: lighten(@color5, 50%);

@color5 之前在导入的 palette.less 文件中定义为深紫色。 nav 元素的背景颜色是预期的淡紫色,显示 lighten()功能本身正在工作。

但是,在文件的下方,在“桌面”显示的媒体查询中(我使用的是移动优先方法),我将导航未访问的链接颜色设置如下:
color: lighten(@color5, 75%);

我希望这个分配输出更浅的薰衣草色,但它只显示白色(#ffffff)。我仔细检查以确保颜色不仅仅是一种超浅的紫色阴影——不。它是白色的。

在同一个媒体查询中,我还使用 spin() 将链接悬停颜色设置为不同的色调。方法。但是,它也不起作用,我的悬停颜色被翻译为白色。没有意义。

我的代码

以下是我的一些代码片段,但我不确定 SE 片段是否适用于 LESS。或者我在这里的 CodePen 上发布了它:

CodePen | CAS215 - Wk8 - MobileFirstResponsive

风格.less

/*

Assignment: CAS 215 - Module 8 Lecture Assignment
File Name: style.less
Date: 11/17/16
Programmer: Eric Hepperle

*/

@import 'palette.less';
@import 'mixins.less';

/* /END PARAMETRIC MIXINS */

/* MOBILE STYLESHEET ------------------------- */

/* UNIVERSAL SELECTOR */
* {
    box-sizing: border-box;
}

/* BODY */
body {
    font-family: "Open Sans", sans-serif;
    font-size: 16px;
    line-height: 1.5em;
    background-color: @lightColor;
    color: @darkColor;
    a {
        &:link {
            color: @color5;
            font-weight: bold;
        }
        &:visited {
            color: @color4;
            font-weight: normal;
        }
        &:hover {
            /* nothing yet */
        }
    }
}

/* SECTION SPACING */
main, header, footer, nav {
    padding: 1em;
}

/* LISTS */
ul {
    list-style-type: disc;
    list-style-position: inside;
    padding-left: 1em;
    text-indent: -1em;
}

li {
    line-height: 1.5em;
}

/* INLINE TEXT ELEMENTS */
em {
    font-style: italic;
}

p {
    margin: 1em 0em;    
    .fontProperties(@darkColor; "Tahoma"; 1em; 1.2em; normal);
}

/* HEADINGS */
h1, h2, h3, h4, h5, h6 {
    margin: 1em 0em;
}

h1 {
    .headingFontAttributes(@color5; 1.8em);
}

h2 {
    .headingFontAttributes(@color5; 1.4em);
}

h3 {
    .headingFontAttributes(@color5; 1.1em);
}

h4 {
    .headingFontAttributes(spin(@color5, 123); .9em);
}

h5 {
    .headingFontAttributes(spin(@color5, 123); .85em);
}

h6 {
    .headingFontAttributes(spin(@color5, 123); .75em);
}

/* BLOCK ELEMENTS */
pre {
    font-family: "Courier New", monospace;
    white-space: pre-wrap;
    .fancyBox(.3em; .3em; .3em; @darkColor);
    background-color: lighten(@color5, 50%);
    padding: 1.2em 0 0 0;
    border: solid 1px @color5;
    font-size: .8em;
    white-space: pre;
    overflow-x: scroll;
}

/* HEADER */
header {
    background-color: @color5;
    h1 {
        color: @color1;
        margin: .2em;
        text-align: center;
    }
    
}

/* NAV */
nav {
    text-align: center;
    background-color: lighten(@color5, 50%);
    width: auto;
    display: block;
    padding: 0;
    margin: 0;
    ul {
        margin: 0;
        padding: 0;
        list-style: none;
        li {
            width: auto;
            a {
                text-decoration: none;
                display: block;
                padding: .4em 0;
                border-bottom: solid 1px black;                
                &:link {
                    color: lighten(@color5, 75%);
                }
                &:focus {
                    background-color: @color2;
                }
            }            
        }
    }
}

/* MAIN */
main {
    background-color: @color6;
}

/* FOOTER */
footer {
    background-color: lighten(@color5, 30%);
    color: @lightColor;
    border-top: .6em solid @color5;
    /*border-bottom: .2em solid @color5;*/
    a {
        &:link {
            color: @color1;
        }
        &:visited {
            color: @color6;
        }
    }
    h2,h3 {
        color: darken(@color5, 10%);
    }
}

/*

TABLET STYLESHEET

The following CSS affects all screen sizes larger than 480 pixels wide.

*/

@media only screen and (min-width: 481px) {

    body {
        font-size: 14px;
    }
    
    pre {
        font-family: "Courier New", monospace;
        white-space: pre-wrap;
        .fancyBox(.3em; .3em; .3em; @darkColor);
        padding: 1.2em;
        border: solid 1px @color5;
        font-size: 1em;
        overflow-x: hidden;
    }

}

/*

DESKTOP STYLESHEET

The following CSS affects all screen sizes larger than 1024 pixels wide.

*/

@media only screen and (min-width: 1025px) {
    header, main, footer {
        padding: 2em;
    }
    
    nav {
        text-align: left;
        background-color: lighten(@color5, 50%);
        /*width: auto;*/
        display: block;
        padding: 1em;
        padding-left: 2em;
        margin: 0;
        ul {
            margin: 0;
            padding: 0;
            list-style: none;
            display: inline;
            padding-left: 1em;
            text-indent: -1em;
            li {
                width: auto;
                margin: 0;
                padding: 0;
                padding-bottom: 0;
                display: inline-block;
                margin-right: 3em;
                a {
                    text-decoration: none;
                    display: block;
                    padding: 0;
                    border: none;                
                    &:link {
                        color: lighten(@color5, 75%);
                    }
                    &:visited {
                        color: lighten(@color5, 75%);
                    }
                    &:hover {
                        color: spin(lighten(@color5, 75%),123);
                    }
                    &:focus {
                        background-color: @color2;
                    }
                }            
            }
        }
    }
    
    header {
        h1 {
            margin: .3em;
            text-align: inherit;
        }
    }

}
<link href="http://erichepperle.com/in-progress/pcc/cas215/project/css/grid.css" rel="stylesheet"/>
<link href="http://erichepperle.com/in-progress/pcc/cas215/project/css/generic.css" rel="stylesheet"/>
<link href="http://erichepperle.com/in-progress/pcc/cas215/project/css/reset.css" rel="stylesheet"/>


元素.html

<!DOCTYPE html>

<!--
File Name: project.html
Date: 11/17/16
Programmer: Eric Hepperle
-->

<html lang="en">

<head>

     <meta name="viewport" content="width=device-width, initial-scale=1">

     <meta charset="UTF-8">

     <meta name="author" content="Eric Hepperle">

     <title>CAS215 Project</title>

     <link href="http://fonts.googleapis.com/css?family=Open+Sans"
     rel="stylesheet" type="text/css">
     <link href="css/reset.css" rel="stylesheet" type="text/css">
     <link href="css/generic.css" rel="stylesheet" type="text/css">
     <link href="css/grid.css" rel="stylesheet" type="text/css">
     <link href="css/style.css" rel="stylesheet" type="text/css">

</head>

<body>

<nav>

    <ul>
        <li><a href="#intro">Intro</a></li>
        <li><a href="#mobile">Mobile First</a></li> 
        <li><a href="#features">Features</a></li>
        <li><a href="#links">Links</a></li>
    </ul>

</nav>

<header>

    <h1>CAS215 Project</h1>

</header>

<main>

    <img src="images/startup-photos.jpg" alt="Image of a designer with a wireframe." title="Image of a designer with a wireframe">

    <section id="intro">

        <h2>Responsive Web Design</h2>

        <p><em>Responsive web design</em> is a new design philosophy that is
        different from fixed design. In responsive web design, page layouts 
        adjust according to the size of the end user's display; in fixed 
        design, sizes of fonts, box elements and images remain unchanged.</p>

        <p>In the past, only desktop and laptop computers were used to surf 
        the web. It was a given that screen resolutions would be 1024 pixels 
        wide or higher. Fixed grid CSS frameworks such as 
        <a href="http://960.gs/">960 Grid System</a> were used to design 
        pages that were laid out in even columns on a fixed-width grid, 
        usually centered on the screen. As mobile devices began to hit the 
        market, it became necessary to create web pages that would work on 
        many devices and resolutions. As the number of devices grows 
        (watches that surf the web are on the horizon), the more important 
        it will be to write responsive sites.</p>

    </section>

    <section id="mobile">

        <img src="images/hand-holding-mobile-phone.jpg" alt="Hand holding a mobile phone." title="Hand holding a mobile phone.">

        <h2>Mobile First Design</h2>

        <p><em>Mobile first design</em> is the next step in responsive web 
        design. In mobile first design, the primary CSS is written for mobile.
        Then, the cascade later contains media queries that add more code for 
        larger device resolutions.</p>

        <h3>Better mobile first web design</h3>

        <p>Some designers write their CSS media queries so that they only 
        affect resolutions that are between two specific break points:</p>

        <pre>
        h3 {
            font-size: 1.75em; 
        }

        @media (min-width: 321px) and (max-width: 480px) {
          h3 {
            font-size: 2em;
          }
        }
        </pre>

        <p>This approach is severely limiting. What if there was CSS code 
        that is useful for <em>all</em> resolutions above 320 pixels, 
        including those 480 pixels and above? Writing media queries in this
        way means that some code will need to be redeclared later.</p>

        <p>We will try a different approach. Our media queries will 
        gradually add to the style of the page the larger the resolution 
        goes, like this:</p>

        <pre>
        h3 {
            font-size: 1.75em;
        }

        @media (min-width: 321px) {
          h3 {
            font-size: 2em;
          }
        }
        </pre>

        <p>See the difference? If an h3 size of 2em happens to work in every 
        browser width above 320 pixels, we're in good shape.</p>

    </section>

    <section id="features" class="section group">

        <div class="col span_4_of_12">

            <img src="images/apple-iphone-books-desk.jpg" alt="Mobile phone and programming book by laptop on desk." title="Mobile phone and programming book by laptop on desk.">

            <h3>Features of Fixed Design</h3>

            <ul>

                <li>A fixed page width in pixels, often centered in the
                browser window.</li>

                <li>Images are set at fixed widths.</li>

                <li>Font sizes are set at fixed pixel or point sizes.</li>

            </ul>

        </div>

        <div class="col span_4_of_12">

            <img src="images/html-code.jpg" alt="Colored HTML code on a black computer screen." title="Colored HTML code on a black computer screen.">

            <h3>Features of Responsive Design</h3>

            <ul>

                <li>Page and box element widths are set in percentages.</li>

                <li>Image widths are set with percentages, often at 100% to 
                fill available width.</li>

                <li>Font sizes are set with em sizes, so that they are sized 
                relative to the parent element's font size.</li>

                <li>The primary style sheet is for desktop devices, and media 
                queries are used toward the end of the cascade to account 
                for mobile devices.</li>

            </ul>

        </div>

        <div class="col span_4_of_12">

            <img src="images/laptop-mobile-phone.jpg" alt="Laptop and mobile phone." title="Laptop and mobile phone.">

            <h3>Features of Mobile First Design</h3>

            <ul>

            <li>The primary style sheet is for mobile devices, and media 
            queries are used toward the end of the cascade for tablet, 
            then desktop devices.</li>

            <li>As a result of this change, web site interfaces are 
            simpler and their design is much cleaner. There is far 
            less CSS to write.</li>

            </ul>

        </div>

    </section>

</main>

<footer>

     <h2>External Links</h2>

    <section id="links" class="section group">

        <div class="col span_4_of_12">

            <img src="images/responsive-design-wikimedia-740x490.png" alt="Graphic showing the same layout on various devices." title="Graphic showing the same layout on various devices.">

            <h3>Responsive Design</h3>

            <ul>
                <li><a href="http://abookapart.com/products/responsive-web-design"
                target="_blank">Ethan Marcotte</a></li>

                <li><a href="https://en.wikipedia.org/wiki/Responsive_web_design"
                target="_blank">Wikipedia</a></li>
            </ul>

        </div>

        <div class="col span_4_of_12">

            <img src="images/eh-mobile-first-design-740x490.png" alt="Graphic showing a mobile phone and a tablet pc." title="Graphic showing a mobile phone and a tablet pc.">        

            <h3>Mobile First Design</h3>

            <ul>
                <li><a href="https://codemyviews.com/blog/mobilefirst"
                target="_blank">Opinion Piece on mobile first design</a></li>

                <li><a href="http://www.sitepoint.com/making-case-mobile-first-designs"
                target="_blank">Making a case for mobile first design</a></li>
            </ul>
        </div>

        <div class="col span_4_of_12">

            <img src="images/mobile-first-responsive-resources.png" alt="Graphic showing various resources including a computer, browsers icons, and books." title="Graphic showing various resources including a computer, browsers icons, and books.">

            <h3>General Resources</h3>

            <ul>
                <li><a href="http://www.smashingmagazine.com"
                target="_blank">Smashing Magazine</a></li>

                <li><a href="https://css-tricks.com/"
                target="_blank">CSS Tricks</a></li>

                <li><a href="http://www.usability.gov/"
                target="_blank">usability.gov</a></li>
            </ul>

        </div>

    </section><!-- END links section -->

</footer>

</body>

<!--

NOTES:

    10/31/16 - * Created file from Ch. 4 project.
               * Changing footer colors
               * Note: temporarily disabled reset styles

-->

</html>

无调色板

@color1: #fcffc5; /* cream */
@color2: #ffc759; /* orange */
@color3: #ff8f5a; /* salmon/papaya */
@color4: #902d59; /* maroon */
@color5: #5e3e67; /* dark purple */
@color6: #f5f5dc; /* beige */

@darkColor: #000000;
@lightColor: #ffffff;


mixins.less

/* PARAMETRIC MIXINS */

.headingFontAttributes(@fontColor; @fontSize) {
    color: @fontColor;
    font-size: @fontSize;
    font-weight: bold;
    margin-top: @fontSize;
    margin-bottom: @fontSize /2;
    text-transform: uppercase;
}

.fontProperties(@fontColor; @fontFamily; @fontSize; @lineHeight; @fontWeight){
    color: @fontColor;
    font-family: @fontFamily;
    font-size: @fontSize;
    font-weight: @fontWeight;
    line-height: @lineHeight;
}

.fancyBox(@borderRadius; @shadowOffset; @shadowSize; @shadowColor) {
    border-radius: @borderRadius;
    box-shadow: @shadowOffset @shadowOffset @shadowSize @shadowColor;
}


我很茫然。我没有看到任何特异性问题,所以我很困惑。 LESS 编译器不知何故看不到或理解我的代码。

我已经尝试过的

我查看了这些帖子,但无法推断出我的情况的明确答案。

Lesscss > bootstrap's darken() and lighten() don't compile

CSS3 Scale, Fade and Spin on the same element (why isn't SCALE working?!?)

最佳答案

您的代码(或)Less 编译器生成的输出都没有问题。这更多的是一个理解问题。
lighten()函数增加 HSL 空间中颜色的亮度。您尝试变亮的颜色是 #5e3e67其亮度值为32.35294118%。 (您可以使用 Less 编译器的内置 lightness() 函数找到它)。

以下是 Less website 的摘录:

Increase the lightness of a color in the HSL color space by an absolute amount.



因此,当您在 lighten() 中设置百分比(数量)值时粗略大于 67.65% 时,得到的亮度值将是 100% 或更高,等于 #fff .

将百分比设置为小于 67.65% 的任何值,您会看到它确实会产生非白色的颜色。例如,

#demo {
  color: lighten(#5e3e67, 67%);
}
#demo2 {
  color: lighten(#5e3e67, 50%);
}
#demo3 {
  color: lighten(#5e3e67, 33%);
}

在编译后的 CSS 中产生以下颜色:

#demo {
  color: #fefdfe;
}
#demo2 {
  color: #d8c7dd;
}
#demo3 {
  color: #b391bd;
}

对于spin()代码中的函数也是相同的原因。没关系hue旋转(spin() 函数所做的),白色将保持白色。由于lighten()函数的输出是白色的,spin() 的输出也只会是白色的。

关于css - 减少变亮、变暗和旋转有时只起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40681986/

相关文章:

javascript - 将按钮放在位置为 :relative 的元素的右上角

css - 如何使用 ReactJSS 覆盖默认样式?

asp.net - 设计在 IE6 和 IE8 浏览器中都好看的网页

html - 如何链接到页面的特定部分(在 Safari 中有效)

javascript - CSS/less 文件未加载

less - 在 LESS 中使用 mixin 命名参数作为伪选择器

css - 元素动画关键帧的继承

css - 我们如何仅使用 flex 来获得这样的布局?

css - 使用 Web Essentials 2012 设置 LESS 关键帧名称