【问题标题】:Centering flex item content居中弹性项目内容
【发布时间】:2020-02-06 04:29:39
【问题描述】:

我在一个弹性容器内有以下弹性项目 (#globalSearchContLi)。容器是一个无序列表。

我的问题是我正在创建一个带有半球形提交按钮的有趣搜索栏。该按钮几乎与带有 inline-block 和 margin 属性的搜索栏相连。

这个包(搜索栏和按钮)不会以我尝试的任何方式在 div 中居中。

我尝试将 #globalSearchCont 设置为特定宽度和自动边距,但整个 flexbox 演示文稿无法在移动设备上正确显示。

有什么建议/建议吗?提前致谢。

#globalSearchContLi {
  flex-grow: 7;
  margin: 0px 15px;
  flex-basis: 100px;
}

#globalSearchContLi {
  flex-grow: 7;
  margin: 0px 15px;
  flex-basis: 100px;
}

#munchGlobalSearchbar {
  width: 240px;
  height: 50px;
  /* box-shadow: 0 0 0 1px#000,0 0 0 3px #FFF, 0 0 0 5px #333; */
  font-weight: 300;
  font-size: 1.6rem;
  border-radius: 10px;
  display: inline-block;
  margin-top: 20px;
  text-align: center;
  background-color: #edad0c;
  border-bottom: 2px solid #333;
  border-top: 2px solid #333;
  border-left: 2px solid #333;
}

#munchGlobalSearchbar::placeholder {
  color: #000;
}


#globalSearchBtn {
  background-image: url(../imgs/addOn/panEmoji.png);
  width: 50px;
  height: 51px;
  margin: 0px 0px -17px -12px !important;
  border-bottom-right-radius: 50%;
  border-top-right-radius: 50%;
  display: inline-block;
  border: 2px solid #333;
  background-color: #38b32b;
  transition: .2s all ease;
}


.backImageCon {
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center;
}
<li id="globalSearchContLi">
  <div id="globalSearchCont">
    <input placeholder="Search..." type="textbox" name="globalSearch" id="munchGlobalSearchbar">
    <div id="globalSearchBtn" class="backImageCon"></div>
  </div>
</li>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    在父级上使用justify-content: center 使按钮元素水平居中。

    #globalSearchContLi {
      list-style-type: none;
      margin-left: 0;
    }
    
    #globalSearchCont {
      display: flex;
      justify-content: center;
      height: 50px;
    }
    
    #munchGlobalSearchbar {
      width: 240px;
      font-weight: 300;
      font-size: 1.6rem;
      border-radius: 10px;
      text-align: center;
      background-color: #edad0c;
      border-bottom: 2px solid #333;
      border-top: 2px solid #333;
      border-left: 2px solid #333;
    }
    
    #munchGlobalSearchbar::placeholder {
      color: #000;
    }
    
    #globalSearchBtn {
      background-image: url(../imgs/addOn/panEmoji.png);
      width: 50px;
      border-bottom-right-radius: 50%;
      border-top-right-radius: 50%;
      border: 2px solid #333;
      background-color: #38b32b;
      transition: .2s all ease;
      margin-left: -10px;
    }
    
    .backImageCon {
      background-repeat: no-repeat;
      background-size: contain;
      background-position: center;
    }
    
    ul {
      margin: 0;
      padding: 0;
    }
    
    * {
      box-sizing: border-box;
    }
    <ul>
      <li id="globalSearchContLi">
        <div id="globalSearchCont">
          <input placeholder="Search..." type="textbox" name="globalSearch" id="munchGlobalSearchbar">
          <div id="globalSearchBtn" class="backImageCon"></div>
        </div>
      </li>
    </ul>

    【讨论】:

    • 谢谢!我很感激。虽然,我不知道为什么当我在移动设备上测试它时,搜索栏(在 safari 中)看起来比在我的桌面(chrome)中大得多,这很常见吗?
    猜你喜欢
    • 2015-03-31
    • 1970-01-01
    • 2016-12-26
    • 2017-10-22
    • 2015-12-26
    • 2018-03-23
    • 1970-01-01
    相关资源
    最近更新 更多