0

When having 2 divs, one on the left and one on the right. Is it possible to have the right div aligned all the way right with a fixed width and have the left div take up all the space left?

I don't want to work with inline-

enter image description here

S. Robijns
  • 1,529
  • 3
  • 14
  • 17

3 Answers3

1

You can use CSS calc() function here to minus the width of fixed .right div from .left div.

The calc() CSS function lets you perform calculations when specifying CSS property values.

#bx{
  background:black;
  height:200px;
  padding:10px;
}
#bx > .left{
  display:inline-block;
  width:calc(99% - 200px); /*Minus width of .right using calc()*/
  height:100%;
  background:yellow;
}
#bx > .right{
  display:inline-block;
  width:200px;
  height:100%;
  background:red;
}
<div id="bx">
  <div class="left">Left Div</div>
  <div class="right">Right Div Fixed Width.</div>
</div>
frnt
  • 8,455
  • 2
  • 22
  • 25
0

I think this accomplishes what you are after but I'm not sure its the best way...

.container {
  width: 100%;
  height: 200px;
  background-color: green;
}

.sidebar {
  float: right;
  width: 200px;
  height: 200px;
  background-color: blue;
}

.content {
  background-color: red;
  height: 200px;
  margin-right: 200px;
}
<div class="sidebar">width: 200px</div>
<div class="content">
</div>
A Friend
  • 1,420
  • 17
  • 22
  • this indeed seems to work, but quite a lot of code and a strange setup since the content div element comes behind the sidebar div in html – S. Robijns Nov 10 '17 at 11:42
  • First, you don't need `width: auto;`, it is the default, second, of you use a _clearfix_ element, it should be positioned _after_ the floated element to have an effect. – Asons Nov 10 '17 at 13:44
  • okay changed my answer – A Friend Nov 10 '17 at 14:06
0

There are plenty of ways to achieve this, but you may want to use flex-boxes as it's widely used these days.

Check caniuse to see if it meets your browser requirements.

Markup:

<div class="container">
  <div class="left-container">Autofill remaining width</div>
    <div class="fixed-container">200px</div>
</div>

CSS:

.container{
  display: flex;
}
.left-container {
  background-color: red;
  flex: 1;
}
.fixed-container {
  flex-basis: 200px;
  background-color: yellow;
  text-align: center;
}

Demo

Rajender Joshi
  • 4,155
  • 1
  • 23
  • 39
  • 1
    flex isn't supported on versions of ie below 11 so its not the most versatile solution https://caniuse.com/#search=flex – A Friend Nov 10 '17 at 11:39
  • 1
    ie10 and below are not supported by microsoft so I don't see that as much of a problem - flex is supported by all latest browsers that are supported by their companies – Pete Nov 10 '17 at 11:41
  • 1
    @RyanEarnshaw Vendor prefix(-ms-) can be used to support IE10. https://msdn.microsoft.com/en-us/library/hh673531(v=vs.85).aspx – Rajender Joshi Nov 10 '17 at 11:51
  • Addressing to both last comments go to this link: https://caniuse.com/#search=flex and read the "notes" and "known issues" section. You'll find that flex, although works, works only partially because it is unsupported – A Friend Nov 10 '17 at 11:55
  • @RyanEarnshaw It works only partially in IE _due to large amount of bugs present_, which is _very_ different from "unsupported". And IE10 support the older version, which in solving this case, has no bugs. – Asons Nov 10 '17 at 13:35