Nidhin_toms
Nidhin_toms

Reputation: 737

Issue with divs side by side

I have 4 HTML elements in a parent div ( "header" ) and I am trying to display the child div's side by side. The problem is that the background color of the div "headerTitle" spills out. How do i restrict the background color to just the font itself? And also if you look at the snapshot, the HTML elements towards the left are getting displaced to below the line ( they are not in line with the other elements. please help...

HTML code:

<div id="header" >
   <div id="City1">
        Cities
   </div>
   <div id="headerTitle">
      Happy Stores
   </div>
   <div id="City2">
      Cities
   </div>
   <div id="City3">
      Cities
  </div>
</div>

CSS code:

#City1
{
    float:left;
}
#headerTitle
{
    float:center;
    background-color:gray;
}
#City2
{
    float:right;
    padding-left: 5px;
    color:orange;
}
#City3
{
    float:right;
    background-color:pink;
}

Snapshot

Upvotes: 0

Views: 59

Answers (3)

Parsa P.
Parsa P.

Reputation: 55

If your problem is about positioning of elements in the page, It's better to use

position:absolute;
left: *px;
top: *px;

instead of

float:left;

& I should say that:

float:center;

Is not an available value for float property.

Upvotes: 1

rybo111
rybo111

Reputation: 12588

How do i restrict the background color to just the font itself?

You would need to add display:inline; or display:inline-block to #headerTitle, depending on your requirements.

the HTML elements towards the left are getting displaced to below the line

See this answer to position your divs side-by-side:

#wrapper {
    width: 500px;
    border: 1px solid black;
    overflow: hidden; /* add this to contain floated children */
}
#first {
    width: 300px;
    float:left; /* add this */
    border: 1px solid red;
}
#second {
    border: 1px solid green;
    float: left; /* add this */
}

Upvotes: 1

s1h4d0w
s1h4d0w

Reputation: 771

You have used float: center; on #headerTitle, that's not a correct value. The only correct values are none, left, right, initial or inherit. Floating is not simply a means to align an element.

You would have to re-align your div's because a right float will be put under a left float if it comes after it. So you'd have to put #city2 and #city3 before #city1, add a text-align: center to #header and add a display: inline-block to #headerTitle.

Check out my jsFiddle for an example: https://jsfiddle.net/fx3ydcfu/

Upvotes: 1

Related Questions