巧用CSS3的calc()宽度计算做响应模式布局代码分享

巧用CSS3的calc()宽度计算做响应模式布局代码分享,这里我们先看下老外程序员写的:

Dealing with CSS browser compatibility and solving technical issues is probably the real charm (I mean it) of front-end development. None of the projects do without it. Working on one of those I finally had my first experience with CSS function calc():

With calc(), you can perform calculations to determine CSS property values.

I know that you know this, and I also know that you have already seen these examples and have even tried them at home:

div
{
	width: calc( 100% - 2.5em );
	background-position: calc( 50% + 50px );
	margin: calc( 1.25rem - 5px );
}

As you see, calc() takes writing CSS to much higher level. It is so powerful that front-endershave no idea what to do with it yet. And with the recent appearance of KitKat (Android 4.4) the function has become supported in all major browsers (sorry 3rd brother, Opera Mini). Moral: start using it, wisely.

Situation

Imagine this painfully standard situation: content and sidebar areas floated side by side, separated by gap:

Usually you have two div’s wrapped into one and styled:

<div class="container">
	<div class="content">Content</div>
	<div class="sidebar">Sidebar</div>
</div>
.container,
.content,
.sidebar
{
	box-sizing: border-box;
}
.container
{
	width: 100%;
	max-width: 80em; /* 1280 */
	padding: 2.5em; /* 40 */
}
	.content
	{
		width: 80%; /* 960 */
		float: left;
	}
	.sidebar
	{
		width: 16.666%; /* 200 */
		float: right;
	}

@media only screen and (max-width: 40em) /* 640 */
{
	.content,
	.sidebar
	{
		width: 100%;
		float: none;
	}
	.sidebar
	{
		margin-top: 1.25em; /* 20 */
	}
}

Looks like a perfectly responsive layout and there is nothing left to add, however…

Problem

The layout I worked on had quite a small initial width of the sidebar and a wide content area. Therefore, when resizing down the viewport, the sidebar becomes extremely tight very quickly. What to do when the sidebar gets too narrow and moving it under the content is yet too early?

Solution

It is a fixed-width sidebar, calc(), and a fallback for browsers that do not support this function. A minor modification of CSS classes .content and .sidebar brings these three bolds to life:

.content
{
	width: 80%; /* 960 */
	width: calc( 100% - 15em ); /* 240 */
	float: left;
}
.sidebar
{
	width: 16.666%; /* 200 */
	width: calc( 12.5em ); /* 200 */
	float: right;
}

If you were attentive you should see that the line width: calc( 12.5em ) is the trick here. Why? Let’s see all these widths like in pairs – exactly what we need browsers to do.

The first pair is width: 80% (content) and width: 16.666% (sidebar) that does the job in calc() incompatible browsers, which will just ignore calc(). It is the callback (backup). It means that the minority of website visitors will see narrower sidebar on narrower viewports. And that should be perfectly fine for people using older browser versions.

The secondwidth: calc( 100% - 15em ) and width: calc( 12.5em ). This is for newer browser versions which will override the previous width definitions. There are a couple of things to explain:

  • Content: calc( 100% - 15em ) = 100% - sidebar width - gap width. Besides that this calculates the width of the content element, it also assures that the gap between the sidebar and content is always equal to 2.5em (40px). Total win for perfectionists.
  • Sidebar: calc( 12.5em ) – who said that you need to calculate something between the brackets. This little trick cheats browsers into seeing width properties in pairs and using the only understandable one for them. I called this “imitating calc() fallback”, but actually it is vice versa. Um…

Party… Demo time! I made a button to imitate calc() support, you may want to try it:

SBB3 PRO3 Key Programmer

One more thing

Just like many other CSS features, calc() was an experimental one and only available under the vendor prefixes some time ago. So let’s get the maximum of it:

.content
{
	width: 80%; /* 960 */
	width: -webkit-calc( 100% - 15em ); /* 240 */
	width: -moz-calc( 100% - 15em ); /* 240 */
	width: calc( 100% - 15em ); /* 240 */
	float: left;
}
.sidebar
{
	width: 16.666%; /* 200 */
	width: -webkit-calc( 12.5em ); /* 200 */
	width: -moz-calc( 12.5em ); /* 200 */
	width: calc( 12.5em ); /* 200 */
	float: right;
}

There is a way to do without calc(), and that is by taking the advantage of CSS property position. However, the whole layout would fail (which you do not want) if the sidebar was higher than the content area.

本文参考:https://www.chinaobd2.com/wholesale/cgdi-at-200-ecu-programmer-for-bmw.html
            https://www.chinaobd2.com/wholesale/autel-maxiim-im508-immo-key-programmer.html

原文地址:https://www.cnblogs.com/aid12580/p/10680757.html

时间: 2024-08-13 23:38:55

巧用CSS3的calc()宽度计算做响应模式布局代码分享的相关文章

巧用CSS3的calc()宽度计算做响应模式布局

今天浏览这个http://www.sitepoint.com站时,因为好奇看了下人家写的代码,结果发现了这行代码, 于是就研究了一下,calc()从字面我们可以把他理解为一个函数function.其实calc是英文单词calculate(计算)的缩写,是 css3的一个新增的功能,用来指定元素的长度.比如说,你可以使用calc()给元素的border.margin.pading.font-size 和width等属性设置动态值.为何说是动态值呢?因为我们使用的表达式来得到的值.不过calc()最

使用rem来做响应式布局(js动态加载)

1 <script> 2 ;(function (doc,win) { 3 var htmlEle=doc.documentElement; 4 var reload="orientationchange" in window ? "orientationchange":"resize"; 5 function setFontsize () { 6 //每次通过屏幕转动或者重新设置宽高的时候获取屏幕宽度 7 var clientWid

CSS网页错位之DIV CSS宽度计算

DIV CSS宽度计算之CSS网页布局错位(体感音乐) 为什么计算宽度计算网页像素宽度是为了CSS网页布局整齐与兼容.常见的我们布局左右结构网页或使用padding.margin布局的时候将计算整页宽度,如果不计算无论是宽度过大过小就会出现错位问题. 怎么计算CSS宽度例一:我们计算一个左右结构的布局样式.假如总宽度为400px,那么左右加起来就应当小于400px,那我们可能左边为300px,右边为100px(体感音乐)正确代码: <!DOCTYPE html> <head> &l

简单的CSS3实现响应式布局

css3的@media属性实现页面响应式布局示例代码 <html><head> <style> * { margin:0; padding:0; } .ul { background-color:rgb(134, 170, 209); height: 55px; } .ul li { float:left; list-style: none; background-color:rgb(134, 170, 209); width: 20%; height: 100%; }

CSS3 calc() 会计算的属性

CSS3 calc() 会计算的属性 calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,你可以使用calc()给元素的border.margin.pading.font-size和width等属性设置动态值. 以前我们可以使用box-sizing:border-box;来设置盒子的属性为不加上边距.现在我们又多了一个选择了.但要注意,两者只能使用一个哦,否则就会造成冲突了. 怎么使用 calc()可以使用数学运算中的简单加(+).减(-).乘(*)和除(/)来解决问

CSS3的calc()使用

转自:http://www.w3cplus.com/css3/how-to-use-css3-calc-function.html calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点 也让我百思不得其解,今天有一同事告诉我,说CSS3中有一个属性能实现自适应的布局,首先让我想到的是box-sizing,但跟我说还可以计算,这让 我不得不想起calc().因为早先在官网和一些blog上看到相关的介绍,但一直没有

【前端】CSS3的calc()使用

calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点也让我百思不得其解,今天有一同事告诉我,说CSS3中有一个属性能实现自适应的布局,首先让我想到的是box-sizing,但跟我说还可以计算,这让我不得不想起calc().因为早先在官网和一些blog上看到相关的介绍,但一直没有深入,也没有自己去测试过.今天花了一下午的时间彻底学习了一下calc().于是就有了这篇blog,希望对大家有所帮助. 平时在制作页面的

CSS的单位及css3的calc()及line-height百分比

锚点:css中百分比减去固定元素 单位介绍 说到css的单位,大家应该首先想到的是px,也就是像素,我们在网页布局中一般都是用px,但是近年来自适应网页布局越来越多,em和百分比也经常用到了.然后随着手机的流行,web app和hybrid app的开发,都用到了css3技术,在css3中,新增了许多单位,rem.vw和vh.vmin和vmax.ch和ex等等,那现在对这些单位分别做一下详细的介绍吧. em 做前端的应该对em不陌生,不是什么罕见的单位,是相对单位,参考物是父元素的font-si

CSS3的calc()用法简单介绍

CSS3的calc()用法简单介绍:calc是calculate(计算的意思)缩写.从calc()的外表来看,它类似于程序语言中的方法,具有计算功能.确实如此,它可以动态的计算css中一些元素的相关属性值,能够运用简单的"+"."-"."*"."/"四则运算.基本规则如下:(1).可以使用百分比.px.em.rem等单位.(2).可以混合使用各种单位进行计算.下面看几个代码片段: .box{ border:1px solid