this is the html
<div id="tab-1">
</div>
her is the css
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
demo
How to display background-image? If I use background-color then it works but why not background-image? And even if sometimes works in jsfiddle but not in my localhost.
You must take out the no-repeat from the background-image as you are using a short hand syntax inside background-image property which is not valid, inorder to use short hand syntax, you need to use background property instead
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: " ";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Demo (Separating background-repeat if you want to keep background-image)
Demo 2 (CSS Short hand syntax using background property)
Check out : http://jsfiddle.net/6nDKP/4/
Instead of :
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
Use :
background: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
Separate the no-repeat
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Fiddle: http://jsfiddle.net/6nDKP/1/
The reason its not working is that your are including no-repeat in background-image style.
Exclude it to background-repeat: no-repeat;. Then it will work fine. Here is the code:-
#tab-1:before{
background-image: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw");
background-repeat: no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Change background-image with background.
#tab-1:before{
background: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
content: "";
display: block;
height: 100px;
position: relative;
top: 8px;
width: 500px;
}
Demo: http://jsfiddle.net/zyuWW/
Related
As per other SO solutions, I have tried giving explicit fixed dimensions to the pseudo element (height: X px; width: X px;) but even if this worked, I want a scaleable, responsive image which fits its parent element, so hence fixed dimensions are not acceptable to me. How can I get the background-image to show up and be automatically scaleable at the same time?
HTML:
<h3>This is a title</h3>
CSS:
h3:before{
background-image:url('https://store.ashenglowgaming.com/wp-content/uploads/2018/02/cropped-agg-store-logo-4-FULLSIZE-1.jpg');
background-size: contain;
background-repeat: no-repeat;
display: block;
}
Try this
h3:before{
background-image: url(https://store.ashenglowgaming.com/wp-content/uploads/2018/02/cropped-agg-store-logo-4-FULLSIZE-1.jpg);
background-size: contain;
background-repeat: no-repeat;
content: '';
height: 110px;
display: block;
}
try adding these styles to your pseudo element
content: "";
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
I have added content: ''; and specified a height and also min-width for the pseudo element.
h3:before {
content: '';
background-image: url('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTMdl8cg3CDenBgBdDey8W2KUfDIfKO7ZcPk6c5KtuLn8OJBzu8');
background-size: contain;
background-repeat: no-repeat;
display: block;
min-width: 150px;
height: 40px;
}
<h3>
This is a title
</h3>
You can use background in h3 itself and use background-size to adjust the size.
Stack Snippet
h3 {
background-image: url(https://store.ashenglowgaming.com/wp-content/uploads/2018/02/cropped-agg-store-logo-4-FULLSIZE-1.jpg);
background-size: auto 40px;
background-repeat: no-repeat;
display: block;
padding-top: 40px;
}
<div class="wrapper">
<h3>This is a title This is a title This is a title This is a title This is a title This is a title This is a title This is a title This is a title</h3>
</div>
I'm trying to get these two objects fixed on the users screen. Please note that I can only modify this with using CSS so the HTML can't be edited!, this is a CSS zen garden example (based on the '90s) I'm trying (which means in short you make a design based on a fixed html file so you can 'show off' what CSS is capable off.)
You can find a live example here.
http://lucasdebelder.be/zengarden/index.html
I got the top fixed and working with the following syntax.
body::before {
content: '';
position: fixed;
width: 100%;
height: 12.5%;
background: url(header_browser.svg);
background-repeat: no-repeat;
background-attachment: fixed;
z-index: 5000;
background-size: 100%;
}
I then tried the ::after statement on the body. But that doesn't work how can I get the bottom image (footer) sticked to the bottom?
body::after {
content: '';
position: fixed;
width: 100%;
height: 12.5%;
background: url(footer_browser.svg);
background-repeat: no-repeat;
background-attachment: fixed;
z-index: 5000;
background-size: 100%;
}
Tell your ::before pseudo element to go up the top at 0.
Tell your ::after pseudo element to go down the bottom at 0.
body::before {
content: '';
position: fixed;
top: 0;
width: 100%;
height: 12.5%;
background: #0f0;
background-repeat: no-repeat;
background-attachment: fixed;
z-index: 5000;
background-size: 100%;
}
body::after {
content: '';
position: fixed;
bottom: 0;
width: 100%;
height: 12.5%;
background: #f00;
background-repeat: no-repeat;
background-attachment: fixed;
z-index: 5000;
background-size: 100%;
}
Why is my background-image inside the pseudo element ::before not showing up? I also tested of replacing the background-image with a background-color and it still doesn't work. This is in SASS format in case some would be wondering of the nested ::before.
.logoframe{
float: left;
height: 817px;
width: 20%;
position: relative;
left: -6%;
transform: skewX(-11deg);
border: 1px solid #e26f6f;
&::before{
content: "";
background-image: url('/images/theseven/seven_img_old.png');
background-repeat: no-repeat;
position: relative;
height: 817px;
width: 150px;
}
}
<div class="logoframe"></div>
the "display" property. display is CSS's most important property for controlling layout. Every element has a default display value depending on what type of element it is. The default for most elements is usually block or inline . A block element is often called a block-level element.
&::before{
content: "";
display: block;/*missing prop*/
background-image: url('/images/theseven/seven_img_old.png');
background-repeat: no-repeat;
position: relative;
height: 817px;
width: 150px;
}
Sometimes you need to add background-size property too with display: block.
&::before{
content: "";
background-image: url('/images/theseven/seven_img_old.png');
background-repeat: no-repeat;
background-size:100%;
position: relative;
height: 817px;
width: 150px;
display:block;
}
You should update below css part. if you need background image in center please update background-position.
.logoframe{
float: left;
height: 817px;
width: 20%;
position: relative;
left: 0;
transform: skewX(-11deg);
border: 1px solid #e26f6f;
}
.logoframe:before {
content: "";
background: url('https://n2.sdlcdn.com/imgs/a/a/1/Chromozome_Yamaha_102025_m_1_2x-4ab77.jpg') 0 0 no-repeat;/* replace 0 0 to center center */
background-repeat: no-repeat;
position: absolute;
background-size:contain;
top:0;
left:0;
height: 817px;
width: 100%;
}
<div class="logoframe"></div>
I don't know if this helps but sometimes if you are using the shortcuts for the background property it might not work, but if you use the properties differently I think it might work. I am saying this from experience.
.showcase::before {
content: '';
background-image: url(../images/Desert.jpg) no-repeat center center/cover;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: -1;
}
But this one did.
.showcase::before {
content: '';
background-image: url(../images/Desert.jpg);
position: absolute;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: -1;
}
Want to make hr like this img1
and
img2
don't known how to achieve this.
I'm pretty sure that using an <hr> is not the recommended way to do that. You could just try:
<div><img src='http://i.stack.imgur.com/Llk2U.png'><div>
Or use a css class to make it a bit more elegant:
.whybuyfromus
{
background: url('http://i.stack.imgur.com/Llk2U.png') no-repeat;
height: 101px;
background-size: contain;
}
<div class="whybuyfromus" />
I know that this already has an accepted answer but I wanted to see if it was possible to make a fully responsive version with CSS. I found a solution using flexbox:
.fancy-hr {
display: flex;
background-image:
url(http://i.imgur.com/ZmheWg5.png),
url(http://i.imgur.com/ph3e3OT.png);
background-size: 115px 100%;
background-position: 0 0, 100% 0;
background-repeat: no-repeat;
height: 37px;
box-sizing: border-box;
padding: 0 115px;
}
.fancy-hr:before,
.fancy-hr:after {
content: "";
flex: 1;
background: url(http://i.imgur.com/NBus6Hr.png) repeat-x;
}
.fancy-hr span {
display: inline-block;
height: 100%;
line-height: 37px;
}
.fancy-hr span:before,
.fancy-hr span:after {
content: "";
width: 10px;
height: 100%;
display: inline-block;
}
.fancy-hr span:before {
background-image: url(http://i.imgur.com/wMU1oDn.png);
float: left;
}
.fancy-hr span:after {
background-image: url(http://i.imgur.com/4Q2el3J.png);
float: right;
}
<div class="fancy-hr"><span>Text here</span></div>
try this one:
.hr {
background-image: url(http://i.stack.imgur.com/Llk2U.png);
height: 100px;
background-repeat: no-repeat;
background-size: 100%;
}
<div class="hr" />
I have two divs. First acts as a banner of sorts. The next is just a small div that I'm trying to place directly below the first div. I've tried taking away float and adding clear: both. Perhaps I'm missing something? Below is my html and css
<div id="background">
</div>
<div id="us">
</div>
#background
{
width: 100%;
height: 10%;
position: absolute;
left: 0px;
top: 0px;
border:1px solid #000;
background-color:black;
background-image: url(resources/images/****.png);
background-repeat: no-repeat;
background-position: center;
clear: both;
}
#us
{
display: block;
width: 165px;
height: 200px;
left: 0px;
align-top: auto;
position: absolute;
background-image: url(resources/images/*****.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
The first div does appear at the top of the page and displays correctly. The second one appears over top of the first div. Any advice?
Check this out.
Fiddle
Just add top:10%; to your #us because you are using position:absolute.
The size of your top in #us must be the same size with your height in #background. I also added box-sizing:border-box; for you borders not to take space.
try this one
#us
{display: block;
width: 165px;
height: 200px;
left: 0px;
align-top: auto;
**margin-top: 50px;**
background-image: url(resources/images/*****.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
You have used position: absolute; in CSS of second div(#us) that's why it is showing on top of first div. Change that to position: relative; or delete that line.
And you are ready to go.