Image not fitting inside of a DIV - html

I am trying to place an image inside of a div. My problem is it is not fitting the image into the div, the image is in the div, it is just not fit to the div. It shows like a portion of the image because the image is bigger than the div. Can anyone help me get it to fit the whole image in the div without it being dis-proportional? I have the following:
Markup
<div class="col-lg-2 col-md-4 col-sm-6 newClass">
<div class="module img-responsive" style="background-image: url(Images/my_image.jpg);">
<header>
<h1 style="font-size: 20px; text-align: center;">Text
</h1>
<h2 style="font-size: 13px; text-align: center;">Some more text
</h2>
</header>
</div>
</div>
CSS
/* Overlay text */
.module {
/*background-color: #abc;*/
background-attachment: fixed;
/*width: 400px;*/
height: 300px;
position: relative;
overflow: hidden;
}
.module > header {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
padding: 20px 10px;
background: inherit;
background-attachment: fixed;
overflow: hidden;
}
.module > header::before {
content: "";
position: absolute;
top: -20px;
left: 0;
width: 200%;
height: 200%;
background: inherit;
background-attachment: fixed;
-webkit-filter: blur(4px);
filter: blur(4px);
}
.module > header::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.25);
}
.module > header > h1 {
margin: 0;
color: white;
position: relative;
z-index: 1;
}
.module > header > h2 {
margin: 0;
color: white;
position: relative;
z-index: 1;
}
Image

I know this is already answered, but you may prefer this solution better:
Demo
It allows scaling the image but without stretching it, as that is usually undesired.
If the image gets too big, you can always add a max-width to prevent that if you want.
HTML
<div class="col-lg-2 col-md-4 col-sm-6 newClass">
<div class="module img-responsive" style="background-image: url('http://i.stack.imgur.com/NuEZ2.jpg');">
<header>
<h1 style="font-size: 20px; text-align: center;">Text
</h1>
<h2 style="font-size: 13px; text-align: center;">Some more text
</h2>
</header>
<img src="http://i.stack.imgur.com/NuEZ2.jpg" style="visibility:hidden; width:100%; display:inherit"/>
</div>
</div>
CSS
/* Overlay text */
.module {
background-size:cover;
background-repeat: no-repeat;
position: relative;
overflow: hidden;
}
.module > header {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
background: inherit;
background-position: bottom;
overflow: hidden;
}
.module > header::before {
content: "";
position: absolute;
left: 0;
width: 100%;
height: 100%;
background: inherit;
background-size: cover;
-webkit-filter: blur(4px);
filter: blur(4px);
}
.module > header::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.25);
}
.module > header > h1 {
margin: 0;
padding: 10px 10px;
color: white;
position: relative;
z-index: 1;
}
.module > header > h2 {
margin: 0;
padding: 10px 10px;
color: white;
position: relative;
z-index: 1;
}

You need to remove background-attachment: fixed and implement background-size as follows. Here's the demo.
.module{
background-size: 100% 100%;
/* other css */
}

you have another class called newClass that you haven't supplied mark-up for _
i'm just wondering if it has relevant parameters that might affect the outcome of .module class?

Try
<div class="col-lg-2 col-md-4 col-sm-6 newClass">
<div class="module img-responsive" style="background-image: url(Images/my_image.jpg);background-size:cover;">
<header>
<h1 style="font-size: 20px; text-align: center;">Text
</h1>
<h2 style="font-size: 13px; text-align: center;">Some more text
</h2>
</header>
</div>
</div>

You should set background-size to cover ie...
.img-responsive{
background-size: cover
}

It looks like you're using a background image here. Try setting background-size to "contain" or "cover" on your module class. Be aware that background size is not supported in older versions of Internet Explorer.

Related

Transparent overlay div layer breaking out of parent container?

Trying to build out a hero style masthead with a transparent cover image, and a color tint overlay; then display some text on top of this. I am using bootstrap 3 as underlying framework.
I have my hero wrapped in a div, I then have two child div. One contains the background tint layer, the other contains the text/title.
The background div layer is breaking out of the parent wrapper div and covering the entire viewport below it. I'm not sure where i went wrong.
Fiddle of my broken attempt:
Fiddle
#page-title {
font-size: 2.2em;
padding: 20px 40px;
margin-bottom: 40px;
}
.bg-layer {
opacity: 0.75;
background-color: #f7f8fa;
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
}
.bg-wrapper {
background-image: url(/whatever.png);
background-position: right center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: scroll;
}
<div class="bg-wrapper">
<div class="bg-layer"></div>
<header id="page-title">
<div class="container">
About Us </div>
</header>
</div>
Add high z-index on .bg-layer, beacuse bootstrap CSS navbar Class default z-index is 1000
.bg-layer {
opacity: 0.75;
background-color: #f7f8fa;
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
z-index:1001;;
}
https://jsfiddle.net/lalji1051/9b46x5yo/3/
All your code is absolutely fine, Just add this line position: relative;to the .bg-wraper class and you will get the desired result!
#page-title {
font-size: 2.2em;
padding: 20px 40px;
margin-bottom: 40px;
}
.bg-layer {
opacity: 0.75;
background-color: #f7f8fa;
background-color: #f005; /* just adding this for visibility*/
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
}
.bg-wrapper {
background-image: url(/whatever.png);
background-position: right center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: scroll;
/*Just this additionale property*/
position: relative;
}
<div class="bg-wrapper">
<div class="bg-layer"></div>
<header id="page-title">
<div class="container">
About Us </div>
</header>
</div>

How to put text bocks over image on a fixed background?

I'd like to put news titles on image background like this:
What I came up with, is this:
<div class="image-container">
<img src="/path/to/img" class="img-thumbnail">
<div class="text-block">
<div class="bottom-right">My News Title</div>
</div>
</div>
CSS:
.text-block {
position: absolute;
bottom: 0px;
right: 0px;
background-color: #fff;
opacity: .8;
color: black;
width: 100%;
}
.bottom-right {
color: black;
position: absolute;
bottom: 8px;
right: 16px;
background-color: #fff;
}
Which results in this:
My problem is how to modify my code so that the title appears on a semi-transparent background which covers 1/3 of the bottom of the block, like the coffee picture above?
You could clean up the markup a bit and use a simple container element with a title (e.g. a div and a heading).
As noted by #zero298 if the image doesn't convey any information it should be defined as a background of the container
Codepen demo
Markup
<div style="background-image: url(https://.../coffee-serum-300x240.jpg)">
<h2>
The title of this article
may span across several lines.
but it's always anchored on the bottom
</h2>
</div>
CSS
div {
width: 300px;
height: 240px;
background-size: cover;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
div h2 {
background: rgba(255, 255, 255, .6);
margin: 0;
padding: 1em 1em 3em 1em;
}
Final result
Since the image is decorative, use background-image:
.card {
width: 100px;
height: 200px;
color: white;
background-color: blue;
background-image: url(https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg);
background-size: contain;
background-position: center;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.text{
background-color: rgba(1,1,1,0.5);
}
<div class="card">
<div class="text">
<h2>Hello World</h2>
<p>Foo bar</p>
</div>
</div>
You could do the following :
.bottom-right {
color: black;
position: absolute;
bottom: 50%;
right: 50%;
background-color: #fff; }
.image-container {
position: relative;
text-align: center;
color: white; }
.text-block {
position: absolute;
top: 95%;
left: 50%;
height: 40%;
transform: translate(-50%, -50%); }
Also, I think this is very helpful form w3schools.
https://www.w3schools.com/howto/howto_css_image_text.asp

Div Before css image not cover in div

before css div background image not be cover in background means background image broken the div and go to another div. Please help me to fix this.
Please Check this codepen link:-
https://codepen.io/anon/pen/wyyJKB
This is my code
.learn-more {
background: #063047;
padding-bottom: 2em;
}
.learn-more:before {
content: "";
background-image: url(https://wallpapers.walldevil.com/wallpapers/a78/preview/breath-
taking-nature-online-definition-high-background.jpg);
display: block;
width: 100%;
height: 100%;
padding-bottom: inherit;
position: absolute;
opacity: 0.15;
left: 0;
background-repeat: no-repeat;
background-size: 100% 100%;
}
<style>
/* css */
</style>
<div class="section learn-more">
<div class="inner">
<h2 class="" style="color:#fff;">I help you for this</h2>
<p class="" style="color:#fff;">Do you want call</p>
<p>
<a href="#">
<span class="" style="color:#fff;">CLICK HERE</span>
</a>
</p>
</div>
</div>
You can try this code.
HTML:
<div class="section learn-more">
<div class="inner">
<h2 class="" style="color:#fff;">I help you for this</h2>
<p class="" style="color:#fff;">Do you want call</p>
<p>
<a href="#">
<span class="" style="color:#fff;">CLICK HERE</span>
</a>
</p>
</div>
</div>
CSS:
.learn-more {
background: #063047;
padding-bottom: 2em;
position: relative;
}
.learn-more:before {
content: "";
background-image: url(https://wallpapers.walldevil.com/wallpapers/a78/preview/breath-taking-nature-online-definition-high-background.jpg);
display: inline-block;
width: 100%;
height: 100%;
padding-bottom: inherit;
position: absolute;
opacity: 0.15;
left: 0;
top: 0;
background-repeat: no-repeat;
background-size: 100% 100%;
}
.inner h2{
margin: 0;
}
.learn-more {
background: #063047;
padding-bottom: 2em;
position:relative;
}
If you positioning any div or element to Absolute. Make sure the parent element should be positon:relative;
Use position: inherit; instead of position: absolute; in class .learn-more::before
.learn-more::before {
content: "";
background-image: url(https://wallpapers.walldevil.com/wallpapers/a78/preview/breath-taking-nature-online-definition-high-background.jpg);
display: block;
width: 100%;
height: 100%;
padding-bottom: inherit;
position: inherit;
opacity: 0.15;
left: 0;
background-repeat: no-repeat;
background-size: 100% 100%;
}

Background image for div not touching the bottom of the page

I'll try to explain this the best that I can. I'm working on a website right now and I want a background image of one of my divs to fall behind the footer.
I've got it working when the image has a height of 450px but when I try to change it to 350px there is white space between it and the footer. As if there is 100px of space between it and the bottom of the page now.
In Chrome it looks fine no matter the size, but all other browsers it creates white space.
Here is my current HTML and CSS for the footer div and the div that is not working properly.
<div class="testimonials">
<div class="col-md-8 text-center">
<?php dynamic_sidebar( 'testimonial-widget' ); ?>
</div>
<div class="col-md-4">
</div>
</div>
<footer class="navbar-bottom">
<div class="row">
<div class="col-md-12 text-center">
<p class="footer-content">Some content...</p>
<p class="footer-content-mobile">Some content...</p>
</div>
</div>
<img src="/wp-content/themes/tct/inc/assets/footer.png" />
</footer>
.testimonials {
background-image: url('/wp-content/themes/tct/inc/assets/mug.jpg');
background-size: cover;
background-position: 100% 70%;
background-repeat: no-repeat;
height: 350px;
margin-bottom: -300px;
font-size: 24px;
}
footer {
word-wrap: normal;
position: absolute;
bottom: 0;
width: 100%;
}
footer a {
color: #ffffff;
}
footer a:visited {
color: inherit;
}
footer a:hover {
color: #404040;
}
#media (min-width: 981px) {
footer img {
height: 300px;
width: 100%;
top: -9999px;
z-index: 10;
}
.footer-content-mobile {
display: none;
}
}
.footer-content {
color: #ffffff;
position: absolute;
left: 0;
right: 0;
top: 250px;
margin: 0 auto;
font-size: 20px;
padding-bottom: 10px;
}
.bullet {
margin-left: 20px;
margin-right: 20px;
}
And lastly, here are screenshot of how it's supposed to look (it looks fine in Chrome) and how it's not supposed to look (how it looks in all other browsers).
Correct:
Incorrect:
Hopefully I explained everything enough so you understand my problem. Let me know if I need to provide any additional information.
Links to the images that I am using:
Mug: http://i60.tinypic.com/f4g3t3.jpg
Footer: http://i59.tinypic.com/xfq6x5.png
Try to wrap all this into another container and set explicit height to it and position: relative
.wrapper {
height: 370px;
position: relative;
overflow: hidden;
}
.testimonials {
background-image: url('http://oi60.tinypic.com/f4g3t3.jpg');
background-size: cover;
background-position: 100% 70%;
background-repeat: no-repeat;
height: 350px;
margin-bottom: -300px;
font-size: 24px;
}
footer {
word-wrap: normal;
position: absolute;
bottom: 0;
width: 100%;
}
footer a {
color: #ffffff;
}
footer a:visited {
color: inherit;
}
footer a:hover {
color: #404040;
}
#media (min-width: 981px) {
footer img {
height: 300px;
width: 100%;
top: -9999px;
z-index: 10;
}
.footer-content-mobile {
display: none;
}
}
.footer-content {
color: #ffffff;
position: absolute;
left: 0;
right: 0;
top: 250px;
margin: 0 auto;
font-size: 20px;
padding-bottom: 10px;
}
.bullet {
margin-left: 20px;
margin-right: 20px;
}
<div class="wrapper">
<div class="testimonials">
<div class="col-md-8 text-center">
<?php dynamic_sidebar( 'testimonial-widget' ); ?>
</div>
<div class="col-md-4">
</div>
</div>
<footer class="navbar-bottom">
<div class="row">
<div class="col-md-12 text-center">
<p class="footer-content">Some content...</p>
<p class="footer-content-mobile">Some content...</p>
</div>
</div>
<img src="http://oi59.tinypic.com/xfq6x5.jpg" />
</footer>
</div>
I managed to find a solution that works for me for now. I'm sure it's not the way to go but it did help.
I added a negative margin-bottom of 100px to my content wrapper and that seemed to do the trick. However, that then screws up Chrome and Safari because they were rendering it properly before. So I used a conditional comment to set the bottom-margin to 0 in Chrome and Safari.
Other answers are of course welcome though!
increase value of height
.testimonials {
background-image: url('http://i60.tinypic.com/f4g3t3.jpg');
background-size: cover;
background-position: 100% 70%;
background-repeat: no-repeat;
height: 560px; //in my case
margin-bottom: -300px;
font-size: 24px;
}
or try it
footer {
word-wrap: normal;
text-align: center;
position: absolute;
bottom: 110px;
width: 100%;
}

Fixing an image to a fullscreen div

my question is simple.
I have this page: http://vacanor.com/tests/lared
There is that image in the middle of the first section that floats on the screen. I want to stick that image in the first section preservating its position whenever I change the screen size. I've tryed everything but I can't.
Here is a video about what is bothering me: https://www.youtube.com/watch?v=U37_1cY8nAs
My html(including second section):
<div class="container-a">
<!--<div class="col-lg-12">-->
<div class="img-cover">
<center><img class="img-cover-i floating" src="img/logo-background.png" alt="Logo">
</center>
</div>
</div>
<!--</div>-->
<!-- Presentación -->
<div class="container-b">
<!-- <div class="col-lg-12">-->
<div class="ani">
<div class="intro">
<h1 class="animated fadeInDown animated-d-1 cd-headline slide">
Bienvenido a La Red
<small>
<span class="text-primary cd-words-wrapper" style="width: 207px;">
<b class="is-hidden">Construir </b>
<b class="is-hidden">Jugar</b>
<b class="is-hidden">Sobrevivir</b>
<b class="is-visible">Divertir-se </b>
</span>
<span>nunca será lo mismo.</span>
</small>
</h1>
</div>
</div>
</div>
And my css:
.img-cover {
margin: 0 auto;
width: 95%;
position: relative;
z-index: 10;
margin-top: 50px;
}
.img-cover-i {
position: relative;
}
.container-a {
margin: 0 auto;
width: 100%;
height: 100%;
position: absolute;
top: 0;
z-index: -2;
background-image: url('../img/cover-background.jpg');
background-position: center;
background-repeat: none;
background-size: cover;
}
.container-b {
margin: 0 auto;
position: absolute;
width: 100%;
height: 100%;
z-index: -3;
top:100%;
}
I did not really understand your question, so here are 3 fixes to how I understood it.
.container-a {
margin: 0 auto;
width: 100%;
height: 100%;
position: absolute;
top: 0;
z-index: -2;
background-image: url('../img/cover-background.jpg');
background-position: center;
background-repeat: none;
background-size: cover;
overflow:hidden;
}
The above code should make your image not get out of the div when resizing the screen, and below is an example of how to resize the image accordingly;
.img-cover-i {
position: relative;
height:calc(100% - 50px)
}
Also, if you want your Logo to follow the screen but not be visible outside the container a, use this code:
.img-cover-i {
margin-left:calc(50% - 237px);
position: fixed;
}
.container-a {
margin: 0 auto;
width: 100%;
height: 100%;
position: absolute;
top: 0;
z-index: -2;
background-image: url('../img/cover-background.jpg');
background-position: center;
background-repeat: none;
background-size: cover;
overflow:hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
backface-visibility: hidden;
}
Also, if using this code, remove the that wraps the img in the html part.
I hope this helped.