0

I am trying to align two divs in a same horizontal position. One div is float left and the other is float right. I've also kept the two divs inside another div but they still can not be aligned to a same horizontal position.

#tray {
  padding: 20px 15px;
  background-color: #36648B;
  color: #FFFFFF;
}
.f-left {
  /*float: left !important;*/
  font-size: 25px;
  width: 400px;
}
.f-right {
  float: right !important;
  width: 200px;
}
.f-left a {
  color: white;
}
<div id="tray">
  <div>
    <div class="f-left">Business Intelligence, CIMB</div>
    <div class="f-right">
      <asp:Label ID="lblUser" Font-Size="Large" Font-Bold="true" runat="server" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <a href="WebLogin.aspx" runat="server" id="logout" onclick="web_logout">Log Out</a>
    </div>
  </div>
</div>

Image Attached :: enter image description here

kouty
  • 320
  • 8
  • 17
user4221591
  • 2,084
  • 7
  • 34
  • 68

7 Answers7

1

in your css

add

.f-left {
    display: inline-block;
}

.f-right a {
    display: block;
    text-align: center;
    padding: 3%;
    color: #fff;
}

and delete non breaking space

<asp:Label ID="lblUser" Font-Size="Large" Font-Bold="true" runat="server" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

to

<asp:Label ID="lblUser" Font-Size="Large" Font-Bold="true" runat="server" />
ha_ryu
  • 568
  • 2
  • 7
  • 20
0

If all the elements are in a single line here, then you can use the line-height fix for the elements. Give a fixed height to the parent element, and make it equal to the line-height of all the child elements.

Refer code:

#tray {
  padding: 20px 15px;
  background-color: #36648B;
  color: #FFFFFF;
}
.f-parent {
  height: 30px;
}
.f-left {
  display: inline-block;
  font-size: 25px;
  line-height: 30px;
}
.f-right {
  float: right !important;
  /* width: 200px; */ /* Uncomment this to give a fixed width */
  line-height: 30px;
}
.f-left a {
  color: white;
}
<div id="tray">
  <div class="f-parent">
    <div class="f-left">Business Intelligence, CIMB</div>
    <div class="f-right">
      <asp:Label ID="lblUser" Font-Size="Large" Font-Bold="true" runat="server" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <a href="WebLogin.aspx" runat="server" id="logout" onclick="web_logout">Log Out</a>
    </div>
  </div>
</div>
nashcheez
  • 5,067
  • 1
  • 27
  • 53
  • Giving height to a div is not good for the responsiveness??? – Shubhranshu Jan 25 '17 at 12:34
  • Not really, if your inner contents have a fixed content then providing a fixed height does not hamper your responsiveness. You can change the height in your media queries if need arises for your small height. – nashcheez Jan 25 '17 at 12:40
0

You would need to add the style:

display: inline-block;

To the parent div.

Yazid Erman
  • 1,166
  • 1
  • 13
  • 24
0

You could use FlexBox:

HTML

<div id="tray">
      <div class="f-left">Business Intelligence, CIMB</div>
        <div class="f-right">
            <asp:Label ID="lblUser" Font-Size="Large" Font-Bold="true" runat="server" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="WebLogin.aspx" runat="server" id="logout" onclick="web_logout">Log Out</a>
        </div>

</div>

CSS

#tray {
   display: flex;
   align-items: baseline;
   justify-content: space-between;
   padding: 20px 15px;
   background-color: #36648B;
   color: #FFFFFF;
 }

 .f-left {
   font-size: 25px;
   width: 400px;
 }

 .f-right {
   width: 200px;
 }

 .f-left a {
   color: white;
 }

I removed the extra div in your HTML and just added display: flex, align-items: baseline and justify-content: space-between.

Sergi
  • 1,192
  • 3
  • 19
  • 37
0

Remove the additional surrounding div and add these properties on the tray class

display: inline-flex;
align-items: center;

#tray {
  padding: 20px 15px;
  background-color: #36648B;
  color: #FFFFFF;
  display: inline-flex;
  align-items: center;
}
.f-left {
  /*float: left !important;*/
  font-size: 25px;
  width: 400px;
}
.f-right {
  width: 200px;
  
}
.f-left a {
  color: white;
}
<div id="tray">
    <div class="f-left">Business Intelligence, CIMB</div>
    <div class="f-right">
      <asp:Label ID="lblUser" Font-Size="Large" Font-Bold="true" runat="server" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <a href="WebLogin.aspx" runat="server" id="logout" onclick="web_logout">Log Out</a>
    </div>
</div>
Deep
  • 9,594
  • 2
  • 21
  • 33
0

Use

float:left;

for both the div.

Shadow Walker
  • 206
  • 1
  • 2
  • 13
0

Please try this one...

<style>
    body {
        padding: 0;
        margin: 0;
    }

    #tray {
        padding: 20px 15px;
        background-color: #36648B;
        color: #FFFFFF;
    }

    .f-left {
        float: left !important;
        font-size: 25px;
        width: 400px;
    }

    .f-right {
        float: right !important;
        width: 200px;
    }

    .f-left a {
        color: white;
    }

    .clearfix {
        clear: both;
    }
</style>

html

<div id="tray">
    <div>
        <div class="f-left">Business Intelligence, CIMB</div>
        <div class="f-right">
            <asp:Label ID="lblUser" Font-Size="Large" Font-Bold="true" runat="server" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="WebLogin.aspx" runat="server" id="logout" onclick="web_logout">Log Out</a>
        </div>
        <div class="clearfix"></div>
    </div>
</div>
Prabin
  • 1
  • 2