将搜索框移至紧邻超链接的右侧

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

Shifting search box to right next to hrefs

问题

You can try using flexbox to achieve the desired layout. Modify your HTML and CSS like this:

HTML:

<div class="navbar">
    <div class="navbar_logo">
        <a href="index.html"><img src="images/logo_transparent2.png"  /></a>
    </div>
    <div class="navbar_text">
        <ul>
            <li>
                <a href="index.html">Home</a>
                <a href="about.html">About</a>
                <a href="contact.html">Contact</a>
            </li>
        </ul>
    </div>
    <div class="search_icon">
        <form class="form-inline">
            <button class="btn btn-outline-none my-2 my-sm-0" type="submit">
                <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAACXBIWXMAAAsTAAALEwEAmpwYAAACiklEQVR4nO2ZzWtTURDFf25ixSiIiO22FdutG8FFa2PpX+CytRQLotD/waILrXv3piCIyySk7VYXfrRd+rVx407blaI2FSNXbyAMk7SJM3kv+g4MhMA95x3umzsz90GGDKnEELAArABbwDZQi/EJ2ASKwFVgkBRiEqgAP4D6AWMPKAMTpABngLUOHr5VVIGRpEzMAV8MTDTiMzDbaxO32jzQa+AOMAWMAkdjjMX/7gJv2qxf6pWJ2y0e4AVQ6IDnAvA0KTNziugucA041AVfWHM9ckjeGRwTW+ZEOF7HDbjDybWj5MwwDpCn066RiQYuxnrTrFHBoU7IrQ+vkzVuKDrjlgIVQf68y5zYD4HzpdAqWbYde4I8vAZeKCgdwGkL4gVB/Ap/vBWa8xakK4I0FDtvLAvNBxakW4I0VGdvTAvNDQvSbUF6Fn+MCs2PFqSy6ubxR15ofvcwcgx/HFeq/D/xar23IN1MQbI/syAtCtIwT3jjntC871EQw1DkjXdC87IF6aDSooQm0gtTSpd90oq8rEyDXk2jzMmipcCE0l6Hyc4ai4rOOWuRqhCoGd9JFZTB6iEOGImFqVlox6iln1RG3XAzeQonzCpbX4uTXbeXD4vKToR4AuRwxJIi2uhQL3V4OsnEllEFBpIwU49D0XKszmOxAczH39Ox2Mk60S5K3jszo+SMV5SBw55mhpWLiU7jJ/AYWE/aDPHKpqR0AO3iG/AIOP+b4U8urO6zptILM8Tbjvk4Y2/EIzS0GF+BD/GqJzSAV4ATyvpUmflb5JSWqC5i1fs066WZtcxMAsj9bzuzDhyhDzBwgNOs598fPXbmJn2GnGKm70xoZvrWRHPOuH0szZCB1vgFpd/exdTtQfAAAAAASUVORK5CYII=" alt=""></button>
            </form>
        </div>
    </div>

CSS:

.navbar{
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #99e6e6;
}

.navbar_logo img{
    width: 230px;
    height: 200px;
    cursor: pointer;
}

.navbar_text{
    margin-right: 40px;
}

.navbar_text ul li a{
    margin-left: 20px;
    font-size: 24px;
}

.navbar_text a:hover {
    color: black;
    transition: 0.5s;
}

.search_icon form {
    display: flex;
    align-items: center;
}

.form-control.mr-sm-2 {
    margin-left: 10px;
}

This should position the search form next to the Home link. Remember to adjust the margins and spacing as needed.

英文:

I need to change search form with icon to left next to Home
HTML

&lt;div class=&quot;navbar&quot;&gt;
        &lt;div class=&quot;navbar_logo&quot;&gt;
            &lt;a href=&quot;index.html&quot;&gt;&lt;img src=&quot;images/logo_transparent2.png&quot;  /&gt;&lt;/a&gt;
        &lt;/div&gt;
        &lt;div class=&quot;navbar navbar-light &quot;&gt;
            &lt;form class=&quot;form-inline&quot;&gt;
              &lt;input class=&quot;form-control mr-sm-2&quot; type=&quot;search&quot; placeholder=&quot;Search&quot; aria-label=&quot;Search&quot;&gt;
              &lt;button class=&quot;btn btn-outline-none my-2 my-sm-0&quot; type=&quot;submit&quot;&gt;&lt;img src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAACXBIWXMAAAsTAAALEwEAmpwYAAACiklEQVR4nO2ZzWtTURDFf25ixSiIiO22FdutG8FFa2PpX+CytRQLotD/waILrXv3piCIyySk7VYXfrRd+rVx407blaI2FSNXbyAMk7SJM3kv+g4MhMA95x3umzsz90GGDKnEELAArABbwDZQi/EJ2ASKwFVgkBRiEqgAP4D6AWMPKAMTpABngLUOHr5VVIGRpEzMAV8MTDTiMzDbaxO32jzQa+AOMAWMAkdjjMX/7gJv2qxf6pWJ2y0e4AVQ6IDnAvA0KTNziugucA041AVfWHM9ckjeGRwTW+ZEOF7HDbjDybWj5MwwDpCn066RiQYuxnrTrFHBoU7IrQ+vkzVuKDrjlgIVQf68y5zYD4HzpdAqWbYde4I8vAZeKCgdwGkL4gVB/Ap/vBWa8xakK4I0FDtvLAvNBxakW4I0VGdvTAvNDQvSbUF6Fn+MCs2PFqSy6ubxR15ofvcwcgx/HFeq/D/xar23IN1MQbI/syAtCtIwT3jjntC871EQw1DkjXdC87IF6aDSooQm0gtTSpd90oq8rEyDXk2jzMmipcCE0l6Hyc4ai4rOOWuRqhCoGd9JFZTB6iEOGImFqVlox6iln1RG3XAzeQonzCpbX4uTXbeXD4vKToR4AuRwxJIi2uhQL3V4OsnEllEFBpIwU49D0XKszmOxAczH39Ox2Mk60S5K3jszo+SMV5SBw55mhpWLiU7jJ/AYWE/aDPHKpqR0AO3iG/AIOP+b4U8urO6zptILM8Tbjvk4Y2/EIzS0GF+BD/GqJzSAV4ATyvpUmflb5JSWqC5i1fs066WZtcxMAsj9bzuzDhyhDzBwgNOs598fPXbmJn2GnGKm70xoZvrWRHPOuH0szZCB1vgFpd/exdTtQfAAAAAASUVORK5CYII=&quot; alt=&quot;&quot;&gt;&lt;/button&gt;
            &lt;/form&gt;
        &lt;/div&gt;

        &lt;div class=&quot;navbar_text&quot;&gt;
                &lt;ul&gt;
                    &lt;li&gt;
                        &lt;a href=&quot;index.html&quot;&gt;Home&lt;/a&gt;
                        &lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;
                        &lt;a href=&quot;contact.html&quot;&gt;Contact&lt;/a&gt;
                    &lt;/li&gt;
                &lt;/ul&gt;
        &lt;/div&gt;
        
    &lt;/div&gt;

CSS

.navbar{
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #99e6e6;
}

.navbar_logo img{
    width: 230px;
    height: 200px;
    cursor: pointer;
}

.navbar_text{
    margin-right: 40px;
}

.navbar_text ul li a{
    margin-left: 20px;
    font-size: 24px;
}

.navbar_text a:hover {
    color: black;
    transition: 0.5s;
}
 
.form-control.mr-sm-2{
    float: right;
    
}

I tried to change with margin-left but i think there is way better solution like serach box somehow connected with navbar texts and moves with them

Here is jsfiddle

答案1

得分: 0

首先,你在主导航栏和表单中使用了 "navbar" 类。你应该将其从表单的 div 中移除。

关于你的问题,如果你想将搜索表单放置在菜单旁边,你可以在该 div 中使用 "margin-left:auto;" 来实现。

另外,你可以在 "form-inline" 中使用 flex 来使搜索栏与按钮居中,还可以在 ".btn" 类中使用 order:-1; 来使搜索按钮出现在输入框的左侧。

希望对你有所帮助。

英文:

First of all, you have used navbar class in main navbar and the form. You should remove it from the form div.

About your question, if you want to position your search form right next to the menu, you can do it using "margin-left:auto;" in the div.

Also you can use flex in the form-inline to center your search bar with the button, and use order:-1; in the .btn class to make the search button to appear in the left side of input.

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

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

.navbar{
    display: flex;
    align-items: center;
    background-color: #99e6e6;
}

.navbar_logo img{
    width: 230px;
    height: 200px;
    cursor: pointer;
}

.navbar_text{
    margin-right: 40px;
}

.navbar_text ul li a{
    margin-left: 20px;
    font-size: 24px;
}

.navbar_text a:hover {
    color: black;
    transition: 0.5s;
}
 
.form-control.mr-sm-2{
  justify-self:end;
    
}
.navbar-light{
 margin-left:auto;
}
.form-inline{
  display:flex;
  
}
.form-inline .btn{
  order:-1;
}
.btn img{
  width:30px;
  height:30px;
}

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

&lt;div class=&quot;navbar&quot;&gt;
        &lt;div class=&quot;navbar_logo&quot;&gt;
            &lt;a href=&quot;index.html&quot;&gt;&lt;img src=&quot;images/logo_transparent2.png&quot;  /&gt;&lt;/a&gt;
        &lt;/div&gt;
        &lt;div class=&quot;navbar-light&quot;&gt;
            &lt;form class=&quot;form-inline&quot;&gt;
              &lt;input class=&quot;form-control mr-sm-2&quot; type=&quot;search&quot; placeholder=&quot;Search&quot; aria-label=&quot;Search&quot;&gt;
              &lt;button class=&quot;btn btn-outline-none my-2 my-sm-0&quot; type=&quot;submit&quot;&gt;&lt;img src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAACXBIWXMAAAsTAAALEwEAmpwYAAACiklEQVR4nO2ZzWtTURDFf25ixSiIiO22FdutG8FFa2PpX+CytRQLotD/waILrXv3piCIyySk7VYXfrRd+rVx407blaI2FSNXbyAMk7SJM3kv+g4MhMA95x3umzsz90GGDKnEELAArABbwDZQi/EJ2ASKwFVgkBRiEqgAP4D6AWMPKAMTpABngLUOHr5VVIGRpEzMAV8MTDTiMzDbaxO32jzQa+AOMAWMAkdjjMX/7gJv2qxf6pWJ2y0e4AVQ6IDnAvA0KTNziugucA041AVfWHM9ckjeGRwTW+ZEOF7HDbjDybWj5MwwDpCn066RiQYuxnrTrFHBoU7IrQ+vkzVuKDrjlgIVQf68y5zYD4HzpdAqWbYde4I8vAZeKCgdwGkL4gVB/Ap/vBWa8xakK4I0FDtvLAvNBxakW4I0VGdvTAvNDQvSbUF6Fn+MCs2PFqSy6ubxR15ofvcwcgx/HFeq/D/xar23IN1MQbI/syAtCtIwT3jjntC871EQw1DkjXdC87IF6aDSooQm0gtTSpd90oq8rEyDXk2jzMmipcCE0l6Hyc4ai4rOOWuRqhCoGd9JFZTB6iEOGImFqVlox6iln1RG3XAzeQonzCpbX4uTXbeXD4vKToR4AuRwxJIi2uhQL3V4OsnEllEFBpIwU49D0XKszmOxAczH39Ox2Mk60S5K3jszo+SMV5SBw55mhpWLiU7jJ/AYWE/aDPHKpqR0AO3iG/AIOP+b4U8urO6zptILM8Tbjvk4Y2/EIzS0GF+BD/GqJzSAV4ATyvpUmflb5JSWqC5i1fs066WZtcxMAsj9bzuzDhyhDzBwgNOs598fPXbmJn2GnGKm70xoZvrWRHPOuH0szZCB1vgFpd/exdTtQfAAAAAASUVORK5CYII=&quot; alt=&quot;&quot;&gt;&lt;/button&gt;
            &lt;/form&gt;
        &lt;/div&gt;

        &lt;div class=&quot;navbar_text&quot;&gt;
                &lt;ul&gt;
                    &lt;li&gt;
                        &lt;a href=&quot;index.html&quot;&gt;Home&lt;/a&gt;
                        &lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;
                        &lt;a href=&quot;contact.html&quot;&gt;Contact&lt;/a&gt;
                    &lt;/li&gt;
                &lt;/ul&gt;
        &lt;/div&gt;
        
    &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 18:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034356.html
匿名

发表评论

匿名网友

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

确定