[英]How to remove the empty space between divs in CSS?
我正在建立一個非常簡單的網站,以基本的HTML和CSS刷新我的自我。 我正在使用Dreamweaver CS6。 我希望以“正確”的方式學習各個方面。 我認為我可以使用邊距將div移動到想要的位置(向上或向下),但是我不確定這是否是實現它的正確方法。
這是一個例子。 我截圖了。
http://i.imgur.com/YyYpfcz.png
標題和容器div的頂部之間存在間隙。 內容div與頁眉和頁腳div之間存在間隙。
如何使這些div彼此齊平以消除間隙? 謝謝!
注意:我已經研究了此站點上與此類似的所有其他問題,但沒有一個完全是這個問題,也沒有以最基本的形式解決這些問題。
編輯:每個人都說了正確的方法后得到了許多不同的答案。 這讓我感到困惑。 而且我沒有得到他們最初的每個帖子的答案。 我很好奇為什么這些div在默認情況下不會彼此齊平。 稍后,我將在網站中創建更多div。 如果我不明白您的方法為何有效,那么我將無法在該站點的更多div上實現它。 謝謝!
不確定您要尋找什么。 觀察者,您仍然需要玩弄事物。 但是,看看這是否對您來說是一個好的開始。 我添加了background color
來顯示這些部分。 您可以刪除。
這是有關使用Dreamweaver制作第一個網站的很好的教程。 從這里開始。 :) http://www.adobe.com/devnet/dreamweaver/articles/first_website_pt1.html
是的,埃里克·邁耶(Eric Meyer)重置是“正確的方法”之一,比使用*更好
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>North Carolina Golf Car Dealers</title>
<style type="text/css">
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
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;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
#container {
width: 960px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>Header goes here.</h1>
</div>
<div id="content">
<p>Content goes here</p>
</div>
<div id="footer">
<p>Footer goes here.</p>
</div>
</div>
</body>
</html>
理想情況下,您應該使用外部樣式表將HTML和CSS分開:
因此是HTML:index.html(例如)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>North Carolina Golf Car Dealers</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="container">
<div id="header">
<h1>Header goes here.</h1>
</div>
<div id="content">
<p>Content goes here</p>
</div>
<div id="footer">
<p>Footer goes here.</p>
</div>
</div>
</body>
</html>
和CSS:style.css(例如)
@charset "utf-8";
/* CSS Document */
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
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;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
#container {
width: 960px;
margin: 0 auto;
}
注意:我從以下位置更改了CSS#容器:
#container {
width: 960px;
margin-right: auto;
margin-left: auto
至:
#container {
width: 960px;
margin: 0 auto;
}
結合右邊距和左邊距(兩種方法都可以使用...)
看起來這是行高問題,而不是div之間的空格。 嘗試為div添加邊框,看看它們是否接觸。
div {border:1px純黑色; }
我認為這是'p'的特征,嘗試刪除div#content內的'p',我認為可以消除差距。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.