【问题标题】:Align button inside search input搜索输入内的对齐按钮
【发布时间】:2016-12-09 13:54:12
【问题描述】:

我需要将按钮放置在右侧的搜索输入中,并且搜索按钮内的文本不能放在该按钮下方。

我使用 CSS 尝试了不同的定位选项,但我找不到解决方案

我将如何以最好的方式做到这一点。希望任何人都可以提供帮助!

$(document).ready(function(){
  var regionDropDown = $('.region_dropdown_section'),
      regionButton = regionDropDown.find('button'),
      regionList = regionDropDown.find('.region_dropdown_content').children();

  $(regionList).on('click', function(e){
    var region = e.target;
    regionButton.text(region.text).val(region.text);
  });
});
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font-family: 'Helvetica Neue', Helvetica, Arial, Sans-Serif;
  vertical-align: baseline;
  outline: none;
}

body {
  background: url(../images/background/body_background.png) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

.logo img{
  margin-left: auto;
  margin-right: auto;
  display: block;
  margin-top: 50px;
  margin-bottom: 50px;
}

.SearchSummoners {
  margin: auto;
  width: 35%;
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.7);
  border: 1px solid;

  -moz-border-image: -moz-linear-gradient(top, #006184 0%, #303142 100%);
  -webkit-border-image: -webkit-linear-gradient(top, #006184 0%, #303142 100%);
  border-image: linear-gradient(to bottom, #006184 0%, #303142 100%);
  border-image-slice: 1;
}

/* Dropdown Button */
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* The container <div> - needed to position the dropdown content */
.region_dropdown_section {
    position: relative;
    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.region_dropdown_content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Links inside the dropdown */
.region_dropdown_content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.region_dropdown_content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu on hover */
.region_dropdown_section:hover .region_dropdown_content {
    display: block;
}

/* Change the background color of the dropdown button when the dropdown content is shown */
.region_dropdown_section:hover .dropbtn {
    background-color: #3e8e41;
}

.Searchbox_Summoners {
  margin: auto;
  display: block;
  width: 65%;
}

#SearchBox{
		margin-right: 10%;
		margin-left:  10%;
		width: 80%;
		background-color: white;
		height: 40px;;
	}

#SearchInput{
		width: 92%;
		line-height: 40px;
		background: white;
		border: 0;
		outline: 0;
		margin: 0;
		padding: 0;
		margin-left: 20px;
    font-size: 24px;
	}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php define('DeniedAccessFiles', TRUE); ?>

<?php include 'header.php'; ?>
<div class="logo">
    <img src="https://placeholdit.imgix.net/~text?txtsize=47&txt=500%C3%97225&w=500&h=225">
</div>
  <div class="SearchSummoners">
    <div id="SearchBox">
      <form method="POST">
        <input id="SearchInput" value="Enter the Summoner Name"  onfocus="if(this.value  == 'Enter the Summoner Name') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Enter the Summoner Name'; }"  type="text" name="SummonerName"></input>
      </form>
    </div>
    <div class="region_dropdown_section">
      <button class="dropbtn">Select Region</button>
      <div class="region_dropdown_content">
        <a href="#">North America</a>
        <a href="#">Europe West</a>
        <a href="#">Europe NE</a>
        <a href="#">Korea</a>
      </div>
    </div>
  </div>
<?php include 'footer.php'; ?>

【问题讨论】:

  • 我没有清楚地了解您的问题。您想要输入右侧的绿色按钮吗?
  • @The_ehT 是的,我基本上想要输入右侧的绿色按钮。但与它对齐或在右侧对齐。 - 就像你在这个 imgur 链接上看到的一样(红色框是绿色按钮)i.imgur.com/JvYRt4B.png
  • 在输入和按钮的当前大小下,它不会在框中对齐。要么盒子需要更大,要么按钮和输入需要小。使用display:inline-box 你可以实现它,如果你愿意,我可以写答案。你是 DOTA 还是 LOL 玩家?
  • @The_ehT 我是英雄联盟玩家。 - 按钮可以更小,这只是一个示例大小。我试过使用display: inline-box,但这对我不起作用?

标签: html css


【解决方案1】:

试试这个,您需要将输入框向左浮动并更改一些尺寸以使其适合。

$(document).ready(function(){
  var regionDropDown = $('.region_dropdown_section'),
      regionButton = regionDropDown.find('button'),
      regionList = regionDropDown.find('.region_dropdown_content').children();

  $(regionList).on('click', function(e){
    var region = e.target;
    regionButton.text(region.text).val(region.text);
  });
});
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font-family: 'Helvetica Neue', Helvetica, Arial, Sans-Serif;
  vertical-align: baseline;
  outline: none;
}

body {
  background: url(../images/background/body_background.png) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

.logo img{
  margin-left: auto;
  margin-right: auto;
  display: block;
  margin-top: 50px;
  margin-bottom: 50px;
}

.SearchSummoners {
  margin: auto;
  width: 35%;
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.7);
  border: 1px solid;

  -moz-border-image: -moz-linear-gradient(top, #006184 0%, #303142 100%);
  -webkit-border-image: -webkit-linear-gradient(top, #006184 0%, #303142 100%);
  border-image: linear-gradient(to bottom, #006184 0%, #303142 100%);
  border-image-slice: 1;
}

/* Dropdown Button CHANGED SOME SIZING FOR THIS*/
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 4px;
font-size: 12px;
border: none;
cursor: pointer;
height: 40px;
}



/* Dropdown Content (Hidden by Default) */
.region_dropdown_content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Links inside the dropdown */
.region_dropdown_content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.region_dropdown_content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu on hover */
.region_dropdown_section:hover .region_dropdown_content {
    display: block;
}

/* Change the background color of the dropdown button when the dropdown content is shown */
.region_dropdown_section:hover .dropbtn {
    background-color: #3e8e41;
}

.Searchbox_Summoners {
  margin: auto;
  display: block;
  width: 65%;
}

/*THIS WAS EDITED*/
#SearchBox {
    	margin-right: 0;
    	margin-left: 0;
    	width: 80%;
    	background-color: white;
    	height: 40px;
    	float: left;
    }
    
        .region_dropdown_section {
        	position: inherit;
        	display: inline-block;
        	/* width: 0; */
        	max-width: 10% !important;
        }

#SearchInput{
		width: 92%;
		line-height: 40px;
		background: white;
		border: 0;
		outline: 0;
		margin: 0;
		padding: 0;
		margin-left: 20px;
    font-size: 24px;
	}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php define('DeniedAccessFiles', TRUE); ?>

<?php include 'header.php'; ?>
<div class="logo">
    <img src="https://placeholdit.imgix.net/~text?txtsize=47&txt=500%C3%97225&w=500&h=225">
</div>
  <div class="SearchSummoners">
    <div id="SearchBox">
      <form method="POST">
        <input id="SearchInput" value="Enter the Summoner Name"  onfocus="if(this.value  == 'Enter the Summoner Name') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Enter the Summoner Name'; }"  type="text" name="SummonerName"></input>
      </form>
    </div>
    <div class="region_dropdown_section">
      <button class="dropbtn">Select Region</button>
      <div class="region_dropdown_content">
        <a href="#">North America</a>
        <a href="#">Europe West</a>
        <a href="#">Europe NE</a>
        <a href="#">Korea</a>
      </div>
    </div>
  </div>
<?php include 'footer.php'; ?>

【讨论】:

    【解决方案2】:

    这是你的代码 sn-p -

    $(document).ready(function(){
      var regionDropDown = $('.region_dropdown_section'),
          regionButton = regionDropDown.find('button'),
          regionList = regionDropDown.find('.region_dropdown_content').children();
    
      $(regionList).on('click', function(e){
        var region = e.target;
        regionButton.text(region.text).val(region.text);
      });
    });
    html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
      margin: 0;
      padding: 0;
      border: 0;
      font-size: 100%;
      font-family: 'Helvetica Neue', Helvetica, Arial, Sans-Serif;
      vertical-align: baseline;
      outline: none;
    }
    
    body {
      background: url(../images/background/body_background.png) no-repeat center center fixed;
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
    }
    
    .logo img{
      margin-left: auto;
      margin-right: auto;
      display: block;
      margin-top: 50px;
      margin-bottom: 50px;
    }
    
    .SearchSummoners {
      margin: auto;
      width: 50%;
      padding: 10px;
      background-color: rgba(0, 0, 0, 0.7);
      border: 1px solid;
    
      -moz-border-image: -moz-linear-gradient(top, #006184 0%, #303142 100%);
      -webkit-border-image: -webkit-linear-gradient(top, #006184 0%, #303142 100%);
      border-image: linear-gradient(to bottom, #006184 0%, #303142 100%);
      border-image-slice: 1;
    }
    
    /* Dropdown Button */
    .dropbtn {
        background-color: #4CAF50;
        color: white;
        padding: 14px;
        font-size: 14px;
        border: none;
        cursor: pointer;
    }
    
    /* The container <div> - needed to position the dropdown content */
    .region_dropdown_section {
        position: relative;
        display: inline-block;
     width:40%;
    }
    
    /* Dropdown Content (Hidden by Default) */
    .region_dropdown_content {
        display: none;
        position: absolute;
        background-color: #f9f9f9;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    }
    
    /* Links inside the dropdown */
    .region_dropdown_content a {
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
    }
    
    /* Change color of dropdown links on hover */
    .region_dropdown_content a:hover {background-color: #f1f1f1}
    
    /* Show the dropdown menu on hover */
    .region_dropdown_section:hover .region_dropdown_content {
        display: block;
    }
    
    /* Change the background color of the dropdown button when the dropdown content is shown */
    .region_dropdown_section:hover .dropbtn {
        background-color: #3e8e41;
    }
    
    .Searchbox_Summoners {
      margin: auto;
      display: block;
      width: 65%;
    }
    
    #SearchBox{
      
    	    width: 60%;
        background-color: white;
       height: 40px;
      display: inline-block;
    	}
    
    #SearchInput{
    		width: 92%;
    		line-height: 40px;
    		background: white;
    		border: 0;
    		outline: 0;
    		margin: 0;
    		padding: 0;
    		margin-left: 20px;
        font-size: 12px;
    	}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <?php define('DeniedAccessFiles', TRUE); ?>
    
    <?php include 'header.php'; ?>
    <div class="logo">
        <img src="https://placeholdit.imgix.net/~text?txtsize=47&txt=500%C3%97225&w=500&h=225">
    </div>
      <div class="SearchSummoners">
        <div id="SearchBox">
          <form method="POST">
            <input id="SearchInput" value="Enter the Summoner Name"  onfocus="if(this.value  == 'Enter the Summoner Name') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Enter the Summoner Name'; }"  type="text" name="SummonerName"></input>
          </form>
        </div><div class="region_dropdown_section">
          <button class="dropbtn">Select Region</button>
          <div class="region_dropdown_content">
            <a href="#">North America</a>
            <a href="#">Europe West</a>
            <a href="#">Europe NE</a>
            <a href="#">Korea</a>
          </div>
        </div>
      </div>
    <?php include 'footer.php'; ?>

    【讨论】:

    • 这已经很好了,唯一的问题是我的定位由于某种原因不再居中:gyazo.com/9d74cd4a16b43c2e96e6257007ba9ece
    • 那么好。我也是 LOL 播放器:D .update the sn-p.
    • @BPrepper 使用 px 单位作为绿色按钮宽度,然后对于 input 使用 css calc 方法计算剩余部分。如果你想把栏全展开。但我认为搜索栏有点长所以最好减少 div.SearchSummoners 元素减少它的宽度。
    • 感谢 The_ehT - 如果您想加入我的项目,请随时将我添加到联赛 (EUW - TheBliveon) 或 Steam:BelgianPrepper
    • link 这行得通,唯一困扰我的是按钮所在的右侧,不是留在盒子里。当您放大时,它只会离开背景框。按钮右侧的空白也困扰着我。
    猜你喜欢
    • 2013-12-05
    • 1970-01-01
    • 2012-10-12
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2023-04-03
    • 2014-06-14
    相关资源
    最近更新 更多