css3常用技巧_您可能错过的5个CSS3技巧和窍门

css3常用技巧

Though, CSS3 isn’t a complex language but you’ll there are greater chances that you’ll stumble across certain new things that’ll probably leave you in a puzzled state. For instance, adding properties that you won’t have used, or adding details of the specifications that you aren’t aware of, and so on.

虽然,CSS3不是一种复杂的语言,但是您将有更多的机会偶然发现某些新事物,这些新事物可能会使您感到困惑。 例如,添加您不会使用的属性,或者添加您不了解的规范的详细信息,等等。

If you want to become a CSS3 styling expert, then below are a few tips that you should consider:

如果您想成为CSS3样式专家,那么以下是您应该考虑的一些技巧:

  1. 以CSS为中心 (Centering in CSS)

    Many people find centering things in CSS pretty hard. In fact, people haven’t been able to crack the centering problem in web design. However, it’s not that hard and is rather a kind of doable thing.

    Generally, designers use absolute positioning to make the elements perfectly centered. But, when it comes to vertical centering you should use a CSS3 transform instead. Though, you’ll require a container div for offering an absolute reference, but you can auto-adjust the vertical positioning by using the transform as follows:

    div.container {
    height: however many em you want;
    position: relative }
    
    div.container p {
    margin: 0;           //Kills lingering offsets
    position: absolute;
    top: 50%;       //(Nearly) centre the element vertically
    transform: translate(0, -50%); }   //Nudge it up by half its height

    This is a great solution for animation, as it visually make changes to the elements by transforming their appearance. Using CSS3 transform you can apply several visual effects to your elements like move, rotate, skew and other effects. In order to center the 3D or 2D element vertically and horizontally, add or change the height and width of the element as shown below:

    许多人发现CSS中的居中很难。 实际上,人们无法解决网页设计中的居中问题。 但是,这并不困难,而是一种可行的事情。

    通常,设计人员使用绝对定位来使元素完美居中。 但是,在垂直居中时,您应该改用CSS3转换。 虽然,您将需要一个容器div来提供绝对参考,但是您可以通过使用以下转换自动调整垂直位置:

    div.container {
    height: however many em you want;
    position: relative }
    
    div.container p {
    margin: 0;           //Kills lingering offsets
    position: absolute;
    top: 50%;       //(Nearly) centre the element vertically
    transform: translate(0, -50%); }   //Nudge it up by half its height

    对于动画而言,这是一个很好的解决方案,因为它可以通过变换外观来直观地更改元素。 使用CSS3转换,您可以将多种视觉效果应用于元素,例如移动,旋转,倾斜和其他效果。 为了使3D或2D元素垂直和水平居中,如下所示添加或更改元素的高度和宽度:

  2. 了解CSS特异性 (Understanding CSS Specificity)

    Specificity isn’t a very common word and chances are that you might not have even heard of it, but it’s a fundamental concept that any person who want to become good at styling CSS must understand. Specificity helps a browser choose most relevant property values to an element and gets them applied by the browsers. The problem with specificity is that it restricts CSS-rules to be applied to some elements, even though you think that the rules should have been applied.

    In order to resolve the problem you need to remember certain specificity rules, such as:

    • ID selectors have higher specificity in comparison to attribute selectors.
    • Rules having more specific selectors will have greater specificity.
    • The last defined rule will override previous and conflicting rules and so on.

    In case you’re facing specificity issues, make sure to avoid using !important declaration. In addition, try using minimum number of selectors required for styling an element. And put your styles in LVHA (link-visited-hover-active) order.

    特异性不是一个很常见的词,可能您甚至没有听说过它,但这是一个想要精通CSS样式的人必须理解的基本概念。 特殊性可帮助浏览器选择与元素最相关的属性值,并使它们由浏览器应用。 特殊性的问题是,即使您认为应该已应用规则,它也会限制将CSS规则应用于某些元素。

    为了解决该问题,您需要记住某些特定性规则,例如:

    • 与属性选择器相比,ID选择器具有更高的特异性。
    • 具有更具体选择器的规则将具有更大的特异性。
    • 最后定义的规则将覆盖先前的规则和有冲突的规则,依此类推。

    如果遇到特殊性问题,请确保避免使用!important声明。 此外,请尝试使用最少的元素样式选择器数量。 并将您的样式置于LVHA(链接访问的悬停活动)顺序中。

  3. 在RGB上使用HSL (Use HSL Over RGB)

    Generally, most of the web designers make use of the age-old “RGB Hex tag system” to define colors. Define red, green and blue colors individually, include opacity if required. This approach is fine until you’re working with a color wheel, and merely want to determine complementary/contrasting hues having resembling saturations.

    In such a situation you’ll have to look for an RGB to HSLa calculator and perform plenty of conversions. Fortunately, CSS3 supports HSLa colour specs, as follows:

    background-color: hsla(120, 50%, 50%, 1);

    通常,大多数网页设计师都使用古老的“ RGB Hex标签系统”来定义颜色。 分别定义红色,绿色和蓝色,如果需要,可以包括不透明度。 除非您使用色轮,并且只想确定具有类似饱和度的互补/对比色相,否则这种方法很好。

    在这种情况下,您将不得不寻找RGB到HSLa计算器并执行大量转换。 幸运的是,CSS3支持HSLa颜色规范,如下所示:

  4. CSS3阴影 (CSS3 Drop Shadows)

    Rather than slicing the image and then capturing the shadow, it would be better if you could replicate the task using CSS3. For this purpose, you’ll have to include two lines of codes into your CSS, so as to make the effect work in all latest browsers:

    -abc-box-shadow: 1px 4px 10px #ccc;
    
    -xyz-box-shadow: 1px 4px 10px #ccc;

    如果您可以使用CSS3复制任务,则比切片图像然后捕获阴影要好。 为此,您必须在CSS中包含两行代码,以使效果在所有最新的浏览器中均有效:

  5. 简化字体样式 (Simplify Your Font Style)

    When using @font-face, probably you’ll use different weights and styles for the same font, but make sure to tag them using font-weight:normal and font-style:normal, as shown in the snippet below:

    @font-face {
    font-family: ‘Italic name goes here’;
    src: url(‘URL-of-italic-font’);
    format:(‘Usually truetype’);
    font-weight:normal;
    font-style:normal;
    }

    Rather than collecting all the related fonts when typing, you should collect them into a single family at the time of loading. And then, use ‘font-weight: bold’ for elements that needs to be bold and use ‘font-style: italic’ for elements that needs to be italic, and apply them to CSS:

    使用@font-face ,可能会对同一字体使用不同的权重和样式,但是请确保使用font-weight:normalfont-style:normal标记它们,如下面的代码片段所示:

    @font-face {
    font-family: ‘Italic name goes here’;
    src: url(‘URL-of-italic-font’);
    format:(‘Usually truetype’);
    font-weight:normal;
    font-style:normal;
    }

    而不是在键入时收集所有相关字体,而是应在加载时将它们收集到一个家族中。 然后,对需要使用粗体的元素使用“字体权重:粗体”,对需要使用斜体的元素使用“字体样式:斜体”,并将其应用于CSS:

结束吧! (Let’s Wrap Up!)

While working in CSS3 you’re less likely to come across obstacles, because of the its syntax. The syntax is clear and easy-to-understand for any newbie or inexperienced designer. In fact, it’s so simple that you could style your CSS just within a few hours of learning it. Even though, your site might look good in browsers like Safari and others, but what if your site doesn’t work in IE, or behave in a different manner as you’ve expected. Well, then it is recommended that you should consider the above discussed CSS3 trips and tricks.

在CSS3中工作时,由于其语法的原​​因,您遇到障碍的可能性较小。 对于任何新手或经验不足的设计师来说,语法都是清晰易懂的。 实际上,它非常简单,您可以在学习CSS的几个小时内就可以设置CSS样式。 即使您的网站在Safari等浏览器中看起来可能不错,但是如果您的网站无法在IE中运行,或者行为方式与您期望的不同,该怎么办。 好了,那么建议您考虑上面讨论CSS3之旅和技巧。

Author Bio: Being a well known PSD to WordPress theme conversion service provider across the globe. Mike is treated as wordpress junkie who shares valuable content on markup conversion and web design aspects.

作者简介:作为全球知名的PSD到WordPress主题转换服务提供商。 Mike被视为Wordpress迷,他在标记转换和网页设计方面分享了宝贵的内容。

翻译自: https://www.journaldev.com/6620/css3-tips-tricks-that-you-might-have-missed

css3常用技巧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值