如何在标题中对齐元素

时间:2017-03-07 01:07:21

标签: html css

我似乎无法弄清楚如何制作它,以便我的HTML元素不会压低我的其他元素。

这是我的标题: header

BitBay标题正在向下推搜索栏,我希望它与文本对齐,所以它看起来并不尴尬。

仔细观察,您可以看到帐户按钮被推了一下。

我尝试过使用text-align属性而没有运气。有什么建议吗?

HTML:

<head>
<script type="text/javascript">
  <!--
  if (screen.width <= 800) {
    window.location = "mindex.html"; //Mobile site
  }
  //-->
</script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BitBay</title>
<link rel="stylesheet" type="text/css" href="CSS/bootstrap.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet">
<link rel="icon" href="IMG/Logo.jpg">
<div id="header">
<a href="index.html"><h1>BitBay</h1></a>
<input type="text" name="search" placeholder="Search..">
<div class="dropdown">
  <button class="dropbtn"><i class="fa fa-arrow-down"></i>Account</button>
  <div class="dropdown-content">
    <a href="account.html"><i class="fa fa-key"></i>Login</a>
    <a href="signup.html"><i class="fa fa-pencil-square-o"></i>Signup</a>
    <a href="help.html"><i class="fa fa-life-ring"></i>Help & Support</a>
  </div>
</div>
</div>
<noscript>
    <div class="noscriptmsg">
    <meta http-equiv="refresh" content="0;url=javascript.html">
    </div>
</noscript>
</head>

CSS:

#header input[type=text] {
    width: 130px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    margin-left: 21%;
    margin-bottom: 40%;
    position: absolute;
}

/* When the input field gets focus, change its width to 100% */
#header input[type=text]:focus {
    width: 17%;
}

/*Header Text*/
#header h1 {
    font-family: "smooth";
    font-size: 45px;
    color: #FFF;
    margin-top: 10px;
    margin-left: 30px;
    height: 0;
}

/* Make unordered list horizontle */
#header ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    margin-top: -12.5px;
    margin-left: 270px;
}

/* Make unordered list horizontle */
#header ul li {
    display: inline;
}

/* Make boxed & colored */
#header ul li a{
    text-decoration: none;
    padding: .2% 3%;
    color: #FFF; /*Text color, when not hovered*/
}

/* Make change colors, when hovered on */
#header ul li a:hover{
    color: lightgray; /*Text color*/
}

/* Header box */
#header{
    height: 60px;
    background-color: #3385ff;
    margin-top: -9px;
    margin-left: -20px;
    padding: 10px;
    width: auto;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

/*Remove line*/
#header a{
    text-decoration: none;
}

2 个答案:

答案 0 :(得分:1)

最好使用div来包含标题内的每个元素,这可能有助于您解决问题,例如您的锚和输入不包含在div中,并将css添加到这些div而不是输入或锚元素。

不要忘记检查每个div或元素的显示值。

答案 1 :(得分:-1)

您可以使用css中的 float 属性来执行此操作。 将 float:left 添加到 #header h1 。我已添加以下代码段。

#header input[type=text] {
    width: 130px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    margin-left: 21%;
    margin-bottom: 40%;
    position: absolute;
}

/* When the input field gets focus, change its width to 100% */
#header input[type=text]:focus {
    width: 17%;
}

/*Header Text*/
#header h1 {
    font-family: "smooth";
    font-size: 45px;
    color: #FFF;
    margin-top: 10px;
    margin-left: 30px;
    height: 0;
    float:left;
}

/* Make unordered list horizontle */
#header ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    margin-top: -12.5px;
    margin-left: 270px;
}

/* Make unordered list horizontle */
#header ul li {
    display: inline;
}

/* Make boxed & colored */
#header ul li a{
    text-decoration: none;
    padding: .2% 3%;
    color: #FFF; /*Text color, when not hovered*/
}

/* Make change colors, when hovered on */
#header ul li a:hover{
    color: lightgray; /*Text color*/
}

/* Header box */
#header{
    height: 60px;
    background-color: #3385ff;
    margin-top: -9px;
    margin-left: -20px;
    padding: 10px;
    width: auto;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

/*Remove line*/
#header a{
    text-decoration: none;
}
<head>
<script type="text/javascript">
  <!--
  if (screen.width <= 800) {
    window.location = "mindex.html"; //Mobile site
  }
  //-->
</script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BitBay</title>
<link rel="stylesheet" type="text/css" href="CSS/bootstrap.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet">
<link rel="icon" href="IMG/Logo.jpg">
<div id="header">
<a href="index.html"><h1>BitBay</h1></a>
<input type="text" name="search" placeholder="Search..">
<div class="dropdown">
  <button class="dropbtn"><i class="fa fa-arrow-down"></i>Account</button>
  <div class="dropdown-content">
    <a href="account.html"><i class="fa fa-key"></i>Login</a>
    <a href="signup.html"><i class="fa fa-pencil-square-o"></i>Signup</a>
    <a href="help.html"><i class="fa fa-life-ring"></i>Help & Support</a>
  </div>
</div>
</div>
<noscript>
    <div class="noscriptmsg">
    <meta http-equiv="refresh" content="0;url=javascript.html">
    </div>
</noscript>
</head>