display:inline with margin, padding, width, height

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

If I set display:inline for any element then will margin, padding, width, height not affect on that element?

Is it necessary to use float:left or display:block or display:inline-block to use margin, padding, width, height on that element?

回答1:

Please see "Inline formatting contexts" at the CSS spec for the full explanation.

Basically margin, padding and border can be set on inline-level elements, but they may not behave as you expect. The behavior will probably be OK if there's only one line, but other lines in the same flow will likely exhibit behavior different from your expectations (i.e. padding will not be respected). In addition, your inline box can be broken if it contains breakable elements and reaches the margin of the containing element; in that case, at the point of break, margins and padding will not be respected as well.

Found a nice example of that with lists: http://www.maxdesign.com.au/articles/inline/



回答2:

Padding will work for inline. Height and width however will not.

http://jsfiddle.net/d89Wd/

Edit:corrected



回答3:

I know this is quite a late answer but I wrote a jQuery plugin which support padding on inline elements (with word breaking) see this JSfiddle:

http://jsfiddle.net/RxKek/

Plugin Code:

$.fn.outerHTML = function () { // IE, Chrome & Safari will comply with the non-standard outerHTML, all others (FF) will have a fall-back for cloning return (!this.length) ? this : (this[0].outerHTML || (     function (el) {         var div = document.createElement('div');         div.appendChild(el.cloneNode(true));         var contents = div.innerHTML;         div = null;         return contents;     })(this[0])); };  /* Requirements:  1. The container must NOT have a width! 2. The element needs to be formatted like this:  
text
in stead of this:
text
*/ $.fn.fixInlineText = function (opt) { return this.each(function () { //First get the container width var maxWidth = opt.width; //Then get the width of the inline element //To calculate the correct width the element needs to //be 100% visible that's why we make it absolute first. //We also do this to the container. $(this).css("position", "absolute"); $(this).parent().css("position", "absolute").css("width", "200%"); var width = $(this).width(); $(this).css("position", ""); $(this).parent().css("position", "").css("width", ""); //Don't do anything if it fits if (width /.test(textArr[i]) || /[a-zA-Z]*>/.test(textArr[i])) { isParsingHtml = false; } } else { //Check for a HTML begin tag if (/").addClass("text-render-container"); new_el_container.append(new_el); $(this).before(new_el_container); } //Finally remove the current element $(this).remove(); }); };


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!