Get height of a website and apply it to a div - html

I have a website, let's take http://www.example.com. In my html I have
<div style="height:100vh;"><iframe src="http://www.example.com" style="position: relative; width: 100%; height: 100%; border:0;"></iframe></div>
Here the div has the height of 100vh but I want to get the height of example.com and automatically apply it to the div. Can I do that?

Here is a working JSFIDDLE https://jsfiddle.net/jsz9ur1g/1/
HTML
Alot of times the html doesn't expand to the full bodys height, So i added a div
called getHeight right before the body tag and gave it a position of absolute. This will ensure that it expands to 100% of the document height
<div id="getHeight">
</div>
<div id="yourElement">
</div>
CSS
As you can see i gave the HTML a min height and set the getHeight div to position absolute and gave it a height of 100%.
html{
min-height:500px;
}
#getHeight{
position:absolute;
top:0;
left:0;
height:100%;
width:100%;
}
#yourElement{
background:red;
color:#fff;
font-size:20px;
}
JS
var bodyHeight = document.querySelector("#getHeight").offsetHeight;
//or use clientHeight if you don't car about scrollbars and borders
var yourElement = document.getElementById("yourElement");
yourElement.style.height = bodyHeight + "px";
yourElement.innerHTML = "see the height of the actual document is " + bodyHeight + "px";

Related

Make child element full horizontal width of scrolling parent

I'm trying to tweak an existing app so would prefer to not change the dom structure. But I can't seem to get it how I need.
here is the example of where I am now.
https://codepen.io/m1nd/pen/QYLMeJ
<div class="canvas">
<div class="node node-1">node 1</div>
<div class="node node-2">node 2</div>
<div class="node node-3">node 3</div>
<div class="wrapper">
<div class="right-target"></div>
</div>
</div>
html, body {
height:100%;
background: yellow;
padding:0;
margin:0;
}
.canvas {
position:relative;
background: red;
height:100%;
overflow:auto;
}
.node {
position:absolute;
background: grey;
width: 200px;
height: 20px;
}
.node-1 {
top: 2px;
left: 2000px;
}
.wrapper {
position:relative;
height:100%;
background:pink;
}
.right-target {
position:absolute;
right:0px;
top:0px;
width:20px;
height: 100%;
background:green;
}
I want the .wrapper div to expand to the full width of the .canvas (scrolling) div, so the green .right-target should be to the far far right.But no matter what I try I can't seem to get the .wrapper div to expand past the viewport width.
I've seen examples that address this vertically (http://bennadel.github.io/JavaScript-Demos/demos/css-position-overflow/) but I can't seem to get the same principle to work in my case.
You have 2 options...
Make the .wrapper overflow the viewport by the exact width of its child element, which is what you actually want to out of the viewport, but this will be near impossible to do precisely (without some extremely new CSS features) becaaus you will have make that .wrapped element 100% + 20px wide (this is actually on the future specs I believe)
A better solution, if it works for your use case, would be having the green .right-target overlap is container (.wrapper) by its exact width...
.right-target {
...
right: -20px;
...
}
Sorry, misunderstood question. Revised answer below...
To do this you will need to use Javascript since the .canvas element is overflowing the viewport. You'll need to...
Calculate initial viewport width
Imperatively set the .canvas width to the viewport width + the offset of the element creating the overflow, .node-1 and 2000px respectively
On resize of the viewport you will have to re-calculate the new viewport.
reset the .canvas to the new viewport width + the same .node-1 offset
$(document).ready(function () {
var viewportStartWidth = window.outerWidth,
$target = $('.wrapper'),
$offsetElems = $('.canvas .node'),
offsetRight = (function () {
var largestOffset = 0;
$offsetElems.each(function(){
var pos = $(this).offset().left;
if (pos > largestOffset) {
largestOffset = pos;
}
});
return largestOffset;
})();
$target.width(viewportStartWidth + offsetRight)
$(window).on('resize', function(){
var viewportNewWidth= $(this).outerWidth;
$target.width(viewportNewWidth + offsetRight)
});
})
This makes you code very brittle though (if 2000px offset ever changes) and also has a significant performance cost (calculating and resetting width) every browser resize. You can improve this by debouncing/rate-limiting these calculations, but I would suggest a solution that does reorganize the DOM instead, even if you have to do it with JS on initial page load.
Full working example: https://codepen.io/anon/pen/QYWwKq
This is the only way you're able to do it with pure CSS, but take a look at the browser support to make sure it is acceptable for you...
.canvas {
...
width: calc(100% + 2000px);
...
}
Working example: https://codepen.io/anon/pen/GzRgeE

How to get div to take up the whole of the body?

I have a notifications panel which I would like to take up the whole of the body without having to specify hard coded values such as height: 100px;. I need it to take up all of the space until it reaches the footer. ViewHeight does not work as it pushes the footer off the page which is not my intended actions. So far I have this:
https://jsfiddle.net/bfbfvop4/2/
Can anyone help?
I'm not sure what you exactly want but mybe this helps.
There's a trick in css for this:
html,body{
height:100%;
width:100%;
margin:0
}
div{
height:100%;
width:100%;
background-color:black;
}
<html>
<body>
<div>
</div>
</body>
</html>
You just have to set html and body to height and width 100%. margin:0 is there just for the body (usually it has a margin of 2-4 px). Then you can make any 'relative' element full-body size.
EDIT 1: As #str ,after you write this code you can use 'calc(100% - ' if you want some space left.
EDIT 2 : Is this what you want? JsFiddle
The style is mostly in html (with style='' attrb.). I had to use calc so that you can see the footer too.
'Holder' must have the height 100%.
Delete min-height from 'class="container-fluid body-content body"' and replace it with 'height: calc(100% - 77px);' (100%-full screen ;77px is 75px -nav height and 2 px for border). Make the footer realtive and delete 'left:0' and 'bottom:0'. Then set row's properties to
position: relative;
height: calc(100% - 77px);
(100% from its parent height -77px from height+border of the footer)
Worked out the answer thanks to #str and #sergiu reznicencu.
So what I did was calculate the height I wanted by doing this calculation:
height: calc(100vh - height of my navbar - height of my footer);
I then set the height for the body and panel to be this variable.
I used sass but you don't need sass to do this, just follow the above way.
OR height: 85vh worked fine too.
Try this,
.notifications {
padding-right: 0px;
height:300px;
}
.notifications .notifications-panel {
height:100%;
background-color: #F5F5F5; }

Centering a div that is displayed as a table and absolutely positioned

As the title implies, I need to know how to center a div that has the following CSS rules applied to it:
display: table;
position: absolute;
The div should be right in the middle of the viewport, and, as per usual with display: table;, it should be the size of it's contents.
HTML
<div class="center"></div>
If the table has a fixed height and width of 400px then you can use this css:
.center {
display:table;
position:absolute;
height:400px;
width:400px;
background:red;
left:50%;
margin-left:-200px; <---- half of width
top:50%;
margin-top:-200px; <---- half of height
}
DEMO WITH CSS
If the table has a dynamic height and/or dynamic width then you can use this jquery:
$('.center').css({
'left': $(window).width() / 2 - $('.center').width() / 2,
'top': $(window).height() / 2 - $('.center').height() / 2
});
DEMO WITH JS

Div placing 100% height

Currently I'm having troubles getting my layout working cross-browser. In the attached image you are able to see a preview.
height: 100%;
Some information:
div #Header
Width: 100%
height: variable
div #Sidebar (overflow-y)
Width: 300px
height: 100% minus header + footer heights
div #frameHeader
Width: 100% minus sidebar width (300px)
height: 100% minus header + footer heights
iframe #iframe (overflow-y)
Width: 100% minus sidebar width (300px)
height: 100% minus header + footer + frameheader height
div #Sticky Footer (sticky to bottom ofcourse)
Width: 100%
height: variable
I've spend countless hours trying to get this to work, I'm thinking someone should have faced this problem before? I'm hoping someone is able to give me a working cross-browser example!
Current code: http://jsfiddle.net/s6wVw/ (ugly css but I think you get the point ;))
Attachment (preview) can be found below
preview image
In your question you keep making false statements and contradicting yourself (e.g. you're talking about a sticky footer but you also imply that the page doesn't scroll - as heights of all elements sum to 100%). However, I'll try to help you none-the-less.
For the reason stated above, I've made the following assumptions:
You want the dimensions of the main areas (header, footer, sidebar, frame header, frame body) to always sum to 100%
You don't want the browser to scroll
You want scrolling in the side bar and frame body if the content overflows
The above would lead to a poor site design because if the browser/window size were to be <= 300px wide then you wouldn't be able to see any of the frame etc.. Similarly, if the browser/window height <= foot height + head height then you wouldn't see any of the sidebar, frame head, or frame body.
That being said, here is an example using jQuery, html, and css.
CSS
html, body{
margin:0; padding:0; border:0;
color:#fff;
}
#head{
width:100%;
background:#aaa;
}
#body{
width:100%;
}
#sidebar{
display:inline-block;
width:300px; height:100%;
background:#111;
vertical-align:top;
overflow:scroll;
}
#frame{
display:inline-block;
vertical-align:top;
height:100%;
}
#fhead{
width:100%;
background:#333;
}
#fbody{
width:100%;
background:#777;
overflow:scroll;
}
#foot{
position:fixed;
top:100%;
width:100%;
background:#aaa;
}
h1{margin:0; padding:10px;}
jQuery
function setSizes(){
var docWidth = $(window).width();
var docHeight = $(window).height();
var headHeight = $('#head').height();
var footHeight = $('#foot').height();
var bodyHeight = docHeight - headHeight - footHeight;
var fHeadHeight = $('#fhead').height();
$('#body').css({
height: bodyHeight
})
$('#sidebar').css({
height: bodyHeight
})
$('#frame').css({
width: docWidth - 300
})
$('#fbody').css({
height: bodyHeight - fHeadHeight
})
$('#foot').css({
"margin-top": -footHeight
})
}
$(function(){
setSizes();
var doit;
$(window).resize(function(){
setSizes();
setSizes();
})
})
HTML
<div id="head"><h1>Head Section</h1><br><br><br><br></div>
<div id="body">
<div id="sidebar"><h1>Side Bar</h1>
</div><div id="frame">
<div id="fhead"><h1>Frame Head</h1><br><br></div>
<div id="fbody"><h1>Frame Body</h1></div>
</div>
</div>
<div id="foot">
<h1>Foot Section</h1><br>
</div>
NOTES
You can put whatever content you like inside of the following divs: #head, #sidebar, #fhead, #fbody, #foot
The jQuery runs the setSizes(); function twice on window resize. This is to account for any scrollbars that may impact the available width/height
You may need to set additional overflow rules to other elements depending on what content you place in the divs

How to set multiline text in floated div not expand his width

I want use floatet image with some text about this image in my content.
I'm using this HTML + CSS for this:
<p class="container">
<img src="http://www.google.com.ua/images/logos/ps_logo2.png" width="200"/>
<span class="text">Some text wider that image image image blablabla</span>
And CSS for it:
.container { float:right; border:2px solid #000; }
.container img { display:block; margin-bottom:10px; }
But, if text about image is wider, it is expand floated parent. I'm not want this behaviour. I want to limit max-width of parent p element to width of image.
Here example on jsfiddle: http://jsfiddle.net/wBVqt/1/
I can do what I want through position:absolute and padding-bottom, but I don't know value for padding-bottom. jsfiddle.net/wBVqt/3/
I don't see solution with only css if you want to have images of different sizes, so chek my solution with jQuery:
var imageWidth = 0;
$('.container img').each(function(index, el){
if(el.width > imageWidth) {
imageWidth = el.width;
}
});
imageWidth = imageWidth ? imageWidth : '100%';
$('.container').css('width', imageWidth);
It will work yet if you have a lot of images in your container. If you have no images, it will set originally 100% width to container.
Have you tried just putting a width on the container? (Which you should do anyway if you want your code to validate, as all floated elemets should have a width).
.container { float:right; border:2px solid #000; width: 200px; }