最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Browser default padding - Stack Overflow

programmeradmin0浏览0评论

Browsers have default padding for HTML page. For example my FF sets 8px margin of body element by default. I can calculate default width padding for HTML page using

jQuery(window).width() - jQuery('body').innerWidth();

due to body element spans all available browser viewport width.
Also I have other browser which sets different values for width and height padding.
Could you propose way to calculate height padding?
Previous code will not work due to body inner height will return actual page content height and will not span all available height.

Browsers have default padding for HTML page. For example my FF sets 8px margin of body element by default. I can calculate default width padding for HTML page using

jQuery(window).width() - jQuery('body').innerWidth();

due to body element spans all available browser viewport width.
Also I have other browser which sets different values for width and height padding.
Could you propose way to calculate height padding?
Previous code will not work due to body inner height will return actual page content height and will not span all available height.

Share Improve this question edited May 13, 2011 at 20:58 Mario 2,9421 gold badge27 silver badges38 bronze badges asked May 13, 2011 at 20:30 Mykhaylo AdamovychMykhaylo Adamovych 21k26 gold badges109 silver badges148 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Do you really need to calculate the padding? The padding is defined in an element's css setting for padding and margin.

You can get these easily in jQuery:

// Select the body element.
var bodyElement = $('body');

// Get the paddings
var widthPadding = bodyElement.css('padding-left') + bodyElement.css('padding-right');
var heightPadding = bodyElement.css('padding-top') + bodyElement.css('padding-bottom');

// Get the margins
var widthMargin = bodyElement.css('margin-left') + bodyElement.css('margin-right');
var heightMargin = bodyElement.css('margin-top') + bodyElement.css('margin-bottom');

You can remove the default user-agent (i.e. the browser's) settings by defining in your css file:

body {
    margin: 0;
    padding: 0;
}

Bypass the problem and use a CSS reset.

With a good reset, you will not have to calculate these as they will be set by yourself.

发布评论

评论列表(0)

  1. 暂无评论