英文:
display: flex moves submit button and how to make it "attached" to the input text
问题
我正在尝试复制这个:
如果你看不到它,左边的图标和文本框都有浅灰色边框,提交按钮(Rechercher)与文本框相连。
出于清晰原因,我更改了边框颜色。
在这里,你可以看到提交按钮不是其他两个元素的一部分,当我将它实现到Flexbox容器中时,会发生以下情况:
我如何使它更像我想要复制的模型?
.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:
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.
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:
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="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);
}
<!-- language: lang-html -->
<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>
<!-- 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="text"] {
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>input[type="submit"] {
color: white;
height: 49px;
border: 1px solid blue;
border-radius: 0 15px 15px 0;
background-color: blue;
}
<!-- language: lang-html -->
<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>
<!-- 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="text"] {
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 > input[type="submit"] {
color: white;
/* height: 49px; */
border: 1px solid #6666ff;
border-radius: 0 15px 15px 0;
background-color: #6666ff;
cursor: pointer;
}
<!-- language: lang-html -->
<div class="barre-rechercher">
<i class="fa-solid fa-location-dot fa-lg" style="color: #000000;">More <br> contents <br> can't <br> break <br> this one</i>
<form method="get" action="">
<input type="text" id="ville" name="ville" required placeholder="Marseille, France">
<input type="submit" value="Rechercher">
</form>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论