I have a background image that i've set to a header element in HTML. The image seems to respond until it hits a ceratin width, then it begins to move left off the page. Can someone help me understand what is going on, i can't seem to get it working no matter what i've tried :(
Below is my code, here is the fiddle http://jsfiddle.net/n7tBp/. If you resize the browser window to under 900px it starts to move the google image to the left
.header_area{
padding-top: 10%; /* slope */
height: 200px; /* start height */
background-image: url("http://musically.com/wp-content/uploads/2013/10/Google.jpg");
background-size: cover;
-moz-background-size: cover; /* Firefox 3.6 */
background-position: center; /* Internet Explorer 7/8 */
}
.header_area h1{
color:#e5e6d6;
font-size:34px;
display: inline-block;
-webkit-transform: skewX(-15deg);
-moz-transform: skewX(-15deg);
-ms-transform: skewX(-15deg);
-o-transform: skewX(-15deg);
transform: skewX(-15deg);
}
.header_area h2{
color:#e5e6d6;
font-size:22px;
display: block;
-webkit-transform: skewX(-15deg);
-moz-transform: skewX(-15deg);
-ms-transform: skewX(-15deg);
-o-transform: skewX(-15deg);
transform: skewX(-15deg);
}
.header_area figcaption{
margin-left:30px;
margin-top: -20px;
}
There are there options you can use for this image resize .
cover:-Scale the background image to be as large as possible so that the background area is completely covered by the background image. Some parts of the background image may not be in view within the background positioning area
-webkit-background-size: cover;
background-size: cover;
DEMO http://jsfiddle.net/n7tBp/ (current one)
contain:-Scale the image to the largest size such that both its width and its height can fit inside the content area
-webkit-background-size: contain;
background-size: contain;
DEMO http://jsfiddle.net/n7tBp/4/
percentage:-Sets the width and height of the background image in percent of the parent element. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto"
-webkit-background-size: 100%;
background-size: 100%;
Demo http://jsfiddle.net/n7tBp/5/
Related
I have a div and the width is 100%. It has a background image. I am doing a zoom in out effect with animation for the background image. So, it will continuously loop zooming in the background image and zooming out.
div {
background-image:url('images/home-background.webp');
background-attachment:fixed;
background-size:cover;
background-repeat:no-repeat;
background-position:center center;
display:flex;
align-items: center;
justify-content: center;
width:100%;
height:700px;
animation:topb 10s infinite alternate;
box-sizing:border-box;
padding:30px;
}
#keyframes topb {
0% {
background-size: 105%;
}
100% {
background-size: 100%;
}
}
This works perfectly but when I resize my screen, particularly below 1200px, I see the background image becomes smaller even the div is 100% width. When I remove the animation line, the background image works fine, covering the screen. Here is a screenshot how it looks on mobile devices. You will see the background image does not cover the screen entirely even width is 100%.
So, can someone help me why is this happening?
The Problem
This is happening because your animation is overriding the Background size attribute, thus it switches from
background-size: cover;
To
background-size: 105%:
Which will make the background 105% of the width
A Solution
Since we want the background to cover the div's area at all times, and we want it to have a scaling animation, we will need to be clever. To do this we are going to nest another div with our properly sized background image inside the first. Then we add overflow: hidden to the parent and add a scale animation to the second div (not the background-size attribute though)
Some Code
body{margin:0;padding:0;background-color:#2D2D2D;}
#background {
background-image: url(https://images.unsplash.com/photo-1439405326854-014607f694d7?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1770&q=80);
background-size: cover;
background-position:center center;
width:inherit;
height:inherit;
animation: scaleAnimation 1s infinite alternate;
position:absolute;
z-index:-1;
}
#parentDiv {
width: 100vw;
height: 500px;
overflow: hidden;
}
#parentDiv h1 {
text-align: center;
margin-top:100px;
}
#keyframes scaleAnimation {
0% {
transform: scale(1);
}
100% {
transform: scale(1.1);
}
}
<div id="parentDiv">
<div id="background"></div>
<h1>Heading 1</h1>
</div>
I have a unique problem which I hope someone can help fix.
I have tweaked my WordPress header on CSS, canceling the background color, so that the background image can be seen instead (it is a white .png image with transparency at the bottom edge so that there is a pattern rather than a straight line at its bottom border).
see http://m.piccolo.co.il or https://piccolo.co.il (2 different websites).
the header's background-image has position: fixed (#masthead)
This works fine when you scroll down on a browser such as Chrome, or Explorer. However, when I scroll on IOS mobile, at some point the header's background-image disappears, leaving only the toggle bar and logo fixed.
screenshot from IOS10 - Iphone SE
This is the header's CSS:
#ht-masthead{
background-image: url('http://www.piccolo.co.il/wp-content/uploads/2017/04/ht-mast.gif');
background-repeat: no-repeat;
background-size: 100% 100px;
background-attachment: fixed !important;
width: 100%;
position: fixed;
top: 0;
z-index: 99;
transform: translateZ(0);
-moz-transform: translatez(0);
-ms-transform: translatez(0);
-o-transform: translatez(0);
-webkit-transform: translateZ(0);
-webkit-font-smoothing: antialiased;
}
Any suggestions would be great, I am getting very frustrated with this.
Seems like I managed a "patch solution" using the CSS after tag.
I gave #masthead:after these attributes forcing the torn pattered background there.
I applied it for now only to m.piccolo.co.il, like this:
#ht-masthead:after {
content: '';
position: absolute;
left: 0;
height: 100px;
right: 0;
background-repeat: repeat-x;
background-size: stretch;
background-image: url('http://m.piccolo.co.il/wp-content/uploads/2018/05/ht-mast2-1.png');
}
Any thoughts?
Thanks
I am trying to achieve an effect I saw recently, where background image zooms on hover. I pretty much did it with example here: https://jsfiddle.net/qyh6nbwt/ but it seems to be very shaky (you will understand what I mean by hovering over it), I'm on osx running latest chrome version, have not checked it in other browsers yet.
Is there a way to make it smoother, so it doesn't "shake" on zoom in?
HTML
<div id="example">
test
</div>
CSS
#example {
background-image: url(http://www.jeroenkemperman.nl/wp-content/uploads/2014/06/Johns_Inc_Pizza_Spaghetti_wikipediacommons.jpg);
background-position: center center;
width: 250px;
height: 250px;
transition:all 1000ms ease;
background-size: 100% auto;
}
#example:hover {
background-size: 160% auto;
}
just use transform, scale.
so just instead of setting the bg image to 160% use
transform:scale(1.5);
some information about the transform css property you can find here
to use the transform scale in your case you will need a wrapper with overflow hidden so just the inner div gets bigger and cut of by the outer div.
see updated fiddle.
greetings timmi
Used transform scale instead of a background-size change transition: https://jsfiddle.net/qyh6nbwt/
transform: scale(2, 2);
So I made this my mission to figure this out, turns out it wasn't quite as simple of a fix as I thought.
It's a little dirty, but you need to frame your div within a div like this:
<div class="example">
<div></div>
<p>test</p>
</div>
Then from here, you can target the zooms more accurately, like this:
div.example {
height: 250px;
width: 250px;
overflow: hidden;
position: relative;
}
div.example > div {
position: absolute;
height: 100%;
width: 100%;
-moz-transition: all 1.5s;
-webkit-transition: all 1.5s;
transition: all 1.5s;
-moz-transform: scale(1,1);
-webkit-transform: scale(1,1);
transform: scale(1,1);
background-image: url('http://www.jeroenkemperman.nl/wp-content/uploads/2014/06/Johns_Inc_Pizza_Spaghetti_wikipediacommons.jpg');
-moz-background-size: cover;
-webkit-background-size: cover;
background-size: cover;
z-index: -1;
}
div.example:hover > div {
-moz-transform: scale(2,2);
-webkit-transform: scale(2,2);
transform: scale(2,2);
}
You can adjust the zoom and speed using the scale and transition properties.
Here is a working fiddle to demonstrate. Hope this helps, I checked in Chrome/Safari/Firefox and it seems to work pretty well.
I would like to rotate a full-width div (from side to side without free space) in which will be some content.
I want the corners on the right side to touch the right side of the page and the corners on the left side to touch the left side of the page. I don't think width:200% and overflow-x:hidden is the best solution.
How can I achieve this?
Here is an example. Note that the corners don't touch the sides of the page.
.rotated {
width: 100%;
height: 100px;
background-color: red;
-moz-transform: rotate(-6deg);
-webkit-transform: rotate(-6deg);
-o-transform: rotate(-6deg);
-ms-transform: rotate(-6deg);
transform: rotate(-6deg);
}
<div class="rotated"></div>
You might find the CSS transform skewY() helpful. It will skew the element without rotating the corners.
I've also set the transform-origin to the top right so that the element doesn't skew off the top of the page.
html,body {
margin: 0;
}
.rotated {
height: 100px;
background-color: red;
-webkit-transform-origin: top right;
-ms-transform-origin: top right;
transform-origin: top right;
-webkit-transform: skewY(-6deg);
-ms-transform: skewY(-6deg);
transform: skewY(-6deg);
}
<div class="rotated"></div>
For further reference, see the Skewing and Translating example at MDN.
You could increase the horizontal proportion with scale, but the content will be scaled as well (as long as you know it you can compensate)
.rotated {
width: 100%;
height: 100px;
background-color: red;
transform: scale(1.2 , 1) rotate(-6deg);
}
<div class="rotated"></div>
I want to rotate the image which is placed in the button of scrollbar in Chrome. Now I have a CSS with this content:
::-webkit-scrollbar-button:vertical:decrement {
background-image: url(images/arrowup.png);
-webkit-transform: rotate(120deg);
-moz-transform: rotate(120deg);
background-repeat: no-repeat;
background-position: center;
background-color: #ECEEEF;
border-color: #999;
}
I wish to rotate the image without rotating its content.
Very well done and answered here – http://www.sitepoint.com/css3-transform-background-image/
#myelement:before {
content: "";
position: absolute;
width: 200%;
height: 200%;
top: -50%;
left: -50%;
z-index: -1;
background: url(background.png) 0 0 repeat;
transform: rotate(30deg);
}
Very easy method, you rotate one way, and the contents the other. Requires a square though
#element{
background : url('someImage.jpg');
}
#element:hover{
transform: rotate(-30deg);
}
#element:hover >*{
transform: rotate(30deg);
}
Update 2020, May:
Setting position: absolute and then transform: rotate(45deg) will provide a background:
div {
height: 200px;
width: 200px;
outline: 2px dashed slateBlue;
overflow: hidden;
}
div img {
position: absolute;
transform: rotate(45deg);
z-index: -1;
top: 40px;
left: 40px;
}
<div>
<img src="https://placekitten.com/120/120" />
<h1>Hello World!</h1>
</div>
Original Answer:
In my case, the image size is not so large that I cannot have a rotated copy of it. So, the image has been rotated with photoshop. An alternative to photoshop for rotating images is online tool too for rotating images. Once rotated, I'm working with the rotated-image in the background property.
div.with-background {
background-image: url(/img/rotated-image.png);
background-size: contain;
background-repeat: no-repeat;
background-position: top center;
}
Good Luck...
CSS:
.reverse {
transform: rotate(180deg);
}
.rotate {
animation-duration: .5s;
animation-iteration-count: 1;
animation-name: yoyo;
animation-timing-function: linear;
}
#keyframes yoyo {
from { transform: rotate( 0deg); }
to { transform: rotate(360deg); }
}
Javascript:
$(buttonElement).click(function () {
$(".arrow").toggleClass("reverse")
return false
})
$(buttonElement).hover(function () {
$(".arrow").addClass("rotate")
}, function() {
$(".arrow").removeClass("rotate")
})
PS: I've found this somewhere else but don't remember the source
I was looking to do this also. I have a large tile (literally an image of a tile) image which I'd like to rotate by just roughly 15 degrees and have repeated. You can imagine the size of an image which would repeat seamlessly, rendering the 'image editing program' answer useless.
My solution was give the un-rotated (just one copy :) tile image to psuedo :before element - oversize it - repeat it - set the container overflow to hidden - and rotate the generated :before element using css3 transforms. Bosh!
try making a div for the image only and then flipping it with transform: scaleY(-1); or transform: scaleX(-1);
if you want to have the navbar in front of the image you can make an overlapping div and set its opacity property to 0;
I tried all solutions but none helped, below is what was my problem and how I solved it:
Problem: we have an image for desktops with landscape orientation but To show the same image but rotated (portrait) for mobile screens.
How: I just rotated the actual image in my assets folder the way I wanted (portrait), and then just used media queries to call that image for my background for mobiles, and that's it.
(this was the easiest and quick solution I did.)
Update Dec 2021
Since the original question is
"..rotate the background image .."
The best answer looks to be here
https://stackoverflow.com/a/62135576/3446280