如何在同一行中保留html中的2个对象

时间:2017-02-13 20:31:55

标签: javascript html

很抱歉问这个基本问题,叫我傻,但是我不能把这两个物品保持在与sam部门相同的行中,只需看看代码,



<html>
  <head>
    <style>
.search-input {
    width: 130px;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-image: url('searchicon.png');
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
}

.search-input:focus {
    width: 60%;
}
</style>
<style>
body {
    font-family: "Lato", sans-serif;
    transition: background-color .5s;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
</style>
    </head>
  <body>
    <div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="index.php">Home</a>
  <a href="reg.php">Join Us</a>
  <a href="abtus.php">About Us</a>
  <a href="srch.php">Search</a>
  <a href="cnt.php">Contact Us</a>
</div>
<div>
<div id="main" >
  
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Menu</span>
</div>

<script>
function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
    document.body.style.backgroundColor = "white";
}
</script>
<!--This object that is to be right aligned-->
   
  <form align="right">

  <input class = "search-input" type="text" name="search" placeholder="Search..">

  </form>
    </body>
  </html>
&#13;
&#13;
&#13;

现在,如果您运行它,您会发现搜索栏和菜单没有在同一行中对齐,尽管彼此没有阻止其他请帮助

4 个答案:

答案 0 :(得分:0)

你可以将你的#main元素浮动。

#main {
    float: left;
    transition: margin-left .5s;
    padding: 16px;
}

你也必须为你的表格填充填充。如果你给它padding: 15px,我觉得它看起来很不错。只需给表单一个类,就可以为它添加一个样式。

答案 1 :(得分:0)

目前,您的菜单和搜索位于不同的元素中,如果您希望它们并排显示,则可能更容易将它们放在同一个容器中:

<div id="main" >
  <!-- Your Menu -->
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Menu</span>
  <!-- Your Search Area -->
  <form align="right">
      <input class = "search-input" type="text" name="search" placeholder="Search..">
  </form>
</div>

然后您可以使用一些CSS,以便您的搜索表单将“浮动”在右侧:

/* This will make your float stick to the right-hand side */
#main form { float: right; }
/* This will clear any existing floating rules after this element */
#main:after { clear: both; }

示例

enter image description here

.search-input {
    width: 130px;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-image: url('searchicon.png');
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
}

.search-input:focus {
    width: 60%;
}
body {
    font-family: "Lato", sans-serif;
    transition: background-color .5s;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}

#main form {
  float: right;  
}

#main:after {
  clear: both;  
}
<html>
  <head>

    </head>
  <body>
    <div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="index.php">Home</a>
  <a href="reg.php">Join Us</a>
  <a href="abtus.php">About Us</a>
  <a href="srch.php">Search</a>
  <a href="cnt.php">Contact Us</a>
</div>
<div>
<div id="main" >
  
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Menu</span>
  <form align="right">

  <input class = "search-input" type="text" name="search" placeholder="Search..">

  </form>
</div>

<script>
function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
    document.body.style.backgroundColor = "white";
}
</script>
<!--This object that is to be right aligned-->
   
  
    </body>
  </html>

答案 2 :(得分:0)

如果你在两个div周围放置一个带有弹性框的div(我会称之为#34;容器&#34;),并设置如下的css:

.container { 
   display: flex; 
   justify-content: space-between; 
   align-items: center;
}

您的商品将在任何尺寸的屏幕上对齐。在这种情况下(当弹性方向为行时 - 未设置,因为它是默认值)align-items:center垂直对齐每个项目(或沿y轴对齐),并且对齐内容水平对齐每个项目(沿x轴) 。

答案 3 :(得分:0)

将您的form移至div#main并将其添加到#main css

display: flex;
justify-content: space-between;

function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
  document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
  document.body.style.backgroundColor = "white";
}
body {
  font-family: "Lato", sans-serif;
  transition: background-color .5s;
}
.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}
.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s
}
.sidenav a:hover,
.offcanvas a:focus {
  color: #f1f1f1;
}
.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}
#main {
  transition: margin-left .5s;
  padding: 16px;
  display: flex;
  justify-content: space-between;
}
@media screen and (max-height: 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
.search-input {
  width: 130px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  background-color: white;
  background-image: url('searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  padding: 12px 20px 12px 40px;
  -webkit-transition: width 0.4s ease-in-out;
  transition: width 0.4s ease-in-out;
}
.search-input:focus {
  width: 80%;
}
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="index.php">Home</a>
  <a href="reg.php">Join Us</a>
  <a href="abtus.php">About Us</a>
  <a href="srch.php">Search</a>
  <a href="cnt.php">Contact Us</a>
</div>
<div>
  <div id="main">
    <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Menu</span>
    <form align="right">
      <input class="search-input" type="text" name="search" placeholder="Search..">
    </form>
  </div>