Hi guys i am trying to create this effect with bootstrap 3 :
The black color being a random image and then just a white strip on were I can put my text etc.
So far I have this :
HTML:
<div class="parallax">
<div class="container">
<h1> Testing </h1>
</div>
</div>
CSS
.parallax {
background-image: url("../img/c.jpg");
min-height: 1000px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.container {
width: 800px;
}
However no matter what I change the width to for the container , it does not become smaller just the text inside of it does.
So again I am just looking to have a background image cover the whole browser and then just a white strip coming down but the width to be around 800px; so it leaves gaps on the side to see the image in the background
You can make use of min-width and max-width on container class. This ensures that when your browser is resized the sides are still visible by setting the width of the container to a relative (%) value. And the max-width limits it from extending beyond that. You can position the container using transform property in CSS and make an animation for the container to come from top and set its position to the vertical center of the webpage.
As far as the background is concerned, you can set the width or height to 100vw, 100vh or even % as you find suitable. This is just a demonstration.
.parallax {
background-image: url("http://via.placeholder.com/300x100");
height: 100vh;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.container {
position: absolute;
left: 50%;
transform: translateX(-50%);
top: -300px;
background: white;
color: black;
min-width: 70%;
max-width: 800px;
height: 100px;
text-align: center;
animation: expand 2s linear forwards;
}
#keyframes expand {
0% {}
100% {
top: 50%;
transform: translate(-50%, -50%);
}
}
<div class="parallax">
<div class="container">
<h1> Testing </h1>
</div>
</div>
html
<div class="parallax">
<div class="cont">
hellowold
</div>
</div>
css
.parallax {
width: 100%;
height: 500px;
position: relative; // this is necessary
background: #000;
}
.cont {
position: absolute;
width: 100%; // for responsive it will take 100% width
max-width: 800px; // for bigger screen it will be max 800px
padding: 15px; // just for decoration
background: #fff;
box-sizing: border-box;
margin: 0 auto; // absoluted element center purpose
bottom: 0; // positioning at the bottom as per your image
left: 0; // absoluted element center purpose
right: 0;// absoluted element center purpose
text-align: center; // just for decoration
}
Related
<div class="divOverlay">
<div class="div-overlay-content" >
<div class="pointer" ></div>
</div>
</div>
I have the following setup where I have a div with a background image and another pointer that always stays in the center. I want to move the background image dynamically to different positions. I am doing that by adjusting the properties
background-position: 0% 0%;
So if I set it to 50% 50%, then the center of the image is aligned with the pointer in the center. which is fine. But I have to tackle the corner scenarios .for eg:- if the value is 0% 0%, then I should have the top left corner of the image aligned to the center (with white background space where there is no image)
How to achieve this just by using CSS (without modifying the image to add the extra white spaces)?
Here is the link to jsfiddle https://jsfiddle.net/mkd914gf/21/
Here is the CSS
.divOverlay {
height: 200px;
width: 200px;
position: fixed;
z-index: 1;
bottom: 0;
left: 0;
overflow-x: hidden;
}
.div-overlay-content{
height: 100%;
width: 100%;
background-image: url(https://topdrawer.aamt.edu.au/var/aamt/storage/images/media/tdt/patterns/p_gt_t3_e1_a1_fig1/278788-1-eng-AU/P_GT_T3_E1_A1_fig1.jpg);
background-position: 0% 0%;
}
.pointer {
left: 50%;
top: 50%;
background-color: red;
position: absolute;
width: 10px;
height: 10px;
background-size: contain;
background-repeat: no-repeat;
}
There are a couple ways you can do this, I've seen some people use ::before and ::after pseudos, or the background-attachment property (works with <img> tags, not backgrounds).
I've opted and gone for absolute positioning the entire div. So we have an absolute positioned div, and a relative positioned parent. We set the height and width for each, plus the background image using background-size. Set overflow to hidden on the overlay. Then just use top right bottom left to position the div holding the image.
I also set you up with a centering method for your red dot that takes the size of the dot into account.
Here's the fiddle: https://jsfiddle.net/9sp2te4o/1/
HTML
<html>
<body>
<div class="divOverlay">
<div class="div-overlay-content" ></div>
<div class="pointer" ></div>
</div>
</body>
</html>
CSS
.divOverlay {
height: 200px;
width: 200px;
position: fixed;
z-index: 1;
bottom: 0;
left: 0;
overflow:hidden; /* Full hidden */
position:relative; /* Set relative so absolute children are contained */
background-color:rgba(255, 0, 0, 0.5);
margin:10px;
}
.div-overlay-content{
position:absolute; /* Absolute the div for positoning */
height: 100%;
width: 100%;
background-size:cover; /* Cover entire div */
left: -25%; /* position the div instead of the image */
top: -25%; /* position the div instead of the image */
background-image: url(https://topdrawer.aamt.edu.au/var/aamt/storage/images/media/tdt/patterns/p_gt_t3_e1_a1_fig1/278788-1-eng-AU/P_GT_T3_E1_A1_fig1.jpg);
}
.pointer {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* Position while taking dimensions of div into account */
height:10px;
width:10px;
background-color:red;
}
I am trying to position a background image on the right side of the screen so that on medium screens one could see a half of it and on big ones the whole image (the image should not be scaled). The problem is that there seems to be no way to position left side of the background in the center of the div that has an unknown width.
And I can't use an img tag because it will result in a horizontal scrollbar.
EDIT:
It seems that there is no way to position a background the way I wanted, at least with background-position. You can offset a background from either side by writing background-position: top 50px left 100px, but you cannot do the same with position center. I wonder why.
Have you try to set a background size and a background position like so :
background-position: 100% 0;
background-size:50%;
You can test it here: https://jsfiddle.net/dL2u6co7/
Here is a working solution. I added another block with an absolute positioning inside the container.
.container {
margin: 50px;
padding: 10px 10px;
position: relative;
width:400px;
height:270px;
border:2px solid red;
}
.text {
float: left;
height: 200px;
width: 150px;
background-color: green;
}
.bg {
position: absolute;
top: 10px;
left: 50%;
width: 50%;
height: 250px;
background-image: url('http://www.gettyimages.pt/gi-resources/images/Homepage/Hero/PT/PT_hero_42_153645159.jpg');
background-position: 0 0;
background-repeat:no-repeat;
background-size: cover;
}
<div class="container">
<div class="text">
Text block
</div>
<div class="bg">
</div>
</div>
I have rotating banner images which I'd like to work (scale to fit) in any screen size.
When I use the following, it works:
.banner{
position:absolute;
width: 80%;
height: 30%;
top:5%;
left:20%;
background:#FFF;
border:hidden;
}
However, when I try to change the width to for example 40%, the images truncate rather than scale down.
When I tried to use, for example, max-width: 80%, or width: auto, the images totally disappear, even if I use a high z-index.
Setting both width and height on your images, will not care about aspect ratio. Just use width = 100%, and leave the height related to it (with the technique below).
And then set the container width to whatever you want:
#banner {
width: 100%;
height: 0;
padding-top: 30%;
background: red;
}
#banner-container {
width: 400px;
}
<div id="banner-container">
<div id="banner"></div>
</div>
If you want to show an image inside it, use CSS background-image with background-size: cover:
#banner {
width: 100%;
height: 0;
padding-top: 30%;
background: gray;
background-position: 50% 50%;
background-size: cover;
background-repeat: no-repeat;
}
#banner-container {
width: 400px;
}
<div id="banner-container">
<div id="banner" style="background-image: url('http://placekitten.com/800/500');"></div>
</div>
I am doing an animation with HTML and CSS3 and I need adapt along with the background image. The problem is that the content stay within that div. I put the height and width fixed for this but don’t work. When I try using dynamic proportions (% or auto) and background-size: contain; the animation does not follow the original path.
With fixed size following the path:
and mobile works fine too
but, not is responsive:
With dynamic size is responsive, but not follow path:
Changed code:
#main{
position:relative;
- left: 0;
- height: 1366px;
- width: 980px;
+ // left: 0;
+ height: 100%;
+ width: 100%;
overflow:hidden;
background: url('../images/bg.png');
background-repeat: no-repeat;
-
+ background-size: contain;
}
DEMO
This is my index.html
<div id="main">
<div class="participant" style="z-index: 4;">
<div class="car">
<img class="photo" src="https://scontent-atl3-1.xx.fbcdn.net/v/t1.0-1/c21.21.259.259/s50x50/529297_568082979888645_1727470385_n.jpg?oh=c75505b8b23ff9abd26be1fd5771f81d&oe=582BAD0F" alt="">
<img class="sprite" src="http://i.imgur.com/OwYhg9T.png" alt="">
</div>
</div>
</div>
And my animation.css
#charset "utf-8";
/* CSS Document */
body, html {
height: 100%;
}
body {
padding:0;
margin:0;
}
#main{
position:relative;
left: 0;
height: 1366px;
width: 980px;
overflow:hidden;
background: url("http://i.imgur.com/G4gs6EG.png");
background-repeat: no-repeat;
}
#-moz-keyframes move
{
from {
right: -30%;
top: 8%;
}
to {
right: 140%;
top: 80%;
}
}
#-webkit-keyframes move
{
from {
right: -30%;
top: 8%;
}
to {
right: 140%;
top: 80%;
}
}
.participant {
position: absolute;
display: inline-block;
height:200px;
width: 200px;
right: 140%;
top: 80%;
-moz-animation:move 10s linear infinite;
-webkit-animation:move 10s linear infinite;
}
.sprite{
width: 100%;
height: 100%;
}
.photo{
position: relative;
top: 128px;
left: 99px;
width: 50px;
height: 50px;
}
This is a little tricky and requires fixed aspect ratio of the background image.
1. Make everything responsive.
First of all, it won't work if everything is %-based but the car is px-based (because if you resize your window everything will get smaller but the car will stay the same), so for starters you are going to have to change the size of your car to percentages.
2. Fix aspect ratio.
Then you need to fix the aspect ratio using mix of absolute and relative positions and paddings.
In your case your wrapper's CSS will look something like:
width: 100%;
padding-bottom: 71.74%; /* 980/1366 = ~ 0.7174 */
(your background image is 980x1366px)
DEMO
3. FUTURE PROOF: Fill the screen on every screen.
Unfortunately you can't do much about white space around your image because of the aspect ratio itself, I'd personally look for a 16:9 image for the background and it will fit most of the desktop/laptop screens perfectly, if you need to cover wide range of screens then you should use media queries with different-sized backgrounds.
Remember to adjust the padding-bottom of your container along with the image itself.
Hope it helps!
Try replacing height and width in your #main css class to:
#main{
height: 100%;
width: 100%;
background: url("http://i.imgur.com/G4gs6EG.png") no-repeat fixed center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I've got this working on codepen.io
http://codepen.io/NosNits/pen/RRqzPy
I want to fill my page with a background image and have the text aligned in place with that background. With the below code, the background image loads at the top of the page, and the text goes under it. I know I can use the "background: " function, but the way it is done in my below code allows for automatic resizing, regardless of browser size (i.e., mobile devices have small browser sizes). So, I just want the background image to go behind the text.
<html>
<head>
<title>Title</title>
<style>
img.bg
{
min-height: 100%;
min-width; 781;
width: 100%;
height: auto;
top: 0;
left: 0;
z-index: -1;
}
#media screen and (max-width: 781)
{
img.bg
{
left: 50%;
margin-left: -390.5;
}
}
#container
{
position: relative;
width: 781;
margin: 50 px auto;
height: 758;
border: 1px solid black
}
#left
{
position: relative;
left: 1.280409731113956%;
top: 14.51187335092348%;
padding-top: 10px;
padding-left: 10px;
padding-right: 10px;
width: 50%;
height: 50%;
color: #FFFFFF;
position: relative;
}
p
{
font: 14px Georgia;
}
</style>
</head>
HTML
<img class="bg" src="background.jpg">
<div id="container">
<div id="left">
<p>
Text
</p>
</div>
</div>
</body>
</html>
Make your BG image have a z-index of 1, and your #container div to have a z-index of 2. Does that work?
img {
position: relative;
z-index: 1;
}
#container {
position: relative;
z-index: 2;
top: 0px;
left: 0px; /*or whatever top/left values you need*/
}
Just use position: fixed for your background image http://dabblet.com/gist/3136606
img.bg {
min-height: 100%;
min-width: 781px;
width: 100%;
top: 0;
left: 0;
position: fixed;
z-index: -1;
}
EDIT (I wish there was a way to make it more visible than this)
OK, after reading the comments for the original question, I understand that the purpose is to have a background that scales nicely for any display sizes.
Unfortunately, quite a lot of mobile devices have a problem with position: fixed - you can read more about this here.
So the best solution in this case is to use a background image, not an img tag, having the background-size set to 100% (which will stretch the image - example), or to cover (which will scale the image such that it completely covers the screen - example)
Well, maybe you can also try that css:
body{
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
it's should cover all youre page even when page size is changed