How can I horizontally align the following header image? - html

I have tried the whole afternoon but I'm missing something, can some one help me with this?
The page can be found at the following link
The page with the image that I need fixed.
The code containing the image is:
<headerimage><span>
<img width="1920" height="600" src="http://www.websu.it/wp-content/uploads/2012/11/suitsheader.jpg" class="attachment-post-thumbnail wp-post-image" alt="Suits Header" title="Suits Header"></span>
</headerimage>
I would like to horizontally center the image with a width of 1920px on smaller screens. When I give a class the property background-position: top center, it works perfectly, but when I need to have a -tag in the page itself I can't seem to make it happen.
Please help me see it :) It's probably very stupid, haha.
Thanks so much!

Set margin: 0 auto; on the image, like:
<headerimage>
<span>
<img style="margin:0 auto;" width="1920" height="600" src="http://www.websu.it/wp-content/uploads/2012/11/suitsheader.jpg" class="attachment-post-thumbnail wp-post-image" alt="Suits Header" title="Suits Header">
</span>
</headerimage>

I would not put this in as an image but as a background on your header.
background-image:url('http://www.websu.it/wp-content/uploads/2012/11/suitsheader.jpg');
background-position-x:center;
background-repeat:no-repeat;
see http://jsfiddle.net/dwat001/Q58YZ/ for a rough demonstration.

Another approach if you cannot use background-image is to use javascript to position the image.
<style>
#imageHolder {
overflow: hidden; // if image is bigger then holder, clip the image, no scollbars.
}
#wideHeaderImage {
position: relative; // treat "left" as relative to the images normal position.
}
</style>
<headerimage id="imageHolder">
<img id="wideHeaderImage" width="1920".../>
</headerimage>
<script src="jquery"></script>
<script>
// create function to center image;
var centerImage = function($) {
var windowWidth = $(window).width(); // get the current width of the window
var imageSize = $('#wideHeaderImage').width(); // get width of image
$('#imageHolder').width(windowWidth); // set the containing element to be size of window.
if(imageSize > windowWidth) { // if image is wider then window
var offset = (imageSize - windowWidth) / 2; // Establish an offset
$('#wideHeaderImage').css('left', '-' + offset + 'px'); // apply offset
}
};
jquery(function($) {
// this code runs within on load
// register a resize event handler
$(window).resize(function(event){centerImage($);});
// resize once on onload.
centerImage($);
});
</script>
I have not run this so I've problably made some mistakes hopefully enough for you to get the gist of what I'm doing.

Related

Is it possible to maximize the dimension of a specific image according to the size of a browser window using greasemonkey/tampermonkey?

For instance if I have a gallery of images that I can browse through, sometimes having multiple galleries open, I have to be careful in resizing one window because it will resize differently for another one of the same page.
The best example I can think of is when you open an image by itself in a new tab and it's auto resized proportionally in the middle of the page no matter big or small the window is. No scrolling required
If it helps here's an example of the code code where the image is shown
<div id="i3">
<a onclick="return load_image(2, 'f46ef2b433')" href="https://testsite.com/b/f46ef2b433/1341428-2">
<img id="img" src="http://testsite.com/fold01/5dde3b620790893d3ffab2da2437077dd41b31cf-230842-1280-1820-jpg/keystamp=1550591100-88d6d61f5f;fileindex=66272627;xres=2400/_000.jpg"
style="height: 1820px; width: 1280px; max-width: 1280px; max-height: 1820px;" onerror="this.onerror=null; nl('27617-430719')"></a></div>
the xpath is: //*[#id="img"]
I've seen plugins do this with videos but I'm looking to just do it with an image. Looking at other "similar" examples is confusing me more than helping at this point
(function() {
'use strict';
var x;
x = document.getElementById("img");
x.style.maxHeight = "100vh";
x.style.maxWidth = "100vw";
x.style.width = "";
x.style.height = "";
x.style.position = "fixed";
x.style.top = "0";
x.style.left = "15%";
})();
Here is my current updated script. i've been unable to change the max-height and max-with values but everything else has worked out for the most part. Without that, I'm not able to finish the task unless there's another method
x.setAttribute("style", "max-Height: 100vh");
this works but wiped away all of the other attributes...
both seem to work only in the console and not in the script as far as modifying the max height and max width values. there's no problem with changing other values
From what you described, you can use vh and vw units. Those units are relative to the size of the viewport.
Try the following exemple in a empty page. The display: body on the image avoid to have a vertical scrollbar
html,
body {
margin: 0;
padding: 0;
}
img {
display: block;
max-height: 100vh;
max-width: 100vw;
}
<a href="https://stackoverflow.com">
<img src="http://www.dummyimage.com/1000x4000/000/fff&text=1000x4000">
</a>

overflow scroll not working. I cannot scroll through my image

So, I have been working on this webpage, and I encountered weird issue, Image should be full size, and I would like to have possibility to scroll horizontally, but it is not working, even though I added overflow-x: scroll in my css.
here is the link to the website.
http://des-iis.ucn.dk/mmdi0915/1055435/WebApp/map.html
Any solutions, Thanks in advance!
Hi, I have tested your code and here is the solution just remove
margin:auto from body i.e. from your css so that body's css will look
like this
body {
font-family: Futura;
font-size: 2em;
color: white;
background: url(../img/BG_nowplaying.jpg);
background-size: cover;
}
You have a issue that your image gets 100% of the width or height of the body, because of the background-size: cover attribute.
The background-size: cover attribute won't overflow the div, since it's a background and not a content.
So, instead of making it 100% via CSS, you should try javascript to make the scroll size correctly.
Maybe you should try a solution like this with javascript:
function fixSize(){
//your image div
var imageDiv = document.getElementById("mapImage");
//your actual image size, I got the values from the image on your example
var imgWidth = 1042;
var imgHeight = 667;
//the size of the body, it gets the current body width and height
var bgWidth = document.body.offsetWidth;
var bgHeight = document.body.offsetHeight;
//the percentage of the body size vs the image size
//this tells which size should be bigger than the body
var dimensionX = bgWidth / imgWidth;
var dimensionY = bgHeight / imgHeight;
if(dimensionX > dimensionY) //if the width is bigger than the height, fix height
{
imageDiv.style.height = (imgHeight * dimensionX);
}else{ //if the height is bigger than the width, fix width
imageDiv.style.width = (imgWidth * dimensionY);
}
}
This should work fine,
you just need to call it when the body loads (onload) and when the body rezise (onresize).
Maybe you could use a <img> tag instead of a background, it can be easier to manipulate the overflow.
I hope I helped ya.

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.

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!

Scroll background image untill the end not further

I am working on a site and I don't want to repeat the background in the y direction.
I know how to do that.
But after the image I don't want background to becomes white or any other color.
I would like it to fix when it reaches that place or to let the background scroll slower then the rest of the site so I wont get to a white part.
Thanks a lot
I found this thread while I was looking for a solution to just this problem. I managed to write a short jQuery script adapting the hints given by Alex Morales.
With the following code, the background-image of the body scrolles down with the rest of the site until its bottom is reached. You can take a look at my homepage (http://blog.neonfoto.de) to see what it does.
$( window ).scroll( function(){
var ypos = $( window ).scrollTop(); //pixels the site is scrolled down
var visible = $( window ).height(); //visible pixels
const img_height = 1080; //replace with height of your image
var max_scroll = img_height - visible; //number of pixels of the image not visible at bottom
//change position of background-image as long as there is something not visible at the bottom
if ( max_scroll > ypos) {
$('body').css('background-position', "center -" + ypos + "px");
} else {
$('body').css('background-position', "center -" + max_scroll + "px");
}
});
This is actually the very first thing I did with JavaScript and JQuery, so any improvement would be great!
It's css3 so it's not super well supported, but I would look at the background-size property.
This is just off the top of my head but I think you will probably have to create a separate div containing the background image. If you place it in your markup before the main content and position the main content absolutely, it will sit behind the main content and at the top of the page. So:
CSS:
#background_div
{
background: url(images/some_image.png);
height: 600px;
width: 900px;
}
#main
{
height: 1200px;
width: 800px;
background: rgba(0, 0, 0, 0.25);
position: absolute;
top: 0;
}
HTML:
<div id="background_div"> </div>
Then what you do is you use javascript (I recommend jQuery) to detect the div's position on the screen.
jQuery:
This code grabbed from http://www.wduffy.co.uk/blog/keep-element-in-view-while-scrolling-using-jquery/
var $scrollingDiv = $("#background_div");
$(window).scroll(function(){
$scrollingDiv
.stop()
.animate({"marginTop": ($(window).scrollTop()) + "px"}, "slow" );
});