display: flex使提交按钮移动,并且如何使其与输入文本“附加”在一起

huangapple go评论70阅读模式
英文:

display: flex moves submit button and how to make it "attached" to the input text

问题

我正在尝试复制这个:

我想要复制的内容

如果你看不到它,左边的图标和文本框都有浅灰色边框,提交按钮(Rechercher)与文本框相连。

这是我所做的

出于清晰原因,我更改了边框颜色。
在这里,你可以看到提交按钮不是其他两个元素的一部分,当我将它实现到Flexbox容器中时,会发生以下情况:

使用 display: flex

我如何使它更像我想要复制的模型?

.barre-rechercher {
  display: flex;
  flex-direction: row;
  margin: 35px 0 35px 0;
}

.fa-location-dot {
  display: flex;
  align-items: center;
  padding: 15px;
  border: 1px solid red;
  border-radius: 10px 0 0 10px;
  background-color: #F2F2F2;
}

input[type="text"] {
  display: flex;
  align-items: center;
  gap: 24px;
  height: 49px;
  border: 1px solid red;
  text-align: center;
  font-weight: 700;
  font-size: 18px;
}

form > input[type="submit"] {
  color: white;
  height: 49px;
  border: 1px solid var(--main-color);
  border-radius: 0 15px 15px 0;
  background-color: var(--main-color);
}
<div class="barre-rechercher">
  <i class="fa-solid fa-location-dot fa-lg" style="color: #000000;"></i>
  <form method="get" action="">
    <input type="text" id="ville" name="ville" required placeholder="Marseille, France">
    <input type="submit" value="Rechercher">
  </form>
</div>
英文:

I'm trying to replicate this:

What I'm trying to replicate

If you can't see it, both the icon on the left and the textbox have a light gray border, and the button to submit (Rechercher) is attached to the textbox.

Here's what I managed to do

For clarity reasons I changed the border colour.
Here you can see that the submit button isn't part of the 2 other items, and when I implement it into the Flexbox container here's what happens:

With display: flex

How can I make it more like the model I'm trying to replicate?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.barre-rechercher {
  display: flex;
  flex-direction: row;
  margin: 35px 0 35px 0;
}

.fa-location-dot {
  display: flex;
  align-items: center;
  padding: 15px;
  border: 1px solid red;
  border-radius: 10px 0 0 10px;
  background-color: #F2F2F2;
}

input[type=&quot;text&quot;] {
  display: flex;
  align-items: center;
  gap: 24px;
  height: 49px;
  border: 1px solid red;
  text-align: center;
  font-weight: 700;
  font-size: 18px;
}

form&gt;input[type=&quot;submit&quot;] {
  color: white;
  height: 49px;
  border: 1px solid var(--main-color);
  border-radius: 0 15px 15px 0;
  background-color: var(--main-color);
}

<!-- language: lang-html -->

&lt;div class=&quot;barre-rechercher&quot;&gt;
  &lt;i class=&quot;fa-solid fa-location-dot fa-lg&quot; style=&quot;color: #000000;&quot;&gt;&lt;/i&gt;
  &lt;form method=&quot;get&quot; action=&quot;&quot;&gt;
    &lt;input type=&quot;text&quot; id=&quot;ville&quot; name=&quot;ville&quot; required placeholder=&quot;Marseille, France&quot;&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Rechercher&quot;&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

添加这两条规则:

.barre-rechercher form {
  display: contents;
}
input {
  box-sizing: border-box;
}

第一条规则是为了使form子元素成为.barre-rechercher容器的flex子元素,第二条规则是为了确保两个input元素的高度相同,无论边框和填充如何设置。

我还向文本输入字段添加了border-right: none;,以将提交按钮直接附加到它而没有边框。

英文:

Add these two rules:

.barre-rechercher form {
  display: contents;
}
input {
  box-sizing: border-box;
}

The first one to make the children of the form be flex-children of the .barre-rechercher container, the second to make sure the height of both input elements is identical regardless of borders and paddings.

I also added border-right: none; to the text input field to attach the submit button directly to it without a border.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.barre-rechercher {
  display: flex;
  flex-direction: row;
  margin: 35px 0 35px 0;
}

.barre-rechercher form {
  display: contents;
}
.fa-location-dot {
  display: flex;
  align-items: center;
  padding: 15px;
  border: 1px solid red;
  border-radius: 10px 0 0 10px;
  background-color: #F2F2F2;
}
input {
  box-sizing: border-box;
}
input[type=&quot;text&quot;] {
  display: flex;
  align-items: center;
  gap: 24px;
  height: 49px;
  border: 1px solid red;
  text-align: center;
  font-weight: 700;
  font-size: 18px;
  border-right: none;
}

form&gt;input[type=&quot;submit&quot;] {
  color: white;
  height: 49px;
  border: 1px solid blue;
  border-radius: 0 15px 15px 0;
  background-color: blue;
}

<!-- language: lang-html -->

&lt;div class=&quot;barre-rechercher&quot;&gt;
  &lt;i class=&quot;fa-solid fa-location-dot fa-lg&quot; style=&quot;color: #000000;&quot;&gt;&lt;/i&gt;
  &lt;form method=&quot;get&quot; action=&quot;&quot;&gt;
    &lt;input type=&quot;text&quot; id=&quot;ville&quot; name=&quot;ville&quot; required placeholder=&quot;Marseille, France&quot;&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Rechercher&quot;&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

替代固定高度,我在容器上设置了min-height。现在,无论有多少内容,它都将始终具有响应性。

.barre-rechercher {
    display: flex;
    flex-direction: row;
    margin: 35px 0 35px 0;
    /* 添加的代码 */
    min-height: 49px;
}

.fa-location-dot {
    display: flex;
    align-items: center;
    padding: 15px;
    border: 1px solid #f2f2f2;
    border-radius: 10px 0 0 10px;
    background-color: #F2F2F2;
}

input[type="text"] {
    display: flex;
    align-items: center;
    gap: 24px;
    /* height: 49px; */
    border: 1px solid #f2f2f2;
    text-align: center;
    font-weight: 700;
    font-size: 18px;
}

/* 添加的代码 */
form {
    display: flex;
}

form > input[type="submit"] {
    color: white;
    /* height: 49px; */
    border: 1px solid #6666ff;
    border-radius: 0 15px 15px 0;
    background-color: #6666ff;
    cursor: pointer;
}
<div class="barre-rechercher">
    <i class="fa-solid fa-location-dot fa-lg" style="color: #000000;">更多<br>内容<br>无法<br>打破<br>这个</i>
    <form method="get" action="">
        <input type="text" id="ville" name="ville" required placeholder="Marseille, France">
        <input type="submit" value="Rechercher">
    </form>
</div>
英文:

Instead of a fixed height, I had set a min-height on the container. Now, it will always be responsive no matter how many contents are there.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.barre-rechercher {
    display: flex;
    flex-direction: row;
    margin: 35px 0 35px 0;
    /* added code */
    min-height: 49px;
}

.fa-location-dot {
    display: flex;
    align-items: center;
    padding: 15px;
    border: 1px solid #f2f2f2;
    border-radius: 10px 0 0 10px;
    background-color: #F2F2F2;
}

input[type=&quot;text&quot;] {
    display: flex;
    align-items: center;
    gap: 24px;
    /* height: 49px; */
    border: 1px solid #f2f2f2;
    text-align: center;
    font-weight: 700;
    font-size: 18px;            
}

/* added code */
form{
    display: flex;
}

form &gt; input[type=&quot;submit&quot;] {
    color: white;
    /* height: 49px; */
    border: 1px solid #6666ff;
    border-radius: 0 15px 15px 0;
    background-color: #6666ff;
    cursor: pointer;
}

<!-- language: lang-html -->

&lt;div class=&quot;barre-rechercher&quot;&gt;
    &lt;i class=&quot;fa-solid fa-location-dot fa-lg&quot; style=&quot;color: #000000;&quot;&gt;More &lt;br&gt; contents &lt;br&gt; can&#39;t &lt;br&gt; break &lt;br&gt; this one&lt;/i&gt;
    &lt;form method=&quot;get&quot; action=&quot;&quot;&gt;
        &lt;input type=&quot;text&quot; id=&quot;ville&quot; name=&quot;ville&quot; required placeholder=&quot;Marseille, France&quot;&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Rechercher&quot;&gt;
    &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月31日 23:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定