節分享了利用css居中的五種方法,現在我們開始創建一個簡單但是有趣的網站。最終的樣子是這樣的:
步驟一
以語義化標簽開始是很好的。下面是我們的頁面構成:
#floater (to push the content into the middle)
#centred (the centre box)
#side
#logo
#nav (unordered list <ul>)
#content
#bottom (for copyright, etc.)
這是我用到的 xhtml 代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>A Centred Company</title>
<link rel="stylesheet" href="styles.css" type="text/css" media="all" />
</head>
<body>
<div id="floater"></div>
<div id="centered">
<div id="side">
<div id="logo">
<strong><span>A</span> Company</strong>
</div>
<ul id="nav">
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
</ul>
</div>
<div id="content">
<h1>Page Title</h1>
<p>Holisticly re-engineer value-added outsourcing after
process-centric collaboration and idea-sharing. Energistically
simplify impactful niche markets via enabled imperatives. Holisticly
predominate premium innovation after compelling scenarios.
Seamlessly recaptiualize high standards in human capital with
leading-edge manufactured products. Distinctively syndicate
standards compliant schemas before robust vortals. Uniquely
recaptiualize leveraged web-readiness vis-a-vis out-of-the-box
information.</p>
<h2>Heading 2</h2>
<p>Efficiently embrace customized web-readiness rather than
customer directed processes. Assertively grow cross-platform
imperatives vis-a-vis proactive technologies. Conveniently empower
multidisciplinary meta-services without enterprise-wide interfaces.
Conveniently streamline competitive strategic theme areas with
focused e-markets. Phosfluorescently syndicate world-class
communities vis-a-vis value-added markets. Appropriately reinvent
holistic services before robust e-services.</p>
</div>
</div>
<div id="bottom">
<p>Copyright notice goes here</p>
</div>
</body>
</html>
步驟二:
現在我們開始用一些基本的 CSS 來給頁面添加樣式。把以下代碼放入在我們的 html 頁面頂部被引入的 style.css。
html, body {
margin: 0;
padding: 0;
height: 100%;
}
body {
background: url('page_bg.jpg') 50% 50% no-repeat #FC3;
font-family: Georgia, Times, serifs;
}
#floater {
position: relative;
float: left;
height: 50%;
margin-bottom: -200px;
width: 1px;
}
#centered {
position: relative;
clear: left;
height: 400px;
width: 80%;
max-width: 800px;
min-width: 400px;
margin: 0 auto;
background: #fff;
border: 4px solid #666;
}
#bottom {
position: absolute;
bottom: 0;
right: 0;
}
#nav {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 70%;
padding: 20px;
margin: 10px;
}
#content {
position: absolute;
left: 30%;
right: 0;
top: 0;
bottom: 0;
overflow: auto;
height: 340px;
padding: 20px;
margin: 10px;
}
#centered {
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
}
h1, h2, h3, h4, h5, h6 {
font-family: Helvetica, Arial, sans-serif;
font-weight: normal;
color: #666;
}
h1 {
color: #f93;
border-bottom: 1px solid #ddd;
letter-spacing: -0.05em;
font-weight: bold;
margin-top: 0;
padding-top: 0;
}
#bottom {
padding: 10px;
font-size: 0.7em;
color: #f03;
}
#logo {
font-size: 2em;
text-align: center;
color: #999;
}
#logo strong {
font-weight: normal;
}
#logo span {
display: block;
font-size: 4em;
line-height: 0.7em;
color: #666;
}
p, h2, h3 {
line-height: 1.6em;
}
a {
color: #f03;
}
在我們能夠把 content 垂直居中之前, body 和 html 應該被拉伸到 100% 的高度。由于 height
在 padding 和 margin 之內,所以我們要把它們設成 0 以防止因為很小的 margin 出現滾動條。
floater 的 margin-bottom 是 content 高度(400px)的一半, -200px。
現在可以看到一下效果:
#centred 的寬度為 80%。這可以市網頁隨著顯示器的大小而變化。一般稱作流體布局。設置 min-width 和
max-width 以避免網頁過大或者過小。 但是 IE 不支持 min/max-width。顯然可以用固定寬度來代替。
因為 #centred 是相對定位的,在它里面我們可以用絕對定位來定位元素。設置 #content 的 overflow:auto;
以避免滾動條的出現。IE 不怎么喜歡 overflow:auto; 除非我們指定高度(不是 top 和 bottom 的定位,也不是 %)
因此我們給它指定高度。
步驟三
最后要做的就是再添加點樣式,讓頁面好看點。從目錄開始吧。
#nav ul {
list-style: none;
padding: 0;
margin: 20px 0 0 0;
text-indent: 0;
}
#nav li {
padding: 0;
margin: 3px;
}
#nav li a {
display: block;
background-color: #e8e8e8;
padding: 7px;
margin: 0;
text-decoration: none;
color: #000;
border-bottom: 1px solid #bbb;
text-align: right;
}
#nav li a::after {
content: '?';
color: #aaa;
font-weight: bold;
display: inline;
float: right;
margin: 0 2px 0 5px;
}
#nav li a:hover, #nav li a:focus {
background: #f8f8f8;
border-bottom-color: #777;
}
#nav li a:hover::after {
margin: 0 0 0 7px;
color: #f93;
}
#nav li a:active {
padding: 8px 7px 6px 7px;
}
需要注意的是 #centred 的圓角。 CSS3 中,應該有 border-radius 屬性來設定圓角的半徑(可參考 CSS3之旅: border-radius(圓角) – 糖伴西紅柿)。現在的流行的瀏覽器都還不支持,除非用 -moz(Molilla Firefox) 或者 -webit(Safari/Webkit) 前綴.
水平居中經常用,其實垂直居中也很有用的。平時用的最多的應該是上節講的方法五了,算是個小技巧吧。誰還有更好的方法嗎,歡迎留言交流。
evExpress WinForms擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
DevExpress WinForm 近日正式發布了2022年第一個重大版本——v22.1,此版本也正式升級了之前版本中發布的HTML CSS模板功能,歡迎下載最新版體驗!
DevExpress WinForms Subscription官方最新版免費下載試用,歷史版本下載,在線文檔和幫助文件下載-慧都網
v22.1 為我們的WinForms ListBox、ComboBox和Alert控件引入了 HTML 和 CSS 標記支持,使用HtmlTemplates屬性定義可應用于項目的HTML-CSS模板集合,閱讀以下文章:
處理以下事件以響應針對 HTML UI 元素的鼠標操作:
您現在可以將存儲庫項目包裝在 <input> 標記內。
'name' 屬性按名稱引用存儲庫項,'value' 屬性指定數據字段名稱。
HTML
<div class='default-avatar-container' hidden='${HasPhoto}'>
<input class='default-avatar' name='pictureEdit' value='${Photo}' />
<a class='choose-photo' id='choose_Photo'>Choose a photo</a>
</div>
<div class='default-avatar-container avatar-container' hidden='${IsDefaultPhoto}'>
<input class='avatar' name='pictureEdit' value='${Photo}' />
</div>
<div class='input-box'>
<input class='input' name='emailEdit' value='${Email}'/>
</div>
我們的Template Designer 在包括'In-place Editor Repository' 選項卡,打開此選項卡來創建要在 HTML 模板中引用的存儲庫項目。
HTML 模板設計器現在附帶以下選項:
我們添加了一個新的TileViewOptionsHtmlTemplate.ItemAutoHeight 選項來支持由HTML和基于CSS模板呈現的圖塊的自動高度模式,在自動高度模式下,磁貼會垂直拉伸以完全顯示內容。
我們支持以下 CSS 屬性:
點贊 + 關注 + 收藏=學會了
*請認真填寫需求信息,我們會在24小時內與您取得聯系。