I have the following CSS class.
.img {
background-size: cover;
box-shadow: 0px 2px 5px 0 rgba(0, 0, 0, 0.26);
color: #fff;
height: 200px;
position: relative;
text-align: center;
width: 100%;
}
What I want to do is the make the height 400px proportionately. (make it responsive for mobile/tablet and desktop)
Currently if I make the height as 400px; The image stretches to right hand side. (in Mobile device screen)
this is how I use it in the code, as a background image
<div class="hero" style="background-image: url('<image path>');">
</div>
So how can I make this background image responsive
To create a ratio-box, you can use padding-top to define the height of the div based as a percentage of the width:
.img {
background-size: cover;
box-shadow: 0px 2px 5px 0 rgba(0, 0, 0, 0.26);
color: #fff;
height: 75%;
position: relative;
text-align: center;
width: 100%;
}
This creates a 4:3 ratio for the image (which from your comment is what your image should be).
What you need to determine is the ratio of the image, so you can set the proper padding amount.
An important thing to know about using padding-top as a percentage: it refers to the width of the containing block. In your case it won't be an issue, since you're using 100% width anyways, but it's something to keep in mind.
Here is a simple example:
.wrap{
width:50%;
margin:0 auto;
}
.ratio-box{
width:100%;
padding-top:100%;
background:blue;
}
<div class="wrap">
<div class="ratio-box"></div>
<p>1:1 ratio box (resize browser for results)</p>
</div>
Related
So I'm trying to make it so that a full page image shows in the page, and resizes responsively on different screens so that it always takes up the whole screen. I looked it up on w3schools and other questions on Stack, but it seems that no matter what I do it never works, I checked if something is overriding my CSS in the browser developer tools but it seems there is nothing wrong, it just simply doesnt work. I'm using bootstrap and the div which background image should be full page is a col-12, would that cause the problem? This is my css:
body, html {
height: 100%;
}
#image-div {
background-image: url("paper.jpeg");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
color: white;
background-color: rgba(0,0,0,0.5);
background-blend-mode:darken !important;
font-size: 20px;
}
and the html:
<div className="row" id="calculator-row">
<div className="col-12" id="image-div">
<div className="over-image">
<p class="try-calculator">
Calculate the possible return of investments
</p>
</div>
</div>
<div className="col-12" id="calculator-div">
<h1>Return of Investments</h1>
<BenefitCalculator />
<strong>*The average conversion percent is 4, but enter yours in case you know it</strong>
</div>
</div>
EDIT: Forgot to mention I am also using REACTJS
Try backgound-size: cover, contain;
If this does not work send an example of you code. Also height in percentage is always a bad idea. If this is for the element to be as tall as the page use 100vh or some other method. Also note that you will probably need a media query for portrait and landscape orientation.
Try this snippit:
body,
html {
height: 100%;
margin: 0;
padding: 0;
}
.row {
display: flex;
flex-wrap: wrap;
margin-right: -15px;
margin-left: -15px;
}
.row-fw {
width: 100vw;
height: 100vh;
}
.col-12 {
flex: 0 0 100%;
max-width: 100%;
}
#image-div {
background-image: url("https://placekitten.com/g/1920/1080");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
display: block;
width: 100%;
color: white;
background-color: rgba(0, 0, 0, 0.5);
background-blend-mode: darken !important;
font-size: 20px;
}
<html>
<body>
<div class="row row-fw">
<div id="image-div"></div>
</div>
</body>
</html>
You could also be using height: 100vh; & width: 100vw; (vw = viewport width, vh = viewport height).
If the parent gets bigger than the size of your screen, so will the background. 100vw & 100vh will only use the viewport width & height.
just add below class to the parent div of image, it will scale itself as per screen sizes.
.img-responsive -> Makes an image responsive (will scale nicely to the parent element).
Is there a way that I can make the width of my navigation bar the same width as the image. The problem is I'm working with an image that has an original size of 497 x 298px but to make the image stretch the full width of the page whilst still having margins of 10px on either side I had to set width: 98.4%.
Also, for some reason when I set the ul to width 100% the navigation is way too long and only fits within a normal desktop screen if I make it 50%. So at the moment they are both the same length, but when I resize the page they start to go out of sync with only the ul element following the rules I've set.
ul {
display: block;
max-width: 100%;
list-style-type: none;
margin: 10px 10px 0 10px;
padding-left: 32%;
padding-right: 25%;
text-align: center;
overflow: hidden;
background-color: #F25f70;
font-family: "Oswald", sans-serif;
border-bottom: 2px solid rgba(0, 0, 0, 0.15)
}
img {
width: 98.4%;
height: 500px;
margin-left: 10px;
margin-right: 10px;
object-fit: cover;
}
You should just wrap your navigation in a <div>. Give it 100% width. Then assign a background-image to your <ul> and make that 98% width. including these background properties
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
I created a DEMO HERE
woohooo moving to Cali in a week!!! So I made it Cali themed.
Note: The Image I used is the same dimensions specified in your post.
Also you may wana check out the background-size properties at w3schools
RESPONSIVE DIV CONTAINING IMAGE AND CONTENT
I am trying to have a div that contains a background image which has content (writing) on it as shown in the picture. The div needs to be responsive, and the image has to remain 100% and reach but not exceed the frame of the screen no matter what size the screen is.
The problem is that when I zoom out, by pressing ctrl and scroll down on the mouse, the image loses alot of it's height rather than stay the same height. How do i make it not lose its height when zooming in and out.
I've tried everything and this is the best I've got
enter code here
<div id="container">
<div class="content-inner">
<h1>HELLO!!</h1>
<hr>
<p>I HAVE A QUESTION AND WAS WONERING IF YOU CAN HELP?</p>
</div>
</div>
<style>
html, body{
margin:0;
}
#container {
position:relative;
border:1px solid red;
position: relative;
width: 100%;
min-height: auto;
text-align: center;
color: #fff;
background: radial-gradient(circle, rgba(17, 5, 19, 0.94), rgba(20, 7, 35, 0.78), rgb(0, 0, 0)), url(backgrounddark.png) no-repeat;
background-position: center;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
#container .content-inner {
position: relative;
width: 50%;
padding: 100px 15px;
text-align: center;
margin:auto;
}
#container .content-inner .content-inner h1 {
margin-top: 0;
margin-bottom: 0;
text-transform: uppercase;
font-weight: 700;
}
#container .content-inner .content-inner hr {
margin: 30px auto;
}
#container .content-inner .content-inner p {
margin-bottom: 50px;
font-size: 16px;
font-weight: 300;
color: rgba(255,255,255,.7);
}
</style>
Thanks
In your code you have not set the height for the container, and the min-height is set to auto, which means it is going to scale to the size of the content that is contained in the div. The background image is NOT considered content.
When you zoom out, you are making the font size and padding smaller compared to the screen size, which is making the div smaller (contents get smaller, div height gets smaller).
If you wish to be able to zoom out without the image getting smaller (even though the text does) I would suggest setting the height of the div. You can set it to a percentage of the viewport height using the vh units (see here for browser support, it's pretty good but I don't know your scenario: http://caniuse.com/#feat=viewport-units). Or you can set all the elements above #container to have a height or min-height of 100%, which will allow you to use a percentage for the value of height / min-height of #container.
Note that your text will NOT be vertically centered automatically if you use a dynamic height, additional CSS will be required (see this Vertically align text in a div).
I have wordpress site with a container at the very top to display a background image. I want the background to stay proportional with the div height to adjust according to screen size. I am currently using the height tag to get this area to show (See CSS below). I basically want it to not overflow when the site is big (it hides the bottom of the image when at higher resolutions) and not show white at the bottom when the site is small.
How can I make the height responsive?
You can see the site here: http://69.195.124.65/~flywitha/
CSS:
.top_site{
background: rgba(0, 0, 0, 0) url("/wp-content/themes/alex/images/mast_w-kid_1920x800.jpg") no-repeat scroll right bottom;
background-size: contain;
}
.top_site h1{
font-family:lato, sans-serif;
color:rgba(40, 40, 40, 0.8);
padding: 15% 0 9.255% 15%;
margin:0;
width:50%;
font-size:6rem;
font-weight:bold;
-webkit-text-stroke: 1px black;
text-shadow:
3px 3px 0 #000,
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
}
HTML:
<div class="top_site">
<h1 class="site-hdr-tag inset-text">
the<br>
INTERACTIVE<br>
AEROSPACE<br>
LEARNING<br>
CAMPUS
</h1>
</div>
You're probably looking for
background-size: cover
or
background-size: contain
simplest solution is to specify the height (and/or width) of the container as a percentage of the Viewport's height (and/or width):
DIV { height: 25vh; width: 50vw; }
.myclass { height: 25vh; width: 50vw; }
both meaning, size the 'container' at 25% of the Viewports height and 50% of it's width.
Cheers
How to auto re size my image, If I change my width and length to auto it won't able to display any image. My image will overlapping the text when I re-size my browser so, I would like to change my image size to dynamic.
HTML source
<div class="span2">
<ul class="nav user_menu pull-left">
<div class="round-pic1" style="background-image: url('http://asianwiki.com/images/a/a4/Andy-lau.jpg');"></div>
</ul>
</div>
CSS
.round-pic1 {
display: block;
width: 170px;
height: 170px;
margin: 0em auto;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
border-radius: 99em;
-webkit-border-radius: 99em;
-moz-border-radius: 99em;
border: 0px solid gray;
box-shadow: 0 3px 2px rgba(0, 0, 0, 0.3);
}
JSFiddle
http://jsfiddle.net/9JVHq/
I've been playing around with your fiddle a bit and have come up with a potential solution here - FIDDLE.
Make your picture square and put it in the code as an image.
Put the image in a div and give the div a min-width and max-width.
It allows the image to shrink down to 100px, keeping the same aspect ratio.
Let me know if that's what your looking for.
CSS
.holder {
min-width: 100px;
max-width: 300px;
}
HTML
<div class='holder'>
<img class="round-pic1" src='http://offsite2.seriousshops.com/53/11/11635/11635_main_400x400.jpg'/>
</div>
Using this one css to resize bg image:
jsfiddle.net/YCtDr/
background-size: 100% 100%;
MOre info
If you want to preserve aspect ratio, there are two more possibilities.
1. background-size: cover; - the background image is completely
covering the element (image can be cut off)
2.background-size: contain; - the image is streched without cutting it