put a background image after text - css

I have a header element into which I want to put a background image but I want to put it on end. So whatever the width of text is it will remain always after the text ending. Possible? How?
<h1>Here is my dynamic text</h1>

If you want to put it really behind the text you should use pseudoelements:
h1:after { content:url(myimage.png); }
Sample here.
If you want to have a real background image you can only do this if you change the h1 to display:inline, since otherwise the element will stretch to the full width of its parent, thus losing all reference to the size of the contained text.
All other solutions (including the other ones mentioned here) require changing the HTML markup, and are as such not pure CSS solutions.

Something like this:
h1
{
background-image:url(https://s.gravatar.com/avatar/c6a0ac3e18f1cd8d0f1be4c2e3a4cfbd?s=32&d=identicon&r=PG);
background-repeat:no-repeat;
background-position:right;
padding-right:40px;
display:inline-block;
}
<h1>Here is my dynamic text</h1>
So backgorund image to the right with padding so it's always outside your text. The display:inline-block is important because it stops your test filling the whole line

Simply at a span for that background image as a child of <h1>, like this:
<h1>Here is my dynamic text <span class="chevron"></span></h1>
CSS:
h1 {
position: relative;
}
.chevron {
background: url(images/chevron.jpg) no-repeat 0 0;
width: xxpx; /* width and height of image */
height: xxpx;
position: absolute;
right: 0;
top: xxpx; /* adjust the position of the image to the heading text from the top */
}

You can use :after in your css like
h1:after
{
background:url(image path)no-repeat;/* apply your image here */
content:" ";
position:absolute;
width:999em;
height:25px;
margin:10px 0 0 5px;
}
look for example http://jsbin.com/uzorew/1

You could try adding div with background img
<h1>Here is my dynamic text <div id="dynamicimage"></div></h1>
#dynamicimage{
background-image: url("images/myimg.png");
}

may i sure tyr this css :
h1 {
height:25px;
overflow:hidden;
position:relative;
}
h1:after {
background:red;/* apply your image here */
content:" ";
position:absolute;
width:999em;
height:25px;
margin:0 0 0 5px;
}
your html :
<h1>Here is my dynamic text </h1>

Pseudo element background image
Although several answers have already suggested the use of CSS's :after, none (at the time of my posting) have shown how to handle the sizing of the image.
Below you'll see that the standard syntax for setting a background image can be applied to an empty content when (as shown in a few other answers) the position is set to absolute.
The width and height properties will set the dimensions of the after pseudo element, but the image size needs to be set via the background shorthand or background-size properties.
i.e. to set the image size to match the size of the after element, use:
background: url( path to image ) no-repeat center/contain;
where center/contain will center and contain the image ;-)
Note that in the snippet below, where the image size hasn't been set correctly, the results are obviously incorrect:
h1:after {
position: absolute;
/* no size or position settings */
background: url(https://lorempixel.com/200/200/) no-repeat;
width: 1em;
height: 1em;
margin-left: .5em;
content: "";
}
<h1>Here is my dynamic text</h1>
But in the following snippet, where the image size has been correctly set, the results are full of awesome:
h1:after {
position: absolute;
/* size and position set properly */
background: url(https://lorempixel.com/200/200/) no-repeat center/contain;
width: 1em;
height: 1em;
margin-left: .5em;
content: "";
}
<h1>Here is my dynamic text</h1>
To adjust the size of the after pseudo element itself, change the width and height properties like so:
h1:after {
position: absolute;
/* size and position set properly */
background: url(https://lorempixel.com/200/200/) no-repeat center/contain;
/* width and height of container reduced */
width: .5em;
height: .5em;
margin-left: .5em;
content: "";
}
<h1>Here is my dynamic text</h1>
And as pointed out by Tepken in his answer, use the top property to adjust the vertical alignment. However, make sure the after's parent also has its position property set, or the image will consider the top to be relative to one of its grandparents:
h1 {
/* the parent must have its position set also */
position: relative;
}
h1:after {
position: absolute;
/* size and position set properly */
background: url(https://lorempixel.com/200/200/) no-repeat center/contain;
/* width and height of container reduced */
width: .5em;
height: .5em;
margin-left: .5em;
content: "";
/* vertically align the image */
top: .35em;
}
<h1>Here is my dynamic text</h1>

Very simple
<div class="content"><h1>Header<img src="one.jpg"></h1></div>
include the image within the header

Related

How do I resize an image that's located INSIDE my CSS?

How do I resize the image?
I have tried using properties like height, width, etc, but this does not work. I can't seem to resize it if I pull it from inside the CSS
.project p::before {
content:url('logo.png');
position:relative; /* or absolute */
}
By default, pseudo-elements such as ::before have a display of inline, so size properties such as width and height do not apply to them. To correct this, you will need to set it to display: inline-block or display: block.
In addition to this, instead of using content to load the URL, you'll need to set content to an empty value and make use of background (or background-image) for the image itself.
This can be seen in the following:
.project p::before {
background-image: url('http://placekitten.com/100/100');
background-size: cover;
position: relative;
display: inline-block;
width: 200px;
height: 200px;
content: ''; /* Required */
}
<div class="project">
<p></p>
</div>
Make the content blank and put the image as the background. Then you can set the width and height of the pseudo element and manipulate the picture with background properties like size or position. More here https://www.w3schools.com/cssref/pr_background-position.asp
.project p::before {
content: "";
position: absolute;
width: 100px;
height: 100px;
background-img: url("some url here");
background-origin: center;
background-size: cover;
}

How can I prevent content of before psuedo element from overflowing its width and height?

How can I prevent content of before psuedo element from overflowing its width and height?
CSS styles for this ::before psuedo element is:
content: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAASCAYAAABFGc6jAAAAAXNSR0IArs4c6QAAAc1JREFUOBHtlE8oBHEUx9+bYVknFMrBRcmJk9pEuXLWXhQHJ/vHvyJOrJKLsGxbW3KghDMOTiiSE22UIncuFLLszPNm1/xm5mftn4uT32W+n/e+772Z329mEKRFgfFaoEQPELowujQrpbMi+UZbAbReUJU1XFk8t5uLTKBYrBgur2dAex/jmApAG2Yu15WCY/WgJdaBkjyIl4bbco1iBGhnR4XLq00gmmDkIfgAqmtKNmdi8o82gPZxAgTpIYBb0OE5lr3pJzo67WNjt0gihDAyfy84m9CTq5yuSVkQX0EtG0SvV5NLFAoul4BO1t0j6gAV67IxE1NguJPj7VaO9nBl7tFiSymg3fXz5tVZIbrF6PSLxVmUTiFHlpQLB9vAOKMuG7PEZydnJgpOVvGZtjiyCjw52AZ8RtRsY0aqpIHhoCNmgApxjCwdirj+2iS0KXRq41rxJqfD+icUle4i+YbeuLnb9Ga9urARw+Ebw0P+ES/o2o/X+Jf6MyXvIUaHJHlEI6IyoXMLT+o7yu37dpCCeXslY2GDpOJC8H9QIbvl8P7h1hk/wvxXwmbN7zeVKsAPfiJcAMSkrUFmiRgHdB+IZHX5PtfFBf8m0r3DX43yh79W6TLRAAAAAElFTkSuQmCC');
position: absolute;
left: 0;
top: 0;
transform: translate(-120%,-10%);
width: 1em;
height: 1em;
(the div to which this psuedo element belongs has position: relative;
Edit:
P.S.: I don't want to change the content of the psuedo element. Is it still possible?
Edit:
I want the pseudo element to be 16px x 16px without cutting the image out
Use scale to reduce the overall width/height and obtain what you want
.box:before {
content: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAASCAYAAABFGc6jAAAAAXNSR0IArs4c6QAAAc1JREFUOBHtlE8oBHEUx9+bYVknFMrBRcmJk9pEuXLWXhQHJ/vHvyJOrJKLsGxbW3KghDMOTiiSE22UIncuFLLszPNm1/xm5mftn4uT32W+n/e+772Z329mEKRFgfFaoEQPELowujQrpbMi+UZbAbReUJU1XFk8t5uLTKBYrBgur2dAex/jmApAG2Yu15WCY/WgJdaBkjyIl4bbco1iBGhnR4XLq00gmmDkIfgAqmtKNmdi8o82gPZxAgTpIYBb0OE5lr3pJzo67WNjt0gihDAyfy84m9CTq5yuSVkQX0EtG0SvV5NLFAoul4BO1t0j6gAV67IxE1NguJPj7VaO9nBl7tFiSymg3fXz5tVZIbrF6PSLxVmUTiFHlpQLB9vAOKMuG7PEZydnJgpOVvGZtjiyCjw52AZ8RtRsY0aqpIHhoCNmgApxjCwdirj+2iS0KXRq41rxJqfD+icUle4i+YbeuLnb9Ga9urARw+Ebw0P+ES/o2o/X+Jf6MyXvIUaHJHlEI6IyoXMLT+o7yu37dpCCeXslY2GDpOJC8H9QIbvl8P7h1hk/wvxXwmbN7zeVKsAPfiJcAMSkrUFmiRgHdB+IZHX5PtfFBf8m0r3DX43yh79W6TLRAAAAAElFTkSuQmCC');
position: absolute;
transform: scale(0.7);
width: 1em;
height: 1em;
}
<div class="box">
</div>
Using background would be the ideal solution:
.box:before {
content:"";
background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAASCAYAAABFGc6jAAAAAXNSR0IArs4c6QAAAc1JREFUOBHtlE8oBHEUx9+bYVknFMrBRcmJk9pEuXLWXhQHJ/vHvyJOrJKLsGxbW3KghDMOTiiSE22UIncuFLLszPNm1/xm5mftn4uT32W+n/e+772Z329mEKRFgfFaoEQPELowujQrpbMi+UZbAbReUJU1XFk8t5uLTKBYrBgur2dAex/jmApAG2Yu15WCY/WgJdaBkjyIl4bbco1iBGhnR4XLq00gmmDkIfgAqmtKNmdi8o82gPZxAgTpIYBb0OE5lr3pJzo67WNjt0gihDAyfy84m9CTq5yuSVkQX0EtG0SvV5NLFAoul4BO1t0j6gAV67IxE1NguJPj7VaO9nBl7tFiSymg3fXz5tVZIbrF6PSLxVmUTiFHlpQLB9vAOKMuG7PEZydnJgpOVvGZtjiyCjw52AZ8RtRsY0aqpIHhoCNmgApxjCwdirj+2iS0KXRq41rxJqfD+icUle4i+YbeuLnb9Ga9urARw+Ebw0P+ES/o2o/X+Jf6MyXvIUaHJHlEI6IyoXMLT+o7yu37dpCCeXslY2GDpOJC8H9QIbvl8P7h1hk/wvxXwmbN7zeVKsAPfiJcAMSkrUFmiRgHdB+IZHX5PtfFBf8m0r3DX43yh79W6TLRAAAAAElFTkSuQmCC') center/contain no-repeat;
position: absolute;
width: 1em;
height: 1em;
}
<div class="box">
</div>
If you use "overflow: hidden" on the :before element is won't overflow anymore and will cut off some of the image. Else it's better to use the image as a background image and set the width of the background image to 100%. play a little with the hieght and width of the element to make it in the right ratio
.box:before {
content: '';
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAASCAYAAABFGc6jAAAAAXNSR0IArs4c6QAAAc1JREFUOBHtlE8oBHEUx9+bYVknFMrBRcmJk9pEuXLWXhQHJ/vHvyJOrJKLsGxbW3KghDMOTiiSE22UIncuFLLszPNm1/xm5mftn4uT32W+n/e+772Z329mEKRFgfFaoEQPELowujQrpbMi+UZbAbReUJU1XFk8t5uLTKBYrBgur2dAex/jmApAG2Yu15WCY/WgJdaBkjyIl4bbco1iBGhnR4XLq00gmmDkIfgAqmtKNmdi8o82gPZxAgTpIYBb0OE5lr3pJzo67WNjt0gihDAyfy84m9CTq5yuSVkQX0EtG0SvV5NLFAoul4BO1t0j6gAV67IxE1NguJPj7VaO9nBl7tFiSymg3fXz5tVZIbrF6PSLxVmUTiFHlpQLB9vAOKMuG7PEZydnJgpOVvGZtjiyCjw52AZ8RtRsY0aqpIHhoCNmgApxjCwdirj+2iS0KXRq41rxJqfD+icUle4i+YbeuLnb9Ga9urARw+Ebw0P+ES/o2o/X+Jf6MyXvIUaHJHlEI6IyoXMLT+o7yu37dpCCeXslY2GDpOJC8H9QIbvl8P7h1hk/wvxXwmbN7zeVKsAPfiJcAMSkrUFmiRgHdB+IZHX5PtfFBf8m0r3DX43yh79W6TLRAAAAAElFTkSuQmCC');
background-size: 100%;
background-repeat: no-repeat;
position: absolute;
left: 0;
top:0;
width: 16px;
height: 16px;
}
It turns out it is not possible to contain the image inside content property of psuedo element since psuedo elements are anonymous replaced elements
from MDN:
In CSS, a replaced element is an element whose representation is outside the scope of CSS; they're external objects whose representation is independent of the CSS formatting model.
Put in simpler terms, they're elements whose contents are not affected by the current document's styles. The position of the replaced element can be affected using CSS, but not the contents of the replaced element itself.
Thus as the other answers suggest, using background CSS property will the solve the problem.
Removing the :before pseudo and using a background image at size 16px
blockquote {
margin:0;
max-width: 130px; /* demo only */
padding-left: 1.5em; /* space for quote icon */
background: 0 0.1em / 16px no-repeat url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAASCAYAAABFGc6jAAAAAXNSR0IArs4c6QAAAc1JREFUOBHtlE8oBHEUx9+bYVknFMrBRcmJk9pEuXLWXhQHJ/vHvyJOrJKLsGxbW3KghDMOTiiSE22UIncuFLLszPNm1/xm5mftn4uT32W+n/e+772Z329mEKRFgfFaoEQPELowujQrpbMi+UZbAbReUJU1XFk8t5uLTKBYrBgur2dAex/jmApAG2Yu15WCY/WgJdaBkjyIl4bbco1iBGhnR4XLq00gmmDkIfgAqmtKNmdi8o82gPZxAgTpIYBb0OE5lr3pJzo67WNjt0gihDAyfy84m9CTq5yuSVkQX0EtG0SvV5NLFAoul4BO1t0j6gAV67IxE1NguJPj7VaO9nBl7tFiSymg3fXz5tVZIbrF6PSLxVmUTiFHlpQLB9vAOKMuG7PEZydnJgpOVvGZtjiyCjw52AZ8RtRsY0aqpIHhoCNmgApxjCwdirj+2iS0KXRq41rxJqfD+icUle4i+YbeuLnb9Ga9urARw+Ebw0P+ES/o2o/X+Jf6MyXvIUaHJHlEI6IyoXMLT+o7yu37dpCCeXslY2GDpOJC8H9QIbvl8P7h1hk/wvxXwmbN7zeVKsAPfiJcAMSkrUFmiRgHdB+IZHX5PtfFBf8m0r3DX43yh79W6TLRAAAAAElFTkSuQmCC');
}
<blockquote>Charlie, from the lorem ipsum book has a nice dog called</blockquote>

Inside transparent arrow on the top

I would like to make a transparent arrow over an image. This triangle should be indented in a block and show the background image.
Desired output:
I used skewX() property that #web-tiki had explained here but I want it to display on the top of the border rather than on the bottom of image and have this issue:
A fiddle demo of my code is available here
Can anyone tell me why it's not working in my case?
As stated in the question, your case is a bit different from the example that was provided by web-tiki. In the example that you were referring to, the border with the transparent cut was included as the bottom border for the image whereas you need it as the top border of the plain text area.
The expected output can be achieved with the same skew technique described in that answer. However, it needs to be tweaked a bit to match your case.
First thing is, the skewed pseudo-elements (that produce the border) should be added to the container of plain text area and not the top section which holds the image. This part you have already done correctly.
Next, you need to position the border such that even with the border the height of your text container will be equal to the other two images placed by its side. For this, you need to position the elements that form the border within the plain text container (top: 0%) instead of above it (bottom: 100% in your code).
Then, if the text container has a non-transparent background, you need to clip it such that it is not present behind the elements that is creating the border effect. This can be achieved by adding a padding-top on the text container equal to the height of the border pseudo-elements and then setting background-clip: content-box to it.
Finally, you need to move the entire bottom part up by the same number of pixels as the height of the border in order for the top image to be seen through the transparent cut out area. This can be done by adding a negative margin-top to the bottom container.
Putting it altogether your code should be similar to the below snippet to achieve the effect that you need. (Note: Your fiddle has way too much code and so I have created a simpler sample for the demo).
.section {
height: 200px;
width: 500px;
background: url(http://lorempixel.com/500/200/nature/3);
}
.bottom-container {
margin-top: -15px;
height: 100px;
width: 500px;
}
.text,
.middle-image,
.right-image {
float: left;
height: 100%;
width: calc(100% / 3);
}
.middle-image {
background: url(http://lorempixel.com/200/100/nature/2);
}
.right-image {
background: url(http://lorempixel.com/250/100/nature/1);
}
.text {
position: relative;
box-sizing: border-box;
height: 100%;
padding-top: 15px;
text-align: center;
line-height: 85px;
background: #F7F7F7; /* Just for demo */
background-clip: content-box; /* needed only if your background is not transparent */
overflow: hidden;
}
.text:after,
.text:before {
position: absolute;
content: '';
top: 0px;
height: 15px;
background: rgb(215,182,115);
}
.text:before {
left: 0px;
width: 25%;
transform-origin: left bottom;
transform: skew(45deg);
}
.text:after {
right: 0px;
width: 75%;
transform-origin: right bottom;
transform: skew(-45deg);
}
<!-- prefix free library to avoid browser prefixes in CSS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<section class="section">
</section>
<div class="bottom-container">
<div class="text">Some text</div>
<div class="middle-image"></div>
<div class="right-image"></div>
</div>
Screenshot:
Note: The images that are displayed when the snippet is executed could be different from those in the screenshot because they are random placeholder images

How to effectively float an image or background image

I need to dynamically display 2 graphic image files that represent opening and closing quotes as shown below in the sample screen shot.
The quotes need to appear just to the left and to the right of the upper content block as shown. Content block widths will vary on the page.
I have tried float and background image. Does anyone have a tip or trick to properly, dynamically and flexibly position 2 image files?
Here is what I have so far after working with #Utkanos answer:
HTML
<div class="postsPage_item_content postsPage_item_quote"><?php the_content();?></div>
CSS
div#maincontentcontainer div#primary div div.postsPage_item_content {
position: relative;
text-align: center;
}
div#maincontentcontainer div#primary div div.postsPage_item_quote::before, div#maincontentcontainer div#primary div div.postsPage_item_quote::after {
background-image: url('../images/QUOTE1.png');
content: '';
display: block;
left: 20%;
height: 28px; /* background-image natural height is 28px */
position: absolute;
top: calc(50% - 50px);
width: 36px; /* background-image natural width is 36px */
}
div#maincontentcontainer div#primary div div.postsPage_item_quote::after {
background-image: url('../images/QUOTE2.png');
left: auto;
right: 20%;
}
Display
Desired results are that (1) each of the dynamically rendered quotes align with the top of the content block, and (2) the quotes dynamically position with margin padding to the left and right of the content block as shown by the red arrows.
Pseudo elements are perfect for this sort of thing.
HTML:
<div id='my_div'>
<p>Content here.</p>
<p>Etc.</p>
</div>
CSS:
#my_div {
position: relative;
}
#my_div::before, #my_div::after {
content: '';
width: 50px;
height: 50px;
position: absolute;
display: block;
background: url('path/to/open_quote_img.png');
left: 5%;
top: calc(50% - 25px);
}
#my_div::after {
background: url('path/to/close_quote_img.png');
left: auto;
right: 5%;
}
That code assumes your quote graphics are 50px in width and height - modify as required.
Finally, to ensure your content doesn't overlay the quote images, set an appropriate padding-left and padding-right on the container (in my example, the div) so the content is sufficiently pushed in away from them.
Another possibility is using absolute positioning inside a relative container. For example:
.container { width:300px; position:relative;padding:20px}
.left-quote {position:absolute; top:10px; left:10px; font-size:30px;}
.right-quote {position:absolute; bottom:20px; right:10px; font-size:30px;}
<div class="container">
<span class="left-quote">"</span>
<span class="right-quote">"</span>
<p>is one of the smartest and most dedicated people that I know... he helped the company achieve incredible share of voice in key publications such as...</p>
</div>

Override/Remove background image from parent element

Scenario
html
<div id='fractal'>
<div class='centerdotline'>
<span>Some text</span>
</div>
</div>
css
#fractal {
background-image:url('fractal.jpg');
background-repeat: repeat;
}
.centerdotline {
background-image:url('dotline.png'); /* with alpha channel */
background-repeat: repeat-x;
background-position: center;
}
.centerdotline span {
padding: 0 20px;
/*
override centerdotline background-image
*/
}
I want to remove centerdotline div ( parent ) background-image but not fractal div background-image.
I can't set the background-image in this element (like a piece of fractal.jpg) because I don't know the element's exact position in relation to the fractal div
Thanks for help
Marco
Perhaps This Workaround
Not knowing specifically what your parameters are, the following solution may or may not be useful to you. I offer it, however, in case it is useful for your situation.
Here is the fiddle example. HTML is just as you had it. I removed the line-height on the span that you said was not necessary. If you were doing that, you would need to adjust the pseudo-element height accordingly below. That height should roughly match line-height, which is typically about 1.1 or 1.2, but if set to 500px would need to be that height for the pseudo-elements.
CSS
#fractal {
background-image:url('http://marcomolina.com.br/lab/fractal.jpg');
background-repeat: repeat;
width:500px;
height:500px;
}
.centerdotline {
position: relative; /* using this to place the dots */
text-align:center;
}
/* your span can also be set to display: inline-block and have top padding or margin */
.centerdotline span {
padding: 0 20px;
/*
Did not remove, but skipped centerdotline background image by not putting the background there to keep the background of fractal div behind the text
*/
}
/* put the dots in pseudo-elements and position off .centerdotline edges */
.centerdotline span:before,
.centerdotline span:after {
content: '';
position: absolute;
height: 1.2em; /* this might vary slightly by font chosen */
width: 40%; /* this will vary by text size */
background-image:url('http://marcomolina.com.br/lab/dotline.png'); /* with alpha channel */
background-repeat: repeat-x;
background-position: center;
}
.centerdotline span:before {
left: 0;
}
.centerdotline span:after{
right: 0;
}

Resources