【问题标题】:Margin, position and padding not working when display:inline is set. also weird behaviour from relative position设置 display:inline 时,边距、位置和填充不起作用。从相对位置也有奇怪的行为
【发布时间】:2012-02-05 15:30:41
【问题描述】:

我有两个 CSS 类:

.class1 {
    height: 100%;
    width: 300px;
    border: 1px none #B0B0B0;
    position: relative;
    display: inline;
    left: 10px;
}
.class2 {
    height: 100%;
    width: 200px;
    position: relative;
    display: inline;
    margin-left: 15px;
    background-color: #00CCCC;
    border-top-width: 1px;
    border-right-width: 1px;
    border-bottom-width: 1px;
    border-left-width: 1px;
    border-top-style: solid;
    border-right-style: solid;
    border-bottom-style: solid;
    border-left-style: solid;
}

现在,如您所见,它们都设置为显示在一行中(元素之间没有换行符)。哪个工作正常。但由于某种原因,自从我将显示设置为内联后,Padding、Positioning 和 Margin CSS 都刚刚停止工作。我可以添加一个左 10 英寸的边距,什么都不会发生。与填充和定位相同。

谁能解释一下如何解决这个问题?

另外,我在两个类上都设置了相对位置,但是在浏览器中查看页面时,.class2 超过圈 .class1 而它应该在 .class1 之后。

有什么想法吗?

编辑

好的,所以我做了一个 JSFiddle,但它似乎在那里玩得更多......

看起来Width 不起作用....

这里是:

http://jsfiddle.net/zYbwh/1/

【问题讨论】:

    标签: css styling


    【解决方案1】:

    这就是你在使用模板时遇到的问题,我用 php 编写了一个网站,但设计让我很生气。 所以我为网页设计师尝试了一些火箭燃料。

    这就是我每一步都遇到的问题...... 内联块对我不起作用,什么都不起作用,因为它不是我的设计,我不知道设置。

    我自己尝试过设计,但是我没时间了,我昨天需要一个设计。

    我建议您从模板中获取您需要的内容并删除其他所有内容,这将缩小您的问题并节省您的时间。

    【讨论】:

      【解决方案2】:

      你需要使用

      display: inline-block;
      

      相反。 margin 不适用于 display: inline 元素,但 inline-block 可以。然后,您可以拥有一个带有边距和明确宽度/高度的内联元素。

      要在 IE7 中实现这一点,请添加以下两行:

      *display: inline;
      zoom: 1;
      

      这很可怕,但它确实有效。

      【讨论】:

      • 我试过内联块。这些元素根本不会出现在一行中。它只是一个低于另一个......
      • 您能在JSFiddle 中发布一个简单的测试用例吗?此外,使用 Firebug 或 Chrome 的检查器查看实际应用到元素的样式会很有帮助。
      • 在 IE 中,如果你从内联元素开始,你不应该需要可怕的东西。因此,当您需要inline-block 时,只需使用span 而不是div,应该没问题。 (我曾经为这个愚蠢花了 2 个小时>.
      • 在小提琴中,你仍在使用inline,你被告知它不起作用。如果你切换到inline-block,它是一个在另一个之下,因为容器宽度不足。如果你解决了这个问题,它可以在 Chrome 上运行;由于上述 IE 错误,它仍然不会在 IE 上运行(只有最初的 inline 元素可以变为 inline-block,因此您需要像 @JamWaffles 描述的那样临时诱使它们变为 inline,或者使用默认的元素到inline,比如span)。
      【解决方案3】:

      我知道这是一个很晚的答案,但我写了一个 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();
      });
      

      };

      【讨论】:

        猜你喜欢
        • 2013-01-09
        • 2018-03-23
        • 2017-04-15
        • 1970-01-01
        • 2015-01-26
        • 2016-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多