How to set absolute position on elements in my case? - html

I have a container div that cover the screen with set ratio.
I want to place some elements with absolute position and to be responsive (scale down or up).
I have something like
http://jsfiddle.net/hsD2G/2/
When I shrink width or height of the browser. The element location will change and looks off. Can anyone help me about it?

Again, this is probably not the optimal solution. At least CSS can be used partly, to center the highlights image, or using %width. If you could, somehow, make the background image containing DIV to be squared, you could do it only with CSS.
Here is the Fiddle with the jquery solution.
$( document ).ready(function() {
function update(){
var top=20,left=40;
var height = $(window).height();
var width = $(window).width();
var highlights = $('#cover-img img');
if (height > width){
// Image limited by width
var blank_top = Math.round((height - width)/2);
highlights.css('top',blank_top+top);
highlights.css('left',left);
// make it responsive 1/3 ratio
highlights.width(Math.round(width/3));
}else{
// Image limited by height
var blank_left = Math.round((width - height)/2);
highlights.css('left',blank_left+left);
highlights.css('top',top);
highlights.width(Math.round(height/3));
}
}
$( window ).resize(update);
update();
)};
Hope this helps

Related

Bing Autosuggest Dropdown Responsive CSS

I am testing out the documentation here. The only difference is my searchBox is responsive and has 100% width.
<div id='searchBoxContainer'>
<input type='text' id='searchBox' style="width:100%"/>
</div>
On mobile the searchBoxContainer does not honor the width of the searchBox. The red overhang below expands my mobile app and doesn't feel good.
I have determined I can fix this by setting the as_container max-width equal to the width of the searchBox as it changes.
#as_container {
max-width: width of search box;
}
Is there a way to set the #as_container max-width dynamically as the searchBox width changes? I've fixed this with $(window).resize and a little javascript but I feel like there is CSS to handle this?
Here is what I came up with. It's really simple I would just prefer a CSS solution. In my case the searchBox is responsive to it's container, style="width:100%". On resize I match bings drop down container's max-width to the searchBox. The container's width can be set with #as_container.
// Our responsive fix for the autosuggest is to limit it's max width to the dropdown container. (Assuming our searchBox is responsive, we'll match widths)
function FixAutoSuggest() {
try {
var css = document.getElementById("autoSuggestFix");
// Create a CSS element for our fix if it hasn't been created already.
if (!css) {
css = document.createElement("style");
css.setAttribute("id", "autoSuggestFix");
css.type = "text/css";
document.body.appendChild(css);
}
// Set the appropriate width for our auto suggest container.
var searchBox = document.getElementById('searchBox')
if (searchBox != null) {
var bb = searchBox.getBoundingClientRect(), columnWidth = bb.right - bb.left;
// We set column width minimum to 150.
columnWidth = ((columnWidth > 150) ? columnWidth : 150);
css.innerHTML = "#as_container { max-width: " + columnWidth + "px }";
}
}
catch (err) {
alert('FixAutoSuggest' + err);
}
}
$(window).resize(function () {
FixAutoSuggest();
});
In my case the search box is not shown initially and I call FixAutoSuggest() when showing it. If you show it on load you could always fix then as well.
$(document).ready(function () {
FixAutoSuggest();
});
You'll notice in FixAutoSuggest() I put a minimum width of 150px on the bing drop down. That's just personal preference and you can remove that line if you want it to shrink to nothing. Hope this helps someone!

CSS - calculate percent minus dynamic image height

I have image with width: 100% so it's height is dynamic.
I want to place a div below it that will take all the height left to the bottom of the page. it is impossible to use height: calc(100% - ... ) because the image height is unknown.
What is the solution?
Thanks!
The only solution to do this is by using JavaScript.
You have to get image's height and than set div's height according to image's height.
First you have to get image's height using this:
var imgH = document.querySelector('my_img_selector').height; //pure JS
var imgH = $('my_img_selector').outerHeight(); //jQuery
Than you have to get page height:
var pageH = window.innerHeight; //pure JS - note that this may not work in all browsers
var pageH = $(window).height(); //jQuery
And than you have to set your div's height to fill rest of page
document.querySelector('my_div_selector').style.height = (pageH - imgH) + 'px'; //pure JS
$('my_div_selector').css('height', (pageH - imgH) + 'px'); //jQuery
Tada!
Hope I helped :D

Issues with width of "floating" link section

I have a list of links on the sidebar of my website that starts out as position: relative, and then when you scroll past the header, it changed to position: fixed. It does so with a short jQuery script that checks the height of the window on scroll and changes the class from "relative" to "fixed" if the height is greater than the height of my header, and corresponding classes in my CSS that change the position attribute. The issue I am having is with the width of the element containing the links. When it is relative, I have the width set to 100% so it fills the sidebar. When it is fixed, width is relative to the window so I set it to width: 25%, which was a close approximation. It worked fine on my screen, however when I tried it on a larger screen the element overlapped the main content area. I then tried changing the CSS to something like the following:
.fixed {
position: fixed;
left: 15%;
right: 70%;
}
But again, it didn't work properly on a large screen. Any ideas on what to try next? Thanks so much!
EDIT: I tried adding this to my script but it still isn't working:
$(window).scroll( function() {
if($(window).scrollTop() > 150){
//begin to scroll
var links = $("#project-links");
links.attr('class', 'fixed');
var windowWidth = $(window).width();
var newLeft = .075 * windowWidth;
var newRight = .68 * windowWidth;
var strLeft = newLeft.toString() + 'px';
var strRight = newRight.toString() + 'px';
links.css({'left': strLeft, 'right': strRight, 'width': ''});
} else {
//lock it back into place
var links = $("#project-links");
links.attr('class','relative');
links.css({'left': '', 'right': '', 'width': '100%'});
}
});
EDIT 2:
Finally fixed it, I was being stupid. Here is what did the trick:
if($(window).scrollTop() > 150){
//begin to scroll
var links = $("#project-links");
links.attr('class', 'fixed');
links.css('width', links.parent().width());
}
Indeed your .fixed container is relative to the window when the position property is set to fixed. Percentage does not help you here, you need absolute values. You have already a script embedded, it could use the width of the parent element and apply it to the .fixed element when resizing.

HTML webpage full screen part 2

So I got the webpage to go full screen now but I am using height 100% for all my css attributes and now I am getting a scroll bar on the right side.
http://www6.luc.edu/test/cabplan/maps/index2.html
when i change the #content_container to height 89% it changes it but its looks different on other screen sizes. How do I make it so that the map is always the height up until the footer with the "esri" logo on the bottom right above the footer as well
This would be something better suited for jquery. You can call it to resize the height on load and on window resize.
To get the window height you would use
windowHeight = $(window).height();
You take this and assign it to a variable and subtract the heights of the header and footer. And in set the css by using
${'#content_container').css({width: windowHeight+"px"});
Then if someone resizes the window you run the same options in a function like the following
$(window).resize(function() {
//update stuff
});
EDIT
$(document).ready(function(){
windowHeight = $(window).height();
divHeight = windowHeight - 100 - 100; // heights of your header/footer
$('#content_container').css({height: divHeight+'px'});
});
$(window).resize(function() {
windowHeight = $(window).height();
divHeight = windowHeight - 100 - 100; // heights of your header/footer
$('#content_container').css({height: divHeight+'px'});
});
you can replace the 100's for static calls of height by assigning variable and call $('#divid').height(); Since your header is position:absolute, if you subtract the header then you will need to position the div from the top the same px.
To call js include the following before the javascript above.
<script src="http://code.jquery.com/jquery-1.6.4.min.js" />

how to define embed player height in fluid design to keep the aspect ratio? (when width is 100%)

Is there a way to treat embed players like images in a fluid design?
I usually set the width of images to 100%, and let the browser calculate the height (modern browsers scale pictures with the original aspect ratio).
That's not the case with the <embed> element. I realize that the video information isn't given, but it should be available somehow, without fixed pixel values. In most cases only 16:9 and 4:3 aspect-ratios are used. If just those two values could be added, it would be a great help.
With width="100%" and/or style="width: 100%;" the embedded video fills the container properly (horizontally). The height should be (width/4*3) or (width/16*9) but as far as I know there's no way to calculate it.
I'd prefer a solution without inline styles, and even though the height and width attributes are required/recommended I don't like to add any specified length values in HTML. A CSS solution or a "secret" parameter for the embedded video would be ideal. If there's a youtube-specific solution, that's enough for me.
This solution worked perfectly for me.
http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php
^ CSS only liquid youtube embed dimensions.
With this you can completely remove the height and width attributes of the iframe. It will automatically fit it's parent container's width.
You could do it with jQuery. Something like this:
HTML:
<div>
<iframe></iframe>
</div>
JS:
var width = $('div').width();
var ratio = 16/9;
var height = width/ratio + 32; /* 32px is approx. height of controls */
$('iframe').css('height', height);
You would, however have to do it on window.resize aswell, if you want that functionality.
also, check out this little jQuery plugin by über-guru Chris Coyer: http://fitvidsjs.com
I just had to do this for a project in which I had an embedded youtube video centered on the page on top of a background image which was set to display 100vh x 100vh minus a bit of space for a header and stickynav bar. It was important that the youtube video not only scale but maintain a precise 30 pixel margin from the bottom of the image.
So I set the size of the background image on page load relative to the size of the window and define the dimensions of the iframe within the onYouTubeIframeAPIReady function which is called by the Youtube API only after the frame is loaded. I've also got the same function on window resize so that it will scale fluidly if you resize the browser window.
Since the vertical margin was an important issue I started with the height, setting the height of the iframe based on the height of the background image minus the space needed for margins. I then manually calculated the 16:9 aspect ratio for the width by dividing the height by 9 and multiplying by 16.
$( document ).ready(function() {
var content = $('#hero');
var contentH = $(window).height() - 158;
var contentW = $(window).width();
content.css('height',contentH);
} );
$(window).resize(function() {
var content = $('#hero');
var iframe = $('.videoWrapper iframe');
var contentH = $(window).height() - 158;
var iframeH = contentH - 150;
content.css('height',contentH);
iframe.css('height',iframeH);
var iframeW = iframeH/9 * 16;
iframe.css('width',iframeW);
var margin = ($(window).width() - iframeW) / 2;
$('.videoWrapper').style.marginLeft = margin;
} );
<div id="hero">
<div class="container">
<div class="row-fluid">
<hgroup class="span12 text-center" role="heading">
<h1></h1>
<h2></h2>
</hgroup>
<script src="https://www.youtube.com/iframe_api"></script>
<center>
<div class="videoWrapper">
<div id="player"></div>
</div>
</center>
<script>
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
videoId:'xxxxxxxxx',playerVars: {
controls:0,enablejsapi:1,iv_load_policy:3
}
} );
var content = $('#hero');
var iframe = $('.videoWrapper iframe');
var contentH = $(window).height() - 158;
var contentW = $(window).width();
var iframeH = contentH - 150;
iframe.css('height',iframeH);
var iframeW = iframeH/9 * 16;
iframe.css('width',iframeW);
}
</script>
</div>
</div>
</div>