background-size: contain height always 0 - html

I'm trying to make an image responsive, using contain for the background-size, but the height is always 0. I've set the height on the parent element, what am I missing?
.masthead .container {
height: 300px;
}
.masthead-title {
margin-top: 0;
margin-bottom: 0;
color: #505050;
background-image: url('../logo.png');
background-repeat: no-repeat;
background-size: contain;
background-position: center center;
}
<div class="masthead">
<div class="container">
<label for="sidebar-checkbox" class="sidebar-toggle"></label>
<h3 class="masthead-title">
</h3>
</div>
</div>

You need to specify a height for your .masthead-title.
.masthead .container {
height: 300px;
border: 1px dashed gray; /* for demo only */
}
.masthead-title {
margin-top: 0;
margin-bottom: 0;
color: #505050;
background-image: url('http://i.imgur.com/60PVLis.png');
background-repeat: no-repeat;
background-size: contain;
background-position: center center;
height: 100%; /* new */
}
<div class="masthead">
<div class="container">
<label for="sidebar-checkbox" class="sidebar-toggle"></label>
<h3 class="masthead-title">
</h3>
</div>
</div>
jsFiddle

The problem is that the <h3> tag you're using for the background-image has zero as height. So either give the .masthead-title a fixed height or do some content into the <h3> or the <a> within.
Option 1: fixed height on element which has property background-image
.masthead-title {
height: 300px;
}
Option 2: content inside the element which has the background-image property
<h3 class="masthead-title">
Test Test Test
</h3>
Option 3:
keep the fixed height on the container and set the child container with the background-image property to 100% height
.masthead .container {
height:300px;
}
.masthead-title {
height:100%;
}

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>

CSS - How to get div on top of background image?

I have a background image half the page, and I have a div underneath it which I also want to put a quarter of it above the background image. I tried to put padding-top on the profile class but it doesn't work whilst wrapping background-pic and profile in a div.
CSS:
.background-pic{
background-image: url('https://images.pexels.com/photos/825262/pexels-photo-825262.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260');
height: 60%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-color: rgba(0,0,0,0);
}
.profile{
padding-top: 300px;
}
HTML:
<div class="background-pic">
</div>
<div class="container">
<div class="profile" style="background-color: grey; height : 300px;">
<div class="profile-pic">
</div>
</div>
</div>
You can apply a negative margin-bottom to the first element.
.background-pic {
background-image: url(https://www.placecage.com/600/400);
height: 400px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
margin-bottom: -100px;
}
.content {
padding-top: 300px;
background: rgba(10, 150, 10, .4);
}
<div class="background-pic"></div>
<div class="content">Profile content</div>
All you need to do is put a negative margin on the .profile element. I cleaned up your HTML and CSS so you could follow this:
.background-pic{
background-image: url('https://images.pexels.com/photos/825262/pexels-photo-825262.jpeg');
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 500px;
width: 100%;
}
.profile{
background-color: pink;
height: 500px; /* optional if you put enough content in */
margin: -100px auto 0 auto;
padding: 25px 50px;
width: 600px;
}
<div class="background-pic"></div>
<div class="profile">
Content goes here.
</div>
Demo:
https://codepen.io/staypuftman/pen/QrNZPz
In CSS of the .background-pic add a property position : absolute.
In CSS of the .profile add property position : relative, then change the padding of text inside .profile according to your need.

Child image(height, width in px dynamically) fit to parent div

My issue is that the child image element must fit to the parent div, but the image's height and width are dynamically set.
.people-image-right-parent { height: 150px; width: 150px;background: #EAEAEA; overflow: auto; }
.people-image-right{ width: 220px;
object-fit: contain;
height: 220px;
background: url(http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png) -491px -235px;}
<div class="people-image-right-parent">
<img class="people-image-right" />
</div>
The image's height and width are dynamic, not fixed, and it should be in pixels (px). I don't want the image to be scrollable or hidden. It must fit to parent element or must be contained in the parent element.
Please guide me on how to fit the image to its parent div.
Thanks in advance.
try this one
.people-image-right-parent {
height: 150px;
width: 150px;
background: #EAEAEA;
overflow: hidden;
}
.people-image-right {
width: 100%;
object-fit: contain;
height: 100%;
background: url(http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png) -491px -235px;
}
<div class="people-image-right-parent">
<img class="people-image-right" class="" />
</div>
Is this what you were looking for?
.people-image-right-parent {
height: 150px;
width: 150px;
background: #EAEAEA;
overflow: hidden; /* If needed you can change this
value to "scroll" or "visible". */
}
.people-image-right{
height: inherit; /* If needed you can change these */
width: inherit; /* values to pixel values. */
object-fit: cover;
margin: 0;
}
<div class="people-image-right-parent">
<img class="people-image-right" src="http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png" />
</div>
Here try with this
If your image is lower(in PX) than your div,you have to use min-width:100% & min-height:100%;
If your image is higher(in PX) than your div,you have to use max-width:100% & max-height:100%;
Here is the example, Your image is higher or lower than the parent div it should fit the parent div.
.people-image-right-parent {
height: 150px;
width: 150px;
background: #EAEAEA;
}
.people-image-right{
height: 350px;
width: 345px;
max-width:100%;
max-height:100%;
min-width:100%;
min-height:100%;
margin: 0;
}
<div class="people-image-right-parent">
<img class="people-image-right" src="http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png" />
</div>
I don't know if modifying the html is an option but...
You are much better of using an img tag inside the parent div
In the example below, the images are completely dynamic and will adjust to fit the div no matter what the aspect ratio is. it will also never stretch the image.
.people-image-right-parent {
display: inline-block;
width: 150px;
height: 150px;
background: darkred;
line-height: 150px; /* should match your div height */
text-align: center;
font-size: 0;
}
img.people-image-right {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
}
<div class="people-image-right-parent">
<img class="people-image-right" src="https://unsplash.it/300/300">
</div>
<div class="people-image-right-parent">
<img class="people-image-right" src="https://unsplash.it/100/300">
</div>
<div class="people-image-right-parent">
<img class="people-image-right" src="https://unsplash.it/300/100">
</div>
Edit:
If you can set the height and width of the img inline - even if it's generated dynamically - but cannot control anything else, see the example below. This will work as long as the width and height are declared in px in the img tag whether it's done manually or dynamically.
.people-image-right-parent {
display: inline-block;
width: 150px;
height: 150px;
background: darkred;
line-height: 150px; /* should match your div height */
text-align: center;
font-size: 0;
}
/* demonstration backgrounds */
.bg1 {background: url(https://unsplash.it/500/400);}
.bg2 {background: url(https://unsplash.it/200/600);}
.bg3 {background: url(https://unsplash.it/900/300);}
.people-image-right {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
background-size: contain;
background-position: center;
background-repeat:no-repeat;
}
<div class="people-image-right-parent">
<img class="people-image-right bg1" style="width:300px ;height:300px">
</div>
<div class="people-image-right-parent">
<img class="people-image-right bg2" style="width:100px ;height:300px">
</div>
<div class="people-image-right-parent">
<img class="people-image-right bg3" style="width:300px ;height:100px">
</div>

How to set position of a div over another div with same position in all screen widths

[http://jsfiddle.net/w7r3gveg/]1
I have 4 facebook logos and wants to be center bottom of my background image.
Div bg is background
div facebook is facebook logo.
I can set facebook logo center of bg div, but It's not responsive.
I want to lock facebook logo over bg div, so If a user changed screen width or have a different screen width facebook logo should be locked in the same position as other widths based bg div.
Try this
JsFiddle
http://jsfiddle.net/46psK/2983/
HTML
<div class="responsive-container">
<div class="dummy"></div>
<div class="img-container">
<img src="http://s2.uploads.im/t/8LauQ.png" alt="" />
</div>
</div>
css
.responsive-container {
position: relative;
width: 100%;
border: 1px solid black;
}
.dummy {
padding-top: 100%; /* forces 1:1 aspect ratio */
}
.img-container {
background-image: url("http://s2.uploads.im/t/VtT8j.png");
position: absolute;
background-repeat: no-repeat;
background-size: 100% auto;
background-position: center;
top: 0;
bottom: 0;
left: 0;
right: 0;
text-align:center; /* Align center inline elements */
font: 0/0 a;
}
.img-container:before {
content: ' ';
display: inline-block;
vertical-align: middle;
height: 100%;
}
.img-container img {
vertical-align: middle;
display: inline-block;
}
You need to make a few changes in your code. In HTML remove col-xs-1 class in .facebook elements, like:
<div class="col-xs-6">
<div class="facebook"></div>
<div class="facebook"></div>
<div class="facebook"></div>
<div class="facebook"></div>
</div>
In CSS just add:
.bg .col-xs-6{
text-align: center;
}
.facebook{
display: inline-block;
}
DEMO

Placing two divs next to each other (responsive theme in mind)

Having an issue getting my divs to fall next to each other. I've been scouring forums for a few hours, but with no success.
I'm trying to create a collage with six images. At the moment, all of my images are running down the left side, one after the other. It's probably important to note that I have set these 6 images as the background of six different divs, all housed within the "Collage" div.
I've tried applying float to one of these 6 relative divs, but it just disappears.
Normally I would have just set this all in pixels and moved everything around manually, but I am aiming for responsive layout.
How can I make the images appear beside each other responsively?
#collage-container {
max-width: 97%;
padding-right: 1%;
padding-left: 5%;
position: relative;
padding-bottom: 15%;
height: 0;
}
#collagecont2 {
position: relative;
max-width: 47%;
min-height: 70em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/PHALANTAFRONTPAGEAD.jpg?10407604049650997072');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont3 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/SANTACLAUSEFRONTPAGEAD.jpg?10527560584571387867');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont4 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/EXORBUTTERFRONTPAGEAD.jpg?10527560584571387867');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont5 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont6 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont1 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
background-size: 100% 100%;
background-repeat: no-repeat;
}
.large:hover {
color: #FF0000;
}
.large {
position: absolute;
color: #00FF00;
font-family: arial;
font-size: 20pt;
bottom: 1%;
}
}
<div id="collage-container">
<div id="collagecont1">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont2">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont3">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont4">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont5">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont6">
<div class="large">
This is a DIV sample.
</div>
</div>
</div>
Show different sized images side by side nicely
To display the images next to each other, use CSS columns and display:inline-block children. Use font-size: 0 on the parent and font-size: insert font size on the children to neutralize the white-space between elements.
#collage-container {
font-size: 0;
-webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
}
[id^="collagecont"] {
font-size: 20pt;
width: 100%;
display: inline-block;
vertical-align: top;
background-size: cover;
background-repeat: no-repeat;
position: relative;
}
Maintain div aspect ratio
Use the css padding trick to maintain the aspect ratio of the images. You have different sized images so the padding-bottom property on the parent will change for each size.
[id^="collagecont"] {
background-size: cover;
background-repeat: no-repeat;
}
#collagecont1 {
padding-bottom: 46.5%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
}
#collagecont2 {
padding-bottom: 120%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/PHALANTAFRONTPAGEAD.jpg?10407604049650997072');
}
#collagecont3 {
padding-bottom: 100%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/SANTACLAUSEFRONTPAGEAD.jpg?10527560584571387867');
}
#collagecont4 {
padding-bottom: 100%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/EXORBUTTERFRONTPAGEAD.jpg?10527560584571387867');
}
#collagecont5 {
padding-bottom: 46.5%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
}
#collagecont6 {
padding-bottom: 46.5%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
}
Other code simplifications of lesser importance in the demo below.
body {
margin: 0;
}
#collage-container {
padding: 5%;
box-sizing: border-box;
font-size: 0;
-webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
}
[id^="collagecont"] {
font-size: 20pt;
width: 98%;
display: inline-block;
vertical-align: top;
background-size: cover;
background-repeat: no-repeat;
position: relative;
color: #00FF00;
font-family: arial;
margin: 1%;
}
[id^="collagecont"]:hover {
color: #FF0000;
}
.large {
position: absolute;
width: 100%;
height: 100%;
}
#collagecont1 {
padding-bottom: 46.5%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
}
#collagecont2 {
padding-bottom: 120%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/PHALANTAFRONTPAGEAD.jpg?10407604049650997072');
}
#collagecont3 {
padding-bottom: 100%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/SANTACLAUSEFRONTPAGEAD.jpg?10527560584571387867');
}
#collagecont4 {
padding-bottom: 100%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/EXORBUTTERFRONTPAGEAD.jpg?10527560584571387867');
}
#collagecont5 {
padding-bottom: 46.5%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
}
#collagecont6 {
padding-bottom: 46.5%;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
}
<div id="collage-container">
<div id="collagecont1">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont2">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont3">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont4">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont5">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont6">
<div class="large">
This is a DIV sample.
</div>
</div>
</div>
With absolute positioning everything becomes posistioned relative to the parent. What the responsive css frameworks like bootstrap use are floating elements.
When you float something it shrink wraps to the size of the content unless you give it a width. So to float 6 items one next to the other they can't be wider than 16.66666667% (100%/6). Your large class would become:
.large {
float: left;
width: 16.6%;
}
The following fiddle demonstrates the effect:
https://jsfiddle.net/30j3046d/
The first problem is that you need to set your divs to be displayed as an inline-block. Then next problem is that you need to set a min-width on your divs. I was able to solve both issues using this:
div[id^=collagecont] {
display: inline-block;
min-width: 30%;
}
However, it would probably be better to add a class to your divs and put all of the like attributes together.
#collage-container {
max-width: 97%;
padding-right: 1%;
padding-left: 5%;
position: relative;
padding-bottom: 15%;
}
#collagecont2 {
position: relative;
max-width: 47%;
min-height: 70em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/PHALANTAFRONTPAGEAD.jpg?10407604049650997072');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont3 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/SANTACLAUSEFRONTPAGEAD.jpg?10527560584571387867');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont4 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/EXORBUTTERFRONTPAGEAD.jpg?10527560584571387867');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont5 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont6 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
background-size: 100% 100%;
background-repeat: no-repeat;
}
#collagecont1 {
position: relative;
max-width: 45%;
min-height: 20em;
background-image: url('https://cdn.shopify.com/s/files/1/0813/2907/files/935COLPAGECOV.jpg?10407604049650997072');
background-size: 100% 100%;
background-repeat: no-repeat;
}
div[id^=collagecont] {
display: inline-block;
min-width: 30%;
}
.large:hover {
color: #FF0000;
}
.large {
position: absolute;
color: #00FF00;
font-family: arial;
font-size: 20pt;
bottom: 1%;
}
<div id="collage-container">
<div id="collagecont1">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont2">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont3">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont4">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont5">
<div class="large">
This is a DIV sample.
</div>
</div>
<div id="collagecont6">
<div class="large">
This is a DIV sample.
</div>
</div>
</div>
add the following code to your css
#collagecont1.large, #collagecont2.large, #collagecont2.large, #collagecont4.large, #collagecont5.large, #collagecont6.large{display:inline-block;}
if you put your class next to your id and get rid of the inside div like this
<div id="collagecont1" class="large">
it'll probably be more what you're looking for
Output