Finding div width - css

I have three divs. The two smaller ones are inside the same container, the third div.
The first child div is 350x350 px big.
The third div is 89% of another container div, meaning I dont know the size of it.
I want to make the second child div span between the edges of the first child div and the container div.
Basically:
<div id="container">
<div id="first_child" style="width:350px; height:350px;"> </div>
<div id="second_child" style="width:???px; height:350px;"> </div>
</div>
How do I figure out the width of my second_child element if I want the second_child element to span precisely between the first_child element and the edge of container?
Edit:
Uploaded a quickly drawn image. The big black square is container, measurements are unknown. The red box is first_child, the blue box is second_child. I want to find the width for second_child so it will stretch from the end of first_child to the right edge of container.

You can do using CSS calc():
#second_child {
width: -moz-calc(100% - 350px);
width: -webkit-calc(100% - 350px);
width: calc(100% - 350px);
}
For IE8 and lower you'll have to use jQuery or javascript

I think what you need is to use some css like so:
#container {
width:89%;
height:350px;
}
#first_child {
float:left;
width:350px;
height:350px;
}
#second_child {
height:350px;
margin-left:350px;
}
Here is a working example (with added styles to see the effect)

A good answer with pure CSS was given above, so I'll just answer the question of "how do I find the width needed?" in javascript.
// This is assuming there is no padding.
totalWidth = document.getElementById('container').offsetWidth;
firstChildWidth = document.getElementById('first_child').offsetWidth;
document.getElementById('second_child').style.width = (totalWidth - firstChildWidth) + 'px';

Using jQuery:
var containerWidth = $('#container').width();
$('#second_child').width(containerWidth - 350);

You can do that with pure css positioning, since you know how big the first child is:
#container {
position:relative
}
#first_child {
width:350px;
height:350px
}
#second_child {
height:350px;
position:absolute;
top: 0;
left:350px;
right:0;
}
Note that the container div has position:relative. This is necessary to make position:absolute use the top left corner of the container div instead of the body.

Related

Height not working while setting it with calc() in stylus

I am using stylus in my React project. I am using calc function to set height of some divs. But the calc is not not getting applied. On the client side when I inspect the element, I see the height attribute as calc but the element doesn't obey it. This is how I am setting height
.ht-full-70 {
height: calc(100% - 70px) !important;
}
When I inspect it, I can see the style there
But element is not taking this. It remains unaffected by this.
I think you need a wrap div around the element that you are trying to set a width for. This will make sure that the element has a set starting point for percentages if you give this wrap a height too. I demonstrate it in this snippet.
.ht-wrap {
padding-top:20px;
display;inline-block;
height:280px;
background-color:blue;
}
.ht-full-70 {
padding:6px;
display;inline-block;
height: calc(100% - 70px) !important;
background-color:red;
}
<div class="ht-wrap">
<div class="ht-full-70">
<p>Text.</p>
</div>
</div>
Because .ht-full-70's parent tag doesn't set height
You should set height of .ht-full-70's parent tag.

Making a div float to the bottom of a static nav element

I have the following Bootstrap based code:
<div id="sidebar-wrapper" class="collapse sidebar-collapse">
<nav id="sidebar">
<ul id="main-nav" class="open-active">
....
</ul>
</nav>
</div>
Here is the CSS for #sidebar:
#sidebar {
width: 100%;
float: none;
position: static;
}
This creates a nice sidebar that stretches from top to bottom. However, I can't seem to figure out how to add a div that rests at the very bottom of the sidebar.
A screenshot of what I mean:
Here is the code for that Test Div in the screenshot:
<div style="position: absolute; bottom: 0;">
Test Div
</div>
If the sidebar fills up 100% of the page's height, you can create a wrapper that is absolute positioned in the sidebar with the bottom property set to 0px.
This should look something like this:
.wrapper {
position: absolute;
bottom: 0;
}
And it's straight from this example. As another commenter already pointed out, you might have to change the static positioning of the parent #sidebar element.
To my knowledge this would be the only non-Javascript approach, but it will only work if the sidebar is at 100%.
Notable alternative option using JavaScript:
Let's say you know the heights of all the elements you are working with. You could then simply adjust the margin-top property of div element to be placed at the bottom.
This would look something like this:
window.onload = function() {
var header = document.getElementById("heading");
var bottom = document.getElementById("bottom");
var height = document.body.scrollHeight;
bottom.style.marginTop = (height - header.clientHeight) + 'px';
}
I hope this helps!
Edit:
Now that I think about this more, this is a perfect use case for the calc() CSS3 function. You might want to check it out.
Using this has a few caveats, like if you don't know the height of the content in the bottom-positioned div 100% of the time or if you care about cross-compatibitly problems (although I would say it has pretty good support).
This code would look something like this:
.bottom {
/* should be moved down a distance equal to the height of
parent container minus the height of the bottom div
and the div above it */
margin-top: calc(100% - (20px + 40px));
}
Check out these neat use cases to see if this is an option you would want to consider exploring further.
Try adding your div within the sidebar, and for your CSS put:
.div {
position: absolute;
bottom: 0;
}
You can't change #sidebar to position:relative? That should make the absolute positioning work.

How to calculate div width after margin?

I'm working on a responsive website and I'm coming across a problem. I've a div container which width is 100%, and inside it I've 2 div's sidebar and content. Sidebar is set 40% wide and content is set to 60%.
Now, I want to give 25px space between them and for that I used margin-left:25px;.
Now, what will the width of content in % or is there any formula to calcute?
Here is what I am to do - JSFIDDLE
You could change your CSS to use calc for the width values, you want to subtract 1/2 the amount of the gap in px you want, then add the same amount to the relevant margins:
Demo Fiddle
.container {
background:#ccc;
}
.sidebar {
width:calc(40% - 12.5px);
margin-right:12.5px;
background:red;
height:50px;
float:left;
}
.content {
width:calc(60% - 12.5px);
margin-left:12.5px;
background:green;
height:50px;
float:left;
}
Yes, there is a way to achieve this using calc function.
.content {
margin-left:calc(40% + 25px);
}
But, the disadvantage is that, calc is not cross browser. It won't work in IE.
See updated fiddle here.

Center Image inside div vertically using display table property

I'm trying to center an image vertically inside the div. I've read few other similar questions here on stackoverflow and decided to use this solution:
<div id="wrapper">
<div id="cell">
<img />
</div>
</div>
#wrapper {display:table;}
#cell {display:table-cell; vertical-align:middle;}
This works great for images smaller than the viewport size. Problem occurs when image is larger in height than the view-port. In that case wrapper div simply becomes the height of the image. And it overflows the page. How do I avoid that.
This wrapper div is part of view-port div. Viewport div is of fixed height and 100% width positioned absolute
#view-port{ height: 600px; width:100% }
EDIT: I think I caused some confusion regarding the question. I've created JSfiddle to explain what I mean
Here is a link: http://jsfiddle.net/sublime/fgTtj/
I want to vertically center the image inside #outer I dont have image dimensions, as you can see on fiddle, it works perfectly, but when #outer divs height goes less than image height, say 200 it cuts the image. I want to instead shrink that image to fit the outer div
This answer has been truncated and edited to meet the needs of the OP.
Using jQuery, here is what I would suggest.
You can get rid of several of your divs and just use a wrapper and your image. The problem with your code above is that you gave your outer div a set height at 300px. This means that it won't ever shrink smaller than that. I've written a small script to account for the window size as well
http://jsfiddle.net/fgTtj/40/
I've set up your HTML like so:
<div class="wrapper">
<img src='http://s13.postimg.org/b7hmfvhyv/css.jpg'></img>
</div>
CSS like so:
.wrapper {
position:relative;
width:100%;
height:300px;
background-color:blue;
overflow:hidden;
}
.wrapper img{
position:absolute;
max-width:100%;
max-height:100%;
}
and the new jQuery looks like this:
function center(){
var imgW = $('img').width();
var imgH = $('img').height();
var half_imgW = imgW / 2;
var half_imgH = imgH / 2;
$('img').css({
left: "50%",
top: "50%",
margin: "-" + half_imgH + "px 0 0 -" + half_imgW + "px"
});
}
$(document).ready(function(){
var wrapper_Height = $('.wrapper').height();
center();
$(window).resize(function(){
console.log(wrapper_Height);
var winH = $(this).height();
var wrapH = $('.wrapper').height();
if(winH <= wrapH){
$('.wrapper').height(winH);
} else {
if(wrapH <= wrapper_Height){
$('.wrapper').height(winH);
}
}
center();
});
});
You can resize this window in any direction and the image will stay centered and not cut off. This only works for one window at the moment, so you would have to adjust the script to accomodate more.
The awesome thing with this is that it will run in just about every browser, where display:table-cell does not work in older browsers such as IE6 and I think IE7.
What is the point of the display:table? You could leave it blank (or use display:inline-block), and add an overflow property.
#wrapper{
overflow:hidden;
}
If you want to be able to scroll to see the rest of the image:
#wrapper{
overflow:scroll;
}
If you don't want the image to extend the viewport height, set the max-height:
#cell img { max-height: 600px; }

CSS - Making a div consume all available space

All,
I have a page which is suppose to take up only the available screen space in the browser.
I have a 'top bar' and a 'bottom bar', both of which are fixed positioned at the top and bottom of the page. I want to have a div which will consume (take up) the remaining of the space inbetween the two bars mentioned above.
Its crucial that the middle div is not overlapped by the top and bottom bars. Is this at all possible with CSS or do I need to make use of js.
Also, if I do go with js, considering the browser loads up the CSS first before the js code, how is the above work out using js for centre positioning?
Many thanks,
You can use relative and absolute positions. Here an example:
css
html,body,#wrapper {
height:100%;
margin:0;
padding:0;
}
#wrapper {
position:relative;
}
#top, #middle, #bottom {
position:absolute;
}
#top {
height:50px;
width:100%;
background:grey;
}
#middle {
top:50px;
bottom:50px;
width:100%;
background:black;
}
#bottom {
bottom:0;
height:50px;
width:100%;
background:grey;
}
html
<div id="wrapper">
<div id="top"></div>
<div id="middle"></div>
<div id="bottom"></div>
</div>
Demo: http://jsfiddle.net/jz4rb/4
This demo works for me in Chrome12 but YMMV depending on which browsers you need to support. For example position:fixed does not work correctly in IE6.
Use absolute positioning on the body tag. position:absolute with zero top and bottom will "stretch" body to be the same size as the browser window. Alternatively, setting height: 100% also works but I remember it works wierd for certain old browsers.
Then use absolute positioning on the center div, with enough top/bottom offsets to avoid your header and footer bars. The header bar is absolutely positioned with top and the fotter is absolutely positioned with bottom.
Note: This won't work on mobile browsers. You'll need to use JS to get the window's height and manually set the center div's height.

Resources