CSS : Make to sibling divs have the same height, relative to page - html

I would like to constraint one div to take the same height as a sibling div that would have variable content. Additionally, i want both divs to have a min-height of 100% of the window's size.
Here is the JSFiddle : http://jsfiddle.net/m5q38/1/
The problem is that, i would like to use min-height on the divs, but this requires the parent container to have a specified height, which i can't fix. And if i use height: 100% on my divs, they will take the height of the window, including when the content of the #container div is too large for this div and would overflow.
Alternatively, if i don't use min-height and the #container div has less content, the div won't take the whole window's height :
(Here is the fiddle for this other try : http://jsfiddle.net/m5q38/3/)
Does anyone know how to solve this ?
To make simple i want :
The Green Div to fit its content
The Green Div to have a min height of 100% the window
The Blue Div to have the same height of the green div.
Thanks for helping !

if you can use jquery, see this FIDDLE and note the commented out CSS
using jquery:
$( function() {
$('#left-bg').height($('#container').height())
});
Setting a consistent div height is always an interesting problem. In the past, when I've had multiple sibling div elements I generally just wrap them in on div and set each div css to display:table-cell and that works nicely to keep heights equal regardless of div content.
Not sure you can use that here as you have positioned elements

This can only be done with Javascript, it's a pretty common problem, and there are numerous (javascript) solutions, depending on whether you've got a generalized, site-wide dilemma or just a pair of elements that need a tweak.
If you are using jquery, this will give you a basic solution that you can complicate to your needs:
/* note: add both ready and resize here, via "on", that way they'll
be bound to any selectors (not just id) in place of "#element1"
(such as ".left-column"), including those that arrive via an ajax call
(ie. aren't present when the call is made) */
$(window).on(["ready","resize"], function() {
var heights = [$("#element1").innerHeight(),$("#element2").innerHeight()]
var tall_then_short = [heights[0] > heights[1] ? heights : [heights[1], heights[0]];
$("#" + tall_then_short[0]).css("min-height", "#" + tall_then_short[1]);
});
But a far nicer way is to use a library with this feature as it'll tend to be more robust; personally I use Foundation 5's Equalizer feature.

Related

How to make a Quasar q-page-container child use full height of its grandparent?

I've got a Quasar layout, and a component that I need to fill 100% of the height of the q-page-container element. The problem is that the container does not fully expand to cover the entire height (unlike the drawer, which, using absolute positioning, does).
All CSS-tricks I've seen to tackle this problem interfere with the properties of the parent containers, which I'm reluctant to do to make sure I don't break any properties necessary for internal Quasar layout. Setting the child div of the container to height: 100% has no impact, setting it to an absolute value such as 100px does correctly set the height, but I need it to adapt to the browser viewport.
I've set up a fiddle to illustrate the problem here.
In this case I'd like #troublemaker to fill entire height of its container - or rather, its grandparent minus the header height, since the parent container simply expands to whatever content is inside.
PS: CSS layout and positioning have always seemed counter intuitive to me, so if anyone has some good advice on resources to learn how to better understand the logics of it I would appreciate it immensely!
If you have a div inside a q-page, I found the proper way to do this is to let the div inherit the min-height CSS property from the q-page component.
I updated the fiddle to show it: https://jsfiddle.net/u39qbrpj/4/
#troublemaker {
min-height: inherit;
background-color: green;
}
I think q-page-container need a q-page.
So just replace your div by a q-page and it's work.
here is your fiddle fixed: https://jsfiddle.net/uab1rnjh/2/
Or if you really want to work with a div.
You can do the trick with css: height: calc(100vh - 50px);
Here is your fiddle with a div: https://jsfiddle.net/yghL6so8/2/
In the documentation, you can see QPageContainer encapsulates a QPage.
at: https://quasar.dev/layout/page#QPageContainer-API
Using a q-page inside a q-page-container is certainly the most common way. Per the doc a q-page must be in a q-page container. However, if you want to put a div in a container and have it fill the container you can use class="fit" and the div will fill the entire container.

Make container div expand to size of inner contents using table-cell display

Seems like this is a pretty standard problem: I need a container div element to expand horizontally according to the width property of the inner fieldset. Everything I've found so far offers two solutions.
First solution is to use display:inline-block. However, I'm using display:table-cell to position the container so this won't work.
The second is to add a min-width to the container div. This does solve the issue, however the width of the inner fieldset will not be static, so while it looks fine when there's a lot of content in the fieldset it looks awkward otherwise (there is a huge gap between this particular div and the one to it's right).
Is there another way to force the container div to automatically resize, based on inner content, without using the two solutions above?
Fiddle: http://jsfiddle.net/mj3h1682/
EDIT
Added the following script to the page. Would like a method using css, however if anyone knows of one.
<script type="text/javascript">
$(document).ready(function() {
var width = $('#Profile').outerWidth() + 20;
$('#partialColumn1').css('min-width', width);
});
</script>

CSS parent element ignore the text within child element to determine width

Without fixing the widths of any of the elements, I would like the parent div element to ignore the text when setting it's width. I want the element's width only to be affected by the width of the image.
<div>
<img src="https://lh4.ggpht.com/9BAW9uE48gxNUmnQ7T6ALpNTsrCHOZBMfF__mbamBC36edSw0uc-kjQxgtZ3O3aQWFY=h900"/>
<p>I want this text to wrap once this paragraph element reaches the width of the image.</p>
</div>
div {
background: green;
display: inline-block;
}
my jsFiddle
Any advice is greatly appreciated
Change display property of div to table-caption
(Tested in firefox and chrome)
Updated jsfiddle
Here's the best that I've found:
http://jsfiddle.net/y8Qnd/3/
What I've done is to take the p tag out of flow with position: absolute so that the containing div has the width of just the image. Then, have the p tag inherit the width of its parent, the container. This does not fix the width of the p tag, and is completely cross browser.
This would mean you would have to move up the DOM tree, as you want the image to determine it's parent width. Moving up the DOM tree is unfortunately not possible (yet).
As an alternative, you could position the text absolute, to lift it out of the document flow, and therefore not influence the width of it's parent div. This however would also mean that the height does not get influenced, which is probably not what you are after. You could mimic the correct height by repeating the parent background, but the content underneath would not get pushed down, so that is also not really an option I think. I set up an example anyway: http://jsfiddle.net/y8Qnd/2/
The only option I can think of is javascript. Get the width of the image and apply it to the parent container. In jQuery (I will probably get bashed for using jQuery for such a trivial thing, but I am just not used to writing 'old school javascript' anymore...) it would look something like this:
var $wrapper = $('div'); // you will probabaly want to use some id or class here
var width = $wrapper.find('img').width();
$wrapper.css('width', width);
and an example: http://jsfiddle.net/y8Qnd/6/

HTML division of space and resizement

I'm currently developping a Chrome extension (which means that I don't need a cross browser solution).
Basically, I want my web page to be separated into two parts. A left panel which would take 20% of the width and 100% of the height and a right panel which would take 80% of the width and 100% of the height. Those panels would contain various elements, textareas, list, select.
The issue is that I don't know how to achieve this and to handle resizement. Currently, I'm using jQuery with a window.onresize hook. I keep changing the width and height based on the window's size.
I want to know if that could be done in a simplier way, using CSS for instance. And what should I use? A div, a table, etc...
Thanks in Advance.
You could put the existing panel, or element divs & spans inside a containing relative div. If
you use position:absolute for the panel divs, they will then be positioned
absolutely, relative to the containing div, not the page body.
http://css-tricks.com/791-absolute-positioning-inside-relative-positioning/
Please adding css into your div, add min-height, and max-height properties
#mydiv{
min-height: 700px;
}

CSS: Tell block element to fill in height

This seems like it should be the easiest thing in the world, but I'm having difficulties. I'm started to think I didn't know as much about CSS as I thought, or CSS was designed more poorly than I thought.
I have a page. At the top, there's an arbitrary amount of markup. Then there's a block element. All I want to do is make this block element extend its height to the bottom of the window.
See http://jsfiddle.net/vHVeC/4/. It's close, but the last block element extends beyond the visible area of the browser, creating scrollbars. No content should extend beyond the dimensions of the viewport (ie there should be no scrollbars).
How can I do this with having to use JavaScript?
Apparently, CSS has massive troubles finding heights. Widths, no worries.
Using Javascript, you'd go:
//Grab div element
var obj = document.getElementById('theDiv');
//Enable sizing
obj.style.position = 'relative';
//Force to window
obj.style.height = document.documentElement.clientHeight+'px';
Incidentally, in your Fiddle, the plaintext node above the div is offsetting the div below. It's finding 100% of the body height, but then being bumped down, causing the scrollbar. The way to fix this in CSS is position:absolute;left:0;top:0 which locks it in place.
Also note that in any of these cases, if you do end up scrolling (e.g. to 150%), you'll see the bottom edge of your div down there at 100%.
You've hit the css box model problem. A quick and dirty solution is to set the overflow: hidden property to prevent the scrollbars but you should be very careful doing this. You will need to make sure your content fits on screen as any content extending beyond the block element will be inaccessible to users.
This is how you can do it using a table (It's pure CSS):
http://vidasp.net/tinydemos/table-layout.html