Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to align two DIVs side by side if one is fixed?

Tags:

html

css

I am trying setup a design where I would like a left bar for navigation and things that remains fixed and doesn't scroll, but have a content box next to it that does scroll as needed. The problem I'm running into, if I position: fixed; the first DIV it technically does what I want, but it overlaps the second DIV. I'm just creating this and using JsFiddle to test easily, so I don't have an actual working code other than this fiddle. I'll admit, I've been awake for about 30 hours now, so if this is a really silly oversight from me, please forgive me. Thanks!

FIDDLE

like image 904
Protocol Zero Avatar asked May 26 '15 08:05

Protocol Zero


People also ask

How do I align two divs side by side?

To position the divs side by side, we are using the float property to float each . float-child element to the left. Since they are both floating to the left, they will display side by side if there's enough space for both to fit.

How do I align a div with another div?

Use the "inline-block" value of the display property to display the inner <div> as an inline element as well as a block. Set the text-align property on the outer <div> element to center the inner one. This property only works on inline elements.

Can we place two div blocks at same line?

The problem of aligning 2 divs horizontally: Since <div> tag is a block element, only one div can be placed in a line, and all the space to the left and right is taken by it.

How do I put divs side by side in HTML?

Give the first div float: left; and a fixed width, and give the second div width: 100%; and float: left; . That should do the trick. If you want to place items below it you need a clear: both; on the item you want to place below it.


2 Answers

I tried to write this code and it is responsive too.

* {
    padding: 0px;
    margin: 0px;
}
#one {
    float: left;
    position: fixed;
    width: 25%;
    background: #666;
    height: 100%;
}
#two {
    box-sizing: border-box;
    padding: 20px;
    position: absolute;
    left: 25%;
    right: 0%;
    float: right;
    width: 75%;
    background: #333;
}

I hope this helps.

like image 78
Jugal Panchal Avatar answered Oct 31 '22 03:10

Jugal Panchal


When you add position:fixed the element is taken out of the flow and its basically functions in respect to the window .

so the following CSS :

#one {
    float: left;
    position: fixed;
    width: 25%;
    background: #666;
    height: 100%;
} 

25% is 25% of the window not 25% of <div id="wrap">(and hence the overlap) , if you take off the position:fixed you'll see no overlap .

with position fixed , you probably want to have some left offset on <div id="two">, you cal experiment with :

margin-left: // DO YOUR MATH.

padding-left: // DO YOUR MATH.
like image 25
Alexander Solonik Avatar answered Oct 31 '22 02:10

Alexander Solonik