Background image disappears with background attachment fixed property - css

I am trying to build a carousel that has a typical slide animation from one slide to the next. However each slide has an image in it that is going to much wider than the slide width and the image will pan animate before the carousel changes to the next slide. So the order is slide > pan image > next slide > pan image, and so on. The image requires some unusual complexity in that it must have a footer that is blurred. The only way I found to do this can be seen in the following codpen:
http://codepen.io/aaronbalthaser/pen/XKmQrG
Notice I set the body to flex and centered the elements for dev purposes. Next I created the elements that would be the actual carousel and I added the above codpen as child elements. Everything looks great which can be seen in the next codepen:
http://codepen.io/aaronbalthaser/pen/WxQWbM
The problem is the flex properties added to the body tag were only for development. Once I remove those properties the image disappears. That can be seen by deleting those properties in the second codepen. Additionally, once you delete those properties you can remove the fixed property found in the background shorthand and it appears again. But this property is needed to get the blurred effect the work. Ideally I need the following code to work.
HTML markup:
<div class="add-size">
<div class="carousel">
<div class="item">
<div class="pan">
<div class="container"> <!-- animation element -->
<div class="inner">
<div class="image"></div>
</div>
</div>
</div>
</div>
<div class="item">
<div class="pan">
<div class="container"> <!-- animation element -->
<div class="inner">
<div class="image"></div>
</div>
</div>
</div>
</div>
</div>
</div>
CSS:
.add-size {
width: 300px;
height: 250px;
border: 1px solid red;
position: relative;
overflow: hidden;
}
.carousel {
width: 600px;
height: 250px;
display: block;
position: absolute;
}
.item,
.pan {
width: 300px;
height: 250px;
display: block;
float: left;
overflow: hidden;
position: relative;
}
.container {
width: 400px;
height: 300px;
position: absolute;
bottom: 0;
}
.inner {
display: block;
width: 100%;
height: 100%;
position: relative;
}
.image {
width: 100%;
height: 100%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url(http://attic24.typepad.com/.a/6a00e551101c548834017d3d4fde82970c-500wi) no-repeat center center fixed;
background-size: fill;
}
.image:before {
left: -5%;
right: -5%;
bottom: -5%;
content: "text";
position: absolute;
height: 26%;
width: 110%;
background: url(http://attic24.typepad.com/.a/6a00e551101c548834017d3d4fde82970c-500wi) no-repeat center center fixed;
background-size: fill;
-webkit-filter: blur(8px);
filter: blur(8px);
}
Any assistance would be awesome. Thanks.

Using the fixed background-attachment property set to fixed, sets it against the viewport's position. You shouldn't use fixed. Drop it and just use background-size: cover instead of fill.
.image {
width: 100%;
height: 100%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url(http://attic24.typepad.com/.a/6a00e551101c548834017d3d4fde82970c-500wi);
background-size: cover;
}

Related

Make an image look like it is placed over 2 <div> boxes with CSS

Hello I need to position an image as in the example. Theoretically it looks like it is positioned over 2 seperate boxes with different background colors, that is the goal, but practically it is not possible, at least for me. How to solve the problem?
Usually you'd do this with flex and vertical alignment, but since you want specifically the image to be between boxes i'd say absolute is the way to go here
.card {
display: block;
margin-left: 80px; /* image width + 20px */
}
.header, .image-container {
display: block;
margin: 0;
}
.header h1 {
margin: 0;
}
.image-container {
height: 1px;
position: relative;
}
.image-container .image {
display; inlnie-block;
width: 60px;
height: 60px;
border-radius: 50%;
background: purple;
position: absolute;
top: -50%;
left: -10px;
transform: translateY(-50%) translateX(-100%);
}
<div class="card">
<div class="header">
<h1>Header</h1>
</div>
<div class="image-container">
<div class="image"></div>
</div>
<div class="header">
<h1>Header 2</h1>
</div>
</div>
The simplest solution will be using a combination of an of z-index and position:absolute.
*A small suggestion if you may encounter the problem: you must use z-index with specifying the position (position: static will not work)
img {
width: 100px;
height: 100px;
z-index: 99;
position: absolute;
}
div {
background-color: black;
z-index: 1;
width: 200px;
height: 100px;
position: absolute;
top: 10px;
left: 5px;
}
<img src='https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/1200px-Wikipedia-logo-v2.svg.png'>
<div></div>

Image is larger than overlay element by 1px

I am trying to overlay an image using a pseudo-element that is aligned to the bottom of a parent element. Then parent then hides part of both the image and the pseudo element using overflow: hidden. This should make the parent clip both the image and the pseudo element at the same place. However the image extends beyond the pseudo element by 1px. This happens in both Chrome and IE at specific breakpoints.
I inserted the code to stackoverflow but I can not reproduce using their code viewer. I can however reproduce on codepen using a screen width of 800px:
https://codepen.io/dwigt/pen/PXyrXq
.wrapper {
margin: auto;
}
.item {
background: lightgrey;
max-height: 500px;
min-height: 500px;
height: 100%;
max-width: 800px;
}
.image {
overflow: hidden;
max-height: 250px;
position: relative;
}
.image img {
max-height: 100%;
width: 100%;
position: relative;
}
.image::after {
z-index: 10;
content: "";
position: absolute;
left: -50%;
top: calc(80%);
width: 200%;
height: 200%;
display: block;
background: lightgrey;
border-radius: 100%;
}
<div class="wrapper">
<div class="item">
<div class="image">
<img src="https://images.unsplash.com/photo-1547039963-8bebea5ff026?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1933&q=80"/>
</div>
<div class="text">
Lorem Ipsum
</div>
</div>
</div>

Center ajax loader

I am trying to center the ajax loader. But no luck. Loader appears on right corner of the screen. Appreciate assistance. Below is the code
div.amshopby-overlay {
background-color: #fafafa;
height: 100%;
left: 0;
opacity: 0.5;
filter: alpha(opacity = 50);
position: absolute;
top: 0;
width: 100%;
z-index: 555;
}
div.amshopby-overlay img {
top: 100px;
left: 45%;
display: block;
position: absolute;
}
div.amshopby-overlay div {
margin: 0 auto;
display: block;
width: 300px;
height: 200px;
background: url('../images/amshopby-overlay.gif') 50% 50% no-repeat;
}
Try this css.
<div class="container">
<img src="loader.gif" class="loader">
</div>
CSS
.container{position:relative; height:300px;width:300px;}
.loader{position:absolute;left:0;right:0;top:0;bottom:0;margin:auto}
A solution I like to do when whatever I'm centering is just an image is to do it with the css background property:
HTML
<div id="container"></div>
CSS
#container.loader{
background:url('loader.gif') center center no-repeat;
}
Now in your javascript, add the class loader when you make the ajax request and remove the class on complete.
So I assume the div inside the amshopby-overlay contains your loader image. Give it a try:
div.amshopby-overlay div {
display: block;
width: 300px;
height: 200px;
background: url('../images/amshopby-overlay.gif') 50% 50% no-repeat;
/* Add this */
position: absolute;
top: 50%;
margin-top: -100px;
left: 50%;
margin-left: 150px;
}
Basically, top and left will push the div 50% from top and left. And we will add -50% of the div width and height value to center in vertically and horizontally. Give it a try. Hope it helps.
"margin: auto" should give you the centering style you want. CSS details below.
HTML
<div class="container">
<img src="http://placehold.it/150x150" class="loader">
</div>
CSS
.container {
/*Absolute positioning will now be relative to this tag*/
position:relative;
/*Arbitrary Height*/
height:300px;
width:300px;
/*border to show container*/
border: 1px solid;
}
.loader {
/*Allow top, left, right, bottom
to be set relative to container*/
position: absolute;
/*Set edges of tag so margin auto knows the max boundry*/
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
/*Allows the use of margin auto*/
display: block;
/*Horizontally and vertically centered
(Display block will fill remaining margin space equally)*/
margin: auto;
}
jsfiddle
http://jsfiddle.net/16vrxgxh/1/

How to give two background-image with out any stretch but need to repeat

Here is my html code:
<div class="header">
<div class="headerBanner">
<img src="img/NewTopBanner.jpg" width="885" height="190" border="0" />
</div>
</div>
Here is my CSS:
.header {
position: relative;
background:url('img/header/CRC_Website_TopBannerLeftStretch.jpg'),url("img/header/CRC_Website_TopBannerRightStretch.jpg");
background-position:left, right;
background-size:50% 100%;
background-repeat:no-repeat;
}
.headerBanner {
width: 885px;
}
This is my HTMl and css code.
it works, but this two background images stretches to middle. I want this two images(left,right) not stretched and repeat to middle of the page.
#leftHalf {
background: url(images/bg-1.jpg);
width: 50%;
position: absolute;
left: 0px;
height: 100%;
}
#rightHalf {
background: url(images/bg-2.jpg);
width: 50%;
position: absolute;
right: 0px;
height: 100%;
}
Try this one, (not tested). It may be help you.

Three DIVs of which two have a dynamic width

What I am trying to is have a header image centered on the top with a different color background on either side, dynamically filling the rest of the page. The structure would look like this:
<div id="Header_Container">
<div id="Header_Left"></div>
<div id="Header_Center"></div>
<div id="Header_Right"></div>
</div>
The Header_Center is of 960px and the Header_Left and Header_Right should fill either side of the image to the edge of the page and change width as the page width changes.
I can not get the CSS to work properly.
I assume you want those 3 divs to fill each with different content, the outsides filled fluidly or multiline. Otherwise the answer could be much 1) more simple. I also assume that the center div defines the total height of the header.
Given these two assupmtions, still a few different scenarios are thinkable of which I will give 4 examples from which you can choose the best fitting solution.
The HTML is exactly yours.
The CSS looks like:
#Header_Container {
width: 100%;
position: relative;
}
#Header_Left {
position: absolute;
left: 0;
right: 50%;
margin-right: 480px;
}
#Header_Right {
position: absolute;
left: 50%;
right: 0;
margin-left: 480px;
top: 0;
}
#Header_Center {
width: 960px;
position: relative;
left: 50%;
margin-left: -480px;
}
Now, you could change behaviour of left and right with a few extra styles:
height: 100%;
overflow: hidden;
See demonstration fiddle.
1) When the sides may be partially invisible outside the browser window (in case which you would align content in de left div to the right, and vise versa), then I suggest the solution in this fiddle demo which does not require absolute positioning at all so that any content below the header is properly cleared in all circumstances.
You must fix it using padding and box model + position : relative - it can be done without HTML Change
<div id="Header_Container">
<div id="Header_Left"></div>
<div id="Header_Right"></div>
<div id="Header_Center"></div>
</div>
And CSS ( 100px is for example )
#Header_Container{ overflow: hidden; height: 100px; }
#Header_Container *{ box-sizing: border-box; height: 100%; }
#Header_Left{ width: 50%; padding-right: 480px; }
#Header_Right{ margin-left: 50%; width: 50%; padding-left: 480px; position: relative; top: -100% };
#Header_Center{ margin: 0 auto; width: 960px; position: relative; top: -200%; }
Example is here http://jsfiddle.net/ZAALB/2/
EDITed incorrect example
If I got you right then this might be a possible solution.
​#container {
width: 100%;
height: 150px;
}
#left {
position: absolute;
left: 0;
width: 50%;
height: 150px;
background-color: #FF0000;
}
#right {
position: absolute;
right: 0;
width: 50%;
height: 150px;
background-color: #0000FF;
}
#center {
position: absolute;
left: 50%;
margin-left: -480px;
width: 960px;
height: 150px;
background-color: #888888;
}
​
#left basically says that the element will be positioned absolute and attached to the left side with a width of 50%. Same applies to #right just for the right side.
#center positions the element absolute pushed 50% to the left and then with a negative margin of width/2 which in your case would be 480px to position it in the center.
The order of the elements in the HTML is important for this hack.
<div id="container">
<div id="left"></div>
<div id="right"></div>
<div id="center"></div>
</div>​
The #center DIV must be the last element if you don't want to work with z-indexes.
Here's a fiddle to test it.
HTML:
<div id="Header_Container">
<div class="Header_Side" id="Header_Left"></div>
<div class="Header_Side" id="Header_Right"></div>
<div id="Header_Center"></div>
</div>
CSS:
#Header_Container {
position: relative;
width: 100%;
}
#Header_Container > div {
height: 158px; /* height of the image */
}
.Header_Side {
position: absolute;
width: 50%;
}
#Header_Left {
left: 0;
background-color: blue;
}
#Header_Right {
left: 50%;
background-color: green;
}
#Header_Center {
position: relative;
width: 158px; /* width of the image */
margin: 0 auto;
background-image: url('...');
}
Also see this example.
This works, but you need to change your HTML: http://jsfiddle.net/gG7r7/1/
HTML
<div id="header_background_container">
<div id="header_left"></div>
<div id="header_right"></div>
</div>
<div id="header_content_container">
<div id="header_content"><p>Content goes here</p></div>
</div>
CSS
#header_content_container {
position:absolute;
z-index:1;
width: 100%;
height: 100%;
}
#header_content {
width: 960px;
margin: 0 auto;
background: red;
height: 100%;
}
#header_left {
background: white;
width: 50%;
height: 100%;
position: absolute;
z-index: 0;
}
#header_right {
background: black;
width: 50%;
height: 100%;
position: absolute;
z-index: 0;
}

Resources