【问题标题】:CSS Search Box ExpandingCSS 搜索框扩展
【发布时间】:2019-04-10 16:09:51
【问题描述】:

https://codepen.io/LukzzXB/pen/yrMjxg

所以我正在使用 CSS 编写文本框。如果您看到上面的链接,您会看到当我将鼠标悬停在按钮上时,“类型搜索”工作正常,因为它向右移动。

现在我只是想让按钮的背景也与文本框一起使用。

所以这里的目标是尝试让盒子正确且仅正确地展开。

<html>
  <head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="styles.css">
  <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js">
  </script>`enter code here`
  </head>
  <head>
    <title>Using CCS for the first time</title>
  </head>  
  <head>
  <style>
    h1 {

    color: orange;
    text-align: left;

    }

    </style>
    </head>
      <body>
    <h1>
      <span style="font-family:Arial">Hello and welcome to a search box</span>
    </h1>
      </body>  

<head>
<body>
<div class="search-box">

  <input class="search-txt" type="text" name="" placeholder="Type to search">
  <a class="search-btn" href="#">
  <i class="fas fa-search"></i>
  </a>
</div>  
</head>

    </body>
</html>
body {
    margin: 0;
    padding: 0;
    background: red;
   }

.search-box{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #2f3640;
    height: 40px;
    border-radius: 40px;
    padding: 10px;
   }

   .search-box:hover > .search-txt{

       width: 240px;
       padding: 0 6px;
   }

   .search-box:hover > .search-btn{
    background: white;

}

   .search-btn{
    float: left;
    color: red;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: none;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: 0.4;   
   }

   .search-txt{
    position: absolute;
    float: right;
    border: none;
    background: none;
    outline: none;
    padding: 0;
    color: white;
    font-size: 16px;
    transition: 0.4s;
    line-height: 40px;
    width: 0px;
   }

【问题讨论】:

  • 你能贴一张你想要的结果的模型图吗?

标签: css


【解决方案1】:

这样的?

https://codepen.io/anon/pen/eovbjW

页面上应该只有一个HEAD 和一个BODY 标记。

float + display:flex 把你搞砸了。老实说,我现在很少使用 float。

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">

    <link rel="stylesheet" href="styles.css">

    <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>

    <title>Using CCS for the first time</title>
  </head>

  <body>
    <h1>Hello and welcome to a search box</h1>

    <div class="search-box">
      <div class="fas fa-search"></div>

      <input class="search-txt" type="text" placeholder="Type to search" />
    </div>  

  </body>
</html>

CSS
[编辑] 如果您不熟悉 CSS 变量,我在这里使用它们,在 .search-text 中作为如何使用它们的指南。在.search-textleft属性中,我什至用calc()将两个CSS变量加在一起。

:root {
  --background-black: #2f3640;
  --background-red: red;

  --search-box-padding: 10px;
  --search-box-size: 40px;
}

html, body {
  margin: 0;
  padding: 0;
  background-color: var(--background-red);
}

h1 {
  color: orange;
  font-family: Arial;
  margin-left: 1rem;
}

.search-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%);
  height: var(--search-box-size);
  width: var(--search-box-size);
  background-color: var(--background-black);
  border-radius: 50%;
  padding: var(--search-box-padding);
  cursor: pointer;
}

.search-box > .fa-search {
  padding: var(--search-box-padding);
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border-radius: 50%;
  color: var(--background-red);
}

.search-box:hover > .fa-search {
  background: white;  
}

.search-txt {
  position: absolute;
  top: var(--search-box-padding);
  left: calc(var(--search-box-size) + var(--search-box-padding));
  border: none;
  outline: none;
  padding: 0;
  height: var(--search-box-size);
  width: 0px;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;

  background-color: var(--background-black);
  color: white;
  font-size: 16px;
  transition: 0.4s width, 0.4s padding;
}

.search-box:hover > .search-txt{
  width: 240px;
  padding: 0 0.75rem;
}

【讨论】:

  • 这正是我正在寻找的,谢谢。现在唯一的问题是“要搜索的类型”似乎低于实际按钮。我试图自我修复这个问题,但我找不到这将是什么类型的代码。刚开始 CSS ;p
  • 对不起。我只在 Firefox 中尝试过。我在帖子中更新了我的答案以使用 Chrome,并希望所有其他浏览器都可以使用。我现在也在 Mac Safari 中尝试过,如果它在那个浏览器中工作,它在任何地方都可以工作。 :)
  • 干杯,辉煌!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多