首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使flex项目内容居中

使flex项目内容居中
EN

Stack Overflow用户
提问于 2019-10-09 04:51:22
回答 1查看 47关注 0票数 0

我在flex容器中有以下flex项(#globalSearchContLi)。容器是一个无序列表。

我的问题是,我正在创建一个看起来很有趣的搜索栏,上面有一个半球形的提交按钮。该按钮与搜索栏的内联区块和边距属性有很大关系。

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

我尝试用特定的宽度和自动边距来设置#globalSearchCont,但整个flexbox演示文稿在移动设备上不能正确显示。

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

代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
<li id="globalSearchContLi">
  <div id="globalSearchCont">
    <input placeholder="Search..." type="textbox" name="globalSearch" id="munchGlobalSearchbar">
    <div id="globalSearchBtn" class="backImageCon"></div>
  </div>
</li>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-09 05:25:45

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

代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58293916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档