I wanted to know if it is possible to have an image within the HTML document that behaves like a background image (currently done in CSS)?. The reason is so that the client can upload a background image via the CMS rather then myself having to manually do it for the client in CSS (which they do not have access to).
This is the effect I would like to create: http://codepen.io/anon/pen/wabjyg
HTML
<div>
<h1>An example of text over an image</h1>
</div>
CSS
div {
background: url(http://lorempixel.com/g/1100/300/) no-repeat;
width:100%;
height:100%;
background-size:cover;
}
One of doing it would be to have the background image as an inline style like this... http://codepen.io/anon/pen/aOrGYX - That way I can create a dynamic tag in the template which pulls the image path they upload in the CMS.
HTML
<div style="background-image: url('http://lorempixel.com/g/1100/300')">
<h1>An example of text over an image</h1>
</div>
CSS
div {
background-repeat: no-repeat;
width:100%;
height:100%;
background-size:cover;
position:relative;
}
Just wanted to know if there was another way of doing it without using inline styles within the HTML markup? Maybe tricking the IMG tag to behave like a background image?
You could use object-fit to get an img to behave like a background image:
html, body {
height:100%;
margin:0;
padding:0;
}
div {
background-repeat: no-repeat;
width:100%;
height:100%;
position: relative;
}
img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
h1 {
font-family:sans-serif;
margin:0;
color:#fff;
position:absolute;
letter-spacing:-1px;
text-align:center;
left:50%;
top:50%;
text-shadow:0 0 30px rgba(0,0,0,0.8);
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
}
<div>
<img src="http://lorempixel.com/g/1100/300">
<h1>An example of text over an image</h1>
</div>
After a bit Googling I found this tutorial that offers a different solution.
You can put a extra stylesheet in your header, for example background.php
<link rel='stylesheet' type='text/css' href='css/background.php' />
In that file you can set your variables and can use them in your stylesheet, like so.
<?php
header("Content-type: text/css; charset: UTF-8");
// DB Query here (or pass it with a function)
$background = "/img/user/background/1.png";
?>
body, html { background: url(<?php echo $linkColor; ?>); }
Hope it helps.
Related
I'm building a simple website right now and faced a small issue. How do I align picture (iMessage text with blue bubble) in the center of the screen so and place a text in the bottom left corner of the image (text is Read + time)?
<style>
html, body
{
height: 100%;
margin:0;
padding:0;
}
div {
position:relative;
height: 100%;
width:100%;
}
div img {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
}
</style>
<head>
<div>
<img src="myimage.jpg"></img>
</div>
</head>
But how do I add text in the bottom left corner right below the image?
Something like this might work for you...
<style>
html, body
{
height: 100%;
margin:0;
padding:0;
}
.centered{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<head>
<div class="centered">
<img src="myimage.jpg"></img>
<p>tester</p>
</div>
</head>
Although I completely agree with Temani, there are lots of resources on centring such as the links below:
css3 pr align-self
how to css image center
css align
Do you want to set your image in the center of screen Or in the center of a div?
If you want to set it in the center of screen horizontally then you shoud set
margin-left:auto;
margin-right:auto;
And if you want add text on image, you shoud set that image in the background of a div and add text in that div wherever you want
.imgdiv{ background: url(your IMG url) no-repeat center;
-webkit-background-size: contain;
-moz-background-size: contain;
-o-background-size: contain;
background-size: contain;
height: 150px; width: 300px;
}
.imgtxt{
left:0; bottom:0;
}
<div class="imgdiv" >
<span class="imgtxt">Your text</span>
</div>
I'm fairly new to web design and I've been working on a pure CSS layered parallax page, like a landscape effect with overlapping div containers stacked at different depths... and to my pleasure I got it working perfectly in Chrome.
However when I load it in Firefox (ver. 47) (using Windows 10) it looks fine until I scroll down. My divs and their content completely disappear after various points and reappear when I scroll back up. Alternatively I can resize the browser and everything reloads perfectly, but will disappear again if I continue scrolling.
In IE and Edge the content disappears after completely scrolling off the top and doesn't appear when I scroll back up. Only reappears after resizing.
I have tried adding the browser specific prefixes and what not and that didn't help at all. I also tried changing the perspective value as noted in a similar post but that didn't fix it either. What is causing this discrepancy between the presentation in chrome and the others?
Here is my code, I have replaced the div background images with colors.
Thanks
HTML:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<link href="../CSS/Styles.css" type="text/css" rel="stylesheet">
</head>
<body>
<img id="bannerlogo" src="http://placehold.it/350x65">
<div id="back1">
<p id="text1">Splash Text 1</p>
</div>
<div id="back2">
<p id="text2">Splash Text 2</p>
</div>
<div id="back3"></div>
<div id="back4"></div>
<div id="content"></div>
<div id="footer"></div>
</body>
</html>
CSS:
html {
height: 100%;
overflow:hidden;
}
body {
text-align:center;
margin:0;
padding:0;
perspective: 1px;
transform-style: preserve-3d;
height: 100%;
overflow-y: scroll;
overflow-y: visible;
overflow-x: hidden;
background-image: url(../IMAGES/Layer1.jpg);
background-size: cover;
background-attachment:fixed;
}
#text1, #text2{
position:relative;
font-weight:normal;
font-family:courier new;
}
#bannerlogo{
width:75%;
max-width:700px;
}
#text1{
left:-10%;
top:60%;
font-size:4vw;
}
#text2{
left:-5%;
top:60%;
font-size:4vw;
}
#content{
position:relative;
height:700px;
width:100vw;
background-color:rgb(3,0,20);
z-index:10;
}
#footer{
position:relative;
height:150px;
width:100vw;
background-color:rgb(30,30,50);
z-index:15;
}
#back1, #back2, #back3, #back4{
position:relative;
right:0px;
background-size:cover;
width:100vw;
height:100vh;
min-width:1280px;
min-height:720px;
background-size: 100% 100%;
text-align:center;
font-family:verdana;
font-size: 200%;
color:white;
font-weight:bold;
letter-spacing:5px;
line-height:100%;
transform-style:inherit;
min-height:100vh;
}
#back1{
background-color:rgb(100,100,150);
transform: translateZ(-5px) scale(6) translateY(0%);
z-index:-1;
margin-bottom:-30vh;
}
#back2{
background-color:rgba(100,150,100,.7);
transform: translateZ(-2px) scale(3.5);
}
#back3{
background-color:rgba(150,100,100,.5);
transform: translateZ(-1px) scale(2) translateY(-25%);
}
#back4{
min-height:0px;
height:30vh;
background-color:rgb(3,0,20);
transform: translateZ(-1px) scale(2) translateY(20%);
margin-bottom:-30vh;
}
I am in the process of making a website in HTML and CSS. I have a background image of me on the Home page. Despite my wild efforts of trying to figure it out, nothing I have tried will work. I have tried to not include the background with the main "Body". To be more clear I will include a Chunk of my CSS code that I have been trying to rotate, but instead it rotates the Whole website.
.site-wrap {
min-height: 100%;
min-width: 100%;
background-color: white;
position: relative;
top: 0;
bottom: 100%;
left: 0;
z-index: 1;
padding: 4em;
background-image: url("/Users/tjle------/Downloads/IMG_2057.JPG");
background-size: 100%;
}
This is only some of the code, but the image comes sideways when excuted. When I tried to rotate it, it would rotate the whole website which is not supposed to happen. Should I try to make the background separate from the site wrap? Thanks! Sorry if the question is hard to understand, please tell me if it is.
Here is the solution :
HTML :
<body>
<div class="bcg"> <!-- this is your background-->
<img src="http://img.theepochtimes.com/n3/eet-content/uploads/2013/06/Canada-Goose-PhotosCom-139956551-Janet-Forjan-Freedman-676x450.jpg"/>
</div>
<!-- The rest of your page -->
</body>
CSS:
body,html{
width:100%;
height:100%;
margin:0;
}
.bcg{
position:absolute;
z-index:-1;
width:100%;
height:100%;
max-width:100%;
max-height:100%;
overflow:hidden;
}
.bcg img{
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-ms-transform: rotate(90deg);
-o-transform: rotate(90deg);
transform: rotate(90deg); //the angle of rotation
margin:auto;
width:100%;
height:100%;
max-width:100%;
max-height:100%;
}
And here is working fiddle : https://jsfiddle.net/vxj4u5dy/2/
But this is not perfect solution so I'd suggest you to rotate your photo in image editing program and then apply it to your web page as original ;)
I am trying to call image using img
and this is my code:
<img src="/pc/images/leader.png">
is this possible to put this in css? like this code:
without using background-image.
css
.leader{
src: /pc/images/leader.png;
}
html
<img class="leader">
thanks :)
No. You can't do it with css.
CSS has no use of foreground images. You can only use background images.
However, if I understand your problem, you can do place the image using pseudo element:
.leader::after{
content: "";
background: url(path.jpg) no-repeat;
position:absolute;
top: 0;
left: 0;
width: 50%;
height: 300px;
}
.leader::after{
background: url(path.jpg) no-repeat 50% 50%;
}
if you use 50% 50% then the image will be at centre possition.
I have this site
https://preview.c9.io/pgonzalez/demo-project/html/test.html?_c9_id=livepreview0&_c9_host=https://ide.c9.io
The logo at the top is an image, I'm using this technique
h1{
position:relative;
}
span{
width:100%;
height:100%;
position:absolute;
background-image:url(Images/headertext.png);
background-repeat: no-repeat;
}
</style>
</head>
<body>
<header>
<h2>
<span></span>
The rainy season
</h2>
</header>
and it works as I expect. However, the same technique doesn't work here
https://demo-project-c9-pgonzalez.c9.io/html/API.html
You will see how the background image shows twice and in a completely different position, the code I'm using is the same
h1 {
position: relative;
}
span {
width: 100%;
height: 100%;
position: absolute;
background-image: url(Images/headertext.png);
background-repeat: no-repeat;
Can't figure out what is causing this. Thoughts?
It is because there is another span on the page.
You want:
h1 span {
width: 100%;
height: 100%;
position: absolute;
background-image: url(Images/headertext.png);
background-repeat: no-repeat;
}
And probably want to reference it more specific than that. You CSS will add that background to ANY span on the page. What I posted above will only affect spans that are children of h1 tags.
This is because one page header is defined as such, which doesn't work:
#headertest{
position:absolute;
width:100%;
height:100%;
background-image:url(../Images/developerstitle.png);
background-repeat:no-repeat;
}
And on the other page that this technique does works is defined as:
header{
color:white;
background-color:black;
padding:10px;
text-align:center;
background-image:url(Images/headerbackground.jpg);
background-attachment:fixed;
background-position:center;
}
My guess is that the Position statement must be changed/removed.