2012-09-07 118 views
0

我有下面的代碼,例如:對齊到最右側

的jsfiddle:http://jsfiddle.net/ntcg3/4/

HTML:

<html> 
    <p class="hey">hi</p> 
</html>​​​​​​​​​​​ 

CSS:

.hey { 
    width:100%; 
    background-color:red; 
    margin:0; 
    padding:0; 
} 

頁面右側仍有一些填充。

我該如何擺脫填充,使紅色背景顏色延伸到頁面的最後?

+0

嗨..歡迎來到SO ..你能不能給我們展示更多的代碼..或者,也許你可以在JSFiddle.net上創建一個小提琴來展示它。 –

+0

你使用什麼瀏覽器? – Michael

+0

我正在使用chrome – agassi0430

回答

0

當你默認了<body>和其他一些元素,如段落標記(<p>),標題標籤(<h1><h2> ... <h6>)等創建一個HTML頁面...有默認paddingmargins

所以你需要通過添加以下CSS來清除這些保證金:

html, body, p, h1, h2, h3, h4, h5, h6 { 
    margin:0; 
    padding:0; 
} 

詳情見CSS RESET

復位樣式表的目的是減少之類的東西默認的行高度,頁邊距和標題的字體大小,等瀏覽器的不一致性。

一個CSS復位的例子:

html, body, div, span, applet, object, iframe, 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
a, abbr, acronym, address, big, cite, code, 
del, dfn, em, img, ins, kbd, q, s, samp, 
small, strike, strong, sub, sup, tt, var, 
b, u, i, center, 
dl, dt, dd, ol, ul, li, 
fieldset, form, label, legend, 
table, caption, tbody, tfoot, thead, tr, th, td, 
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary, 
time, mark, audio, video { 
    margin: 0; 
    padding: 0; 
    border: 0; 
    font-size: 100%; 
    font: inherit; 
    vertical-align: baseline; 
} 
0

嘗試使用下面的CSS

*{margin:0; padding:0} 
+0

試過了。沒有幫助 – agassi0430

1

試試這個:

display:block; 
text-align:right; 

你會得到你需要的輸出

0

這一個嘗試:

.hey { 
    width:100%; 
    margin:0; 
    padding:0; 
    background-color:red; 
} 
0

正如您應該知道的那樣,瀏覽器會將其自己的樣式添加到元素中,並且與在ul標籤上創建的邊距相同,則會在body標籤上創建邊距。我會建議首先檢查元素(在大多數瀏覽器上使用control + shift + c),以便熟悉這些生成的樣式,或者有許多「重置」css文件可以從樣式中刪除填充和邊距或其他類似的東西

總之,如果你包括: html,body {margin:0;填充:0; } 它將確保html和body標籤不會用完任何邊距或填充

0

在您的css文件的開始處,您需要重置值。

html, body, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, caption, tbody, tfoot, thead, article, aside, dialog, figure, footer, header, hgroup, nav, section {margin:0;padding:0;border:0;font-weight:inherit;font-style:inherit;font-size:100%; } 

它會將您的所有默認值重置爲零。

0

嘗試設置;

body,html{ 
    padding: 0; 
    margin: 0; 
} 

Here是工作提琴。