Is it possible to style an image added by the "content" property - css

Here is my code:
#my_div:before
{
/* displaying the image */
content: url("img path");
/* centering the image */
display: block;
text-align: center;
/* making the image responsive */
max-width: 100%;
}
<div id="my_div"></div>
I'm trying to make the image responsive through the max-width:100% property but it is not working
My Question: Is it possible to do such a thing?
Edit
The question is not a duplicate, I want the dimensions to scale automatically on screen resize while the other question sets a fixed size to the image

Try this:
#my_div:before
{
/* displaying the image */
content: url("img path");
/* centering the image */
display: block;
text-align: center;
/* making the image responsive */
max-width: 100%;
height: auto;
}
If this doesn't work, try removing the :before pseudo-element
#my_div
{
/* displaying the image */
content: url("img path");
/* centering the image */
display: block;
text-align: center;
/* making the image responsive */
max-width: 100%;
height: auto;
}
Without seeing the context of your html, this is the best solution I can offer. We'll need more information in order to understand your situation better.

I confess I'm not entirely sure what effect you are after but I think this might be a better option for you.
Don't put the image in the content property...make that pseudo-element 100% size of the div and use a background image.
A couple of options for you.
.my_div {
position: relative;
height: 150px;
border: 1px solid grey;
width: 75%;
margin: 1em auto;
}
.my_div:before {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
content: '';
background-image: url(http://lorempixel.com/g/400/200/);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.cover:before {
background-size: contain;
}
<div class="my_div"></div>
<div class="my_div cover"></div>
JSfiddle Demo

Related

invariant scaling for CSS based image map

I'm using a CSS based image-map, which I want to display correctly whatever the size of the browser window. Of course there are actually multiple links.
My HTML ...
<div id="sitemap" >
<img src="img.jpg" class="center"/>
<a href="url1.html" id='id1'></a>
</div
And the CSS ...
#sitemap img{
max-width: 100vw;
max-height: 100vh;
position: relative;
}
#sitemap a {
display: block;
position: absolute;
}
#sitemap a:hover {
background: rgba(255, 255, 0, 0.5);
border-radius: 20px;
}
a#archive {
top: 48%;
margin-left: 14%;
width: 20%;
height: 15%;
}
This works great in a tall, narrow browser, but when the browser window is wider than it is tall, the percentages consider the dead space in the blank sidebars. How can I make the percentages consider only the actuall image?
So you know the reason.
This is because of the div(id=sitemap)'s width.
How about this one?
#sitemap {
/* for debug background-color: red; */
/* make sure the div width only size of contents */
display: inline-flex;
/* You set position relative to "img", but it semmed doesn't work because it isn't a parent‐child relationship */
position: relative;
}
#sitemap img{
max-width: 100vw;
max-height: 100vh;
/* position: relative; */
}
a#archive {
/* I think it's good enough setting two properties, unless you aren't particular about the details. */
top: 10%;
left: 10%;
}

Responsive-images and Semantic HTML

I often use the ‘background-image” css property to achieve nicely behaving responsive images. I add the following CSS:
background-size: cover;
background-position: center center;
Which yields a centered image which takes the whole space of the containing div/element.
I use it for images which are a part of the content. The problem is it is probably incorrect semantically, as background-images should be set for decoration not content. I consider using either of the following methods to solve this issue:
Find a method to make the a background-image more semantically appropriate. Perhaps holding it in a figure html tag instead of a div. Perhaps using the title property as a replacement for the alt property of the image element.
Provide the advantages a background-image has to the ordinary img tag. Is there a way for an image element to cover the containing div and also to center in the middle of that containing div?
Say hello to the css property object-fit (css-tricks). The behavior is kinda the same as background-size, you got kinda the same options. Although the support for it is not great for IE and only partial in Edge (caniuse).
There are polyfills for it. They are different though
calulates the position with javascript (found here)
puts the image as a background-image (found here) (note there are several polyfills that does this)
I rolled my own for a project (it's more complete then the example below), but it's the same approach as in option two. I'm not sure about the support for media object on that one though as I haven't had the need for it.
if you prefer jsfiddle
if ("objectFit" in document.documentElement.style === false) {
$(".block__image").each(function() {
var $image = $(this);
if ($image === undefined) {
return;
}
$image
.parents(".block")
.css("background-image", "url(" + $image.attr("src") + ")")
.addClass("block--object-fit");
});
}
* {
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
padding: 2rem;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.block {
height: 25rem;
width: 30%;
background-color: deepskyblue;
overflow: hidden;
position: relative;
-webkit-transition: height 200ms;
transition: height 200ms;
}
#media screen and (min-width: 1000px) {
.block {
height: 35rem;
}
}
.block.block--object-fit {
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.block__image {
height: 100%;
width: 100%;
-o-object-fit: cover;
object-fit: cover;
}
.block--object-fit .block__image {
opacity: 0;
}
.block__image-alternative {
max-height: 100%;
max-width: auto;
position: absolute;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="block">
<img src="https://source.unsplash.com/cXU6tNxhub0/1600x900" alt="" class="block__image">
</div>

Setting full screen img background in CSS

I have a web page which I'd like to set full screen image right when users enter the page. I don't want it to be fixed or anything. Just at the size of the window.
Now, I also have a footer which is positioned absolutely at the bottom of the web page. Here is the styles for the footer:
html {
height: 100%;
box-sizing: border-box;
}
body {
position: relative;
margin: 0;
min-height: 100%;
padding-bottom: 80px;
color: white;
background: white;
font-family: "Quicksand";
fill: currentColor;
}
/* Footer Section */
footer {
display: flex;
position: absolute;
right: 0;
bottom: 0;
left: 0;
height: 80px;
align-items: center;
justify-content: center;
background-color: $accent-color;
color: #fff;
}
Now, when I try to set my full screen image like this:
.fullscreen-bg {
height: 100%;
background-image: url("/assets/images/scorpion.png");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
The image doesn't show at all, but when I change min-height: 100% to height: 100%, The image suddenly shows up, but the footer now is at the bottom of the viewport, not the page. Here is an image that will explain this perfectly:
https://i.gyazo.com/d47e2e1fcdeaf4f8f8cab8b847c00f43.png
As you can see, the footer now jumps up and resides at the bottom of the screen.
If I change this attribute back to min-height: 100%, the image doesn't show at all:
https://i.gyazo.com/b3d8b941222ac16455d220f25da8bfbf.png
How can I fix this? I want the image to be full screen but also I don't want the footer to jump up from the bottom of the page. How can I combine these 2 behaviors?
Use height: 100vh; it will cover 100% height for all screen sizes.

I have made a square image round with CSS, however I can't seem to make it responive

I am making a profile page and have turned a square image into a circle using the CSS code below:
.circular {
width: 250px;
height: 250px;
background-image: url('./images/profile.jpg');
background-size: cover;
display: block;
border-radius: 125px;
-webkit-border-radius: 125px;
-moz-border-radius: 125px;
margin-left: auto;
margin-right: auto;
margin-top: -150px;
}
However I am struggling to make this responsive within the header portion of my page, any ideas how I could have rounded corners and make the image scale at the same time?
Thanks!
I would suggest like already mentioned to avoid fixed px values and instead use percentages.
Another approach (if applicable in your case) could be the use of screenbased responsive styling like:
#media screen and (min-width:960px) {
.circular {
/* ... */
}
}
#media screen and (min-width:1440px) {
.circular {
/* ... */
}
}
With that you can ensure that you have fixed transition points when you resize your design/image.
You could try something like the following.
Just ensure that the parent container is of reasonable width, because .circular will take all of its width.
.wrap {
width: 400px; /* for example... */
}
.circular {
width: 100%;
padding-bottom: 100%;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
margin-left: auto;
margin-right: auto;
margin-top: -150px;
background-image: url(http://placehold.it/200x200);
background-size: cover;
}
<div class="wrap">
<div class="circular"></div>
</div>

CSS background color wrapper (Cause by mistake)

I have meet a interesting problem here.
my structure is
body
|----site-wrapper
|-----cover-wrapper
both the height is set to 100%, when I set the site-wrapper background color, it looks fine,
but when I set cover-wrapper background color, it only wrapper the background color to fit the content(some text), not expand to the whole screen, I have tried both on Chrome and Firefox. check the effect here: http://jsfiddle.net/h82Ne/
Here is the css:
html{
height: 200%
}
body {
height: 100%;
background-color: #333;
}
.site-wrapper {
display: table;
width: 100%;
height: 50%; /* For at least Firefox */
min-height: 50%;
background-color: #000;
}
.cover-wrapper {
display: table;
width: 100%;
height: 100%; /* For at least Firefox */
min-height: 100%;
background-color: #0f0;
}
If you are looking to get the .cover-wrapper to cover the entire page you can use:
.cover-wrapper {
width: 100%;
height: 100%; /* For at least Firefox */
position: absolute;
top: 0;
bottom: 0;
background-color: #0f0;
}
This will cause it to act more like an overlay.
The problem solved, it make by my own mistake, i miss css for site-wrapper-inner here.
Sorry about that

Resources