I have to float the class time
to right but when I try to float it right, its content moves down. Without float contents look fine but I have to float it.
h
Is it this what you are looking for?
Code:
.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 <span id="hm_timer" class="style colorDefinition size_sm">08:20:04</span> </li>
</ul>
</nav>
Add overflow: hidden
to the ul
element:
.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 <span id="hm_timer" class="style colorDefinition size_sm">08:20:04</span>
</li>
</ul>
</nav>
When using float
ing container, always remember to clear the float
s- see this answer for more information.
Note that while using floating containers, you should always clear them before the next container that follows thereby creating a fresh block formatting context as it is called. Otherwise you will see unpredictable behavior.
Instead of overflow: hidden
you can also clear
the float
by using:
.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 <span id="hm_timer" class="style colorDefinition size_sm">08:20:04</span>
</li>
</ul>
</nav>
First of all, don't use margin-top: -3px
to fight off that default padding
browsers set to body
, as this padding varies from browser to browser. To achieve a more consistent visual output, you can try normalising this properties with something like:
html, body {
margin: 0;
padding: 0;
}
Now, coming to your alignment issue, try removing height
and width
from .time
and vertical-align: middle
to your li
elements.
jsFiddle: → here
Code: (expand the snippet)
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%;
}
<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
<span id="hm_timer"class="style colorDefinition size_sm">08:20:04</span>
</li>
</ul>
</nav>