我正在try 使右上角容器中的搜索栏具有响应性,以便它缩小到足以避免与其他图标重叠,如下所示:

What is happening when I have the window set to 600px

我正在使用媒体查询和css变量来使网站响应.

到目前为止,我一直在努力:

  • 用硬像素值设置最大宽度 使用css变量的文本类型的输入字段.
  • 通过以下方式更改该行中所有项目的宽度 对其进行硬编码,然后使用css变量.
  • 在文本输入栏中添加了右页边距(这行得通,但看起来不对,因为在"背景"中,图标仍然覆盖在文本栏上).

将非常感谢任何解决这个问题的方案.

What I want the search bar row to look like for all window sizes.

PS.我知道图标没有加载,但我找不到添加它们的方法,但当我在Stackoverflow上运行代码片段时,输出仍然与我看到的相同,只是没有图标.

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}

/*---------------MEDIA QUERIES-----------------*/
@media screen and (max-width: 360px) {
    :root {
        --sidebar_heading: 20px;
        --sidebar_sub_heading: 15px;
        --container_padding: 15px;
        --header_height: 16.5vh;
        --content_height: 83.5vh;
        --search_max_width: 100px;
        --bg: orange;
    }
    .home:hover, .profile:hover, .messages:hover, .history:hover, .tasks:hover, .settings:hover, .support:hover {
        font-size: 30px;
    }
}

@media screen and (max-width: 375px) and (min-width: 360px) {
    :root {
        --sidebar_heading: 25px;
        --sidebar_sub_heading: 17.5px;
        --container_padding: 12.5px;
        --header_height: 17.5vh;
        --content_height: 82.5vh;
        --search_max_width: 170px;
        --bg: orange;
    }
    .home:hover, .profile:hover, .messages:hover, .history:hover, .tasks:hover, .settings:hover, .support:hover {
        font-size: 30px;
    }
} 

@media screen and (max-width: 720px) and (min-width: 375px) {
    :root {
        --sidebar_heading: 30px;
        --sidebar_sub_heading: 20px;
        --container_padding: 15px;
        --header_height: 17.5vh;
        --content_height: 82.5vh;
        --search-max-width: 770px;
        --bg: orange;
    }
    .home:hover, .profile:hover, .messages:hover, .history:hover, .tasks:hover, .settings:hover, .support:hover {
        font-size: 30px;
    }
}

@media screen and (max-width: 1366px) and (min-width: 720px) {
    :root {
        --sidebar_heading: 35px;
        --sidebar_sub_heading: 22.5px;
        --container_padding: 17.5px;
        --header_height: 17.5vh;
        --content_height: 82.5vh;
        --bg: orange;
    }
    .home:hover, .profile:hover, .messages:hover, .history:hover, .tasks:hover, .settings:hover, .support:hover {
        font-size: 30px;
    }
}

@media screen and (max-width: 1920px) and (min-width: 1366px) {
    :root {
        --sidebar_heading: 40px;
        --sidebar_sub_heading: 25px;
        --container_padding: 20px;
        --header_height: 17.5vh;
        --content_height: 82.5vh;
        --bg: black;
    }
    .home:hover, .profile:hover, .messages:hover, .history:hover, .tasks:hover, .settings:hover, .support:hover {
        font-size: 30px;
    }
}

@media screen and (max-width: 2300px) and (min-width: 1920px) {
    :root {
        --sidebar_heading: 45px;
        --sidebar_sub_heading: 27.5px;
        --container_padding: 22.5px;
        --header_height: 11.5vh;
        --content_height: 88.5vh;
        --bg: black;
    }
    .home:hover, .profile:hover, .messages:hover, .history:hover, .tasks:hover, .settings:hover, .support:hover {
        font-size: 30px;
    }
}

@media screen and (min-width: 2300px) {
    :root {
        --sidebar_heading: 50px;
        --sidebar_sub_heading: 30px;
        --container_padding: 25px;
        --header_height: 12.5vh;
        --content_height: 87.5vh;
        --bg: black;
    }
    .home:hover, .profile:hover, .messages:hover, .history:hover, .tasks:hover, .settings:hover, .support:hover {
        font-size: 35px;
    }
}

/*---------------MAIN CONTAINERS-----------------*/
.container {
    display: grid;
    grid-template: var(--header_height) var(--content_height)/ min-content auto;
    background-color: var(--bg);
}

.sidebar-container {
    grid-column: 1/2;
    grid-row: 1/3;
    background-color: #1e3a8a;
    color: white;
}

.header-container {
    grid-column: 2/3;
    grid-row: 1/2;
    background-color: #cffafe;

}

.content-container {
    grid-column: 2/3;
    grid-row: 2/3;
    background-color: white;
    background-repeat: no-repeat;
    background-image: url(./images/wave.svg);
    background-position: 0% 100%;
    background-attachment: fixed;
}
.sidebar-container, .header-container, .content-container {
    padding: var(--container_padding);
}

/*---------------SIDEBAR STYLING-----------------*/
.row, .header-row-space-between, .header-row-end {
    display: flex;
    align-items: center;
    gap: 0 10px;
}

.row {
    width: 100%;
}

.header-row-space-between {
    width: 80%;
    justify-content: space-between;
}

.header-row-end {
    width: 20%;
    justify-content: end;
    gap: 0 40px;
}

.column {
    display: flex;
    flex-direction: column;
}

.heading {
    font-size: var(--sidebar_heading);
    padding-bottom: 25px;
    font-weight: 800;
    transition: all 0.2s ease-in-out;

}

.sub-heading {
    padding: 7.5px 0;
}

.settings {
    padding-top: 30px;
}

.sub-heading, .settings {
    font-size: var(--sidebar_sub_heading);
    font-weight: 100;
    transition: all 0.2s ease-in-out;
}

.sidebar-margin {
    margin-left: 10px;;
}

.image {
    width: 1em;
    height: 1em;
}


.box {
    height: 100%;
    width: 100%;
    background-color: blanchedalmond;
}
form {
    width: 100%;
}
input[type='text'] {
    border: none;
    border-radius: 20px;
    padding: 10px 20px;
    max-width: var(--search_max_width);
    width: 100%;
}
/*-----COLORS---------
    sidebar: #1e3a8a
    header: #cffafe
    content: #ecfeff
*/
<!DOCTYPE html>

<html>
  <head>
    <title>Admin Dashboard</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="sidebar-container">
        <div class="row heading">
          <img class="image" src="./images/icon.svg" />
          <div class="sidebar-margin">Dashboard</div>
        </div>
        <div class="row sub-heading home">
          <img class="image" src="./images/home.svg" />
          <div class="sidebar-margin">Home</div>
        </div>
        <div class="row sub-heading profile">
          <img class="image" src="./images/profile.svg" />
          <div class="sidebar-margin">Profile</div>
        </div>
        <div class="row sub-heading messages">
          <img class="image" src="./images/message.svg" />
          <div class="sidebar-margin">Messages</div>
        </div>
        <div class="row sub-heading history">
          <img class="image" src="./images/history.svg" />
          <div class="sidebar-margin">History</div>
        </div>
        <div class="row sub-heading tasks">
          <img class="image" src="./images/task.svg" />
          <div class="sidebar-margin">Tasks</div>
        </div>
        <div class="row settings">
          <img class="image" src="./images/settings.svg" />
          <div class="sidebar-margin">Settings</div>
        </div>
        <div class="row sub-heading support">
          <img class="image" src="./images/support.svg" />
          <div class="sidebar-margin">Support</div>
        </div>
      </div>

      <div class="header-container">
        <div class="row">
          <div class="header-row-space-between">
            <img class="icon" src="./images/search.svg" />
            <form action="">
              <input type="text" placeholder="Search..." />
            </form>
          </div>
          <div class="header-row-end">
            <img class="icon" src="./images/notification.svg" />
            <img class="icon" src="./images/person-profile.svg" />
            Hasin Mahmood
          </div>
        </div>
      </div>

      <div class="content-container">
        <div class="box"></div>
      </div>
    </div>
    <script src="" async defer></script>
  </body>
</html>

推荐答案

就我个人而言,我认为如果没有@媒体查询,使用display: grid来实现这一点会容易得多.围绕这么多宽度断点进行设计可能会变得非常麻烦,尤其是.当有一个更优雅的解决方案时.

例如:

*{
  font-size: 3vmin;
}
header{
  display: grid;
  grid-template:  1fr / 1fr 2fr 1fr 1fr 1fr;
}
h3{
  align-self: center;
  justify-self: center;
}
<header>
  <button>search</button>
  <input type="search"/>
  <button>icon</button>
  <button>icon 2</button>
  <h3>name here</h3>
</header>

通过将grid的大小设置为分数单位,您将永远不会有*个元素彼此重叠,如果视区变得太小而无法同时显示它们,则会得到overflow-x个元素.

要解决溢出问题,然后将元素inside of the grid的大小设置为相对于该视口值.在我的例子中,我通过将font-size设置为3vmin来实现这一点.但是,您可以使用它来设置图标、输入框的大小,无论您想要什么.如果需要,还可以将最小高度设置为grid本身,以便它保持其相对于文档其余部分的比例.

在这种情况下使用grid的另一个好处是,您将从JUSTY-SELF和ALIGN-SELF获得可靠的行为,而flexbox的子代可能不会.

Html相关问答推荐

XPATH text()函数遇到困难

Blazor中有1像素高行的表格

如何使用bslb设置可导航页面侧边栏的样式

UseEffect()从不调用preact

如何删除在小屏幕上单击按钮时出现的蓝色方块?

禁用与行分开的边框折叠span

我无法使用背景图像css,因为我在VS代码中将照片放在不同的文件夹中

Flexbox在元素之间造成了太大的差距

不呈现孙子元素,使用R htmlTools::Taglist为分页的pdf生成HTML

如何在ASP.NET Core中添加换行符

将三个div转换为显示flex

我如何使用 html 在表格的单元格内实现下面显示的背景 colored颜色 加载器(请判断设计参考的图像链接)

当作为常规图片输入时,Cloudfront分布式图像可显示,但作为背景图像不显示

HTML 如何根据屏幕尺寸调整/裁剪视频背景?

子 div 在父 div 中溢出

如何根据行中的其中一列将行居中?

我用 ejs 修复了一个奇怪的语法错误,我想知道问题出在哪里

flex-box 中的图像在 Chrome 和 Firefox 中看起来不同 - 如何使它们在 Firefox 中看起来像?

CSS:如何在模糊的背景上剪切文本?

以 HTML 格式显示的 LaTeX 表格