css3 calc()自适应布局

我们想要实现页面自适应布局时,通常因为margin的存在,而比较麻烦;有时候想要实现宽度自适应的输入框时,也因为padding或margin的存在,而相当繁琐,同时由于浏览器兼容性而导致最终效果不一致。css3新添加属性box-sizing,在一定程度上解决了上面的问题,而在今天的文章中我们来通过css3新增加的另外一个属性calc()来实现适应布局。

calc()是css3新添加属性,它可以让你使用一个算术表达式来表达长度值,这意味着可以用它来定义div的宽度,并设置margin、padding、border等。

calc()的运算规则

  • 使用”+”、”-”、”*”、”/”四则运算;
  • 可以使用百分比、px、em、rem等单位;
  • 可以混合使用各种单位进行计算。

实例1:定位在页面上的块元素,含有外边距

.banner {
  position:absolute;
  left: 40px;
  width: -moz-calc(100% - 80px);
  width: -webkit-calc(100% - 80px);
  width: calc(100% - 80px);
  border: solid black 1px;
  box-shadow: 1px 2px;
  background-color: yellow;
  padding: 6px;
  text-align: center;
}

实例2:自动调整大小的表单,又适应容器

input {
  padding: 2px;
  display: block;
  width: -moz-calc(100% - 1em);
  width: -webkit-calc(100% - 1em);
  width: calc(100% - 1em);
}  

#formbox {
  width: -moz-calc(100%/6);
  width: -webkit-calc(100%/6);
  width: calc(100%/6);
  border: 1px solid black;
  padding: 4px;
}
<form>
  <div id="formbox">
  <label>Type something:</label>
  <input type="text">
  </div>
</form>

浏览器兼容性

  • firefox 4.0+已经开支支持calc()功能,需要使用-moz-calc()私有属性;
  • chrome从19 dev版,开始支持私有的-webkit-calc()写法;
  • IE9支持原生写法,calc();
  • Opera貌似还不支持~~

马上分享给你的朋友吧~

css3 calc()自适应布局》上有 4 条评论

  1. Pingback 引用通告: CSS3 width: calc() | The WordPress by NoNo

  2. 厦门最好的花场ktv是www.nyjzktv.com
    厦门最好的夜场首选www.nyjzktv.com
    厦门最好的夜总会是www.nyjzktv.com
    厦门夜场招聘首选www.nyjzktv.com

  3. sex toys
    Pucker up It makes you live longer and the longest one recorded was 58 HOURS 11 things you DIDN’T
    know about kissingNews Group Newspapers Limited in England
    No. 679215 Registered office: 1 London Bridge Street, London, SE1
    9GF. “The Sun”, “Sun”, “Sun Online” are registered trademarks or trade names of News Group Newspapers Limited.
    I’ve been telling one of my guy friends about my struggles and he has
    offered to help me out a little, mostly with helping me be able to climax.

    Kinda like a sexual physical therapy. And he’s willing to allow me to live
    out my kinks with him, whether they be physical or not.

    vibrators “Now, if I’m insulted I will counterattack, or if something is unfair I will counterattack, but I don’t feel like I insult people. I don’t want to do that. But if I’m attacked, I will counterattack.”.
    I would say this: that thank God for us spending the time to
    reflect on one of the most powerful Americans, who bridged
    a gap in a way that none of us could have ever seen.
    We’re living now. I’m I’m standing on the shoulders of
    folks like John Lewis and the Reverend Dr. vibrators

    cheap vibrators “Effectiveness rates of contraceptive methods are all figured based on one full year of use, not for each single incident of sex or use of a method. Perfect, or proper, use of a method means that in one year, that method was always used, and always used following the directions for that method to the letter, without any errors or mistakes. Perfect use of most methods in one full year is unusual, except for methods like the IUD or implant where a user does not have to do anything. cheap vibrators

    fleshlight toy Basically, I have come to the conclusion that love drops are more suitable for use with oral sex or body taste foreplay, not massages. I still don’t think I would purchase this again even for oral play. It is just way too sticky and way too messy. But others are just tastes. Teases, even. Terrible, haunting, ragged edged things that arrive, cut you, then recede into the cracks between the pages with a smirk (a very, very British smirk) to leave you dotting the carpets with your blood.. fleshlight toy

    cheap vibrators Ever since I noticed the slightest fuzz of pubic hair when i hit puberty I wasn gonna have it. I have been shaving ever since. I hate my boyfriends pubes and force him to shave or add a spider web to his crotch because it wont be getting anyEver since I noticed the slightest fuzz of pubic hair when i hit puberty I wasn gonna have it. Excuse me. Two white students from St. Albans School. cheap vibrators

    cheap dildos It’s confession time, but before I get to that, let me tell a true tale about penises and pills. Years ago, before yours truly decided to write about love and its usual sidekicks, sex and relationships, I sold drugs for a living, as in the legal pharmaceutical kinds. I remember the frenzy in the field when another company launched Viagra. My periods have always varied in pain/discomfort from month to month, and when I was in my mid teens I got so sick one month I was prescribed medication that I now take most months just to be on the safe side. However, as I have got older, I have developed worse discomfort, in that it’s sometimes uncomfortable just to sit down on the first couple of days of my period. I assume this is just another thing that happens as bodies change, etc, but does that sound like anything to get looked at? I am reluctant to go to the doctor for ‘just a period’ if nothing else is up cheap dildos.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>