当我应用float属性时内容向下移动

时间:2016-10-04 05:49:02

标签: html css css-float

我必须将类time向右浮动,但当我尝试将其向右浮动时,其内容会向下移动。没有浮动内容看起来很好,但我必须漂浮它。

https://jsfiddle.net/jyckLwv7/

.main-nav {
  color: #FFF;
  width: 100%;
  background-color: #5e2d91;
  float: right;
  line-height: 42px;
  margin-top: -3px;
}
.main-nav ul li {
  display: inline;
  padding: 0px 10px;
}
.main-nav ul li a {
  color: #FFF;
  text-decoration: none;
  padding: 20px 14px;
}
.main-nav ul {
  margin-bottom: 7px !important;
}
.main-nav ul li a:hover {
  background-color: #0098aa;
}
.main-nav ul li a:active {
  background-color: #6A006A;
}
.time {
  float: righ;
  background: #5e2d91;
  color: #FFFFFF;
  border-color: #718b88;
  font-family: sans-serif;
  font-weight: bold;
  border-style: solid;
  margin-left: 10%;
  width: 228px;
  height: 33px;
  margin-top: 9px;
  float: right;
}
<nav class="main-nav">
  <ul>
    <li><a href="http://marketinthepocket.com/"> Home </a>
    </li>
    <li><a href="transaction.php"> Trade Now</a>
    </li>

    <li><a href="view.php"> Transactions  </a>
    </li>
    <li><a href=""> Performance </a>
    </li>



    <li><a href="complete.php">History </a>
    </li>
    <li class="time">US Markets Open in&nbsp;<span id="hm_timer" class="style colorDefinition size_sm">08:20:04</span> 
    </li>
  </ul>
</nav>

3 个答案:

答案 0 :(得分:1)

首先,请勿使用 margin-top: -3px 来抵制设置为 {{的默认 padding 浏览器1}} ,因为此填充因浏览器而异。要获得更一致的视觉输出,您可以尝试使用以下内容对此属性进行规范化:

body

现在,遇到对齐问题,请尝试从 html, body { margin: 0; padding: 0; } height width >和 .time vertical-align: middle 元素。

jsFiddle:here

代码: (展开代码段)

&#13;
&#13;
li
&#13;
html, body {
  margin: 0;
  padding: 0;
}
.main-nav, .main-nav ul li {
  display: inline-block;
}
.main-nav {
  color: #FFF;
  width: 100%;
  background-color: #5e2d91;
  line-height: 24px;
}
.main-nav ul li {
  padding: 0px 10px;
  vertical-align: middle;
}
.main-nav ul li a {
  color: #FFF;
  text-decoration: none;
  padding: 20px 14px;
}
.main-nav ul li a:hover {
  background-color: #0098aa;
}
.main-nav ul li a:active {
  background-color: #6A006A;
}
.time {
  float: right;
  border-color: #718b88;
  font-family: sans-serif;
  font-weight: bold;
  border-style: solid;
  margin-left: 10%;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

overflow: hidden添加到ul元素:

.main-nav ul {
  margin-bottom: 7px !important;
  overflow: hidden;
}

.main-nav {
  color: #FFF;
  width: 100%;
  background-color: #5e2d91;
  float: right;
  line-height: 42px;
  margin-top: -3px;
}
.main-nav ul li {
  display: inline;
  padding: 0px 10px;
}
.main-nav ul li a {
  color: #FFF;
  text-decoration: none;
  padding: 20px 14px;
}
.main-nav ul {
  margin-bottom: 7px !important;
  overflow: hidden;
}
.main-nav ul li a:hover {
  background-color: #0098aa;
}
.main-nav ul li a:active {
  background-color: #6A006A;
}
.time {
  background: #5e2d91;
  color: #FFFFFF;
  border-color: #718b88;
  font-family: sans-serif;
  font-weight: bold;
  border-style: solid;
  margin-left: 10%;
  width: 228px;
  height: 33px;
  margin-top: 9px;
  float: right;
}
<nav class="main-nav">
  <ul>
    <li><a href="http://marketinthepocket.com/"> Home </a>
    </li>
    <li><a href="transaction.php"> Trade Now</a>
    </li>

    <li><a href="view.php"> Transactions  </a>
    </li>
    <li><a href=""> Performance </a>
    </li>



    <li><a href="complete.php">History </a>
    </li>
    <li class="time">US Markets Open in&nbsp;<span id="hm_timer" class="style colorDefinition size_sm">08:20:04</span> 
    </li>
  </ul>
</nav>

使用float容器时,请务必清除float s-请参阅this answer以获取更多信息。

  

请注意,使用浮动容器时,应始终清除   它们在下一个容器之前,从而创造一个新鲜的   在调用时阻止格式化上下文。否则你会看到   不可预测的行为。

您可以overflow: hidden代替clear而不是float使用:

.main-nav ul:after {
    content: '';
    display: block;
    clear: both;
}

.main-nav {
  color: #FFF;
  width: 100%;
  background-color: #5e2d91;
  float: right;
  line-height: 42px;
  margin-top: -3px;
}
.main-nav ul li {
  display: inline;
  padding: 0px 10px;
}
.main-nav ul li a {
  color: #FFF;
  text-decoration: none;
  padding: 20px 14px;
}
.main-nav ul {
  margin-bottom: 7px !important;
}
.main-nav ul:after {
  content: '';
  display: block;
  clear: both;
}
.main-nav ul li a:hover {
  background-color: #0098aa;
}
.main-nav ul li a:active {
  background-color: #6A006A;
}
.time {
  background: #5e2d91;
  color: #FFFFFF;
  border-color: #718b88;
  font-family: sans-serif;
  font-weight: bold;
  border-style: solid;
  margin-left: 10%;
  width: 228px;
  height: 33px;
  margin-top: 9px;
  float: right;
}
<nav class="main-nav">
  <ul>
    <li><a href="http://marketinthepocket.com/"> Home </a>
    </li>
    <li><a href="transaction.php"> Trade Now</a>
    </li>

    <li><a href="view.php"> Transactions  </a>
    </li>
    <li><a href=""> Performance </a>
    </li>



    <li><a href="complete.php">History </a>
    </li>
    <li class="time">US Markets Open in&nbsp;<span id="hm_timer" class="style colorDefinition size_sm">08:20:04</span> 
    </li>
  </ul>
</nav>

答案 2 :(得分:-1)

这是你要找的吗?

<强>代码:

.main-nav{
	color:#FFF;
	width:100%;
	background-color:#5e2d91;
	float:right;
	line-height:42px;
	    margin-top: -3px;
	}
.main-nav ul li{
	display: inline;
    padding: 0px 10px;
    
	
}
.main-nav ul li a{
	color:#FFF;
	text-decoration:none;
	padding: 20px 14px;
	
}
.main-nav ul{
	margin-bottom:7px !important;}
.main-nav ul li a:hover {
	background-color:#0098aa;
}
.main-nav ul li a:active{
	
	background-color:#6A006A;
}
.time {
 float: right;
    background: #5e2d91;
    color: #FFFFFF;
    border-color: #718b88;
    font-family: sans-serif;
    font-weight: bold;
    border-style: solid;
    margin-left: 10%;
    width: 228px;
    height: 33px;
    margin-top: 0;
    float: right;
        line-height: 2.2;
}
<nav class="main-nav">
<ul>
<li><a href="http://marketinthepocket.com/"> Home </a></li>
<li><a href="transaction.php"> Trade Now</a></li>

<li><a href="view.php"> Transactions  </a></li>
<li><a href=""> Performance </a></li>



<li><a href="complete.php">History </a></li>
<li class="time">US Markets Open in&nbsp;<span id="hm_timer" class="style colorDefinition size_sm">08:20:04</span> </li>
</ul>
</nav>