CSS pitfalls and how to overcome them

本文介绍了一些CSS技巧,包括按钮样式调整、使用伪元素设计美观的CSS3搜索框、实现渐变背景和过渡效果,以及解决CSS中常见的问题如表格圆角、输入框大小不一等。

When you write CSS, there are some problems you may run into. Then, to overcome the situation, you’ll need to use some small and effective CSS tricks.

In this article I’ll try to show you some tips and tricks that can definitely help you when writing CSS.

Buttons styling

I’ll begin with classical HTML buttons. When trying to style buttons like input type"submit" orbutton, if you want to achieve pixel perfection in your designs, don’t forget about the following:

/* removes extra side spacing in IE */
.button{
  overflow: visible;
}

/* removes extra inner spacing in Firefox */
.button::-moz-focus-inner{
  border: 0;
  padding: 0;
}

Want to see some buttons that are using the above snippet?

Using pseudo-elements

I’m sure you already know that using pseudo-elements like :before and :after helps you specifying which content should be inserted before (or after) the content of that element. Besides that, elements like input or img have no content, therefore, the pseudo-elements will not render anything for them. An exception is the hr element, which, for some reason, allows adding pseudo-elements to it.

Also, keep in mind the difference between :before and ::before. To summarize it, IE8 is the only reason to use the single colon syntax.

Gradient background for whole body

If you tried before to add a CSS3 gradient for your body, then you noticed that it will not stretch and it will repeat instead. To fix that, just add the following lines:

html {
    height: 100%;
}
body {
    height: 100%;
    margin: 0;
    background-repeat: no-repeat;
    background-attachment: fixed;
}

Fake transitions on gradients

Speaking of gradients, want transitions for CSS3 gradients? Unfortunately, there’s no browser support for gradients transitions (yet).

But, this can be done using some tricks like:

  • Use background-position on :hover to give the illusion that the gradient is transitioning. Actually, it’s shifted using the Y-axis only. This technique is used by Twitter’s Bootstrap.
    a{
        background: linear-gradient(orange, red) repeat-x;
        display: inline-block;
        padding: 20px;
    }
    
    a:hover{
        background-color: red;
        background-position: 0 -15px;
        transition: background-position .1s linear;
    }
  • The second option (and my favorite) is to transition the background-color and apply a gradient image to it which fades to transparent value. Again, this gives the illusion of an transitioning gradient.
    a{
        background-color: orangered;
        background-image: linear-gradient(top, rgba(255,255,255,.3), rgba(255,255,255,0));
        transition: background-color .1s linear;
        display: inline-block;
        padding: 20px;
    }
    
    a:hover{
      background-color: red;
    }

Inline-block gaps

Let’s say you’d like to use inline-block instead float for a horizontal list. When doing that, a horizontal space will appear between your inline-block elements. To avoid that, you’ll need to use a dirty (also good) method, namely adjust your markup.

So, instead using a clean structure like the below one:

<ul>
        <li>one</li>
        <li>two</li>
        <li>three</li>
</ul>

you’ll have to use something like this:

<ul>
        <li>one</li><li>two</li><li>three</li>
</ul>

Read more about the CSS display inline-block value: Why it rocks, and why it sucks.

When height 100% actually works?

It’s actually quite simple: when you set a height: 100% for an element, always think about the parent’s height. If the parent’s height is not defined in your styles, then the inner element will have height: 100% of nothing.

Please consider the following working (and rough) example:

<div id="parent">
    <div id="child">2</div>
</div>

and

#parent{
    height: 400px;
    padding:10px;
    background:red;
}

#child{
    height: 100%;
    background:green;
}

In the above example, the green colored #child, using height: 100%, will stretch and fill the whole 400px vertical space. Also, you can check my CSS3 progress bars for a live example of the above trick.

Rounded corners for tables

The trick consist of setting the table’s border-spacing to 0. The border-collapse‘s default value is separate, you’ll only need to set it to collapse for IE7 and lower ( for a graceful degradation).

table {
    *border-collapse: collapse; /* IE7 and lower */
    border-spacing: 0;
    border-radius: 5px;
}

Check my CSS3 tables with rounded corners article to read more about it.

Inputs box model

When designing a search box, for example, you’ll notice that after setting the same height orwidth for your input type="submit" and input type="text", the result will most likely disappoint you.

Quite frustrating, but the thing is that input type="submit" has a border-box box model, whileinput type="text" has a content-box box model (on IE and FF).

To overcome this kind of of issue, you can set for both form’s inputs (text and submit) the same box sizing model. This can be done using CSS3 box-sizing:

      box-sizing: content-box | padding-box | border-box;

Conclusion

The above are just some tricks I often use when writing CSS. I found them very useful, and I sincerely wish I knew them at the beginning.

I hope you enjoyed these and feel free to share yours by adding a comment below. Thank you


当前版本:1.031 软件语言:非中文 软件类别:记事 软件大小:26.51 MB 适用固件:3.2及更高固件 内置广告:有广告 适用平台:Android 24me是一个屡获殊荣的智能个人助理,结合你的待办事项列表,自动任务管理器和提醒服务为一体的智能应用程序。它是目前最先进的应用程序来管理你的生活之一。 24me给你一个“抬起头来”有关的一切,是发生在你一天通过智能连接到你的现实生活中,提醒你什么是未来的旁边,并自动照顾你的东西。它只是最终的个人助理,你一直想要的。少工作,少担心,更多的时间给自己。而且它是免费的! - 什么使24me如此受欢迎: •24me为你 - 只要你想要做的,24me会提醒你只是在正确的时间的东西添加一个基于时间的提醒。你甚至可以使任务可重复的。 •美丽与功能 - 24me设计精美,使用简单,人性化。 •就像现实生活 - 与轻扫手指任务标记为完成。 •采取行动 - 用24me您可以轻松地安排,以拨打电话,文本,电子邮件,甚至安排从应用程序内的差事。 •语音到文本的作品在许多语言。 •您的任务,您的选择 - 可以查看日期(今天,明天,即将)和标签视图之间切换,这取决于你。 •24me使电源在你的手中-轻松一键来计划你的日程,建立任务,将其标记为已完成。它只是感觉不错。 •轻松共享待办事项列表或跑腿,并与家人,朋友和同事合作 •说出你的想法 - 而不是打字只需轻按麦克风图标,并说你想要做什么。 24me会自动转换成文字文本几乎任何语言。 •捕获moment-您可以从相机创建任务,以节省你想要的东西。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值