Fixed width div on left, fill remaining width div on right
Asked Answered
D

2

51

I want a div with a fixed width image on the left and a variable width div with a background color, which should extend its width 100% on my device. I can't stop the second div from overflowing my fixed div.

When I add overflow:hidden at the variable width div it just jumps under the photo, on the next row.

How can I fix this the right way (i.e. without hacks or margin-left, since I need to make the site responsive later with media queries and I have to change the image with other resolution images for each device)?

  • beginner web designer trying to tackle the horror of responsive websites -

HTML:

<div class="header"></div>
<div class="header-right"></div>

CSS:

.header{
    float:left;
    background-image: url('img/header.png');
    background-repeat: no-repeat;
    width: 240px;
    height: 100px;
    }

.header-right{
    float:left; 
    overflow:hidden; 
    background-color:#000;
    width: 100%;
    height: 100px;
    }
Doucet answered 4/10, 2012 at 10:9 Comment(0)
O
60

Try removing the float:left and width:100% from .header-right — the right div then behaves as requested.

.header {
  float: left;
  background: #efefef;
  background-repeat: no-repeat;
  width: 240px;
  height: 100px;
}

.header-right {
  overflow: hidden; 
  background-color: #000;
  height: 100px;
}
<div class="header"></div>
<div class="header-right"></div>
Oxcart answered 4/10, 2012 at 10:54 Comment(5)
Thanks so much! It works on both the iPhone and Android Galaxy S plus! I think I just have to watch out and make the right media query min-device-pixel-ratio. Wish I could vote up, but I'm too new.Doucet
If you wanted to reverse the setup and have the right div fixed and the variable-width div on the left, would it be as simple as as changing the float:left on .header to be float:right? I've been playing with this at jsfiddle.net/veW2z/14 and I can't seem to make it behave the way I want it to.Pagan
@PaulGear yep that should do it. keep the order of the divs the same though.Oxcart
Any ideas why jsfiddle.net/veW2z/19 isn't doing the right thing? I couldn't find a way to make it work apart from adding a margin-right to the left div that exceeds the width of the right div.Pagan
I tested this with height: 100% on .header and .header-right. When inspected with FireBug, the .header-right element appears to fill the entire area instead of the remaining area only. Is there any explanation for this behavior?Anallise
P
0

with the use of css grid you can achieve this more easily

  • you need to wrap those divs in a wrapper, lets say parent
  • give .parent display: grid
  • give grid-template-areas in .parent and grid-area in both children
  • no need to use float: left in both children adjust the width of left div using grid-template-columns in .parent

.parent {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "header-left header-right";
}
.header {
    background-image: url(img/header.png);
    background-color: #ebebeb;
    background-repeat: no-repeat;
    height: 100px;
    grid-area: header-left;
}

.header-right {
    overflow: hidden;
    background-color: #000;
    width: 100%;
    height: 100px;
    grid-area: header-right;
}
<div class="parent">
  <div class="header"></div>
  <div class="header-right"></div>
</div>

css has become much more advanced and answering with the help of that advanced css just if it can be useful to someone :)

Pharmacopsychosis answered 18/9, 2019 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.