Table of Contents
Reply to discussion (solution)
Home Web Front-end HTML Tutorial Why does the div centered with margin:0 auto; have spacing up and down? Even setting margin:0px; will not work_html/css_WEB-ITnose

Why does the div centered with margin:0 auto; have spacing up and down? Even setting margin:0px; will not work_html/css_WEB-ITnose

Jun 24, 2016 pm 12:17 PM

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en">	<head>		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />		<title>show2</title>		<meta name="author" content="Administrator" />		<!-- Date: 2012-12-12 -->		<style>			body{			}			.alignCenter{				margin: 10px auto;				width: 950px;							}		</style>	</head>	<body>								<style>		._head{width: 100%; float: left}		a{display:inline-block;margin-left: 20px;text-align: center;font-family:"Arial" "黑体";color: white;}		.nv_logo{width: 950px;height: 128px;}		.logo{width: 246px;height: 128px;float: left}		.nv{width: 704px;height: 30px;padding-top: 98px;background-image: url(src/nv.png);background-repeat: no-repeat;float: left;background-position: -10px 80px;text-align: center;}		</style>		<div class="_head">			<div class="alignCenter">				<div class="nv_logo">					<!--logo-->					<div class="logo">						<img src="src/logo.png" />					</div>					<!--导航-->					<div class="nv">						<a style="height: 20px;width: 80px;">首页</a><a style="height: 20px;width: 80px;">关于火狼</a><a style="height: 20px;width: 80px;">服务项目</a><a style="height: 20px;width: 80px;">案例展示</a><a style="height: 20px;width: 80px;">客户服务</a><a style="height: 20px;width: 80px;">联系我们</a><a style="height: 20px;width: 80px;">VIP服务</a>					</div>				</div>						</div>		</div>						<style>		._center{width: 100%;background-image: url(src/background.png);float: left}		._center dl{padding-top: 20px;margin-left: 20px;width: 910px;}		._center dt{}		._center dd{border-style: solid;border-color: #FFFFFF; border-left-width:0px;border-right-width:0px; margin-left: 0px;margin-top: 20px;}		</style>		<div class="_center">			<div class="alignCenter" style="background-image: url(src/centerground.png);">				<dl>					<dt><img src="src/showcases.png" /></dt>					<dd>						<a></a>					</dd>					<dt style="text-align: right;"><a>上一页</a><a>下一页</a></dt>				</dl>			</div>		</div>						<style>		._bottom{width: 100%;background-image: url(src/bottom.png);height: 95px;background-repeat: repeat-x;padding-top: 0px;float: left}				</style>		<div class="_bottom">			<div class="alignCenter">							</div>		</div>							</body></html>
Copy after login


Reply to discussion (solution)

.alignCenter{
margin : 10px auto;
width: 950px;

}


Is this the problem?

Initialize css
*{padding:0;margin:0;}

This method is not very recommended

Delay the loading speed
Generally I do this
/*Clear Internal and external margins*/
html,body,div,p,h1,h2,h3,h4,h5,h6,blockquote,ul,ol,li,table,th,td,span,img,dl,dd, dt,input,textarea,button{border:medium none;padding:0;margin:0;}
/*Set font*/
body,textarea,button,input,select{font:12px/1.5 Verdana , Geneva, sans-serif,'宋体','Microsoft Yahei';color:#333;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
em {font-style:normal;}
/*Set list*/
ul,ol{list-style:none;}
/*Set table*/
th,td{border-collapse :collapse;border-spacing:0;}
/*Set image*/
img{border:none;}
/*Set link*/
a{color:#999;text- decoration:none;outline:none;}
a:hover{text-decoration:underline;color:#06C;}
:focus{outline:none;}



This is about the effect after removing 10px of .alignCenter{

margin: 10px auto;

width: 950px;}


Ultimately

you just haven’t initialized the css

The most basic thing to write a page is to initialize the css

How to write initialization
I’ve already given it to you
It’s not complete
But it’s already a lot

In the final analysis
you just haven’t initialized the css

The most basic thing to write a page is to initialize the css

How to write the initialization
I have already given it to you
It’s not quite complete
but it’s quite a lot. Wow, newcomers don’t understand. Then I realized that I finally learned a trick, but I still don’t know where the problem lies. Where's definition overrides my definition. I added a *{margin:0;padding:0} to the header and solved it. But I used Firefox plug-ins for debugging, and the calculated CSS style has margins even though margin and padding are both 0. Anyway, thank you very much

Initialization is needed for every page you write
What you see with the plug-in is only what you wrote

There is space between each element

That’s why we need to initialize
Come on
(Initialization is to reset everything to zero)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles