placing an image and text hyper lin over each other - html

I have the code below to display the image and the code as a hyperlink. But I would like the image placed behind the text and person can click on either
<h1>test</h1> <img scr="#"/>

Use CSS to achieve this:
test
Here you can see more info about background-image: https://developer.mozilla.org/en-US/docs/Web/CSS/background-image
You have syntax error in your code, you must use <h1> outside the <a> tags, like this:
<h1> test </h1>

You can also use position: absolute so the text will be on the top of the image.
HTML
<a href="#">
<h1>test</h1>
<img src="http://spi3uk.itvnet.lv/upload2/articles/54/542197/images/Jauka-vasara-8.jpg"/>
</a>
CSS
a {
position: relative;
}
a h1 {
position: absolute;
z-index: 100;
color: #FFF;
}
Demo Fiddle

I recommend you to use background:
<h1>test</h1>
But if you want, you can use another variant (negative margin):
<h1 style="height: 24px;">test</h1><img style="margin-top: -24px;" src="..."/>
Or absolute position:
<h1 style="position: absolute; z-index: 9;">test</h1><img src="..."/>
P.S. You have some typos in code: scr -> src and <\h1> -> </h1>

Well, it depends how you'd be using this.
Either set is as a background image instead of adding an img-tag.
But that would mean you'd have to set the height. Otherwise your image would crop.
...or you can set the (wouldn't use h1 in this case for semantic reasons) span with the text to:
CSS
span {
display: block;
position: absolute;
}
and then position it wherever you want inside the image.

Related

How to add title on the background img in html

in the picture is my code and what shall I add to make "Lanlan's Dinner" show on the background1.jpg? This code is what the textbook saying about how to add a background img under your title. But in this code, only the picture shows, the title does not show.
Thank you
You could so something like this. Place the heading inside a div and set the background of the div to an image as suggested. Then set the position of the heading to relative and have the ability to adjust the text placement on the image.
I think this is what you wanted? No js is really needed for this.
.header {
background: url(http://via.placeholder.com/200x50) no-repeat;
font-size: 14px;
width: 200px;
height: 50px;
}
.header h1 {
position: relative;
top: 20px;
left: 2px;
}
<div class="header">
<h1>
Hello, world
</h1>
</div>
To have an image as a background, use the background-image CSS property (or the background property). Note the associated background-* properties, many of them are useful to arrange the image (e.g, I'm using background-repeat below so it won't tile the element).
h1 {
background: url(http://via.placeholder.com/200x50) no-repeat;
}
<h1>
Hello, world
</h1>
What if we use the follow code?
<figure>
<img
src="http://via.placeholder.com/200x50"
alt="An awesome picture">
<figcaption>Text below the image.</figcaption>
</figure>
<figcaption> is recommended for these types of situations, so you should make a use of it.
Edit; since the question was, how to add background image above title, here's how you do it:
<figure>
<figcaption>Text above the image.</figcaption>
<img
src="http://via.placeholder.com/200x50"
alt="An awesome picture">
</figure>
If you want show title on the picture in html, you must do something:<img src="images/background1.jpg" alt="Lanlan's Dinner" title="Lanlan's Dinner" width="700" height="700" />.
The ALT attribute is used to display the value when the image is not displayed.
Best,
thanhnt
What you put in the alt attribute is an alternative text. It is shown if the image is not loaded (not yet or not at all). It is also used by search engines to "understand" what is in the image.
If you want to display text on your page, you have to put that text inside your h1 markup:
<h1><img src="image.jpg">your title</h1>
and then apply some styles to display that as you want:
h1{
position:relative;
}
h1 .title{
position: absolute;
/*set that values to position your title, you can also use right and bottom, regarding on how you want to place your title */
left: 0;
top:0;
}
<br/>
<br/>
<br/>
<h1>
<img src="https://via.placeholder.com/200x50"/>
<span class="title">your title</span>
</h1>

Connect "div" and "p" with CSS

I want to put text in "p" into center of my "div". My html is like this:
<div class="picture">
<img src="/sites/default/files/default_images/colorful-triangles-background_yB0qTG6.jpg" width="1080" height="1920" alt="background to view" typeof="foaf:Image">
</div>
<p class="textik"">Hello handsome.</p>
But i cant change html tags. I can only us CSS. Is there a way to do this? I tried some ways, but none seems to work.
The only way to do this without changing the HTML is to artificially align the p tag above the div tag like so.
p.textik {
margin-top: -5px;
}
Adjust as needed, and test for mobile devices.
If you don't want the elements underneath to move up as well, you can use either padding-bottom to prevent this.
p.textik {
padding-bottom: 5px;
}
I'm assuming you actually want to make the p tag appear in the centre of your image, so you can position it absolute and transform it's location using css. By using absolute positioning and transforming the location you don't need to reposition should the size of your image/outer elements change. Something like:
<div class='someOuterElement'>
<div class="picture">
<img src="http://placehold.it/500/500" width="500" height="500" >
</div>
<p class='textik'>Hello handsome.</p>
</div>
and style it with:
.someOuterElement {
position: relative;
width: 500px
}
.textik {
text-align; center;
position: absolute;
top:50%;
left: 50%;
transform: translate(-50%, -50%)
}
There's more info about css transform here

Text not show when write on images

I have two images, one on the left and one on the right. I am facing issues in writing text onto both images.
I am pasting HTML code and CSS code. Please correct it.
#container {
width: 400px;
height: 400px;
position: relative;
}
#image {
position: absolute;
left: 0;
top: 0;
}
#text {
z-index: 999999999;
position: absolute;
color: white;
font-size: 24px;
font-weight: bold;
}
<div id="header">
<img src="images/banner-img1.jpg" class="left">
<p id="text"> jhcjdhdjshdjdhjd</p>
<img src="images/banner-img2.jpg" class="right">
<p id="text"> jgdhdgdhddhhsd</p>
</div>
Both of your paragraphs refer to the same ID, but ID's must be unique. Try using a class here instead.
.text {
...
}
and
<p class="text"> ... </p>
Also, you use class="left" and class="right" on your image tags, but these classes don't yet exist in your css code.
Create a container for the image and make it’s position relative. (to get along with the rest of the page)
Place the image within the container.
Place the text immediately below the image within the container and make it’s position absolute (to overlay text on the image) and place it as you please.
Finally, add some CSS tricks to suit your needs.

Why image is not displaying upon defining background: url() rule in CSS but is displaying when adding 'src' attribute to <img> tag?

I would like to know why images are not displaying after adding
.image_container img {
background: url(http://domainname.com/something_something_2014_something/something_something_1320_something/something_something.jpg);
width: 900px;
height: 500px;
cursor: pointer;
}
in inline CSS. This little set of rules is supposed to style
<div class="image_container">
<img>
</div> <!-- end image_container -->
The image is not showing up but when I delete the
background: url()
and add
src=""
to an
<img>
tag then the image is displaying properly.
Can someone explain to me this phenomenon ? I thought that it looks pretty logic, I set up an element inside a div - and set up definitions of this element in section that is more appropriate for it - inline CSS.
I've double checked if CSS is placed properly inside
<head>
tags as well as double checked the directory where image is being placed.
You can not add an background to an img tag.
(in fact you can but the background-image will show up behind your image in the src-Tag. You can use this for special effects if you have a semi transparent image)
Solution
Try to add the background to your div instead: http://jsfiddle.net/aoy95s75/
HTML:
<div class="image_container">
<img src="">
</div> <!-- end image_container -->
CSS:
.image_container {
background-image: url(http://www.imag.de/images/10_welt_startseite.png);
width: 900px;
height: 500px;
cursor: pointer;
display: block;
}
In this case you can drop your <img>-Tag and just write
<div class="image_container"></div> <!-- end image_container -->
Use another div instead of img tag and change your css accordingly (.img-container .img) :
<div class="image_container">
<div class="img"></div>
</div> <!-- end image_container -->
I think you miss coma ,
replace your css with this
.image_container img {
background: url('http://domainname.com/something_something_2014_something/something_something_1320_something/something_something.jpg');
width: 900px;
height: 500px;
cursor: pointer;
}
for reference checkout this Link
hope this help..
For all poor souls still trying to understand this phenomenon - type :
display : block;
and magically the image appears :)
However, please follow a simple rule that I've learnt upon researching internet - HTML is for content, CSS is for styling it. Never the other way.

How to keep <img> over text, without hiding the text and using CSS background or text-indent?

See js fiddle here http://jsfiddle.net/Ws8ux/
Is it possible to keep the text under logo without hiding it using display:none or text-indent? I want to bring the image up and keep logo behind it. Like is PSD layers. And Don't want to use Logo Image as a CSS background
<a href="/" title="Return to the homepage" id="logo">
<img src="http://lorempixum.com/100/100" alt="Nike logo" />Logo Text
</a>
Like this (fiddle)?
HTML:
<a href="/" title="Return to the homepage" id="logo">
<img src="http://lorempixum.com/100/100" alt="Nike logo" /><span>Logo Text</span>
</a>
CSS:
a { display: block; position: relative; }
a span {
display: block;
position: absolute;
top: 40%;
}
What's the purpose of keeping the text if it's to be hidden? If your goal is to hide the text underneath the image for the purposes of accessibility, you may be interested to know that most search engines won't fault you if you just leave the text as an alt attribute on your image. In contrast, you might find some techniques for deliberately hiding content could prove detrimental to your cause.
If it's important to have both the image and text present, you may want to try wrapping the text in a <span>, using an accessible style on that and then disabling it in your print stylesheet.
#jitendra; may be you have to play with css:
CSS:
a { position:relative; }
img { position:absolute; top:0; left:0 }
HTML:
<a href="/" title="Return to the homepage" id="logo">
Logo Text<img src="http://lorempixum.com/100/100" alt="Nike logo" />
</a>
check the fiddle may that's help your http://jsfiddle.net/sandeep/Ws8ux/11/
You can do this by setting position: absolute for the image. You should probably also make sure the anchor is the same size as the image, so that it doesn't break the layout of other elements on the page.
img {
position: absolute;
}
a {
display: inline-block;
height: 100px;
position: relative;
width: 100px;
}
The updated fiddle: http://jsfiddle.net/Ws8ux/7/