Bing Autosuggest Dropdown Responsive CSS - html

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!

Related

Responsive Design: Is there a way to automatically enlarge and reduce my website to the height instead of the width of the window?

I have a website that has content. As an example it is 900px high and 1014px wide. On mobile phones etc. the size of the ad automatically adapts to the width of the screen and if there is an excess of height, the display simply scrolls. But I want it to adapt to the height instead of the width of the page/screen.
I want to do it this way because on most mobile devices my content is too long in height, which is a problem, and if it is a bit shorter in width, then it's not a problem because I would simply have space left and right.
Thanks in advance
Cyknos
Edith:
So here some Example Code (i'm not allowed to show the original Code):
HTML (Body):
<div class="content">
//here are some images, other divs and much more
</div>
CSS:
.content {
position: absolute;
left: 50%;
margin-left: -512px;
width: 1024px;
height: 910px;
}
Two examples to descripe how it is now:
If the Window/Screen has a size of 1200px width and only 800px in height, the overflow-down is only with scroll visible -> thats normal.
If the Windoe/Screen has a size of 900px width and 1000px height it would "zoom out" my page, so the hole content is Visible -> thats also normal.
My Goal:
My goal is, that if the height of the Window/Screen will become smaller as the height of the Content -> then the same thing should happen as normaly it does on example 2
Here is my solution (Example-Code):
var result = [];
var isFirefox = typeof InstallTrigger !== 'undefined';
var contentHeight = 800; //your content
function bodySizing() {
if(window.innerHeight < contentHeight) {
var diff = contentHeight-window.innerHeight;
var substractor = diff/contentHeight;
var scale = 1-substractor;
if(scale<1) { //only downsizing, no upsizing
$('body').css('zoom', scale);
$('body').css('-moz-transform', 'scale('+scale+')');
$('body').css('-o-transform', 'scale('+scale+')');
if(isFirefox) {
$('body').css("background-size", result[0]*scale+"px " + result[1]*scale + "px");
} else {
$('body').css("background-size", result[0]+"px " + result[1] + "px");
}
}
}
}
EDIT: Added FF-Fix

Allowing images to shrink, but not stretch

I have a site with 4,000+ pages and 10 or more jpeg images per page, of varying sizes. I'm trying to make the site more mobile friendly. To that end, i want to make it possible for the images to shrink to fit on smaller screens. I know that i can do something like this to signal that the images can shrink:
img.bodyImg
{
width: 100%;
max-width: 357px;
height: auto;
}
But what if not all images have a width of 357 (or whatever), and i don't want smaller images stretched beyond their true dimensions? And just to make things more fun, what if the images tags don't have height and width attributes specified?
My goal is to find a solution that doesn't require me to adjust tens of thousands of image calls manually, but i can do a search and replace. Images are currently wrapped in a div container and have a class, like so:
<div class="imgDiv"><img class="bodyImg" src="http://www.example.com/image.jpg"></div>
I'm also open to the possibility that i'm going about this in the wrong way entirely.
Using max-width is simple, effective, and requires no JavaScript.
The CSS below creates responsive images that shrink to fit the container's width but won't expand beyond their native sizes.
img.bodyImg {
max-width:100%
}
In the demonstration below, both images are 300px X 75px. The first container is 200px wide and the second one is 400px wide. Notice that the first image shrinks to fit in the container, but the second image does not expand beyond its native size. Also note that the proportions of each image remain accurate.
div {
background-color: #CCC;
margin:0 0 .5em;
padding:.25em;
}
div.one {
width: 200px;
}
div.two {
width: 400px;
}
img {
display:block;
max-width: 100%;
}
<div class="one">
<img src="http://lorempixel.com/300/75/abstract/4/" />
</div>
<div class="two">
<img src="http://lorempixel.com/300/75/abstract/4/" />
</div>
Additional Notes
I've included display:block to remove the descender space below the image.
If your images have specific height and width attributes (as they arguably should), you can add height:auto and/or width:auto to override those attributes.
Bootstrap uses this method for responsive images.
You can use a little jQuery to figure out each image's native width, and set perscriptive max-widths for each image afterward:
$('.bodyImg').each(function() {
// Create new offscreen image to test
var theImage = new Image();
theImage.src = $(this).attr("src");
// Get accurate measurements from that.
var imageWidth = theImage.width;
$(this).css({
"max-width" : imageWidth
});
}
UPDATE: And if you want each image to have a uniform width, you can store the smallest max width and apply it to all of the images:
var smallMax;
$('.bodyImg').each(function() {
// Create new offscreen image to test
var theImage = new Image();
theImage.src = $(this).attr("src");
// Get accurate measurements from that.
var imageWidth = theImage.width;
// if the variable exists and is bigger than
// the current width, use the new max width
if (smallMax !== undefined && smallMax > imageWidth) {
smallMax = imageWidth;
}
// set the variable if it hasn't been set yet
else if (smallMax == undefined) {
smallMax = imageWidth;
}
// keep the old variable if it is defined and smaller
else {}
$(this).css({
"max-width" : smallMax
});
}
Why not just:
max-width:100%; /*Ensure the width scales to the width of the parent container*/
width:auto; /* Not required, but sometimes is nice to ensure the width not being overridden by other factors like browser quirks */
height: auto; /*Ensure the image keeps its ratio*/
Try using max-width:100% and height: auto in your css. If you want to make your site mobile friendly I would suggest looking into bootstrap framework for more flexibility.

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.

How to set absolute position on elements in my case?

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

layerslider WP 100% width and height

Can someone please guide me how to get an image full screen so that it stays in the centre both horizontal and vertical like the link provided.
http://css-tricks.com/examples/FullPageBackgroundImage/progressive.php
I'm only after the background bit and i need the image in a div apposed to in the css like in the example as i am using it for a slider in wordpress.
In order to make your LayerSlider display at 100% height and width, leave the "Slider height" attribute in "Slider Settings" blank, and then use a script like the following to set the height:
<script type="text/javascript">
function resize()
{
var heights = window.innerHeight;
document.getElementById("layerslider_1").style.height = heights + "px";
}
resize();
window.onresize = function() {
resize();
};
</script>
Insert the script into your footer.php file before the closing body tag. If your slider ID isn't number 1, then change "layerslider_1" to the correct ID.
you can add style to the div element
<div style="background: url(images/xyz.jpg) no-repeat center center fixed;">...</div>
I just took the css from your sample page and copy/pasted!