Since I faced an issue with the background image beeing too short for the content at different resolutions, I tried to split the background into 3 parts and stretching the middle one automatically to fill the space between the top and bottom image accordingly. Unfortunately I didn't manage to realize this in CSS. How to do this exactly?
content_background_top.png: 1024 x 68px
content_background_middle.png: 1024 x 6px (Stretched)
content_background_bottom.png: 1024 x 71px
Something like this:
#content{
width: 1024px;
height: 950px;
text-align: center;
padding: 35px 0 35px 0;
background: url(img/content_background_top.png), url(img/content_background_middle.png), url(img/content_background_bottom.png);
background-repeat: no-repeat;
background-size: 1024px 68px, 1024px auto, 1024px 71px;
}
You'll need to specify the background-positions and background sizes. Also note that you'll need to list your larger "middle" background last so that it doesn't cover the others.
#content {
width: 1024px;
height: 950px;
text-align: center;
padding-top: 35px;
background: url(http://i.stack.imgur.com/vNQ2g.png?s=128&g=1), url(http://i.stack.imgur.com/vNQ2g.png?s=128&g=1), url(http://i.stack.imgur.com/vNQ2g.png?s=128&g=1);
background-repeat: no-repeat;
background-position: 0% 0%, 0% 100%, 0% 50%;
background-size: 100px, 100px, cover;
}
<div id="content"></div>
#content{
width: 1024px;
height: 750px;
text-align: center;
padding: 40px 0 40px 0;
background: url(img/content_background_top.png), url(img/content_background_bottom.png), url(img/content_background_middle.png);
background-repeat: no-repeat;
background-position: 0% 0%, 0% 100%, 0% 50%; /* Order: Top, Bottom, Middle */
background-size: 1024px 68px, 1024px 71px, 1024px 100%;
}
Related
This question already has an answer here:
symetric div with background image without clip-path
(1 answer)
Closed 2 years ago.
I'm trying to skew two div, similar to this:
Desired result
However, there is always a white line in between. I tested with a negative top margin but it doesn't work in responsive.
My result
with this code:
...
<div class="img-box"></div>
<div class="map-box"></div>
<footer>...</footer>
...
.img-box {
background: url("https://via.placeholder.com/2560x2000/0000000") no-repeat;
background-size: cover;
background-position: center center;
position: relative;
min-height: 100vh;
clip-path: polygon(0 0, 100% 10%, 100% 90%, 0 100%);
}
.map-box {
background: url("https://via.placeholder.com/2560x600/DDDDDD") no-repeat;
background-size: cover;
background-position: center center;
overflow: hidden;
position: relative;
height: 600px;
display-block;
}
footer{
height:100px;
background-color: #4D4E4C;
}
All you gotta do is add transform: translateY(10%); and z-index: 999; in your .img-box class, and it should work, let me know if it doesn't !
By the way, z-index doesn't strictly gotta be 999, I put the highest number just in case that something wont get over it later on if you decide to add more things to your code, you can put z-index: 1;, it will also work, or any number higher then 0 really :)
Just replace your css with this one :
.img-box {
background: url("https://via.placeholder.com/2560x2000/0000000") no-repeat;
background-size: cover;
background-position: center center;
position: relative;
min-height: 100vh;
transform: translateY(10%);
z-index: 999;
clip-path: polygon(0 0, 100% 10%, 100% 90%, 0 100%);
}
.map-box {
background: url("https://via.placeholder.com/2560x600/DDDDDD") no-repeat;
background-size: cover;
background-position: center center;
overflow: hidden;
position: relative;
height: 600px;
display-block;
}
footer{
height:100px;
background-color: #4D4E4C;
}
I am attempting to do something like the following:
The code I currently have loads the image as the background but I cannot seem to get the diagonal div box on top of the image and also make it diagonal
HTML
<div>
<style>
landingDiv {
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
img.bg {
/* Set rules to fill background */
min-height: 100%;
min-width: 1024px;
/* Set up proportionate scaling */
width: 100%;
height: auto;
/* Set up positioning */
position: fixed;
top: 0;
left: 0;
opacity: 10;
background-image: url("https://assets.fanart.tv/fanart/movies/155/moviebackground/the-dark-knight-51f269c2ce53a.jpg")
}
#media screen and (max-width: 1024px) { /* Specific to this particular image */
img.bg {
left: 50%;
margin-left: -512px; /* 50% */
}
}
</style>
<img class="landingDiv bg" />
<div style="height: 30px; width: 100%; background-color: gray;">
Hey
</div>
</div>
JSFiddle
You can add a gradient overlay in the background-image rule :
background-image:
linear-gradient(-10deg, transparent 20%, rgba(0,0,0,0.3) 20%, rgba(0,0,0,0.3) 80%, transparent 80%),
url("https://assets.fanart.tv/fanart/movies/155/moviebackground/the-dark-knight-51f269c2ce53a.jpg")
landingDiv {
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
img.bg {
/* Set rules to fill background */
min-height: 100%;
min-width: 1024px;
/* Set up proportionate scaling */
width: 100%;
height: auto;
/* Set up positioning */
position: fixed;
top: 0;
left: 0;
opacity: 10;
background-image: linear-gradient(-10deg, transparent 20%, rgba(255, 255, 255, 0.3) 20%, rgba(0, 0, 255, 0.3) 80%, transparent 80%), url("https://assets.fanart.tv/fanart/movies/155/moviebackground/the-dark-knight-51f269c2ce53a.jpg")
}
#media screen and (max-width: 1024px) {
/* Specific to this particular image */
img.bg {
left: 50%;
margin-left: -512px;
/* 50% */
}
}
<div>
<img class="landingDiv bg" />
<div style="height: 30px; width: 100%; background-color: gray;">
Hey
</div>
</div>
A few possible examples:
https://jsfiddle.net/mv75qn9c/1/ (darker)
https://jsfiddle.net/mv75qn9c/2/ (lighter)
any rgba() colors will do , same or mixed https://jsfiddle.net/mv75qn9c/3/
tune degrees and transparent/rgba() areas to your needs.
Note : crispy edges can be blurred setting values of color stop and start with litlle difference in values :
linear-gradient(-10deg, transparent 20%, rgba(0,0,0,0.3) 20.1%, rgba(0,0,0,0.3) 80%, transparent 80.1%)
Z-index property might help here as i see your problem,
The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order. Note: z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).
So here if you want your image in background and div in the front so
Add
img.bg { z-index:-1 ;}
So here you can see that am giving -1 to img.bg which makes it low priority compair to div ... you can now use it whatever you want.
landingDiv {
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
img.bg {
/* Set rules to fill background */
min-height: 100%;
min-width: 1024px;
/* Set up proportionate scaling */
width: 100%;
height: auto;
/* Set up positioning */
position: fixed;
top: 0;
left: 0;
opacity: 10;
background-image: url("https://assets.fanart.tv/fanart/movies/155/moviebackground/the-dark-knight-51f269c2ce53a.jpg");
z-index : -1 ;
}
#media screen and (max-width: 1024px) { /* Specific to this particular image */
img.bg {
left: 50%;
margin-left: -512px; /* 50% */
}
}
<div>
<img class="landingDiv bg" />
<div style="height: 30px; width: 100%; background-color: gray;">
Hey
</div>
</div>
This is my code.I want to move that yellow circle to right (about 25px) .how can I give that style to the second image only.
.img_home
{
background-repeat: no-repeat;
height: 1000px;
background-image: url(http://www.spidycode.com/testing/revibe/images/Circle1.png),url(http://www.spidycode.com/testing/revibe/images/Circle2.png);
}
<div class="img_home"></div>
Try this
.img_home
{
background-repeat: no-repeat;
height: 1000px;
background: url(http://www.spidycode.com/testing/revibe/images/Circle1.png) 25px top no-repeat,url(http://www.spidycode.com/testing/revibe/images/Circle2.png) no-repeat;
}
<div class="img_home"></div>
simply add background-position: 25px 0px, 0px 0px;
Try this
.img_home
{
background-repeat: no-repeat;
height: 1000px;
background: url(http://www.spidycode.com/testing/revibe/images/Circle1.png) 25px 0 no-repeat,url(http://www.spidycode.com/testing/revibe/images/Circle2.png) no-repeat;
}
<div class="img_home">
You could do with setuping parameters with background:
background: url repeat posX posY
.img_home
{
height: 1000px;
background: url(http://www.spidycode.com/testing/revibe/images/Circle1.png) no-repeat,url(http://www.spidycode.com/testing/revibe/images/Circle2.png) no-repeat 200px 0px;
}
<div class="img_home"></div>
been thinking of what would be the best way to achieve a background image with a sharp corners. I tried some css like border-radius and clip-path but no luck. I guess clip-path is the nearest possible answer but can't get it. Please see my sample below.
.main-header {
background: url('http://placehold.it/150x150') no-repeat center center;
background-position: 0% 33%;
background-size: cover;
min-height: 480px;
border-radius: 0 0 45% 45%;
}
<div class="main-header">
<br>
</div>
I'm looking to have below image.
How about this approach?
JSBin
body {
background: white;
margin: 0;
}
.main-header {
background: url('http://www.stevensegallery.com/g/400/200') no-repeat center center;
background-position: center;
background-size: cover;
min-height: 480px;
position: relative;
}
.main-header::after {
content: '';
display: block;
position: absolute;
background: transparent;
/* You can play around with these numbers to adjust the curve */
bottom: -4rem;
left: -25%;
width: 150%;
height: 400px;
border-bottom: solid 4rem white;
border-radius: 0 0 50% 50%;
}
<div class="main-header">
<br>
</div>
You can use below code for it
Add border-bottom-left-radius:60% 30%; border-bottom-right-radius:60% 30%; css. You can play with radius properties to know how it works..
.main-header {
background: url('http://placehold.it/150x150') no-repeat center center;
background-position: 0% 33%;
background-size: cover;
min-height: 480px;
border-bottom-left-radius:60% 30%;
border-bottom-right-radius:60% 30%;
}
<div class="main-header">
<br>
</div>
if you can use image by img, not background. I think you can try "clip-path" as below.
Codepen example
HTML:
<div class="main-header">
<img src="http://placehold.it/150x150">
</div>
CSS:
img {
-webkit-clip-path: circle(100% at 50% 0);
clip-path: circle(100% at 50% 0);
}
and this site is useful to make clip-path (http://bennettfeely.com/clippy/)
This question already has answers here:
turning a div into transparent to see through two containers
(4 answers)
Closed 6 years ago.
Is there any way to have a div with a background-color that takes up 100% width and a transparent box inside it that shows the original background?
Solution 1: Clip-path
Clip path can be quite useful, as it keeps the code clean and simple. However, it does not have great support (yet) in browsers, and should hence only be used in test environments.
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
height: 300px;
width: 100%;
background: tomato;
position: relative;
-webkit-clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0, 50% 0, 50% 20%, 80% 20%, 80% 80%, 20% 80%, 20% 20%, 50% 20%, 50% 0);
}
<div>
</div>
Solution 2: Box shadow Trick
The box shadow trick uses a pseudo element and overflow:hidden; to create the box shadow/colouring of the element.
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
height: 300px;
width: 100%;
overflow:hidden;
position: relative;
}
div:before{
content:"";
position:absolute;
top:20%;width:60%;height:60%;left:20%;
box-shadow:0 0 0 999px tomato;
}
<div></div>
Solution 3: Gradients
You could use multiple gradient background, however this may or may not be suitable as gradients don't always turn out rendered very nicely:
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
position: relative;
height: 300px;
width: 100%;
background: linear-gradient(tomato, tomato), linear-gradient(tomato, tomato), linear-gradient(tomato, tomato), linear-gradient(tomato, tomato);
background-size: 100% 20%, 20% 100%, 100% 20%, 20% 100%;
background-position: left bottom, right bottom, left top, left top;
background-repeat: no-repeat;
}
<div></div>
Solution 4: Borders
Whilst this may or may not be suitable for you, there is still a chance that it may help, so will post here anyway:
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
position: relative;
height: 300px;
width: 100%;
box-sizing: border-box;
border-left: 20vw solid tomato;
border-right: 20vw solid tomato;
border-top: 50px solid tomato;
border-bottom: 50px solid tomato;
}
<div></div>
Solution 5: Background attachment
I have recently come across the background-attachment property, so am still coming to grips with it. However, if you wished the background to appear behind you may be able to alter the below snippet to your needs:
body {
background: url('http://butlers-web.co.uk/Content/Images/BWLOGO.png');
background-attachment: fixed;
}
.wrapper {
width: 100%;
height: 300px;
background: tomato;
position: relative;
}
.inner {
width: 80%;
height: 80%;
background: url('http://butlers-web.co.uk/Content/Images/BWLOGO.png');
background-attachment: fixed;
position: absolute;
top: 10%;
left: 10%;
box-sizing:border-box;
border:2px solid black;
}
<div class="wrapper">
<div class="inner"></div>
</div>
You're going to need two div for that. A parent, with the red background, then the inner div.
give the inner div margin: 10px auto; as a start.