【问题标题】:space-around behaving differently in chrome vs firefox when column / row is single当列/行为单行时,chrome 与 firefox 中的空间行为不同
【发布时间】:2019-03-07 11:19:13
【问题描述】:

在 chrome 中,如果它是单列,则空格不会将项目居中。 但在 Firefox 中,它可以工作。

如何让它表现得像 firefox?

另外,请记住文本右对齐


.flex-cont {
  display: flex;
  justify-content: flex-start;
  flex-flow: column wrap;
  align-content: space-around;
  align-content: space-evenly;
  align-items: flex-end;
}

.flex-item {
  /* display: inline-block; */
  flex: 0 1 auto;
  width: fit-content;
}

http://jsfiddle.net/f6k7xoe0/1/

edit: 我也可以这样做,但这会导致文本向右对齐:

.flex-cont{
  align-items: center;
}

编辑:老实说,如果它是一种爱好,我不会太在意,但我在我的应用程序中添加了 cefsharp(chrome)。将投入生产。没有其他办法。我必须在 cefsharp 中获得该渲染。

编辑: 这不是重复的。

  • 我不问为什么它不起作用。我想要一个解决方案
  • 我的输出不同。其他问题的输出甚至不是多列的。

【问题讨论】:

  • 首先要注意的是space-around 甚至没有被应用。根据代码中的顺序,它被space-evenly 覆盖。
  • 要注意的第二件事是问题甚至与space-around / space-evenly 无关。这是关于align-content,它仅适用于多行弹性容器(即,它在单行弹性容器中被忽略)。
  • 但是,正如您所注意到的,不同的浏览器对规范语言有不同的解释。该行为在两个副本中进行了解释。
  • 好的,我明白了。但它不能解决问题。我必须让它工作。无论是它的解释/还是他们懒于解释。我现在做了一个工作。无论如何,谢谢。

标签: html css google-chrome firefox flexbox


【解决方案1】:

edit2:我通过 js getboundrect compare 解决了它获取每个项目的最大宽度,如果发生换行,它们会应用边距。但它的凌乱不想使用它。但我必须这样做。

如果您在 doit() 函数中提供适当的 CssSelector,我清理了代码以使其应用所有 flex-container、flex 项。它会起作用的。但这是用于列的。

http://jsfiddle.net/yeaqrh48/1203/

    var debug = true;

    class ertTimer {
      constructor(funcName ,intervalms=3500, maxRunDuration=20000 , StopIfReturnsTrue=true ) {

          this.intervalObj = setInterval(function(){
            console.log("interval - funcName:" + funcName.name);
            try{  
                var res = funcName();

                if(StopIfReturnsTrue)
                    if(res == true)
                        clearInterval(intervalObj);

            } catch(exx){console.warn(exx.message, exx.stack);}
        }, intervalms);
        // after 15 sec delete interval
        setTimeout( function(){ clearInterval( intervalObj ); },maxRunDuration);

        this.intervalms = intervalms;
        this.maxRunDuration = maxRunDuration;
      }

      get getter_intervalms() { return this.intervalms; }
      calcRepeatTimes() { 
       return this.maxRunDuration / this.intervalms;
      }
    }


    var center_ONsingleCol_nonFF = function(contNode, itemSelector) {
      var items = contNode.querySelectorAll(itemSelector);
      //arr.count shoud be 1 element  // items[0].style.alignItems = "center";
      var parItem = items[0].parentNode;
      var parItemR = parItem.getBoundingClientRect();
      var parWidth = parItemR.width;
      var maxItemWidth = 0;

      for (var k = 0; k < items.length; k++) {
        var currItem = items[k].getBoundingClientRect();
        if (currItem.width > maxItemWidth) 
           maxItemWidth = currItem.width;
        //console.log(parWidth, itemWidth);
      }

      var alignItemsVal = getComputedStyle_propValue(parItem , "align-items");
      var flexDirVal = getComputedStyle_propValue(parItem , "flex-direction");


      var iswrapped = isWrapped(contNode ,itemSelector );
      for (var k = 0; k < items.length; k++) {
        if(iswrapped && flexDirVal ==  "column" ){
          if(alignItemsVal == "flex-end"){
            items[k].style.marginRight = "" + ((parWidth - maxItemWidth) * 0.5) + "px";
            items[k].style.marginLeft = "";
          }
          else if(alignItemsVal == "flex-start"){
            items[k].style.marginRight = "";
            items[k].style.marginLeft = "" + ((parWidth - maxItemWidth) * 0.5) + "px";
          }else
          {
            items[k].style.marginRight = "";
            items[k].style.marginLeft = "";
          }
        }
        else{
          items[k].style.marginRight = "";
          items[k].style.marginLeft = "";
        }
      }

    };
    var getComputedStyle_propValue = function(element , CssPropName){
    //var element = document.querySelector( selector );
    var compStyles = window.getComputedStyle(element);
    var comStyle_xxx = compStyles.getPropertyValue(CssPropName);
    return comStyle_xxx;
    };

    var colorizeItem = function(items) {
      for (var k = 0; k < items.length; k++) {
        items[k].style += ";background:Red;";
      }
    };
    var detectWrap = function(contNode, item_selector) {
      var wrappedItems = [];
      var prevItem = {};
      var currItem = {};
      var items = contNode.querySelectorAll(item_selector);
      //console.log("wrapped item arrrat::",items);

      for (var i = 0; i < items.length; i++) {
        currItem = items[i].getBoundingClientRect();
        if (prevItem && prevItem.top > currItem.top) {
          wrappedItems.push(items[i]);
        }
        prevItem = currItem;
      }

      return wrappedItems;
    };
    var isFirefox = function() {
      var _isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
      return _isFF;
    };
    var isWrapped = function(contNode, item_selector){
         var wrappedItems = detectWrap(contNode, item_selector);
        //colorizeItem(wrappedItems);
        if (wrappedItems == null || wrappedItems.length == 0) 
            return true;
        else
          return false;
    };
    var isWired_listenContResize = false;
    var doit = function() {

      if (isFirefox()) {
        console.log("ff already works Right. ");
        return;
      } else {
        console.log("not ff. script will run. ");
      }

      /* flexItem_selector   must be relative to flexCont*/
      var flexContainer_selector = ".flex-cont.cont-resize"; /*specific flex-cont */
      var flexItem_selector = ".flex-item";

      var contList = document.querySelectorAll(flexContainer_selector);
      for (var i = 0; i < contList.length; i++) {
        //no such event   //there is external lib..
        // call doit after you change size in the code;
        if (!isWired_listenContResize) {
          contList[i].onresize = function() {  doit();  };
        }

        center_ONsingleCol_nonFF(contList[i], flexItem_selector);
      }
      isWired_listenContResize = true;


    };

    window.onresize = function(event) {  doit(); };
    window.onload = function(event) { 
      doit(); 
        const et1_ = new ertTimer(doit , 500, 320000,true );

    };

【讨论】:

    猜你喜欢
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2012-08-06
    • 2018-02-16
    • 2015-10-17
    • 2014-10-24
    相关资源
    最近更新 更多