How to fill 100% of remaining width - css

Is there any work around to do something like this work as expected?
I wish there were something like that width:remainder; or width:100% - 32px;.
width: auto; doesn't works.
I think the only way possible is working around with paddings/margins, negative values, or float, or some html tags hack. I tried also display:block;.
I like to get the same result as this, without tables http://jsfiddle.net/LJGWY/
<div style="position: absolute; width: 100%; height: 100px; border: 3 solid red;" id="container">
<div style="display:inline; width: (100%-100px); border: 3 solid green;">Fill</div>
<div style="display:inline; width: 100px; border: 3 solid blue;">Fixed</div>
</div>

Updated answer:
The answers here are pretty old. Today, this can be achieved easily with flexbox:
.container {
border: 4px solid red;
display: flex;
}
.content {
border: 4px solid green;
flex-grow: 1;
margin: 5px;
}
.sidebar {
border: 4px solid blue;
margin: 5px 5px 5px 0;
width: 200px;
}
<div class="container">
<div class="content">
Lorem ipsum dolor sit amet.
</div>
<div class="sidebar">
Lorem ipsum.
</div>
</div>
Original answer:
Block level elements like <div> will fill 100% of the available width automatically. If you float one of them to the right, the contents of the other will fill the remaining space.
<div style="height: 100px; border: 3px solid red;" id="container">
<div style="float: right; width: 100px; border: 3px solid blue;">Fixed</div>
<div style="border: 3px solid green;">Fill</div>
</div>
http://jsfiddle.net/5AtsF/

For anyone looking over this now theres a newish css property method called calc which can perform this in a much more flexible fashion.
<div class="container">
<div class="fixedWidth"></div>
<div class="variableWidth"></div>
</div>
.fixedWidth{
width:200px;
}
.variableWidth{
width:calc(100%-200px);
}
As a word of warning, this is not very portable and support is ropey on mobile devices. IOS 6+ and andriod 4.4 i believe. Support is significantly better for desktop though, IE 9.0+.
http://caniuse.com/calc
I have used a JS hack in the past to achieve this technique if anyone is incredibly stuck, a different layout is more advisable though as resize is slower.
window.addEventListener('resize', function resize(){
var parent = document.getElementById('parent');
var child = document.getElementById('child');
child.style.width = parseInt(parent.offsetWidth - 200) + "px"; //200 being the size of the fixed size element
}, false);

If you don't know how big will be the fixed part you can use the flex 9999 hack.
<div class="container">
<div class="fixedWidth"></div>
<div class="variableWidth"></div>
</div>
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.fixedWidth {
flex: 1;
}
.variableWidth {
flex: 9999;
}

This should do for you:
<div style="position: absolute; width: 100%; height: 100px; border: 3px solid red;" id="container">
<div style="float: right; width: 100px; border: 3px solid blue;">Fixed</div>
<div style="display: block; margin-right: 100px; border: 3px solid green;">Fill</div>
</div>
See the jsFiddle
This is assuming you're going to be removing the 3px borders from the end result (they overlap in the example because border width is not included in the width).

You can acheive this without change your markup with use display:table property for this:
.parent{
position: absolute;
left:0;
right:0;
height: 100px;
border: 3px solid red;
display:table;
}
.fill{
margin-right: 100px;
border: 3px solid green;
display:table-cell;
width:100%;
}
.fixed{
width: 100px;
border: 3px solid blue;
display:table-cell;
}
Check the live example with no horizontal scrollbar
http://jsfiddle.net/WVDNe/5/
Another example but in better way check this:
http://jsfiddle.net/WVDNe/6/
note: it not work in IE7 & below
Check this also
http://jsfiddle.net/LJGWY/4/
It's work in all browsers.

Try setting the position like so:
<div style="position: absolute; width: 100%; height: 100px; border: 3 solid red;" id="container">
<div style="position:absolute; left: 0; top: 0; right: 100px; border: 3 solid green;">Fill</div>
<div style="position:absolute; top: 0; right: 0; width: 100px; border: 3 solid blue;">Fixed</div>
</div>

You could put the fixed div inside the the fill div.
<div id="container">
<div>Fill
<div>Fixed</div>
</div>
</div>
CSS
#container{
position:absolute;
width:90%;
height:100px;
border:3px solid red;
}
#container div{
height:95%;
border:3px solid green;
width:100%;
}
#container div div{
height:95%;
width:100px;
border:3px solid blue;
float:right;
}
Example: http://jsfiddle.net/EM8gj/3/

you can use table style.
create a div with table style and sub items be that's table-cell styles
label text

Try CSS calc() function.
width: calc(100% - 100px);
That's it

Related

How to overlay a div without messing with other div

I have a container div like this:-
<div class="container">
<div class="parent"></div>
<div class="child"></div>
</div>
I want to overlay the child div like this
But without touching the parent div css. How can I achieve this. I'll appreciate any kind of suggestions.
It is not clear from what you want to achieve but can try some of the below stuf :
You can achieve that by using top/bottom/left/right property and position: relative to the .child according to need.
You can achieve by using extra container and add display: flex;width:100% and using justify-content properties according to need .
float: left/right can also be used according to need(but you will not able to position other than left, right and use margin)
Simply by top/bottom/left/right margin can achieve the position .
* {
box-sizing: border-box;
}
.container {
position: relative;
width: 400px;
height: 300px;
border: 3px solid #73AD21;
}
.parent {
width: 200px;
height: 80px;
margin: 10px;
border: 3px solid #73AD21;
}
.child {
position: relative;
left: 180px;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
<div class="container">
<div class="parent"></div>
<div class="child"></div>
</div>

Horizontal Bar with Circle in Middle

I am trying to create a horizontal bar the spreads across the full width of my container. In the middle of the bar, I want a circle that is both horizontally/vertically centered. The circle will overlap the horizontal bar. Right now I can get it so be horizontally aligned but I'm having difficulty vertically centering the bar behind the circle. Here is my code:
#wrapper {
width: 1200px;
height: auto;
margin: 0 auto;
}
#navigation {
display: block;
width: 100%;
height: 50px;
background-color: #275337;
}
#navstamp {
background: white;
width: 218px;
height: 218px;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
margin: 0 auto;
border: 1px solid;
border-color: rgba(19, 36, 17, 1);
}
And my HTML:
<div id="wrapper">
<div id="navigation">
<div id="navstamp">
</div>
</div>
</div>
Any help would be greatly appreciated!
I'd make use of the css3 ::before pseudo element here, if ancient browser support is not a problem,
<div id="wrapper"> <!-- replaced the navigation with ::before-->
<div id="navstamp">
</div>
</div>
#navstamp::before {
display: block;
content:""; // add the content you need here
position:absolute;
top:50%;
left:0;
width: 100%;
height: 50px;
background-color: #275337;
}
as in this JSFiddle
Your first div tag has not been closed if that's your complete html . As in the fiddle your output is as you are explaining.
<div id="wrapper">
<div id="navigation">
<div id="navstamp">
<div id="headlinetext">
<p class="headline">Lorem Ipsum</p>
<p class="subheadline">- Ipsum -</p>
</div>
</div>
</div>
</div>
Fiddle here:
http://jsfiddle.net/b6n2C/
#navstamp {
background: white;
width: 218px;
height: 218px;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
margin: 0 auto;
border: 1px solid;
border-color: rgba(19, 36, 17, 1);
display:table;
}
#headlinetext{
text-align:center;
vertical-align:middle;
display: table-cell;
}
with the help of display:table; and display:table-cell to child.
it will be aligned using vertical-align:middle;
demo: http://jsfiddle.net/8q4zP/1/

Position div pictures in 2 columns layout

i have a web page with an image panel and a few div elements, with the same width, each containing an image.
I want to put them into 2 cascading columns with no spaces between them (except for the padding)
I have the following css code for image container:
.imageContainer
{
position:relative;
margin:4px 4px 4px 4px;
border:4px solid #333;
float: left;
display:inline-block;
min-height: 40px;
width: 48%;
}
This causes the columns to like like image 1 but i need the columns to look like number 2
Thank you!
EDIT:
.pg-main
{
float: left;
width: 100%;
padding: 0;
}
.entries
{
float: left;
width: 800px;
padding: 8px 20px 0 0;
}
.entries p
{
display: block;
}
.imageContainer
{
position:relative;
margin:4px 4px 4px 4px;
border:4px solid #333;
float: left;
display:inline-block;
min-height: 40px;
width: 48%;
}
Here is a possible solutions using columns -- does not work on IE9 or earlier.
FIDDLE: http://jsfiddle.net/YjHzd/
HTML
<div id="container">
<div class="block" style="height:30px"></div>
<div class="block" style="height:30px"></div>
<div class="block" style="height:70px"></div>
<div class="block" style="height:70px"></div>
<div class="block" style="height:50px"></div>
</div>
CSS
#container {
-moz-column-count:2;
-webkit-column-count:2;
column-count:2;
height: 145px;
width: 80px;
}
.block {
width: 40px;
background-color: red;
margin: 0 0 5px 0;
}
Unlike many of the other solutions on this page, mine works in all semi-modern browsers, including IE 6, 7, 8, 9; Firefox 3.6 through 29, and all versions of Chrome. See below for screenshots.
If they are of fixed widths, as illustrated, just 1) wrap your boxes inside another div and 2) do the following:
Here's the JSFiddle: http://jsfiddle.net/SqQqZ/
HTML:
<div>
<div id="leftBoxesBox">
<div class="redbox"></div>
<div class="redbox"></div>
<div class="redbox" style="height: 15em"></div>
</div>
<div id="rightBoxesBox">
<div class="redbox" style="height: 12em"></div>
<div class="redbox"></div>
</div>
</div>
CSS:
div.redbox {
background: red;
width: 10em;
height: 5em;
margin: 10px;
}
div#leftBoxesBox {
position: absolute;
left: 0;
width: 12em;
}
div#rightBoxesBox {
position: absolute;
left: 13em;
width: 12em;
}
Internet Explorer 6:
Firefox 3.6:
Please post your HTML, it would help.
Maybe this will help :
clear:left;
after your 2nd imagediv. so the 3rd div can float without problems.
Basically there are only two ways to do this. You can use Javascript to determine the position of the element. Or you can create two seperate columns that have the images contained within them. Left and Right Column

converting frameset into css

I'm trying to build a CSS page layout based on this old frameset:
<frameset cols="30%,70%">
<frame>left</frame>
<frameset rows="80%,20%">
<frame>top</frame>
<frame>bottom</frame>
</frameset>
</frameset>
What was easy 15 years ago seems to get a bit more complicated nowadays. I wrote the following HTML:
<div id="container">
<div id="left">left</div>
<div id="right">
<div id="top">top</div>
<div id="bottom">bottom</div>
</div>
</div>
Together with this CSS:
#container {
border: 1px solid black;
height: 300px;
}
#left {
border: 1px solid red;
float: left;
width: 30%;
height: 100%;
overflow-y: scroll;
}
#right {
border: 1px solid blue;
margin-left: 30%;
height: 100%;
}
#top {
border: 1px solid red;
height: 80%;
overflow-y: scroll;
}
#bottom {
border: 1px solid red;
height: 20%;
}
I put a demo here.
What I want to achieve and failed so far is the following: Height of #bottom shall be a certain pixel height, not percents. When I do this, I mess up with #top. I tried to use absolute positions to stick #bottom at the bottom but then I don't know how to let #top use the rest of the height.
Any ideas would be appreciated. Thank you.
I think this (fiddle) what you are looking for.
#top {
border: 1px solid red;
height: 80%;
overflow-y: scroll;
}
#bottom {
bottom:0;
border: 1px solid red;
height: 20%;
}
The divs will look like they don't precisely fit but it's only because of the borders. If you want the borders then you can use the css box-sizing property and set it to border-box. See this page for reference. Basically it makes the size of an element include the border which it doesn't by default.

Weird three divs side by side

I'm a tables guy, but I'll need to drag and drop some divs, so I tried doing it tabeless (the right way).
This is what I want to do:
The space between all elements should be 24px. My main problem is having the divs (1,2,3) occupying 100% of available space. The width: 100% its sending them beyond the main container.
This is my code so far:
html
<div id="mainContainer">
<div id="topContainer">Just the top one
</div>
<div id="table">
<div id="Line1Container">
<div id="container1" class="container">1
</div>
<div id="container2" class="container">2
</div>
<div id="container3" class="container">3
</div>
</div>
<div id="Line2Container">
<div id="container4" class="container">4
</div>
<div id="container5" class="container">5
</div>
<div id="container6" class="container">6
</div>
</div>
</div>
</div>
And my css
#mainContainer {
border: 1px solid lightgray;
position:fixed;
top: 80px;
bottom:20px;
left:80px;
right:80px;
overflow-y: scroll;
overflow-x: hidden;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
#topContainer {
border: 1px solid lightgray;
border-radius: 10px;
margin-left: 24px;
margin-right: 24px;
margin-top: 24px;
}
#table {
display: table;
margin: 24px;
width: 95%;
}
#Line1Container, #Line2Container {
display: table-row;
}
.container {
border: 1px solid lightgray;
display: table-cell;
width: 33%;
border-radius: 10px;
}
As you see I tried the table-cell approach, but before I have tried the float: left approach.
Thanks
Fiddle
You can't properly use px values with % values together with dynamic sizes.
You should use x% instead of 24px.
And you can use float: left on the "cells"
How about using a table for separating the divs? that way with the td padding there will always be 24px between them
check out this fiddle:
http://jsfiddle.net/5zfEq/
added:
#Line1Container {
padding:12px;
}
#inner-table {
width: 100%;
}
#inner-table td {
padding: 12px;
}
based on #Edifice fiddle .... thanks ;)

Resources