CSS border/outline image over image - image

I have an image that needs a border-image over the image and not around it.
The border is an transparent png.
See here what happens if I use border:
You can see that the image holds a white background.
I would like to have the border to be over the image. After some Googleling I found outline. Here I can set a negative value. The problem here is that outline can't hold images......
I tried setting negative values on margin for the image and the border but that didn't help.
Anyone ideas?
Here is the code (not very interesting):
<div class="top_img">
<!-- some content -->
</div>
Here is the CSS:
<style>
background-image: url(the_image.jpg);
background-repeat: no-repeat;
background-attachment: scroll;
background-size: cover;
border: 20px solid transparent;
border-image: url(images/border_image.png) 30 round;
</style>

Related

Header with transparent navbar

enter image description here
is it possible to create something like the picture?
(so a header with an image and at the bottom side of it a semitransparent navbar ( so I can see the bottom of the picture)
What I did was that I used background-image to add the image to the background of your header, a whitespace div to move the navbar to the bottom, and then added an RGBA color (red, green, blue, alpha, with alpha meaning transparency) onto the background-color of the navbar. Here is my code:
Codesandbox Sandbox
Or
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
header {
border: 5px solid black;
height: 400px;
background-image: url("https://images.unsplash.com/7/Top_view.jpg?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format");
/* background-size: cover; */
background-repeat: no-repeat;
background-size: 100% 100%;
}
div#whitespace {
height: calc(100% - 40px); /* 20 px is the height of the navbar */
}
nav {
display: flex;
justify-content: space-evenly;
align-items: center;
background-color: rgba(255, 255, 255, 0.35);
height: 40px;
}
a {
/* text-decoration: none; */
color: black;
font-weight: bold;
}
<header>
<div id="whitespace"></div>
<nav>
Home
Link 1
Link 2
Link 3
</nav>
</header>
What I am doing here is defining a header element with the background-image of url("https://images.unsplash.com/7/Top_view.jpg?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format"). Now url() is a CSS function that allows you to include a file, namely our header image. Then, we set background-size to 100% 100%, so the image will resize to fit the dimensions of our header and completely fill the header. We also set background-repeat to no-repeat so the image will not repeat (though it is unnecessary because the image already fills all of the header elements). Then, I add styles to the whitespace div, making the height of the div 100% (or all the height of our header) and subtracting the height of the div (40px), and I use the CSS function calc which allows me to do mathematical operations on some CSS units. So, by using this whitespace div, it pushes the navbar down to the bottom so it is placed correctly. Finally, for our navbar, I use flexbox to align the items and evenly space the links. Then, I set the background color to rgba(255, 255, 255, 0.35). This is rgb(255, 255, 255 which is white, but now we set the transparency (alpha) value to 0.35, making the color transparent so we can see the background image under the navbar.

Fallback image with CSS

I have an <img> that shows a remote image.
I want it to fallback to another local image, in the case where the remote one is not reachable.
<img class="cc_image fallback" src="http://www.iconarchive.com/download/i82888/limav/flat-gradient-social/Creative-Commons.ico">
.cc_image {
width: 256px;
height: 256px;
}
.cc_image.fallback {
/* this URL here is theoretically a local one (always reachable) */
background-image: url('https://cdn2.iconfinder.com/data/icons/picons-basic-3/57/basic3-010_creative_commons-256.png');
}
It works so that when the src image is not found then the background image will be shown.
The drawbacks are:
it will always load the background image (additional HTTP request)
it shows a little not-found-icon (a question mark on Safari) at the place of te original image, that is displayed above the background-image (not a big issue, but I'd like to get rid of it)
How could I solve these issues?
Or: are there other technics to achieve the same result?
I found this question but the given solutions rely on Javascript or on <object> (that seems to not work on Chrome). I would like a pure CSS/HTML solution, without Javascript if possible.
I know about the multiple background-image but am not sure whether it is a good option (browser support? and will it fallback with an unreachable image?).
Or I was thinking about embedding a SVG image as data-uri.
Suggestions for the most flexible (and compatible) method?
Unfortunately, you can't achieve both without Javascript or object tag.
You could do this to avoid the missing image icon:
Place your image in a container (it might already be in one).
Make the container have the same width and height as the image.
Set the fallback image as the background image of the container.
Set the remote image as the background image of your img tag.
Load an 1x1 pixel transparent png as the src of your image (see code for how that can be done without an extra HTTP request).
Code:
HTML
<!-- you could use any other tag, such as span or a instead of div, see css below -->
<div class="cc_image_container fallback">
<img class="cc_image" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" style="background-image: url(*your remote image*)"/>
</div>
CSS
.fallback {
background-image: url(*fallback image here*);
display: inline-block; /*to ensure it wraps correctly around the image, even if it is a a or span tag*/
min-width: specify a minimum width (could be the width of the fallback image) px;
min-height: specify a minimum height (could be the height of the fallback image) px;
background-position: center center; // fallback for older browsers
background-size: cover;
background-repeat: no-repeat;
}
.cc_image {
min-width: same as container px;
min-height: same as container px;
background-position: center center; // fallback for older browsers
background-size: cover;
background-repeat: no-repeat;
}
min-width and max-width make sure that the background images remain visible.
background-position makes sure that the central part of the images remains visible and is a graceful degradation for older browsers
background-size resizes the background image to fill the element background. The cover value means that the image will be resized so it will completely cover the element (some of the outer edges of the image may be cropped)
The base64 data in the img src tag is a transparent 1px png.
This will have an additional benefit that regular users and some bots may not be able to save your images (a rudimentary image protection)
The only drawback is, that you will still have one extra HTTP request for the fallback image.
I have found a solution on Codepen, which I would like to share with you:
https://codepen.io/anon/pen/Eqgyyo
I prefer this solution, because it works with real image tags, not background images.
body {
color: #2c3e50;
font-family: 'verdana';
line-height: 1.35em;
padding: 30px;
}
h1 {
margin-bottom: 40px;
}
ul {
list-style: none;
padding: 0;
}
* {
box-sizing: border-box;
}
img {
color: #95a5a6;
font-size: 12px;
min-height: 50px;
position: relative;
}
img:before {
background: #f1f1f1;
border: 1px solid #ccc;
border-radius: 3px;
content: '\1F517' ' broken image of 'attr(alt);
display: block;
left: 0;
padding: 10px;
position: absolute;
top: -10px;
width: 100%;
}
<h1>Broken image fallback CSS</h1>
<img src="no-image-here" alt="Cats with synthesizers in the space " />
<br /><br />
<ul>
<li>✓ Firefox</li>
<li>✓ Chrome</li>
<li>✓ Opera</li>
<li>✗ Safari (desktop, mobile)</li>
<li>✗ iOS webview</li>
</ul>

Is it possible to avoid blur when scaling down image with transform?

I have a background-image set on a div at 100%.
<div style="width: 990px; height: 742px; background-image: url('http://images.nationalgeographic.com/wpf/media-live/photos/000/913/cache/deadvlei-africa-namibia_91343_990x742.jpg'); background-size: 100%; background-repeat: no-repeat;"></div>
When I scale that image down by editing the width/height of the element, it retains its quality.
<div style="width: 247.5px; height: 185.5px; background-image: url('http://images.nationalgeographic.com/wpf/media-live/photos/000/913/cache/deadvlei-africa-namibia_91343_990x742.jpg'); background-size: 100%; background-repeat: no-repeat;"></div>
However, when I scale the image down using transform: scale, the image loses quality.
<div style="width: 990px; height: 742px; background-image: url('http://images.nationalgeographic.com/wpf/media-live/photos/000/913/cache/deadvlei-africa-namibia_91343_990x742.jpg'); background-size: 100%; background-repeat: no-repeat; transform: scale(.25);"></div>
Here is a demo of the results.
The image only seems to lose quality in webkit browsers. Chrome is worse than Safari.
Does anyone know why the image loses quality using transform: scale and if there is a way around it?
This question has been already asked. Simply set blur filter to 0.
Check -webkit-transform: scale / blurry images

How do you put space between an image and colored background

I am trying to put basically a border around an image of just white space then a colored background within the block element and caption for the image. Here is the page I'm trying to do this to http://www.metnews.org/news/aurora-remembers-holmes-victims/
thanks.
Your writing your border css wrong, look at tags in article.post .wp-caption img on line 1012 of style.css:
border: 5px #FFF;
Should probably read:
border: 5px solid #FFF;
When I add that in, the border shows around the images.
If the elements are constructed like
<div class="container">
<img src="...">
</div>
, you can style a white background around the image like this:
.container {
background-color: red;
}
.container img {
display: block;
padding: 5px /* to expose the white background */
background-color: white;
}

Centering an image for responsive website

So i am making a responsive design for a school assignment and i am trying to center an image width.
i scaled the image in photoshop so i don't have to set any width or height to the image
this is what i got now but this does not work
<header>
<img src="afb%20/logo3.png" alt="logo3" width="" height="" />
</header>
header{
width: 100%;
margin-left: auto;
margin-right: auto;
}
Can someone help me out ? really need this !
thanks in advance
Just set text-align: center in the header CSS.
You are currently making the header 100% width, so your margins wont apply. Furthurmore since your container is 100% wide and there is no text-alignment, the img will ALWAYS be aligned on the left.
Here:
header { width: 100%; text-align: center; }​
http://jsfiddle.net/xMPZ4/
.center{margin: 0px auto; display: block;}
<header><img src="updates_button.jpg" alt="logo3" class="center"></header>
You could add a logo class to the image and set the CSS to : margin:0px auto; width:200px; of course replace the image width...

Resources