Image behind the link - html

So I have a menu and on it there is a button with text and I want behind the text to be an image that shows that you are on the page and this is the code:
HTML:
<div id="menu">
<div id="about">About Us</div>
</div>
CSS:
a {
text-decoration:none;
color: white;
background: url(images/hover.png);
width: 100%;
height: 38px;
}
#about {
background: url(images/button.png);
width: 168px;
height: 51px;
font-family: Anivers;
font-size: 20pt;
text-align: center;
color: white;
line-height: 50px;
vertical-align: middle;
margin-top: 1%;
}
So far, so good, except that the image will only show the height and width that coresponds to the size of the text. For instance if I make the text 24pt, the image behind it will grow larger, but if I make it smaller, the image will become smaller and I don't want that. So how do I stop it from happening. I already searched all over the place, sadly I couldn't find similar topic. I hope you can help me :).

If I understand your question correctly you need to add display: block to the <a> element and set height: auto; instead. As for the image it should not scale anymore and I centered an image for demo purposes.
DEMO

You can accomplish this by displaying your "a" element as a "block". This will allow you to specify the size of the element independent from the size of the font. You can then inherit the width and height of the "#about" css styling if that's the size of "hover.png", or specify your own size based on the actual size of "hover.png" if its different than that stated in "#about", it sounds like 38px for hover.png is what you want as opposed to the 51px height of the #about parent. Without setting "a" as a block, the font size of the text in "#about", the parent element, would rule the overall size of the a element and styling, and your background "images/hover.png" will only provide a background for that size.
Here's what your a element in css would look like with the 38px height, you could also say "inherit" for the height if desired. I tested this and it works:
a {
text-decoration:none;
color: white;
background: url(images/hover.png);
display: block;
width: inherit;
height: 38px;
}
I hope this helps.

<div id="menu">
<img src="images/4.png" />
About Us
</div>
#menu {
position: relative;
width: 168px;
height: 51px;
}
img {
width: 100%;
height: auto;
}
img:hover {
background: blue;
}
a {
position: absolute;
z-index: 100;
/* top: 0; PLACE LINK CORRESPOMNDING TO IMG
left: 0; PLACE LINK CORRESPOMNDING TO IMG */
background: red;
font-family: Anivers;
font-size: 23pt;
color: white;
line-height: 1.2;
}

Related

How do I get my Logo to overlay the blue banner?

Can someone please help me how to overlay my globe logo over my blue horizontal bar? Thanks! I have attached a photo of how it looks. I do not want to lose the positioning or anything.
CSS
.logo {
display: inline-block;
width: 100px;
margin-left: 100px;
margin-top: 20px;
max-height: 100%;
}
.title {
display: inline-block;
font-size: 40px;
vertical-align: top;
margin-top: 50px;
font-family: arial;
}
#bannerTitle {
background: steelblue;
height: 60px;
position: relative;
margin-top: -50px;
background: linear-gradient(steelblue, steelblue, white);
}
h2 {
color: white;
padding-left: 120px;
padding-top: 11px;
font-size: 30px;
}
HTML
<img class="logo" src="img/globe.png" alt="">
<h1 class="title">The Inter<span>net</span></h1>
<div id="bannerTitle">
<h2>The World Wide Web</h2>
</div>
https://www.w3schools.com/cssref/pr_pos_z-index.asp
You need to add z-index
#bannerTitle{
background: steelblue;
height: 60px;
position: relative;
margin-top: -50px;
background: linear-gradient(steelblue,
steelblue, white);
z-index: -1;
}
It's as matti said; you need to consider the z-index variable. What z-index does is relayer elements within the same stacking context.
From your HTML markup, I can see that your <img> and <div id="bannerTitle"> are sibling elements, so they are within the same stacking context. Therefore, whoever has a higher z-index will display on top of the other.
One way to do that is to demote the "bannerTitle" div, as matti did: z-index:-1.
An alternative way is to promote the <img>: .logo { z-index:99; }.
It's good to know that z-index only applies to block elements, and img is inline by default, but you're already made it a block element with inline-block.

Why doesn't the background image show up without specific width and height?

Here's an example code
I've been wondering why the background-image doesn't show up unless I specific the image's width and height in pixels. I tried to specific only the width in percentage but it didn't work. The w3cschools.com was able to show the background image without specifying the width and height, but it works only in body background. Any explanation or solution workaround?
HTML
<div class="pic"></div>
CSS
.pic {
background: url("http://i.imgur.com/HIt6f8r.png") no-repeat;
display: block;
position: relative;
width: 244px;
height: 230px;
background-size: contain;
}
Your <div> element don't have any content, so the <div> height is 0px.
The width of the <div> is still 100%.
If you add any content to the div it will have some height and it will show a portion of image.
<body> by default has the height of the window, so you can see the background-image.
I found a great alternative without specifying the height, thanks to http://blog.brianjohnsondesign.com/maintain-aspect-ratio-for-html-element-using-only-css-in-a-responsive-design/.
HTML
<div class="pic"></div>
CSS
.pic {
background: url("http://i.imgur.com/HIt6f8r.png") no-repeat;
display: block;
position: relative;
width: 20%;
height: 0;
padding-bottom: 20%;
background-size: 100%;
}
All you need to do, assuming that it's a square, to match the padding-bottom to the width in css.
Update:
I also heard about another solution that may be useful. http://www.mademyday.de/css-height-equals-width-with-pure-css.html
CSS
.pic {
position: relative;
width: 50%;
}
.pic:before {
content: "";
display: block;
padding-top: 100%;
}
although I haven't tested it out yet....
<div class="pic"></div>
Div is container, it expects to have inner elements, when it's empty you must explicitly define height.
Your background image will not show because the div element has no content, this means that its height is 0.
You could use this jQuery code to make your div take the size of the window.
$(function () {
'use strict';
$('.div').height($(window).height());
$(window).resize(function () {
$('.div').height($(window).height());
})
});
If you don't specify height, the size of your div is given by the size of its contents, i.e. it's 0x0, so you don't have much chance of seeing a background image. Add
border: 1px solid red;
to see how large your div is (or isn't).
I am struggling with similar, trying to put text on top of image using css, but as I dont set height, it doesnt show. Have tried code above as well
.module5 {
background: url(image.jpg);
display: block;
background-size: 100%;
width: 100%;
height: 0;
position: relative;
float: left;
border: 1px solid red;
}
.mid h2 {
font-family: 'Roboto', sans-serif;
font-weight: 900;
color: white;
text-transform: uppercase;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
font-size: 2rem;
transform: translate(-50%, -50%);
}
And where pointing it:
<div class="module5 mid" style="width: 100%;">
<h2>My Text</h2>
</div>
So unless I set a height of module, it just shows a red line(my border for testing)

Adjusting image size using HTML and CSS

Hello I have made a website where my header adjusts to the size of the screen, but the contents inside the header are not adjusting. For instance, there is an image that is unable to adjust itself and ends up leaving the header to go under it once the browser window is resized. Font is also not adjusting.
I have already tried min-width and max-width but it is not working. I am using #media queries and was wondering if there is someway to use this query to make the contents inside the header to adjust themselves along with the header.
My <header> style is as follows:
header {
position: relative;
width: Auto;
height: 239px;
background: #FED93F;
}
Edit
What I have done is added a <header> tag and placed two images, along with a heading inside it. In the heading, the text does not adjust it self. In the image, the first image is a logo and the second is an image on the far right. The logo is placed somewhere at the left, with the text in the middle of it all.
<header>
<img src="images/Logo_image.png" style="margin-left:50px; margin-top:50px"/>
<h1>Company name</h1>
<p>Tag Line</p>
<img src="images/side_image.png" style="position: relative; align: right; float:right" />
</header>
When all this stuff is added, I want it to adjust itself to the size of the window just as the main header does. But it is not being so.
The CSS of the Text:
header h1
{
font-family: "Arial";
font-size: 72px;
color: #6A1111;
position: absolute;
left: 212px;
top: 78px;
}
header p
{
font-family: "Myriad Pro";
font-size: 21px;
color: #404041;
position:absolute;
margin: -35px 0 0 250px;
}
Try this:
header {
position:relative;
width: 100%;
height:239px;
background:#FED93F;
}
header img {
width: 100%;
height: auto;
}
The element inside your header should be 100% wide.
You should try like this -
header img {
max-width: 100%;
}
I hope it will helps you.
header {
position:relative;
width: 100%;
height:239px;
background:#FED93F;
}
header img {
width: 100px; /*Need give fixed width */
height: auto;
}

HTML / CSS Display fixed ratio images based on the height of the page

There is probably a relentlessly simple solution to this but I've been chasing my tail for a while so I've come to ask those wiser and smarter than me.
I've got a website for a personal project I'm making which displays images within a lightbox. See image:
The header area (red) is fixed height.
I want the images (yellow) to sit within a light box (green) which also has a caption. Crucially the images displayed need to retain their aspect ratio, 5:4, and fill the remaining height left below the header (bar a small margin top and bottom).
There's probably a really simple, elegant solution out there but I've not found it.
Any help gratefully received.
EDIT ---
Here's a fiddle of what I'm trying to do: http://jsfiddle.net/qh2V8/
Even this isn't right as I've had to put a fixed width in to even try and get it to work.
CSS:
#header{
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 145px;
background-color: #F00;
}
#overlayBg {
position: fixed;
top: 155px;
bottom: 20px;
padding: 8px;
padding-bottom: 30px;
left: 0px;
right: 0px;
margin-left: auto;
margin-right: auto;
background: #FF0;
width: 400px;
}
#overlayContainer img {
position: relative;
height: 100%;
width: 100%;
}
#overlayBg p {
position: relative;
padding-top: 8px;
padding-bottom: 10px;
font-family: 'Josefin Sans', sans-serif;
font-weight: 600;
font-size: 14px;
}
HTML:
<div id="header"></div>
<div id="overlayBg">
<div id="overlayContainer">
<img src="http://i.imgur.com/u9VIg60.jpg" />
</div>
<p>Caption</p>
</div>
The image size need to be set through scripting, unless the images are a fixed constant size. The following link is of good help to your problem: Change image size with JavaScript
I'm pretty sure that you can get the original size of the image through yourImg.Style.Height and yourImg.Style.Width, and then make the calculations required to make it a 5:4 picture..
Here's where I got to.
There are fixed ratio solutions if you are basing the size of the element on width, using :before and padding-top. There's a good write up here.
There is a fixed ratio solution if you are basing the size of the element on height, however the height must be a % of the height of the screen. Written up here in another Stackoverflow question:
Resize a Div Based on Height but Retain Aspect Ratio (almost got it) Strange Reload bug
If you have a fixed pixel size header or footer and need an element to expand to fill the exact size remaining you can't do it with just HTML and CSS.
Here's a codepen.io of where I got to:
http://codepen.io/niazipan/pen/ydkGt
JS to set the image height, and CSS to style everything else around it. Code below:
HTML
<div id="overlayBg">
<div id="overlayContainer">
<img src="http://i.imgur.com/u9VIg60.jpg" id="yourImgId" />
</div>
<p>Caption</p>
</div>
CSS
html, body {
height: 100%;
text-align: center;
}
#header{
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 50px;
background-color: #F00;
}
#overlayBg {
position: fixed;
top: 55px;
padding: 8px;
margin-left: auto;
margin-right: auto;
background: #FF0;
position: relative;
text-align: left;
}
#overlayContainer {
height: 100% !important;
width: 100%;
}
#overlayBg p {
font-family: 'Josefin Sans', sans-serif;
font-weight: 600;
font-size: 14px;
margin-top: 5px;
margin-bottom: 5px;
}
JS
var size = window.innerHeight - 120;
document.getElementById('yourImgId').style.height = size + 'px';
document.getElementById('overlayBg').style.width = size * 1.25 +'px';

Font breaks responsive design?

I'm having a problem with a design I'm coding that I intend to make creative. Right now, I have a header that has 3 separate areas, one div in the middle with 30% width and two on either side with 35%. In these I contain my header elements. The middle one contains my logo, which scales just fine. When I re size the window it scales down and maintains it's positioning inside the div.
The problem arises when I try to add the navigation on either side. I have the navigation text inside the first div currently, and it doesn't behave how I intend it to. I have it relatively positioned and it doesn't maintain that positioning. When the browser is re-sized the text remains fixed, so at some point, as the divs grow smaller, the text is completely outside it's containing div. Also, at some point the text begins to stack instead of maintaining it's in-line style. I have a separate style sheet that has a few font size media queries to remedy this, they simply drop the font size at certain intervals, but this doesn't do anything. The text gets smaller, but still remains fixed on re-size and breaks out as well as begins to stack.
I simply cannot figure out why this is happening. The logo image remains perfectly in it's div and maintains it's position whilst the text doesn't move at all. I guess I'm just not understanding text properties as well as I need to I've included the HTML and CSS below, I left out the media queries as it's pretty self explanatory.
<div id=container>
<div id=header>
<div id=headerone>
<div id=leftnav>
<ul>
<li>Home</li>
<li>Services</li>
</ul>
</div>
</div>
<div id=headertwo>
<img src="images/title.png">
</div>
<div id=headerthree>
</div>
</div>
and the CSS
html, body {
width: 100%;
height: 100%;
margin:0;
font-size: 100%;
padding:0;
}
img {
max-width:100%;
width: 100%;
}
ul {
list-style: none;
}
ul li{
text-decoration: none;
color: white;
display: inline-block;
padding: 20px;
}
#container{
width: 100%;
height: 100%;
}
#header{
background-size: auto 100%;
width: 100%;
height: 19.1%;
background-image: url(images/headerbg2.jpg);
}
#header img{
width: 70%;
position: relative;
left: 15%;
top: 30%;
}
#headerone{
width: 35%;
height: 100%;
float: left;
background-color:black;
}
#leftnav{
word-break:keep-all;
font-size: 1.5em;
position: relative;
left:10%;
top: 40%;
font-family:Rockwell Extra Bold, Georgia, "Times New Roman", Times, serif
}
#headertwo{
width: 30%;
height: 100%;
float: left;
}
#headerthree{
width: 35%;
height: 100%;
float: left;
background-color:green;
}
I appreciate any help.