如何让元素水平居中于动态宽度页面或容器中

现如今,自适应窗口的页面布局已经十分常见了,那么不同的显示器,不同的人群可能在查看页面时的显示效果必然不会相同.为了保证风格整体一致,那么在一个动态变化宽度的页面或容器中,元素居中将被常常用到.这里我并不是简简单单的给div加上一个text-align:center;属性后,其单独的块级子元素(例如img)自动居中,这种方法估计人人皆知.我将用另一个办法解决这个问题,例如子元素内比较复杂的导航条等等.这里我就以导航条的居中为例.

先来看看相关效果:

http://jsfiddle.net/kingterrors/AqTJV/embedded/result,html,css/

PS:由于近日(2014年6月)jsfiddle无法正常访问,可能受内网影响,现将之前所有jsfiddle预览去除,不过你仍然可将以上地址拷贝到浏览器预览,或选择尝试以下代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Horizontally center elements of a dynamic width</title>
<style type="text/css">
ul {display:table;margin:10px auto;min-width:320px;}
li {float:left;list-style:none;margin-left:5px;padding:5px 0;}
li:first-child { margin-left:0;}
li a {background:#82B5DA;border:1px solid #599CCE;border-radius:3px;box-shadow:0 0 3px rgba(0,0,0,0.3);padding:5px;color:#333;text-decoration:none;text-shadow:1px 1px 0 rgba(255,255,255,0.3);}
li a:hover { background:#599CCE;}
</style>
</head>
<body>
<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">Blog</a></li>
  <li><a href="#">Tutorials</a></li>
  <li><a href="#">About whidy</a></li>
</ul>
</body>
</html>

如果点了CSS查看,大家会发现这里用了display: table;这个估计很少会有人用,而且它有个很大的问题,就是这种居中的方式仅仅支持IE8+,那么IE7-不是悲剧了.因此,这里又提供一个更好的方案:

http://jsfiddle.net/kingterrors/AgxR5/embedded/result,html,css/

PS:由于近日(2014年6月)jsfiddle无法正常访问,可能受内网影响,现将之前所有jsfiddle预览去除,不过你仍然可将以上地址拷贝到浏览器预览,或选择尝试以下代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Horizontally center elements of a dynamic width</title>
<style type="text/css">
ul {margin-top:10px;text-align:center;min-width:330px;}
li {display:inline-block;list-style:none;margin-left:5px;padding:5px 0;}
li:first-child {margin-left:0;}
li {background:#82B5DA;border:1px solid #599CCE;border-radius:3px;box-shadow:0 0 3px rgba(0,0,0,0.3);padding:5px;color:#333;}
li:hover {background:#599CCE;}
li a {color:#333;text-decoration:none;text-shadow:1px 1px 0 rgba(255,255,255,0.3);}
li {*display:inline;zoom:1;}
</style>
</head>
<body>
<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">Blog</a></li>
  <li><a href="#">Tutorials</a></li>
  <li><a href="#">About whidy</a></li>
</ul>
</body>
</html>

这里有几点需要说明的是,块级元素前面已经提到是可以直接用text-align:center;居中的,而内联的也就是含有浮动的块级元素怎么处理呢,IE7对这个内联块级支持仍然不是很好.那么我们还要给li元素增加一个inline-block. zoom: 1;这样IE7就能很好的工作了.IE6好像还有点问题,那么就让IE 6 去shit吧.

总结:这是一个十分简单的关于动态宽度的页面或容器内元素自动居中的例子,我也参考过其他网友的方法,比如利用父子关系的定位,父级元素相对定位,子级元素按照百分比绝对定位的方法,不过个人感觉过于复杂,大部分情况下如果采用此文的方法也许会更好些.当然,面对不同的情景,大家也就根据自己需要进行选择了.

参考文章:http://css-plus.com/2012/05/how-to-horizontally-center-elements-of-a-dynamic-width/

[好歌推荐]Shivaree – Goodnight Moon

歌名: Goodnight Moon

歌手: Shivaree

专辑: I Oughtta Give You a Shot in the Head for Making Me Live in This Dump

歌词:

There’s a nail in the door
And there’s glass on the lawn
Tacks on the floor
And the TV is on
And I always sleep with my guns
When you’re gone

There’s a blade by the bed
And a phone in my hand
A dog on the floor
And some cash on the nightstand
When I’m all alone the dreaming stops
And I just can’t stand

继续阅读“[好歌推荐]Shivaree – Goodnight Moon”

[好歌推荐]Lisa Ekdahl – The Color Of You

歌名: The Color Of You

歌手: Lisa Ekdahl

专辑: Heaven Earth and Beyond

歌词:

Blue is the color of love
When your lover has gone from you
Your heart is filled with longing to have new
What came once from others
When you open your heart for two
But now it’s true and you realize
What illusion can do
If you close your eyes
But the dreaming is over now

So blue is the color of you
When you’re waiting for someone who
Will always make your dreams come true
But now it’s true and you realize
What illusion can do
If you close your eyes
But the dreaming is over now
So blue is the color of you
When you’re waiting for someone who
Will always make your dreams come true

The Color Of You
The Color Of You – Lisa Ekdahl

音乐轻快,活泼歌词中也体现了作者乐观积极的生活态度… 🙄

[好歌推荐]Matthew Barber – And You Give

歌名: And You Give

歌手: Matthew Barber

专辑: Ghost notes

歌词:

It started out two winters past
Time stood still we were moving too fast
You ran away April, May and June
When you came back I was over the moon

I was high, I was high over the moon
You were there, you were there midnight and noon oh
Bring it back, bring it back
We started too soon
I was high, I was high over the moon oh oh

Then came fall babe and we fell hard
Bruised our bodies, skinned our knees and our hearts
Then I got sick yeah but no one could tell
Now I drink your love drink right from the well

And it’s good and its good right from the well
Wanna taste, wanna touch, wanna see and smell oh
And it feels like it feels, heals me so well
Wanna drink, wanna drink right from the well oh oh

Now here I sit in my burning cell
Summer sun beating hotter than hell
I need your love like the air I breath
I need it more than you could ever believe

And you give and you give, give it to me
And you give and you give all that I need oh
And I take and I take, blood that you bleed
And you give and you give, give it to me oh

And you give and you give, give it to me
And you give and you give all that I need oh
And I take and I take, blood that you bleed
And you give and you give, give it to me oh

音乐轻松,优美…另外也推荐Where the river bends 😀

Matthew Barber – And You Give

儿时的回忆之格什温<<蓝色狂想曲>>有感

前几天, 听着音乐时,随机播放到这一曲<<Rhapsode in Blue(extract)>>,来自<<Best Piano Classics 100 (CD2)>>内track03.突然间,心中一股难以表达的心情.当然其他专辑也有,比如:<<The 50 Most Essential Pieces of Classical Music>>,当然也有电影Manhattan发行于1979年的插曲也是.

就是这曲,有一次几乎花了2,3个小时翻遍了每一首歌曲,却没有找到它,这次偶然再次遇到,当然不能再次忘记它的曲名,那么这个交响乐实际上应该是叫做<<Rhapsody in Blue>>于1924年美国作曲家乔治·格什温创作的.至于他的音乐背景大家可以自己去了解下.

好了,进入正题,为何称之为儿时的回忆,记得很小的时候,至于小学几年级看过电视节目上一部动画短片.当时感触也很深,至今记忆犹新.回想,从来没有想过小时候竟然有机会接触到如此名曲,甚至是当地电视台节目,每天大概6,7点因为电视节目少而随便穿插的一些国外的动画.现在回想,原来十五年前,当地电视台的节目策划人如此有品位…现如今再次看了一遍这段动画,觉得毫不落后,仍有着现代气息…只在网上找到了土豆的视频动画地址,与大家分享一下了.

不知道大家看了有什么感受…只能感觉大现代都市,繁华,仓促,不同角色的人们为生活而努力奋斗,虽然生活充满了无奈,心酸,悲伤,我们拥有的梦想始终难以实现,我们仍在继续…动画充实饱满,虽然不清楚作者的本意是否如此,但总能给人一些共鸣.极其感染力的节奏,让人回味无穷.

在网上也找了许多版本,感觉也都差不多.这里分享给大家,可以右键另存为,或者直接下载:

Gershwin: Rhapsody in Blue—Richard Trythall, pianist  18.1MB (00:19:51)

Rhapsody In Blue—Andre Previn,伦敦交响乐团 20.5MB (00:15:00)

格什温 蓝色狂想曲—Unknown 😯 6.54MB (00:03:33)

好了就写这么多吧.电脑上还有下载几个版本自己收藏着咯