How to align a background image with padding? - html

I have a <div> with a left aligned background image and some text.
I need the div (or an inner div) to have some padding so the text will start next to background image, not on it. Is there a way to do this with CSS?
Here is a fiddle and the CSS:
.mydiv {
background-image: url('http://vignette3.wikia.nocookie.net/starwars/images/d/d6/Human_NEGAS.jpg/revision/latest?cb=20090709062312');
background-position: left bottom;
background-size: contain;
background-repeat: no-repeat;
background-color: #fff;
width: 500px;
height: 700px;
}

Flexbox and a pseudo-element could do this...assuming I have your intention right.
I need the div (or inner div) has some padding so the text will start next to background image, not on it
Basically, the inner div is 100% height of the parent element but any space after the text is taken up by the pseudo-element which has the image as a background.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
margin: 0;
}
.mydiv {
background-repeat: no-repeat;
background-color: white;
width: 100vw;
height: 100vh;
}
.mydiv div {
display: flex;
height: 100%;
}
.mydiv div:after {
content: '';
background-image: url('http://vignette3.wikia.nocookie.net/starwars/images/d/d6/Human_NEGAS.jpg/revision/latest?cb=20090709062312');
background-position: left bottom;
background-size: contain;
background-repeat: no-repeat;
flex: 1;
}
<div class="mydiv">
<div>My Text here</div>
</div>
REVISION/ALTERNATE after commented requirements updated
I want the text starting next to image (on the right side of the background image), not on the image. The reason I want the image background because I want it to be a fixed background located bottom-left corner. I want the background image contained because I want the width of the image automatically calculated.
You would need to change the structure and put the image inline
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.mydiv {
width: 100vw;
height: 100vh;
display: flex;
}
.imgdiv {
height: 100%;
}
.imgdiv img {
max-height: 100vh;
max-width: 100%;
}
.textdiv {
flex: 1 0 auto;
}
<div class="mydiv">
<div class="imgdiv">
<img src="http://vignette3.wikia.nocookie.net/starwars/images/d/d6/Human_NEGAS.jpg/revision/latest?cb=20090709062312" alt="">
</div>
<div class="textdiv">My Text here</div>
</div>
JSFiddle Demo

If you want the text not to appear on the background image but below it, I have two solutions:
First:
Use separate div for background image like
.mydiv {
background-image: url('http://vignette3.wikia.nocookie.net/starwars/images/d/d6/Human_NEGAS.jpg/revision/latest?cb=20090709062312');
background-position: left bottom;
background-size: contain;
background-repeat: no-repeat;
background-color: #fff;
width: 100vw;
height: 100vh;
}
<div class="mydiv"></div><div>My Text here</div>
Second:
Apply the following CSS to the div containing text.
.mydiv {
background-image: url('http://vignette3.wikia.nocookie.net/starwars/images/d/d6/Human_NEGAS.jpg/revision/latest?cb=20090709062312');
background-position: left bottom;
background-size: contain;
background-repeat: no-repeat;
background-color: #fff;
width: 100vw;
height: 100vh;
}
#innerText {
padding-top: 105vh;
}
<div class="mydiv"><div id="innerText">My Text here</div></div>

No need for 2 div , you can achieve all with only one in a very simple way
Try this code:
.mydiv {
background-image: url('http://vignette3.wikia.nocookie.net/starwars/images/d/d6/Human_NEGAS.jpg/revision/latest?cb=20090709062312');
background-position: 0 0;
background-size: 250px auto;
background-repeat: no-repeat;
background-color: #fff;
width: 500px;
height: 700px;
padding-left: 255px;
text-align: left;
}

Related

How do i set a background image for a division with an image already in it?

I've ran out of hope for this for the past few days,what I'm basically trying to do is to do this:
CSS:
.div1{
/* background-image code */
}
HTML:
<div class="div1">
<!--Image here-->
</div>
Is it even possible to have a background image larger than the image in the div itself?
See the following example to achieve what you are looking for. Basically you can combine a color and an image by using both the background-color and background-image props at the same time. Position and scale the image with background-size and background-position. background-repeat: no-repeat; is important to be able to see the area that is the simple color background.
.div1 {
background-color: blue;
background-image: url(https://thumbs.dreamstime.com/b/forrest-27720334.jpg);
background-size: 50%;
background-position: center;
background-repeat: no-repeat;
width: 400px;
height: 300px;
}
<div class="div1">
</div>
For two images layered in this way:
.div1 {
background-image: url(https://www.realtree.com/sites/default/files/styles/site_xl/public/content/inserts/2022/imagebybarriebird-ducklings.jpg);
width: 400px;
height: 300px;
position: relative;
color: white;
background-size: 50%;
background-repeat: no-repeat;
background-position: center;
/*to center the text */
display: flex;
align-items: center;
justify-content: center;
}
.div1::before {
content: "";
position: absolute;
width: 100%;
height: 100%;
background-image: url(https://thumbs.dreamstime.com/b/forrest-27720334.jpg);
background-size: cover;
/*to set this image layer behind the duck one */
z-index: -1;
}
<div class="div1">
Example content text
</div>
you can add width and height in each img and background-image
.div1{
width: 100vw;
height: 500px;
/* background-image code */
}
img {
width : 200px;
height : 200px;
}
<div class="div1">
<img src="code.png" alt="">
<!--Image here-->
</div>
Give the img some padding and put the background image on it.
div {
width: fit-content;
}
img {
background-image: url(https://picsum.photos/id/1015/300/300);
background-size: cover;
padding: 30px;
}
<div>
<img src="https://picsum.photos/id/1016/200/300">
</div>

Slight transparent space between two divs

I have two div's above one another. The top div has a background .svg at the bottom with the same color as the background of the bottom div. These should align perfectly, however, they do not. There is an ever so slight amount of transparent space between them. This space disappears when zooming in and reappears when zooming in even further (see screenshots).
.top {
width: 100%;
height: 100vh;
background-color: #3772ff;
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20viewBox%3D%220%200%20378%20378%22%3E%3Cpath%20d%3D%22M0%2C378l378%2C-0l-0%2C-47.25l-378%2C47.25Z%22%20style%3D%22fill%3A%2301161e%3B%22%2F%3E%3C%2Fsvg%3E");
background-repeat: no-repeat;
background-position: left bottom;
background-attachment: scroll;
background-size: 100%;
margin: 0;
}
.bottom {
background-color: #01161e;
padding: 128px 20%;
}
<div class="top"></div>
<div class="bottom"></div>
Screenshots:
100% zoom:
A bit zoomed in:
Zooming in even further:
There may be a more elegant solution to be had, but simply pulling the lower element up a fraction of a pixel overcomes the sub-pixel rounding issue.
.top {
width: 100%;
height: 100vh;
background-color: #3772ff;
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20viewBox%3D%220%200%20378%20378%22%3E%3Cpath%20d%3D%22M0%2C378l378%2C-0l-0%2C-47.25l-378%2C47.25Z%22%20style%3D%22fill%3A%2301161e%3B%22%2F%3E%3C%2Fsvg%3E");
background-repeat: no-repeat;
background-position: left bottom;
background-attachment: scroll;
background-size: 100%;
margin: 0;
}
.bottom {
background-color: #01161e;
padding: 128px 20%;
margin-top: -.5px;
/* transform: translateY(-.5px); alternative approach */
}
<div class="top"></div>
<div class="bottom"></div>
Of course, you could just set the body background (or that of a container element) to hide it as well:
.container {
background-color: #01161e;
}
.top {
width: 100%;
height: 100vh;
background-color: #3772ff;
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20viewBox%3D%220%200%20378%20378%22%3E%3Cpath%20d%3D%22M0%2C378l378%2C-0l-0%2C-47.25l-378%2C47.25Z%22%20style%3D%22fill%3A%2301161e%3B%22%2F%3E%3C%2Fsvg%3E");
background-repeat: no-repeat;
background-position: left bottom;
background-attachment: scroll;
background-size: 100%;
margin: 0;
}
.bottom {
background-color: #01161e;
padding: 128px 20%;
}
<div class="container">
<div class="top"></div>
<div class="bottom"></div>
</div>

Setting "background-attachment: fixed" on a CSS Grid Cell causes background image to become fixed the parent Grid rather than the Cell it's set on [duplicate]

I have made a codepen to explain my problem:
When the user scroll, the blue images should follow the user scroll
The blue images should be stuck on the opposite side of the aside parts (right for the left one | left for the right one)
The pb is that
background-attachment : fixed;
isn't working this the css rule
background-position: left 0px;
Someone can help me by forking the codepen to show me a working implementation ?
.wrapper {
display: flex;
}
main {
background-color: red;
height: 1000px;
max-width: 992px;
width: 100%;
}
aside {
min-width: 150px;
background-color: green;
}
.left {
background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
background-repeat: no-repeat;
background-position: right 0px;
/*background-attachment: fixed; Doesn't work*/
}
.right {
background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
background-repeat: no-repeat;
background-position: left 0px;
/*background-attachment: fixed; Doesn't work*/
}
<div class="wrapper">
<aside class="left"></aside>
<main></main>
<aside class="right"></aside>
</div>
Why is this happening?
This is working as intended, when you use background-position: fixed; the background is positioned relative to the viewport. This means in your example the background is now aligned on the very left of the viewport outside of the .right element.
You can see this by positioning .right along the left edge of the viewport in the snippet below.
.wrapper {
display: flex;
}
main {
background-color: red;
height: 1000px;
max-width: 992px;
width: 100%;
}
aside {
min-width: 150px;
background-color: green;
}
.left {
background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
background-repeat: no-repeat;
background-position: right 0px;
/*background-attachment: fixed; Doesn't work*/
}
.right {
background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
background-repeat: no-repeat;
background-position: left 0px;
background-attachment: fixed;
order: -1;
}
<div class="wrapper">
<aside class="left"></aside>
<main></main>
<aside class="right"></aside>
</div>
What can you do?
There is no way to position the background relative to the element when using background-position: fixed; but you can achieve a similar desired result by using a position: fixed; pseudo element:
Add a new selector .left:before, .right:before with the following rules
background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png); - The background image
background-repeat: no-repeat; - Stop the background from repeating
content: ""; - Required for the pseudo element to show
position: fixed; - Set the pseudo element to be fixed relative to the viewport
height: 100%; - Make the pseudo element fill the entire height
width: 100px; - Same as the width of the background image
.wrapper {
display: flex;
}
main {
background-color: red;
height: 1000px;
max-width: 992px;
width: 100%;
}
aside {
min-width: 150px;
background-color: green;
}
.left {
direction: rtl;
}
.left:before, .right:before {
background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
background-repeat: no-repeat;
content: "";
position: fixed;
height: 100%;
width: 100%;
}
.left:before {
background-position: right top;
}
.right:before {
background-position: left top;
}
.right div {
position: relative;
}
<div class="wrapper">
<aside class="left"></aside>
<main></main>
<aside class="right">
<div>content</div>
</aside>
</div>
Please note, if you intend to put other content into .right you will need to add position: relative; to the element to set the stacking context above the pseudo element (see the div in the snippet).
Why does this work?
position: fixed; fixes the element to a set position relative to the viewport. By not setting a bottom, left, right or top position the pseudo element stays where it is originally positioned. The background can them be applied to the element in the usual way.
The problem is that you don't scroll the aside because you scroll the body
You should avoid that because it's not responsive but you can get the idea of it
.wrapper {
width: 558px;
background-color: green;
background-image: url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png), url(http://www.bodyacademy.fr/wp-content/uploads/2015/02/Bande_bleu1-100x500.png);
background-repeat: no-repeat, no-repeat;
background-position: left 47px top 0px, right 104px top 0px;
background-attachment: fixed;
}
main {
background-color: red;
width: 280px;
height: 1000px;
margin: 0px auto;
}
<div class="wrapper">
<aside class="left"></aside>
<main></main>
<aside class="right"></aside>
</div>

multiple background slider

I'm trying to create slider backgrounds. Basically create a container and put couple images inside of this container, each image is a responsive background for the whole page. In result we can have multiple backgrounds and a slider to change them. Well if I put all measurement in px than everything is work.but as soon I put in percentage since it suppose to be a responsive design. It get me instead of horizontal scroll a vertical one.
Something like that:
<div class="slider-wrap">
<div class="slide" id="slide-0">
</div>
<div class="slide" id="slide-1">
</div>
</div>
css:
* {
box-sizing: border-box;
font-family: sans-serif;
font-size: 10px;
color: white;
margin: 0;
padding: 0;
outline: none;
text-decoration: none; /*clear href decoration*/
}
body {
margin: 0px auto;
min-height: 100vh;
}
.slider-wrap {
width:100%;
height:100vh;
min-height: 100vh;
overflow-x: scroll;
}
.slide {
display: inline-block;
width: 100%;
min-height: 100vh;
float:left;
}
#slide-1 {
background-image: url(img/m_m.jpg);
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
}
#slide-0 {
background-image: url(img/b_m.jpg);
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
}
So how to make a multiple background images with horizontal scroll

background-image: auto 100%; and responsive content inside it

I want to create a div with background image and fit his height to 100%. I can achieve it with background-image: auto 100%;, but how to place the text above the image in this case?
Please check jsfiddle (try to increase the width of the window with the result to understand what I mean).
P.S. The solution should be responsive.
html, body {
height: 100%;
width: 100%;
}
body > .container {
background-image: url('https://paulmason.name/media/demos/full-screen-background-image/background.jpg');
background-repeat: no-repeat;
background-position: center top;
background-size: auto 100%;
width: 100%;
height: 100%;
}
<body>
<div class="container">
text
</div>
</body>
you could keep the container at least within the ratio of the background-image setting a max-width.
100vh equals the window's height
image is 1600px wide for 1100px of height. width's ratio from height is average 1.44 (1600/1100).
At the most, if the container is 144vh width (140% of height) content will stand hover the background filling up the entire container:
html, body {
height: 100%;
width: 100%;
margin:0;/* reset */
}
body > .container {
background-image: url('https://paulmason.name/media/demos/full-screen-background-image/background.jpg');
background-repeat: no-repeat;
background-position: center top;
background-size: auto 100%;
max-width:144vh;/* update*/
margin:auto;
border:solid;;
height: 100%;
}
<div class="container">
text
</div>
Other approach would be to clip the image as i commented earlier (let's keep here the elephant at middle and in sight):
html,
body {
height: 100%;
width: 100%;
margin: 0;
/* rset */
}
body>.container {
background-image: url('https://paulmason.name/media/demos/full-screen-background-image/background.jpg');
background-repeat: no-repeat;
/* update*/
background-position: 100% 50%;
background-size: cover;
/* end update */
width: 100%;
height: 100%;
}
<div class="container">
text
</div>
Make the background-size:100%. You will get the responsive background-image with text
html, body {
height: 100%;
width: 100%;
}
body > .container {
background-image: url('https://paulmason.name/media/demos/full-screen-background-image/background.jpg');
background-repeat: no-repeat;
background-position: center top;
background-size: 100%;
width: 100%;
height: 100%;
}
<body>
<div class="container">
text
</div>
</body>