简体   繁体   English

如何在导航栏中将文本元素向右浮动而png元素向左浮动

[英]How to float text element to right with png element to left in nav bar

I'm trying to set up a nav bar with a png link to the top left corner, and a text element "menu" to the top right corner. 我正在尝试设置导航栏,该导航栏的png链接到左上角,文本元素“菜单”到右上角。 I haven't been able to get this to work with "float: right;" 我还无法使它与“ float:right;”一起使用。

I've included the code that shows I used float: right; 我已经包含了显示我使用float的代码: for the .topnav elements. .topnav元素。 I'm not sure if .logo is interfering with this. 我不确定.logo是否会干扰这一点。 I needed the png logo to be aligned with the text element which was not possible without putting them in separate divs. 我需要将png徽标与text元素对齐,这需要将它们放在单独的div中才能实现。

 .container { position: absolute; margin: 20px; width: auto; } .topnav { overflow: hidden; float: right; } .topnav a { float: left; color: #f2f2f2; text-align: center; padding: 10px 10px; text-decoration: none; font-size: 17px; } .topnav_right { float: right; } .logo { float: left; } 
 <div class="container"> <div class="logo"> <a href="index.html"><img src="####.png" style="max-width:100%;"></a> </div> <div class="topnav"> <div class="topnav_right"> <a href="index.html">Menu</a> </div> </div> </div> 

The text still remain next to the logo, when it should be in the opposite corner to the right. 文字仍应保留在徽标旁边,但应位于右侧的对角。

In the container class, instead of having position: absolute , do position: flex . container类中,不要使用position: absolute ,而是使用position: flex It will fix the problem. 它将解决问题。

Since you want a Navbar with left-aligned png-link and right-aligned text, it can be achieved in a much simpler way using flex-box, with the need of nesting them. 由于您希望导航栏具有左对齐的png链接和右对齐的文本,因此可以使用flex-box以更简单的方式来实现,并且需要嵌套它们。 It also handles alignment easily 它还易于对准

You can read more about flexbox from csstricks 您可以从csstricks阅读更多有关flexbox的信息。

<style>
    .topnav {
        display:flex;
        justify-content:space-between;
        align-items:center;
    }
</style>
<div class="topnav">
    <div class="logo">
        <a href="index.html"><img   src="###.png"></a>
    </div>
    <div class="text">
        <a href="index.html">Menu</a>
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM