I need to make a background for a website cover the entire screen, BUT tile vertically. I cant use HTML5. Here is the website, sorry the code is messy I wrote the original code a few years back when I didnt know how to right organized code.
the problem with using CSS background-image with the repeat-y option is that the image won't fill the width of the screen.
To get the image to fill the width and ALSO tile vertically do the following.
I've tested and it works.
Remove the current <div class="background"> block containing your background images
Add the following code block to the <center> element you have on your page
<center style="">
<div class="background">
<img src="BG/b1.jpg" />
<img src="BG/b1.jpg" />
<img src="BG/b1.jpg" />
</div>
...rest of code
now add/modify the following CSS statements
center
{
position:relative;
}
.background
{
height:100%;
width:100%;
min-height:800px;
min-width:760px;
position:absolute;
overflow:hidden;
left:0px;
top:0px;
z-index:-1;
}
.background img
{
display:block;
height:auto;
width:100%;
min-height: 800px;
min-width:760px;
}
In your CSS use background-repeat:repeat-y to repeat vertically. Use background-repeat:repeat to repeat vertically and horizonally.
http://www.w3schools.com/cssref/pr_background-repeat.asp
CSS
body
{
background-image:url('paper.gif');
background-repeat:repeat-y;
}
Here's a solution that I believe is what you're looking for. You can also use a length (e.g., 50px) instead of auto for the height.
#background {
background-image:url("image.png");
background-size:100% auto;
background-repeat:repeat-y;
}
Working Example
Horizontal Stretch + Vertical Repeat Base64 Background (tested in Firefox 11 and Chrome)
Browser Support
Firefox 4+ (3.6 if you include -moz-background-size)
Safari 4.1+ (3.0 if you include -webkit-background-size)
Chrome
Opera 10+ (9.5 if you include -o-background-size)
Internet Explorer 9+ (non-resized background for IE8 and lower)
Related
I want to have an image on my page that has certain parts that are transparent, but not all of it. Is it possible to make just certain parts of an image/div transparent? For example, just a circle in the bottom right corner, or the top right portion?
In this wonderful future world of 2022, you can now use mask-image to achieve this type of effect:
img {
mask-image: linear-gradient(to left, transparent 5%, black);
}
https://codepen.io/reynoldsalec/pen/OJOwZmV
Note that, although mask-image should be supported by all modern browsers, I've noticed that sometimes it needs to be prefixed (ex: -webkit-mask-image).
DEMO
Check this Demo, you can do by adding a span tag and give absolute position add opacity. and also you can increase the opacity.
Hope this is the one you are looking for. :)
html :
<div class="imgWrap">
<img src="https://www.google.co.in/images/srpr/logo11w.png" />
<span class="tranparentClass"></span>
</div>
CSS:
.imgWrap img{
width:80%;
height:80%;
position:relative;
border:1px solid #900;
}
.tranparentClass {
opacity:.5;
border:1px solid #f00;
border-radius : 50%;
display:block;
padding:55px;
position:absolute;
top:0;
left:0;
background:#fff;
}
Here is another CSS option. It simulates a transparent area within an image by sharing a fixed background with background-size:cover on both the background and the circle. This technique also creates interesting effects when used for other html elements that can have a background like divs, headers, paragraphs...
JSFiddle
Main CSS
.main-background, .circle-div {
background-image:url(http://i.imgur.com/1aAo20a.jpg);
background-repeat:no-repeat;
background-position:center top;
background-attachment:fixed;
background-size:cover;
-webkit-background-size:cover;
-moz-background-size:cover;
-o-background-size:cover;
}
HTML
<div class="main-background">
<div class="demo-holder">
<img class="img-size" src="http://i.imgur.com/OaE8VAj.jpg" />
<div class="circle-div">Transparent<br />Effect</div>
</div>
</div>
Another suggestion would be to use the inline image as a background on "demo-holder".
I'm trying to make a site similar to this: http://www.awerest.com/demo/myway/light/
A single paged site with a responsive image that takes up the full screen, on any device. That's my issue I can't figure out a way to get a background image to go full screen on any device.
<img src="C:\Users\Jack\Desktop\City-Skyline.jpg" class="img-responsive" alt="Responsive image">
I came across this but no luck, if some one can point me into the right direction on how to do this it would be very appertained.
The crucial part here is to set up the height of your content as 100% relative to the viewport (html element). By applying the background image to a div and not just using an img you also have a lot more flexibility in how its displayed, background-position keeps it always centered, background-size:cover keeps it scaled.
Demo Fiddle
HTML
<div></div>
<div>More Content</div>
CSS
html, body {
height:100%;
width:100%;
position:relative;
margin:0;
padding:0;
}
div:first-of-type {
height:100%;
width:100%;
background-image:url(https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSbcnkIVXLz23PALu8JD-cTGe8KbXKC1JV0gBM_x1lx3JyaNqE7);
background-size:cover;
background-position:center center;
}
div:last-of-type {
background:green;
position:relative;
color:white;
height:100%;
}
I have an image I'd like to show in a browser such that:
If the image is smaller than the browser viewport, the image is centered
horizotally and vertically.
If the image is larger than the viewport, the image is scaled down to fill
as much of the viewport as possible without adjusting the aspect ratio of the
image. Again, the image is centered horizotally and vertically.
I do not want to use JavaScript; what's the best/most semantic HTML and CSS to do this?
Update I've been asked for clarification regarding semantics: the image is content; the only content within the HTML.
Solution
#GionaF ideas got me to a happy (and very simple) solution:
HTML
<!DOCTYPE html>
<head>
<title></title>
<LINK href="test2.css" rel="stylesheet" type="text/css">
</head>
<body>
<div>
<img src="photo.jpg" alt="photo" />
</div>
</body>
CSS
img {
max-width:100%;
max-height:100%;
position:absolute;
top:0; left:0; right:0; bottom:0;
margin:auto;
}
You can achieve it in many ways, but i can't be "semantic" without knowing the context (is the image the main/only content of the page? is it in the middle of a blog post?), so i'll go for a div.
1. position:absolute; + margin:auto;
Support: crossbrowser
HTML
<html>
<body>
<div id="container">
<img src="your-image.jpg">
</div>
</body>
</html>
CSS
html,body,#container {
height:100%;
}
#container {
width:100%;
position:relative;
}
#container > img {
width:100%;
max-width:400px; /* real image width */
position:absolute;
top:0; left:0; right:0; bottom:0;
margin:auto;
}
Demo
2. display:table; + display:table-cell; + vertical-align:middle;
Support: IE8+, all other browsers - with IE7 fallback (Source 1) (2) (3)
HTML
<html>
<body>
<div id="container">
<span> /* it's important that you use a span here
not a div, or the IE7 fallback won't work */
<img src="your-image.jpg">
</span>
</div>
</body>
</html>
CSS
html,body,#container {
height:100%;
}
#container {
width:100%;
display:table;
*display:block; /* IE7 */
}
#container > span {
display:table-cell;
*display:inline-block; /* IE7 */
vertical-align:middle;
text-align:center;
}
#container > span > img {
width:100%;
max-width:400px; /* real image width */
}
Demo
3. background-size:contain;
Support: IE9+, all other browsers - with vendor prefixes (Source 1) (2)
HTML
<html>
<body>
<div id="container"></div>
</body>
</html>
CSS
html,body,#container {
height:100%;
}
#container {
margin:0 auto;
max-width:400px; /* real image width */
background:url(your-image.jpg) 50% 50% no-repeat;
background-size:contain;
}
Demo
Be careful for how IE8 renders height:auto;, may not keep the ratio.
Edit: i just realized that you wrote "without adjusting the aspect ratio of the image". If you really don't want to keep the ratio, it's easier ... but do you really mean that?
You won't be able to accomplish this unless you have a set height for the container that houses the image. In order for the viewport to know where to have the image centered, it will need know the full height you are working with, as opposed to staying the same size as the image. Height will only expand if it is told to, or if there is actual content filling it up.
To center horizontally you will need to set a container around the image and give it a margin of '0, auto'. Set the image width to be 100% within the container (this will keep the proportions correct as the height will scale appropriately with it), and give the container a percentage based width as well.
You will need to give your image or surround div a set width and height for margin: auto to center the image. See how the code below works for you.
Css
#container {
width: 1000px;
height: 1000px;
}
#img {
background-color:#000;
width: 100px;
height: 100px;
margin: 0 auto;
}
HTML
<div id="container">
<div id="img">
</div>
Edit
Set image as background?
Then set the body to 100%.
body
{
background-image: url('background.jpg');
background-repeat: no-repeat; /* you know... don't repeat... */
background-position: center center; /*center the background */
background-attachment: fixed; /*don't scroll with content */
}
I wasn't able to find a perfect solution (from what I've read it's not possible to do what you want using only CSS and HTML). But I've found a solution closer to what you need. I repeat, it's not perfect. So here it goes (you actually put your image as a background for a div):
#mydiv {
width: 100%;
height: 100%;
position: relative;
background-image: url(photo.jpg);
background-position: center;
background-repeat: no-repeat;
background-size: auto 98%, cover;
}
So, the key here is the background-size property. What it does here: force the image to scale (up or down) to a specified percentage of the width/height of the div/container (the width and height of the div is dictated by the viewport). For images bigger than viewport, this solution is good, but the problem is with smaller images (which are scaled up). Unfortunely, the current implementation of CSS doesn't permit to specify a max-height or max-width for the background-image. If you want to read more on this subject open this webpage: http://www.css3.info/preview/background-size/.
Anyway, a JavaScript solution is better. Hope it helps.
Currently I'm using this:
HTML:
<div id="container">
<img src="x.jpg" id="bg" />
<div id="content">
<h1>Welcome to my website.</h1>
<p>Boo!</p>
</div>
</div>
CSS:
#bg{
position:absolute;
top:0;
left:0;
height:100%;
z-index:10;
}
#container{
/* max values provided due to the max size of the image available with me(1200x800) */
max-width:1200px;
max-height:800px;
}
#content{
position:absolute;
top:10px;
left:100px;
z-index:100;
}
The advantage here is that I'm not using any Javascript at all. But then, the absolute-ly positioned elements become a nightmare when viewed on different screens.
Currently the solution I have is write and position these elements according to different screen sizes (for example, 1024x768 would have the id content's top value as 10px whereas 1280x800 will have something like top:25px; and so on..) and store them as a separate css file so I can load the appropriate CSS during page load. I feel this is time-consuming and probably in-efficient too. Using percentage values is an option I haven't explored yet. If you know of an elegant solution, or how the big guys at about.me do it, it would help.
Thank You.
Have you tried using background-image on the body with one of the background-size values? You could use cover or perhaps 100% 100% depending on your needs.
Demo: http://jsfiddle.net/ThinkingStiff/UBaN6/
body {
background-image: url('http://thinkingstiff.com/images/matt.jpg');
background-size: cover;
background-repeat: no-repeat;
}
I cant seem to get this to work.
http://www.keironlowe.host56.com
What I need is the banner with the low opacity image on it to be centered no matter the resolution, Ive tried a wrapper but because the wrapper is a width of 800 it cuts of the image, i've tried margin:0 auto; and i've even tried using the tag but it still doesnt center in higher resolutions.
You shouldn't need the tags in #Logan's example. That tag is deprecated anyway. Setting a width (not auto) and setting margin-left and margin-right to 'auto' in your stylesheet should handle the centering just fine.
Try taking the centering and pic out of the CSS and into the HTML. the css would look like this:
#banner {
background-color:#000000;
height:350px;
width:auto;
margin:0 auto;
}
and your HTML would look like this:
<div id="banner">
<center>
<img src=".....">
</center>
</div>
That is what I would do.
First, get rid of that <center> tag you have around the <div id="banner"></div>. You don't need it and it's deprecated.
Then, swap out your current CSS of the following block:
#banner {
background-color:#000000;
background-image:url(../IMG/Banner_BG.png);
background-repeat:no-repeat;
height:350px;
width:auto;
margin:0 auto;
}
For this:
#banner {
background:url("../IMG/Banner_BG.png") center #000000 no-repeat;
height:350px;
margin:0 auto;
}
Swapped out the many background attributes for the shorthand. Since you're showing the image as a background, added in the background-position property of center. This will now bullseye your image into the centre.