I want to create a header with a fixed background. So I defined the following properties:
header {
margin: 0;
padding: 0;
width: 100%;
height: 300px;
display: block;
background-image: url('...');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
background-attachment: fixed;
}
Now I have the following problem. Currently, the background is centered according to the Screen Width and Height. Since the header is at the top, the actual background of the header is just the top of the image. In addition, the header image section changes every time I change the screen height, which is not my goal.
I want the image to be centered within the header (center of the image is at the center of the header, but only if I have not scrolled down). In addition, the header image section should only change if I change the header width, height or screen width but not if the screen height is changed.
You can rely on vh unit combined with some calc(). The center is initally 50vh and you want it to be 150px from the top so we need a translation of 50vh - 150px. You should also get rid of cover if you want the image to not change when the screen height change but it may not render like you want.
I replaced 300px with 100px for the demo.
.header {
height:100px;
border:1px solid;
background:
url(https://picsum.photos/id/1014/1200/800) 50% calc(50% - (50vh - 50px)) fixed;
}
.not-fixed {
background-attachment:initial;
background-position:center;
margin-top:20px;
}
body {
min-height:200vh;
margin:0;
}
<div class="header">
</div>
<div class="header not-fixed">
</div>
With the use of cover
.header {
height:100px;
border:1px solid;
background:
url(https://picsum.photos/id/1014/1200/800) 50% calc(50% - (50vh - 50px))/cover fixed;
}
.not-fixed {
background-attachment:initial;
background-position:center;
margin-top:20px;
}
body {
min-height:200vh;
margin:0;
}
<div class="header">
</div>
<div class="header not-fixed">
</div>
You can clearly see how the first image is centred exactly like the second one without fixed
To get more details about the caluclation check this: Using percentage values with background-position on a linear gradient (the section Combining pixel and percentage values)
Try to wrap the img (outside the header div) and header div and play with position relative/absolute to superimpose header on top of the image.
Having done that, you can use z-index to push image backwards
Related
I would like to put below image in a responsive footer.
I would like to display the image in footer when width is 400px like below. I would like to keep the height as like previous. How can I do that ?
You can set the background to be
background: #eee url(image_path) center center;
and set max-wdith=100%, while setting the height to be a fixed px value;
see below:
#catcat {
background: #eee url(https://i.stack.imgur.com/wyc3V.jpg) center center;
max-width: 100%;
height:300px;
}
<div id=catcat></div>
I've got a page with sections and the first section has got a min-height of 100% (html and body are also set to 100%) so that the first section always fills the whole viewport on page load. Since I've done this before it works so far.
Problem for me is that I want to place text inside this first section which is centered vertically and horizontally. This also works but here's the issue:
I decrease the browser's height -> the text is partially out of the viewport. It's alright. But when I scroll down then the second section already begins but the text overflows the second section and the first section does not extend its height according to the text belonging to it.
Sorry for my bad English I hope you understand what I mean.
I thought min-height means that the height of the first section changes according to the amount of text but everytime it just has the 100% viewport height and what doesn't fit into this space overlaps the section.
What could be wrong there?
#section-0-bg-image-red {
width:100%;
min-height:100%;
position:relative;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box;
/* background: some full page image with no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='...', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='...', sizingMethod='scale')";
*/
background-color:red;
}
#section-0-white-overlay {
background: rgba(255,255,255,0.8);
width:100%;
height:100%;
position:absolute;
}
.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
max-width: 200px;
padding-left: 20px;
padding-right: 20px;
}
.headline {
font-size:24px;
text-align:center;
}
#section-1-yellow {
background-color:yellow;
width:100%;
height:600px;
}
<div id="section-0-bg-image-red">
<div id="section-0-white-overlay">
<div class="outer">
<div class="middle">
<div class="inner">
<div class="headline">text to be centered horizontally and vertically but overlaps the first section when window height is decreased and user scrolls down
</div>
</div>
</div>
</div>
</div>
</div>
<div id="section-1-yellow"></div>
UPDATE with a jQuery solution:
Hey,
after spending some more hours on this I found out what prevented the first section from adapting to the text:
To center the text vertically the div with class "outer" had to be positioned absolute but this way the 100%-height-section had "no reason" to adapt to anything. Guess it's one of the CSS basics but what do I know? Too little. ;)
So I dismissed the CSS centering solution and decided to implement some kind of vertical centering for the text with jQuery by setting the padding-top of the text div depending on the window height:
$(window).on('load resize', function(){
var paddingHeight = ( $(window).height() / 3 ) - 80;
$(".inner").css({"padding-top": paddingHeight + "px"});
});
May be not very elegant but it does what it's supposed to.
I think I see what you're talking about. Rather than using min-height I think you should try setting the height to the full viewport like this:
height:100vh;
This will make whatever element that has that tag fill the entire viewport no matter what the viewport size is.
Here's a link to the JSfiddle. Let me know if that's what you had in mind.
https://jsfiddle.net/w4k6rzsy/1/
I've a problem with an HTML page. This page has 2 elements:
1 div with position: absolute in the left part of the page that is a logo of the page
1 table always at the center of the page.
If I resize the page, the table is always at the center of the page, but I'd want that the div crops the background in this mode:
How can I do this trick?
The css of the left div is :
position:absolute;
z-index:2;
width:40%;
height : 100%;
background-image : url('img.png');
background-size : contain;
background-repeat : no-repeat;
background-position:fixed;
resize: none;
overflow: none;
Try setting the background-position:fixed for the logo.
You can do the image cropping by giving a negative value as the left div's left coordinate, using calc(), like:
left:calc(35% - 400px);
Where 35% is the horizontal point where you want to crop the image, and 400px is the left div's width.
#cendiv{position:absolute;
width:30%;
height: 20%;
background-color: yellow ;
left:35%;
top:40%;
}
#leftdiv {position: absolute ;
width:400px;
left:calc(35% - 400px);
top:0 px;
}
#leftdiv img {
width:100%;
}
<div id="cendiv"></div>
<div id="leftdiv">
<img src="http://www.freeimages.com/assets/182924/1829230933/leaf-collection-1-895724-m.jpg">
</div>
I am trying to integrate a hover effect to an img in css but the problem occurs when I hover it, the hover area is misplaced and the the hover effect occur even when the mouse is not over the img.
<body>
<div id='backgroundContainer'>
<div id='background31'></div>
</div>
</body>
CSS:
html, body {
max-height:100%;
width: 300%;
background: url('background.png');
top: 0;
left: 0;
background-size: cover;
background-repeat:no-repeat;
}
#backgroundContainer {
top:0;
left:0;
margin:0;
padding:0;
height:100%;
width:100%;
}
#background31 {
top:45%;
position: absolute;
margin:0;
padding:0;
background: url('alure.png');
background-repeat:no-repeat;
height:55%;
width:70%;
left:230%;
background-size: 5%;
}
#background31:hover{
background-size: 7%;
}
I was thinking about using background-position:x% y% or margin-left to simplify the code but it did not work what I tried.
You are applying the hover effect on an div which is set to a large area (the area in red in my fiddle below). This is why the hover is activated even when the mouse is not over the image.
If you add an image to the nested div, and apply the hover effect to this image it should work.
<div id='backgroundContainer'>
<div id='background31'>
<img src='http://www.sjiep.nl/images/sjiep.gif' id='testImage'>
</div>
</div>
and the css
#testImage{
width: 100px
}
#testImage:hover{
width: 150px;
}
See also: http://jsfiddle.net/2CbTX/1/
Update
Added a link to the image, see: http://jsfiddle.net/2CbTX/2/
because you have put the hover for the div the whole div , not just the image and this div background31 occupies the lower right corner square of your window .
see here : http://jsfiddle.net/Pda5e/
your image size becomes very small as compared to the div in which it is in. Since you have made it 5% of the div.
Resize the div to make it smaller and increase the background size to fill the div
so if you have to make the hover only affect the image, you must give the hover to image only.
like here : http://jsfiddle.net/Pda5e/1/
Try replacing this code
#background31{
background: url(maxresdefault.jpg);
background-repeat:no-repeat;
height:50px;
width:100px;
background-color:#066;
background-size: 5%;
}
#background31:hover{
background-size: 100%;
}
The hover effect occurs not over the image because you only change background-size, but not the size of #background31 element, it always remains width:70%.
So you should use background-size: 100% and change the width of the background31 element.
#background31 {
background-size: 100%;
width: 5%
}
#background31:hover{
width: 2%;
}
But background-size is not supported in IE8. If you want IE8 suuport than use <img> element instead of a div.
Sorry but I can't get this to work. Should be a quick answer.
My html is laid out like so:
<html>
<header>
...
</header>
<body>
<div class = "background"></div>
<div class = "content">
...
</div>
<body>
</html>
The I want the background div to simply place a 1000px background colour down the entire length of the page. The content is then padded 40px on each side, inside this background colour.
The css is like so:
body {
width:1000px;
margin-left:auto;
margin-right:auto;
}
.background {
position:absolute;
top:0px;
width:1000px;
height:100%;
}
.content {
min-height:100%;
padding-left:40px;
padding-right:40px;
}
I thought it worked like so... The body div would expand to hold the min-height of the .content div. This means that 100% height of the .background div would fill the entire body and so the length of the page. However it does not. It only fills the window height. Where am I going wrong?
Thanks
As topek guessed, this will do it:
html, body{
height:100%
}
The reason this works is because percentage CSS heights only work if the parent element has a height defined on it. By adding the above, you're giving .background's parents a height.
Update: based on OP's comment, here's how you would get the .background div to always appear to fill the viewport:
html, body {
height: 100%;
padding: 0;
margin: 0;
}
/* Fixed element that takes up entire viewport */
.background {
position: fixed;
z-index: 1;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
/* Content that stacks above .background */
.content {
position: relative;
z-index: 2;
}
As .content grows larger than the viewport and the user scrolls, the fixed position of .background will keep it always in view.
And of course, a handy example.
All you need is:
body, html {
height:100%
}
Then specify height:100%; any DIV you want to have full height.
BTW - 1000px wide is a bad unit to use. People with 1024 wide screens will get horizontal scrollbars. Better to stick to 980 or less. 960 is good because it can be divided by many factors.
I think this is what you're looking for.
http://jsfiddle.net/sg3s/GxRcp/
The key in this little example is the position: fixed; for .background so that it is kept in the screen while scrolling.
If you don't really want to do this and want the background to expand ARROUND the content just make it a normal / relatively positioned element, and wrap it arround .content...
If you give a more acurate description of the layout you're trying to create (and maybe why in such a way) we may be able to help you better.
Btw, in your example html there is an error, header should be head.
You should put bg into html or body elements as the first choices.
html { background: url("bg.jpg") no-repeat top center; }
or
body { background: url("bg.jpg") no-repeat top center; }
Fixed:
background: url("bg.jpg") no-repeat top center fixed; /* And bg will stay in fixed position */