0

Say I have 3 divs like this:

#wrapper {width:100%; border:1px solid #ccc}
#wrapper div {box-sizing: border-box; float:left; border:1px solid #777}
#wrapper #first {width:30%}
#wrapper #second {width:70%}
#wrapper #third {width:30px}
<div id="wrapper">
  <div id="first">A</div>
  <div id="second">B</div>
  <div id="third">C</div>
</div>

What I want:

  1. The 3 divs all floating in a row in a flexible container;
  2. The width of 3rd div is always a fixed value, like 30px;
  3. The first and second div has width in a percentage value, to use the rest room of the container.

For instance, if the container is 100px, the 3rd div is 30px and the first is (100-30)*30% = 21px, the second is (100-30)*70% = 49px.

What is the correct way to do this?

Thanks

AGamePlayer
  • 7,404
  • 19
  • 62
  • 119

4 Answers4

1

You can use calc:

#wrapper {width:100%; border:1px solid #ccc}
#wrapper div {box-sizing: border-box; float:left; border:1px solid #777}
#wrapper #first {width:calc(30% - 30px)}
#wrapper #second {width:calc(70% - 30px)}
#wrapper #third {width:30px}
<div id="wrapper">
  <div id="first">A</div>
  <div id="second">B</div>
  <div id="third">C</div>
</div>

If you want, you can use a css variable to not have to write 30px three times.

vityavv
  • 1,482
  • 12
  • 23
0

You don't have to use width calculation for this. you can do something like this.

you can also do max-width to the selector class which you want keep varying the size dynamically and you can freeze the size of your last block i.e. third div.

#wrapper {width:100%; border:1px solid #ccc}

#wrapper #first{
width:30%;
display:inline-block;
border:1px solid red;
}
#wrapper #second{
width:60%;
display:inline-block;
border:1px solid teal;
}
#wrapper #third{
width:30px;
display:inline-block;
border:1px solid blue;
}
<div id="wrapper">
  <div id="first">A</div>
  <div id="second">B</div>
  <div id="third">C</div>
</div>
Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
0

#wrapper {width:100%; border:1px solid #ccc}
#wrapper div {box-sizing: border-box; float:left; border:1px solid #777}
#wrapper #first {width:calc(30% - 15px)}
#wrapper #second {width:calc(70% - 15px)}
#wrapper #third {width:30px}
<div id="wrapper">
  <div id="first">A</div>
  <div id="second">B</div>
  <div id="third">C</div>
</div>
halfer
  • 19,824
  • 17
  • 99
  • 186
vadivel a
  • 1,754
  • 1
  • 8
  • 18
  • When posting a possible solution, consider adding an explanation as to why it might help. This will make the question and answer more useful for future readers, who may want to learn from it as well. – halfer Mar 17 '20 at 00:59
  • Incidentally, I have removed "hope this helps" from your text, since there is a preference on Stack Overflow for technical writing. Everyone hopes their answer helps, so it is quite redundant. Please refrain from adding this (and any other conversational material) if you can. – halfer Mar 17 '20 at 01:01
0

#wrapper {width:100%; border:1px solid #ccc; display: flex}
#wrapper div {box-sizing: border-box; float:left; border:1px solid #777}
#wrapper #first {flex-basis: 30%}
#wrapper #second {flex-basis: 70%}
#wrapper #third {flex-basis: 30px}
<div id="wrapper">
  <div id="first">A</div>
  <div id="second">B</div>
  <div id="third">C</div>
</div>

Another way to achieve this using flexbox. Hope this helps.

Vishnu
  • 1,611
  • 1
  • 14
  • 27