2013-03-30 7 views
0

Для следующего кода он делает снимок следующим образом: enter image description here Эффект, который я хочу, заключается в том, что «Hello, user1» выровнен с черным ящиком, и оба они выровнены с правой стороны из серой коробки, как я должен это достичь?Выравнивание элементов друг с другом

<html> 
<head> 
<style> 
    #header { 
    width: 100%; 
    height: 81px; 
    background: url(http://www5.picturepush.com/photo/a/12541848/img/Anonymous/header-bg.png) 0 0 repeat-x; 
} 
#header h1 { 
    display: block; 
    float: left; 
    margin: 30px 0 0 35px; 
    font-size: 18px; 
    color: #6a6a6a; 
} 
#header #logo { 
    display: block; 
    float: left; 
} 
#header #userlog { 
    width: 272px; 
    height: 31px; 
    background-color: #363636; 
    border-radius: 2px; 
    float: right; 
    margin: 27px 26px 0 0; 
}#header #userlog a { 
    font-size: 13px; 
    float: left; 
    color: #b7b7b7; 
    margin-top: 6px; 
} 

#welcome_msg { 
    color: #b7b7b7; 
    float: right; 

} 
    </style> 

</head> 
<body> 
<div id="header"> 
    <a id="logo" href="dashboard.php"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/archive/c/c1/20070624012131!Fuji_apple.jpg/120px-Fuji_apple.jpg" alt="" /></a> 
    <div class="welcome_msg" id="welcome_msg">hello, user1</div> 
    <h1> My Portal </h1> 
    <div id="userlog"> 
     <a href="#" style="margin-left:25px;text-decoration:underline;">Contact Support</a> 
     <a href="#" style="margin-left:20px;">FAQ</a> 
     <a href="../index.php" style="margin-left:25px;">logout</a> 
    </div> 
</div> 
</body> 
</html> 

ответ

0

Нравится?

enter image description here

Я добавил <div> контейнер для хранения #userlog и #welcome_msg.

CSS

#header #userlog { 
    margin: 0 26px 0 0; 
} 
#welcome_msg { 
    margin-right:26px; 
} 

HTML

<div id="container"> 
    <div class="welcome_msg" id="welcome_msg">hello, user1</div> 
    <br /> 
    <div id="userlog"> 
     <a href="#" style="margin-left:25px;text-decoration:underline;">Contact Support</a> 
     <a href="#" style="margin-left:20px;">FAQ</a> 
     <a href="../index.php" style="margin-left:25px;">logout</a> 
    </div> 
</div> 
3

Положите обе элементы в DIV и всплывают, что Див право. Дополнительное пространство справа - это потому, что элемент имеет 26-кратный запас.

<div id="whatever"> 
    <div class="welcome_msg" id="welcome_msg">hello, user1</div> 
    <div id="userlog"> <a href="#" style="margin-left:25px;text-decoration:underline;">Contact Support</a> 
     <a href="#" style="margin-left:20px;">FAQ</a> 
     <a href="../index.php" style="margin-left:25px;">logout</a> 
    </div> 
</div> 


#whatever { 
    float:right; 
} 

#header #userlog { 
    width: 272px; 
    height: 31px; 
    background-color: #363636; 
    border-radius: 2px; 
} 

Демо: http://jsfiddle.net/kzhuL/

Смежные вопросы