css 显示:内嵌边距、填充、宽度、高度

bpzcxfmw  于 2023-03-14  发布在  其他
关注(0)|答案(3)|浏览(137)

如果我为任何元素设置display:inline,那么marginpaddingwidthheight是否不会影响该元素?
在该元素上使用marginpaddingwidthheight时,是否必须使用float:leftdisplay:blockdisplay:inline-block

b4qexyjb

b4qexyjb1#

请参阅CSS规范中的“内联格式上下文”以获得完整的解释。
基本上,可以在内联级元素上设置边距、填充和边框,但它们的行为可能与您预期的不同。如果只有一行,行为可能还可以,但同一个流中的其他行可能会表现出与您预期不同的行为(即将不考虑填充)。此外,如果内联框包含可断开的元素并到达包含元素的边距,则内联框可能会断开;在这种情况下,在断点处,也将不考虑边距和填充。
我找到了一个很好的列表例子:http://www.maxdesign.com.au/articles/inline/

hgb9j2n6

hgb9j2n62#

填充将适用于inline。但高度和宽度不适用。
http://jsfiddle.net/d89Wd/

avwztpqn

avwztpqn3#

我知道这是一个很晚的答案,但我写了一个jQuery插件,支持填充内联元素(与断字)看到这个JSfiddle:
http://jsfiddle.net/RxKek/
插件代码:

$.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:

<div>text</div>

in stead of this:

<div>
    text
</div>
*/

$.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 < maxWidth) {
        return;
    }

    //Check how many times the container fits within the box
    var times = Math.ceil(width / maxWidth);

    //Function for cleaning chunks
    var cleanChunk = function (chunk) {
        var thisChunkLength = chunk.length - 1;

        if (chunk[0] == " ") chunk = chunk.substring(1);
        if (chunk[thisChunkLength] == " ") chunk = chunk.substring(0, thisChunkLength);

        return chunk;
    };

    //Divide the text into chunks
    var text = $(this).html();
    var textArr = text.split(" ");

    var chunkLength = Math.ceil((textArr.length - 1) / times);
    var chunks = [];

    var curChunk = "";
    var curChunkCount = 0;

    var isParsingHtml = false;

    //Loop through the text array and split it into chunks
    for (var i in textArr) {
        //When we are parsing HTML we don't want to count the
        //spaces since the user doesn't see it.
        if (isParsingHtml) {
            //Check for a HTML end tag
            if (/<\/[a-zA-Z]*>/.test(textArr[i]) || /[a-zA-Z]*>/.test(textArr[i])) {
                isParsingHtml = false;
            }
        } else {
            //Check for a HTML begin tag
            if (/<[a-zA-Z]*/.test(textArr[i])) {
                isParsingHtml = true;
            }
        }

        //Calculate chunks
        if (curChunkCount == (chunkLength - 1) && !isParsingHtml) {
            curChunk += textArr[i] + " ";
            chunks.push(cleanChunk(curChunk));
            curChunk = "";
            curChunkCount = -1;
        } else if ((i == (textArr.length - 1))) {
            curChunk += textArr[i];
            chunks.push(cleanChunk(curChunk));
            break;
        } else {
            curChunk += textArr[i] + " ";
        }

        if (!isParsingHtml) {
            curChunkCount++;
        }
    }

    //Convert chunks to new elements
    var el = $($(this).html("").outerHTML());

    for (var x in chunks) {
        var new_el = el.clone().html(chunks[x]).addClass("text-render-el");
        var new_el_container = $("<div/>").addClass("text-render-container");

        new_el_container.append(new_el);

        $(this).before(new_el_container);
    }

    //Finally remove the current element
    $(this).remove();
});
};

相关问题