Using CSS, Can I specify a background-image outside that object? - html

I have an object like this:
<div class="myObject">My Object</div>
I want to display a image in front of that <div>'s box, I thought code like this will work:
.myObject {
background-image: url(foo.png);
background-position: 0px -20px;
}
But, unfortunately, this does not work.
How can I accomplish this goal using CSS?
PS: What I want is just to display that image outside that tag's box, don't tell me to do like this:
.myObject {
padding-left: 10px;
background-image: url(foo.png);
background-position: 0px 0px;
}
Thank you.

You can't position a background image on an element outside the box.
To place an image to the left of an element, using pure CSS, the :before pseudo-element can be used:
Demo: http://jsfiddle.net/2HEpn/
.myObject:before {
content: " ";
background-image: url("/favicon.ico"); /* a 16x16 image, for example */
width: 16px;
height: 16px;
display: inline-block; /* pseudo-elements are inline by default */
}

you might need to place it relativly inside the object as i understand from u to the div
#img{
position:absolute;
top:0px;
left:-30px;
}
.myobject{
position:relative;
width:30px;
}

Related

Replace text with stars (*) in html with directives in angularjs

I need a textarea control with mask able property, if the textarea is mask able then the text should appear as stars instead of actual text.
I can have any no of textareas in my form, So i can't save actual text in other variable and save the stars or dots for actual textarea.
Can somebody help me to solve this issue?
As others have already pointed out, it's not possible and should not be done. But here is something which you should give a try. If you really want to achieve it, you'll have to compromise on something. Use contenteditable div instead of input and use following CSS:
Demo: http://jsfiddle.net/GCu2D/793/
CSS:
.checked {
font-size:20px;
position:relative;
display:inline-block;
border:1px solid red;
}
.checked:before {
font-size: inherit;
content:" ";
position: absolute;
top: 0;
bottom: 0;
left: 0;
background-color: #FFF;
width: 100%;
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Asterisk.svg/32px-Asterisk.svg.png");
background-repeat: repeat-x;
z-index: 1;
background-size: 12px;
background-position: left center;
}
HTML:
<div contenteditable class="checked">Sample Text</div>
Obviously, this is not a perfect solution, but you can start from here.
Note: You will need to adjust the font-size and the image used. Both dimensions needs to be in sync. Ofcourse you can change the size of image using background-size . Border here is just for visual feedback. If you need to adjust the width of the stars, then you may use calc() and play around with the exact dimension.

Why HOVER on image behind the the normal image?

I have a problem with hover. I have a normal pic with opacity and another one with white background. I did this:
html:
<div id="glob" style="margin-right:5px;margin-top:2px;float:right;height:45px;width:40px;">
<img src="images/icon_globus.png" width="32" height="33" alt="" style="margin-right:5px;margin-top:7px;"/>
</div>
css:
#glob:hover{
background:url('../images/icon_globushover.png') no-repeat;
}
As you can see from the pic, the white one (upper globus) is behind the other one. It should not be behind.
What is not good?
EDIT: Thanks all, it's working. I did like the first answer. Don't know why I used img tag. I always use divs as image, but here I was stupid. Thanks a lot all for your help!
Because background always has the lowest z-index of all elements and content will be on top of it. I would suggest you to put "images/icon_globus.png" this as a background for div glob and remove that image tag. Then it should work the way you want it.
try without the image part.
glob {
background: url('icon_globus.png');
}
glob:hover {
background: url('icon_globus_hover.png');
}
Depending on the browsers you want to support, you could do something like this in CSS:
#glob { position: relative; }
#glob:hover:after {
content: '';
display: block;
position: absolute;
z-index: 1000;
left: 0;
top: 0;
right: 0;
bottom: 0;
background:url('../images/icon_globushover.png') no-repeat;
}
Because your image icon_globus.png is above your div background so when you hover, you actually apply a background to the container containing your image, you globushover appears behind your ..
try this instead:
html:
<div id="glob></div>
css:
#glob {margin-right:5px;margin-top:2px;float:right;height:45px;width:40px;background:url('../images/icon_globus.png') no-repeat;}
#glob:hover{
background:url('../images/icon_globushover.png') no-repeat;
}
this way you change/overwrite the default background-image. css means cascade style, cascade is wat permits you to overwrite styles depending on a certain condition, thats theory applied here. just overwrite a background-image with another one on hover.
remove all style and change as below
// CSS
glob:hover{background: url("/images/icon_globushover.png") no-repeat;}
.glob{
background: url("/images/icon_globus.png") no-repeat;
float: right;
margin: 0px 0px 15px 20px;
overflow: hidden;
padding: 0px;
width: 32px;
height: 33px;
}
<div class="glob"></div>
if your path source is like this
myhardrive/MysiteFolder/Images/ and your index.html is in MysiteFolder so in css must be (/images/)
if your path source is like this
myhardrive/MysiteFolder/files/mystyle.css here you do ("..\images\")

In CSS, how can I make a vertically centered line which takes up the remaining horizontal space after a block element?

I am implementing a design which contains elements which are semantically headers (h3, h4, etc.), are full-width block elements, and are followed by a vertically centered horizontal line which extends the full width of the header a la:
I know I could take care of this by wrapping each header in a <div> and inserting another block-level element after it but would prefer not to pollute my HTML like that. My first inclination was to use a ::after element on the headers, like so:
.line-header::after {
content:'\00a0';
display: inline-block;
float: right;
width: 55%;
margin-top: -12px;
border-bottom: 1px solid gray;
}
However, this requires making the ::after element fixed-width, which obviously doesn't work with different-width headers: http://jsfiddle.net/nbSTf/
Any ideas on how to get a variable-width line which will fill the space to the right of the header, without inserting extra elements into the HTML?
EDIT: Tyriar's answer below (suggesting drawing the line full-width behind the header and setting the background-color to erase the line behind the text) reminded me that this is taking place in front of a repeating background image - so no background tricks are possible unfortunately.
Here's a method you could do it with, unfortunately it relies on an attribute (not necessarily title), this could easily be set programmatically with JavaScript though.
It uses :before and :after pseudo elements, :after is used to draw the line the full width and :before is used to draw white background behind the text of the size of the text. The pseudo-elements need their z-index set correctly to layer in the correct order.
jsFiddle
HTML
<h3 class="line-header" title="Longer Header Looks Good">Longer Header Looks Good</h3>
<h3 class="line-header" title="Short Doesn't">Short Doesn't</h3>
CSS
.line-header {
margin-top: 15px;
position:relative;
}
.line-header:before {
position: absolute;
display:block;
top:0;
left:0;
bottom:0;
content: attr(title);
background-color:#FFF;
color:#FFF;
z-index:-1;
padding-right:.5em;
}
.line-header::after {
position:absolute;
content:'';
display: block;
left:0;
right:0;
top:50%;
background-color:#555;
height:1px;
z-index:-2;
}
Try this for your CSS:
EDIT:
body { background: red; overflow-x: hidden }
.line-header {
margin-top: 15px;
margin-right: 5px;
display: inline;
z-index: 100;
float: left;
clear: left;
}
.line-header::after {
position:absolute;
content:'\00a0';
width: 100%;
margin-top: -12px;
margin-left: 5px;
border-bottom: 1px solid gray;
z-index: 0;
}
I only tested this in Chrome. Here's the Fiddle.
Well, how about doing it in reverse? Draw the lines (in any way you like) then use :after to insert the headers, no additional HTML, but some extra CSS.
HTML
<div id="header1"></div>
<div id="header2"></div>
To Draw the Line, simply border-top-style:solid;.
To Write the headers, :after.
CSS
div
{
position:relative;
border-top-style:solid;
margin-top:20px; /* just for spacing */
}
div:after
{
display:block;
background-color: #FFFFFF;
position:absolute;
top:-10px;
left:0px;
}
#header1:after
{
content:"Hello;"
}
#header2:after
{
content:"Hello Again";
}
You'll get something like this.
For more customization, you can enlarge the font, change the colors, add padding (for more white space), just change top the way it suits you.

How to show an image on html page using only css?

I want to show images on the page but I don't want to hardcode the references to the images in html.
Is it possible to do something like:
HTML:
<span id="got-easier"></span>
CSS:
#got-easier { image: url(/i/trend-down.gif); }
(IE6 should be supported)
Yes, use a background image :)
#got-easier { background-image: url(/i/trend-down.gif); }
Remember to set a span to display: block; and set width/height of your image if you use it.
As David Dorward pointed out, if it's an image relevant to the information, it should be included in the document with an <img> tag and alt attribute.
Heya, the common term for it is css Image Replacement technique (or IR). Here are the commonly used methods currently. Just choose any of the two ;)
/* Leahy Langridge Method */
span#imageName {
display: block;
height: 0 !important;
overflow: hidden;
padding-top: 0px; /* height of image */
width: 0px; /* width of image */
background: url(url/of/image.jpg) no-repeat
}
/* Phark Method */
span#imageName {
display: block;
width: 0px;
height: 0px;
background: url(url/of/image.jpg) no-repeat;
text-indent: -9999px
}
In case you want to display the images inline, position:absolute does the trick:
#got-easier {
display:inline;
position:absolute;
width:img-Xpx;
height:img-Ypx;
background:url(/i/trend-down.gif) no-repeat;
}
The only problem with this is that, since the image position is absolute, it will overlay whatever is next to it (in IE6 it might appear behind), and the workarounds that I found to fix this (with both CSS and jQuery) aren't supported in IE6. Your image-container will have to be followed by new line.
This might be useful when, for instance, you'd like to place a (?) image next to a form caption or a button (that usually have nothing next to them) to display help with onmouseover.

How to declare a link without any content?

I want to declare a link without any content, instead I want to use a background image for it. But I can see the background only when I put something between <a></a> tags. How can I solve it?
Make the link a block-level element, and give it a width and height:
a.somelink {
display: block;
width: 100px;
height: 20px;
background-image: url(someimage.png)
}
Or just use an <img /> inside the <a> instead of using a background-image.
A link should always have text, whether it is direct text content or the alt tag of an image. You can use a negative text-indent style to hide the text from view, replacing it with the image. Example:
check out my important stuff
#important-link {
background: transparent url(../images/important-stuff.png) no-repeat top left;
display: block; /* needed as <a> tag is inline by default */
height: 100px; /* whatever image width is */
text-indent: -9999px; /* moves the text off the screen */
width: 100px; /* whatever image height is */
}
This is a common technique for image replacement where specific fonts are needed, while preserving accessibility (mostly, CSS+no images is the only caveat) and SEO benefits from the text.
Best practice and SEO friendly CSS Text Replacement With Images:
http://css-tricks.com/css-image-replacement/
To use a background image, give it a style of display: block; or display: inline-block; depending what you're after.
Example HTML:
CSS:
a.ImgLink {
display: block;
background: #FFFFFF url(img.jpg);
width: 200px;
height: 100px;
}
/* Add a hover if you want */
a.ImgLink:hover {
background: #FFFFFF url(imgHover.jpg);
}
What about this:
<a href="#> </a>
then remove the possible underline with the following css:
a.somelink {
display: block;
text-decoration:none;
background-image: url(someimage.png)
}