I've got a div which absolutely needs an auto width that depends on text length inside it. I want a blue halo showing off as background image of that button when I hover it with my mouse.
But here's the challenge I can't solve: I want that blue halo png image to fetch the exact width of the div, I want a real distortion of the halo image without respecting any proportion.
How can I accomplish this without giving an absolute value for the width of the div? And without using anything more than HTML and CSS3?
HTML side:
<div class="button">
<p>Hover here!</p>
</div>
CSS side:
.button {
width: auto;
height: 80px;
line-height: 81px;
font-family: sans-serif;
display: inline-block;
}
.button:hover {
background: url("http://img15.hostingpics.net/pics/300291bluehalo.png") center bottom no-repeat;
}
JSFiddle: https://jsfiddle.net/ynwsmkrz/5/
Everything you have to do is add a background-size: cover; to the .button:hover like this:
CSS:
.button {
width: auto;
height: 80px;
line-height: 81px;
font-family: sans-serif;
display: inline-block;
}
.button:hover {
background: url("http://img15.hostingpics.net/pics/300291bluehalo.png") center bottom no-repeat;
background-size: cover;
}
It makes the background image cover the whole div, without cropping it. Hopefully my answer was helpful!
Related
I am trying to position a background image on the right side of the screen so that on medium screens one could see a half of it and on big ones the whole image (the image should not be scaled). The problem is that there seems to be no way to position left side of the background in the center of the div that has an unknown width.
And I can't use an img tag because it will result in a horizontal scrollbar.
EDIT:
It seems that there is no way to position a background the way I wanted, at least with background-position. You can offset a background from either side by writing background-position: top 50px left 100px, but you cannot do the same with position center. I wonder why.
Have you try to set a background size and a background position like so :
background-position: 100% 0;
background-size:50%;
You can test it here: https://jsfiddle.net/dL2u6co7/
Here is a working solution. I added another block with an absolute positioning inside the container.
.container {
margin: 50px;
padding: 10px 10px;
position: relative;
width:400px;
height:270px;
border:2px solid red;
}
.text {
float: left;
height: 200px;
width: 150px;
background-color: green;
}
.bg {
position: absolute;
top: 10px;
left: 50%;
width: 50%;
height: 250px;
background-image: url('http://www.gettyimages.pt/gi-resources/images/Homepage/Hero/PT/PT_hero_42_153645159.jpg');
background-position: 0 0;
background-repeat:no-repeat;
background-size: cover;
}
<div class="container">
<div class="text">
Text block
</div>
<div class="bg">
</div>
</div>
I have a header at the top of my page that is currently overlaying an image. the image is of a paralympian running and my webpage is about why amputees need to keep participating in sport.
I really want the image to show him running and of his prosthetic legs but when I insert the image it only shows the top half of him. I want to keep the image dimensions the same as they span across the whole page and look good but I basically just need the image to move upward without changing the dimensions.
.image1 {
background: url(https://images.unsplash.com/photo-1474014465286-9887839a5971?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=76a7a6bde1dd9e68d64b55ea5d907991) no-repeat;
position: relative;
bottom: 20px;
background-size: cover;
height: 350px;
padding-top: 20px;
}
<div class="image1">
<h1 class="headline"> how sport can help those suffering from lower-limb amputations </h1>
</div>
I guess what you are looking for is background-position property - see a demo below where I have positioned it to the center:
.image1 {
background: url(https://images.unsplash.com/photo-1474014465286-9887839a5971?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=76a7a6bde1dd9e68d64b55ea5d907991) no-repeat;
position: relative;
bottom: 20px;
background-size: cover;
height: 350px;
padding-top: 20px;
background-position:center;
}
<div class="image1">
<h1 class="headline"> how sport can help those suffering from lower-limb amputations </h1>
</div>
See this reference: w3-shcools
You can specify x and y positions too using percentages / pixels - see demo below:
.image1 {
background: url(https://images.unsplash.com/photo-1474014465286-9887839a5971?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=76a7a6bde1dd9e68d64b55ea5d907991) no-repeat;
position: relative;
bottom: 20px;
background-size: cover;
height: 350px;
padding-top: 20px;
background-position: 25% 25%;
}
<div class="image1">
<h1 class="headline"> how sport can help those suffering from lower-limb amputations </h1>
</div>
I see 3 options here:
simple one: using css bg position property e.g.
background-position-y: bottom;
put this div with height: 100%; inside another div which will
have same image with width: 100%; and with blur applied (so your image will have full height and the rest of space will be covered with the same scaled and blurred image). you can
use this reference for blurring
http://thenewcode.com/534/Cross-browser-Image-Blur-with-CSS
use aspect ratio for header to maintain image proportions - here's
the reference for this
https://css-tricks.com/snippets/sass/maintain-aspect-ratio-mixin/
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).
Is there a way of having a resizable dynamic background image in css such as.
#mainPage {
width: 100%;
height: 100%;
font-size: 3vw;
text-transform: uppercase;
background: black;
font-weight: bold;
position: fixed;
background-image: url('imagesfolder/someImage.jpg');
}
It works when I put the image in the html page, but then the image is treated as inline and any element I try to place on top of the image is shunted to the right of it or below if I use display block on them.
What I'm trying to do is have an image that resizes with the browser and place divs containing text on top of the image also rescaling with the browser and the image.
Apologies if this seems simple but I am used to absolute positioning not responsive layouts.
try:
#mainPage {
width: 100%;
height: 100%;
font-size: 3vw;
text-transform: uppercase;
background: black;
font-weight: bold;
position: fixed;
background-image: url('imagesfolder/someImage.jpg');
background-size: cover;
background-position: center center;
}
It's the background-size property you want to be looking at here, does exactly what you need. If you set it to cover, your image will always cover its entire container.
Note this is not supported in IE8 and earlier.
More info and possible values at: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
you can use the
background-size: cover;
or
background-size: 100% 100%;
Using web-tiki's responsive square grid lay-out's I have made some responsive squares with background images and text on it as follows:
HTML:
<div class="square bg imgautumn1">
<div class="content">
<div class="table">
<div class="table-cell months">
VISIBLE TEXT
</div>
</div>
</div>
</div>
CSS:
.square {
float: left;
position: relative;
margin: 0.25%;
width: 50%;
padding-bottom : 50%; /* = width for a 1:1 aspect ratio */
background-color: #1E1E1E;
overflow: hidden;
}
.content {
position: absolute;
height: 90%; /* = 100% - 2*5% padding */
width: 90%; /* = 100% - 2*5% padding */
padding: 5%;
}
.table {
display: table;
width: 100%;
height: 100%;
}
.table-cell {
display: table-cell;
vertical-align: middle;
}
.months {
font-size: 40px;
font-weight: 900;
}
.imgautumn1:before {
background-color: black;
}
/* For responsive images as background */
.bg:before {
background-position: center center;
background-repeat: no-repeat;
background-size: cover; /* you change this to "contain" if you don't want the images to be cropped */
content:'';
position:absolute;
top:0;left:0;
right:0;bottom:0;
}
.bg{color: #fff;}
/*CHANGE OPACITY ON HOVER*/
.bg:hover:before{opacity:0.2;}
Now I am trying to only make the background transparent, not the text.
While using the opacity: 0.3 property on the imgautumn1 CSS-class the image becomes transparent, but also the text in it. Other techniques like the one from this SO-answer with using a separate div for the background, or a technique with using the :after element from here for the background plus opacity make the positioning of the background go wrong (i.e., image not centred) and I find it hard to implement. Another possibility might be to place a transparent div square on top of the image, but I don't think that is possible with the background-image property.
I hope someone here can provide me with some help on how to only make the background transparent and not the text.
JSFiddle: http://jsfiddle.net/L7m5psrm/
Seems to work fine if you use the :after/:before solution (setting the image as the background)
You just need to make sure you apply the same background properties.
.imgautumn1:before {
background-image: url('https://raw.githubusercontent.com/erooijak/zaaikalender/master/Zk/Content/Images/Autumn/1.jpg');
}
/* For responsive images as background */
.bg:before {
background-position: center center;
background-repeat: no-repeat;
background-size: cover; /* you change this to "contain" if you don't want the images to be cropped */
content:'';
position:absolute;
top:0;left:0;
right:0;bottom:0;
}
Demo at http://jsfiddle.net/L7m5psrm/2/