Background images: how to fill whole div if image is small and vice versa
Resize the image to fit the div size.
With CSS3 you can do this:
/* with CSS 3 */
#yourdiv {
background: url('bgimage.jpg') no-repeat;
background-size: 100%;
}
How Do you Stretch a Background Image in a Web Page:
About opacity
#yourdiv {
opacity: 0.4;
filter: alpha(opacity=40); /* For IE8 and earlier */
}
Or look at CSS Image Opacity / Transparency
Fitting a large background image to fit in a small div
Define both width and height and then use background-size: cover
Make background image cover div by stretching, not maintaining aspect ratio
Turns out there was also a problem in the actual svg.
The solution below:
div {
position: relative;
&:after {
content: '';
display: block;
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
opacity: 0.5;
background-repeat: no-repeat;
background-position: center center;
background-size: 100% 100%;
}
}
I removed the height and width parameters from my svg and also added: preserveAspectRatio="none"
How to fill background image?
I believe you want background-size: cover
. Other background-size options.
Background Image Distorted when using cover and appending lots of images
Not 100% I get you, but you could try having two div
s.
One div
would contain your background image and be set to height: 100vh;
and width: 100vw;
. This will ensure the image will always stay the same size as the viewport and thus won't change size when things are added.
Under this div, you could have another, with a simple colour property set.
I.e.,
.bg-image { width: 100vw; height: 100vh; background-image: url(someurl); background-size: cover; background-repeat: none;}
.bg { background-color: grey; height: 100%; width: 100%;}
<div class="bg"> <div class="bg-image"> <!-- Place where the images go --> </div></div>
How do i show the entire image in the container?
Try this. background-size:contain;
<html>
<head>
<title>Javascript</title>
<style>
.section1{
background-image: url('../Downloads/1.jpg');
height: 600px;
width: 100%;
background-size:contain;
}
</style>
</head>
<body>
<section>
<div class="section1">
</div>
</section>
</body>
</html>
Display image original size, if possible, else shrink
I think you can achieve what you want with a combination of the two properties: max-width: 100%
and max-height: 100%
:
.container {
display: flex;
}
.item {
margin: 1rem;
width: 200px;
height: 200px;
border: 1px solid;
display: flex;
}
img {
max-width: 100%;
max-height: 100%;
margin: auto;
}
<div class="container">
<div class="item">
<img src="https://via.placeholder.com/150x150" />
</div>
<div class="item">
<img src="https://via.placeholder.com/300x200" />
</div>
<div class="item">
<img src="https://via.placeholder.com/200x300" />
</div>
</div>
Background image with opacity and cover whole screen
You can combine multiple background images and stack them above each other. But then there is no way to control their opacity.
.backgroundImage {
background-image: url('http://www.css3.info/wp-content/themes/new_css3/img/sheep.png'), url('http://lorempixel.com/300/400');
background-position: center bottom, left top;
-webkit-background-size: 80px 60px, cover;
-moz-background-size: 80px 60px, cover;
-o-background-size: 80px 60px, cover;
background-size: 80px 60px, cover;
background-repeat: repeat-x, no-repeat;
width: 100%;
height: 100vh;
}
In your case the img
tag is not closed. It should look like this <img src="Image.jpg">
.
Further you can not specify the dimensions of an img
with background-size:
you should use width:
and height:
.
How to achieve background-image: cover CSS effect for dynamic images?
I ended up doing the following, without altering the html nor using other language:
PHP
$img_url = get_img_url();
HTML
<div>
<img src="<?php echo $img_url; ?>">
</div>
CSS
div {
width: 600px;
height: 200px;
}
div img {
min-width: 100%;
min-height: 100%;
}
Related Topics
Google Maps V3 - Prevent API from Loading Roboto Font
How to Change Bootstrap Version 4 Button Color
How to Prevent Different Browsers Rendering Fonts Differently
Why Doesn't Min() (Or Max()) Work with Unitless 0
Is It Bad Practice to Comment Out Single Lines of CSS with //
How to Style Menu Button and Menu Items
Comma in CSS, Multiple Selectors Using the Same CSS
How to Implement Wordwrap on Jqgrid Which Works on IE7, IE8 and Ff
How to Make Jquery UI Nav Menu Horizontal
Input Height Differences in Firefox and Chrome
Draw Angular Side/Parallelogram Using CSS
How to Use Vertical Align in Bootstrap
How Do Browsers Read and Interpret CSS
Prevent Children from Inheriting Transformation CSS3
Setting Different Lengths for Grid Gaps in CSS Grid
Are There Any Good Reasons for Using Hex Over Decimal for Rgb Colour Values in CSS