1

So, I have this div container that spans across the page (please see the picture).

<div class="container">
  <div class="A">Content length changes</div>
  <div class="B">Width dependent on "A"</div>
  <div class="C">fixed 27px width</div>
  <div class="D">fixed 27px width</div>
</div>

enter image description here

The issue is that the width of "A" is dynamic and it changes which means "B" width also changes (Although "B" will have minimum width of 27px).

I can measure the width of the "A" and change the css using jQuery but I want to know if there is a way to handle the width variables for "A" and "B" simply using CSS.

4
  • you mean min-width? change it to 'width'. Please provide fiddle Commented May 18, 2016 at 5:15
  • sounds good. I will make one. thanks! Commented May 18, 2016 at 5:16
  • You can probably solve this pretty cleanly using Flexbox CSS properties like flex-grow or flex-shrink. Commented May 18, 2016 at 5:23
  • mm i see. I will look into that! Thanks! Commented May 18, 2016 at 5:24

2 Answers 2

2

Here is how I did using flexbox

div {
  border: 3px solid;
}
.child {
  padding: 10px;
  margin: 10px;
  background-color: #eee;
}
.container {
  padding: 10px;
  background-color: yellow;
  display: -webkit-flex;
  display: flex;
}
.child.one {
  -webkit-flex: 5 1 200px;
  flex: 5 1 200px;
  color: green;
}
.child.two {
  -webkit-flex: 1 3 200px;
  flex: 1 3 200px;
  color: purple;
}
.child.three,
.child.four {
  width: 50px;
}
<div class="container">
  <div class="child one">
    Child One
    <br>Lorem ipsum
    <br>dolor sit amet
    <br>This is a bit longer line
  </div>

  <div class="child two">
    Two
  </div>

  <div class="child three">
    Three
  </div>

  <div class="child four">
    Four
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Why are you including -webkit- prefixes on flex? Chrome hasn't used prefixes for flex in over twenty major versions.
1

I have a solution without any flex. Simple CSS only.

.A {
  float:left;
}
.B {
  overflow: hidden;
}
.C, .D {
  float: right;
  width: 27px;
  min-width: 27px;
}

/* beautiful appearance */
.container {
  border: 4px solid #999;
  padding: 8px;
}
.A, .B, .C, .D {
  padding: 4px 8px;
}
.A { background: #ff6; }
.B { background: #69f; }
.C { background: #f60; }
.D { background: #6c6; }
<div class="container">
  <div class="D">D</div>
  <div class="C">C</div>
  <div class="A">A. Content length changes</div>
  <div class="B">B. Dependent on "A"</div>
</div>

http://jsfiddle.net/glebkema/Lf5fw9e6/

UPD. I've added a border to the container for clarity.

2 Comments

This is pretty clever! Thanks! =D
@HEYHEY I am glad to be helpful. I've added a border to the container for clarity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.