简体   繁体   English

html-Div对齐问题

[英]html - Div alignment issues

So I am currently making a website for a friend of mine and I have set the left and right margin to 80px. 因此,我目前正在为我的一个朋友创建一个网站,并且将左右边距设置为80px。 This works for everything but my main body. 这适用于除主体以外的所有物体。 It seems that it expands past the right margin, and simply has a margin of 60px instead of 80px. 似乎它扩展到了右边距之外,并且仅有60px的空白,而不是80px。

Here is a screenshot: http://i.imgur.com/XtRdlUv.png 这是屏幕截图: http : //i.imgur.com/XtRdlUv.png

EDIT: I cut off some of the left margin, sorry for the confusion 编辑:我切断了一些左边距,对不起造成混乱

As seen with the red arrow, there seems to be an offset when their shouldn't. 如红色箭头所示,似乎不应该有偏移。

Here is my code: 这是我的代码:

 body { background: url(image) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; background-size: cover; text-align: center; margin-left: 80px; margin-right: 100px; } .wrapper { padding-top: 10px; text-align: left; margin: 0px auto; } .mainbody { width: 100%; outline: #293135 solid; background-color: #444444; margin-top: 40px; text-align: left; padding: 20px; color: #FFFFFF; } 
 <div class="mainbody" style="text-align: center"> <font face="Arial, Helvetica, sans" size="4"> <h1 style="text-decoration: underline">Download</h1> <p>Features Include:</p> </font> </div> 

You don't need 你不需要

width: 100%;

Since .mainbody is a block element, it will expand to fill all the remaining space. 由于.mainbody是一个块元素,它将扩展以填充所有剩余空间。

Otherwise, adding it produces the problem because of the content-box sizing model. 否则,由于内容框大小模型的原因,添加它会产生问题。

 body { background: url(image) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; background-size: cover; text-align: center; margin-left: 80px; margin-right: 100px; } .wrapper { padding-top: 10px; text-align: left; margin: 0px auto; } .mainbody { outline: #293135 solid; background-color: #444444; margin-top: 40px; text-align: left; padding: 20px; color: #FFFFFF; text-align: center; font-family: Arial, Helvetica, sans; font-size: 18px; } 
 <div class="mainbody" style=""> <h1 style="text-decoration: underline">Download</h1> <p>Features Include:</p> </div> 

It is likely because your .mainbody element is using the default content-box , which adds an extra left and right padding of 20px each on top of the 100% width. 这可能是因为您的.mainbody元素使用的是默认的content-box ,它在100%宽度的顶部分别添加了一个20px的左侧和右侧填充。 Therefore, the final computed width of the element would be 100% + 40px , which causes it to 'overflow' of sorts. 因此,元素的最终计算宽度将为100%+ 40px ,这将导致其“溢出”。

To fix this, simply declare box-sizing: border-box , ie: 要解决此问题,只需声明box-sizing: border-box ,即:

.mainbody {
    box-sizing: border-box;
    width: 100%;
    outline: #293135 solid;
    background-color: #444444;
    margin-top: 40px;
    text-align: left;
    padding: 20px;
    color: #FFFFFF;
}

In fact, it is recommended that you use this rule: * { box-sizing: border-box;} as recommended here . 实际上,建议您使用此规则: * { box-sizing: border-box;} ,如此处所建议

I think you're using old ways. 我认为您正在使用旧方法。 Try this! 尝试这个! .mainbody { width: calc(100% - 120px); .mainbody {width:calc(100%-120px); } This is your new css and JSFiddle link! 这是您的新CSS和JSFiddle链接! http://jsfiddle.net/Leo4v9rc/ http://jsfiddle.net/Leo4v9rc/

body {
  background: url(image) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  background-size: cover;
  text-align: center;
}
.wrapper {
  padding-top: 10px;
  text-align: left;
  margin: 0px auto;
}
.mainbody {
  width: calc(100% - 120px);
  outline: #293135 solid;
  background-color: #444444;
  margin:40px auto 0 auto;
  text-align: left;
  padding: 20px;
  color: #FFFFFF;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM