在try 设置搜索栏的宽度时,我遇到了一个奇怪的问题.以下是我的部分HTML代码.

<header class="header-main">
     <div class="header-main-logo">
          <a href="index.php" class="logo">LOGO</a>
     </div>
     <div class="search-container">
      <form method="get" action="search.php?page" class="search-form">
           <input class="search-bar" type="search" name="query" value='<?php echo isset($_GET["query"]) ? trim($_GET["query"]) : "" ?>' placeholder="Search">
               <div class="search-btn">
           <input type="hidden" name="page" value="1">
               <button type="submit" name="search" value="1">Search</button>
               </div>
          </form>
     </div>
</header>

这是我的css代码

.header-main {
    box-sizing: border-box;
    width: 100%;
    height: 60px;
    background-color: rgb(107, 23, 23);
    display: flex;
    justify-content: space-between;
}

.search-container {
    box-sizing: border-box;
    width: 50%;
    height: 100%;
    background-color: rgb(51, 52, 59);
    display: flex;
    align-content: center;
}

.search-container .search-bar {
    box-sizing: border-box;
    border: 1px solid rgb(60, 255, 0);
    height: 40px;
    width: 100% !important;
    cursor: text;
    font-size: 14px;
    font-weight: 600;
    font-family: Arial, Helvetica, sans-serif;
    color: rgb(0, 0, 0);
    background-color: rgb(248, 248, 248);
    align-self: center;
}

如果我将宽度设置为.search-container .search-bar%,它不会生效,但如果我输入一个值,如400px,它就会起作用.我希望它是在百分比,以便它是动态的页面大小.

我以为它只是将宽度设置为一个HTML元素.我希望搜索占据其父级的全宽,即搜索容器.

推荐答案

问题出在你的表格上,它不是100%地占据你表格的宽度,这就是为什么.也try 将宽度应用于窗体(您可能希望为其指定一个类名)

.header-main {
    box-sizing: border-box;
    width: 100%;
    height: 60px;
    background-color: rgb(107, 23, 23);
    display: flex;
    justify-content: space-between;
}

.search-container {
    box-sizing: border-box;
    width: 50%;
    height: 100%;
    background-color: rgb(51, 52, 59);
    display: flex;
    align-content: center;
}
form{width: 100% !important;}
.search-container .search-bar {
    box-sizing: border-box;
    border: 1px solid rgb(60, 255, 0);
    height: 40px;
    width: 100% !important;
    cursor: text;
    font-size: 14px;
    font-weight: 600;
    font-family: Arial, Helvetica, sans-serif;
    color: rgb(0, 0, 0);
    background-color: rgb(248, 248, 248);
    align-self: center;
}
<header class="header-main">
     <div class="header-main-logo">
          <a href="index.php" class="logo">LOGO</a>
     </div>
     <div class="search-container">
      <form method="get" action="search.php?page" class="search-form">
           <input class="search-bar" type="search" name="query" value='11111' placeholder="Search">
               <div class="search-btn">
           <input type="hidden" name="page" value="1">
               <button type="submit" name="search" value="1">Search</button>
               </div>
          </form>
     </div>
</header>

Css相关问答推荐

text-size-adjust在我的css中曾经在我的手机上的Chrome上工作,停止工作,仍然在我妻子的手机上工作

如何在不重叠侧边栏的情况下尽可能使元素居中?

防止在添加更多项目时扩展CSS网格行

MUI Textfield-悬停时输入标签 colored颜色

如何为 MUI Slider 字段中 markLabel 的第一个和最后一个实例提供边距?

Firefox未正确设置SVG的父div的宽度

React Tabs 在 Tabs 之间垂直渲染标签内容

Img 未拉伸以满足所需的纵横比

:is() 伪类是否支持相邻sibling 表达式中第二个元素的组合器?

如何增加 PrimeFlex 网格的间隙?

如果中间元素的内容不适合 CSS,如何将其分隔到新行?

努力将 CSS 样式应用于 Elm 应用程序

css - 悬停访问的链接没有背景 colored颜色

使用 Webpack5 在 CSS 文件中内联字体和图像?

删除在 ios safari / chrome / firefox 中单击的链接上的灰色背景

表格的边框半径没有按预期工作

导航栏中的 Bootstrap 3.0 按钮

名称-值对的语义和 struct

@font-face src: local - 如果用户已经拥有本地字体,如何使用它?

为什么我的 div 边距重叠,我该如何解决?