文字卡在div的中间而不是最左边

时间:2016-08-26 04:15:16

标签: html css html5

As you can see i goes straight to the middle instead of the starting point of the div.我为margin设置了widthdiv,当我输入<H1>标记时,文字只是{{1} } floats的中间,而不是div开始的最左边。我已设置div,因此它可以从该起点开始,而不是左侧width

这是HTML

45px

这是我的CSS

<div id="topbar">
  <div id="logo"> <img src="Pictures/bbc.png"> </div>
  <div id="signin" class="border-part"> </div>
  <div> <img id="logo-image" src="Pictures/signin.png">
    <p class="signin-text">Sign in</p>
  </div>
  <div id="dash"> <img id="dash-image" src="Pictures/bash2.png"> </div>
  <div> <img id="bell"src="Pictures/bell3.png"> </div>
  <div class="border-part stuff-section"> News </div>
  <div class="border-part stuff-section"> Sport </div>
  <div class="border-part stuff-section"> Weather </div>
  <div class="border-part stuff-section"> iPlayer </div>
  <div class="border-part stuff-section"> TV </div>
  <div class="border-part stuff-section"> Radio </div>
  <div class="border-part stuff-section"> More </div>
  <div class="seacher-border">
    <input id="search-box" type="text" placeholder="Search">
    <img id="search-pic" src="Pictures/search.png"> </div>
</div>
<div id="second-bar">
  <div id="newsbar">
    <h1>NEWS</h1>
  </div>
</div>

2 个答案:

答案 0 :(得分:2)

用于对齐的flexbox选项

#second-bar {
  display: flex;
  background-color: #BB1919;
  width: 100%;
  height: 80px;
}
#newsbar {
  display: flex;
  justify-content: flex-start; /* horizontal alignment - change to flex-end or center to see difference */
  align-items: center; /* vertical alignment */
  width: 100%;
  margin:0 auto;
}
h1 {
  display: flex;
  padding:0;
  margin:0;
}
<div id="second-bar">
  <div id="newsbar">
    <h1>NEWS</h1>
  </div>
</div>

<强>拨弄

https://jsfiddle.net/Hastig/zn4qkok2/

可能的非Flexbox解决方案

#second-bar {
  background-color: #BB1919;
  width: 100%;
  height: 80px;
}
#newsbar {
  text-align: left; /* change to center or right to see difference */
  vertical-align: middle;
  width: 100%;
  line-height: 80px; /* match to height of parent for centered vertical alignment */
  margin:0 auto;
}
h1 {
  display: inline-block;
  padding:0;
  margin:0;
}
<div id="second-bar">
  <div id="newsbar">
    <h1>NEWS</h1>
  </div>
</div>

<强>拨弄

https://jsfiddle.net/Hastig/zn4qkok2/1/

答案 1 :(得分:1)

只需将clear: both添加到h1

&#13;
&#13;
#topbar {
  width: 1080px;
  margin: 0 auto;
  height: 40px;
}

body {
  margin: 0;
  padding: 0;
  font-family: Helmet, Freesans, Helvetica, Arial, sans-serif;
  ;
}

#logo {
  margin-top: 8px;
  float: left;
  margin-right: 8px;
}

.border-part {
  float: left;
  height: 100%;
  border-left: 1px #CCCCCC solid;
}

#logo-image {
  width: 20px;
  margin: 12px;
  float: left;
}

.signin-text {
  margin: 14px;
  font-size: 90%;
  font-weight: bold;
  float: left;
}

#dash-image {
  padding-left: 50px;
  float: left;
  height: 40px;
}

#bell {
  margin: 8px;
  height: 28px;
  float: left;
}

.stuff-section {
  font-weight: bold;
  font-size: 90%;
  padding: 13px 15px 0 15px;
  height: 27px;
}

.seacher-border {
  float: left;
  height: 100%;
  border-left: 1px #CCCCCC solid;
  margin-left: 20px;
}

#search-box {
  margin: 10px;
  font-weight: bold;
  background-color: #E4E4E4;
  font-size: 14px;
  border: none;
  height: 24px;
  float: left;
}

#search-pic {
  position: relative;
  right: 10px;
  top: 10px;
  height: 25px;
}

#second-bar {
  background-color: #BB1919;
  width: 100%;
  height: 80px;
}

#newsbar {
  width: 1080px;
  margin: 0 auto;
  background-color: blue;
}

h1 {
  padding: 0;
  margin: 0;
  clear: both;
}
&#13;
<div id="topbar">
  <div id="logo"> <img src="Pictures/bbc.png"> </div>
  <div id="signin" class="border-part"> </div>
  <div> <img id="logo-image" src="Pictures/signin.png">
    <p class="signin-text">Sign in</p>
  </div>
  <div id="dash"> <img id="dash-image" src="Pictures/bash2.png"> </div>
  <div> <img id="bell" src="Pictures/bell3.png"> </div>
  <div class="border-part stuff-section"> News </div>
  <div class="border-part stuff-section"> Sport </div>
  <div class="border-part stuff-section"> Weather </div>
  <div class="border-part stuff-section"> iPlayer </div>
  <div class="border-part stuff-section"> TV </div>
  <div class="border-part stuff-section"> Radio </div>
  <div class="border-part stuff-section"> More </div>
  <div class="seacher-border">
    <input id="search-box" type="text" placeholder="Search">
    <img id="search-pic" src="Pictures/search.png"> </div>
</div>
<div id="second-bar">
  <div id="newsbar">
    <h1>NEWS</h1>
  </div>
</div>
&#13;
&#13;
&#13;