What I am trying to achieve seems relatively simple, but I can't seem to get it to work.
I want to have my article previews on my website appear in tiled form.
The tiles, for the sake of the argument would be a fixed height and width. Lets say 300px by 300px.
I want then for the title of the article and perhaps even a short excerpt to appear, overlaying the image. Kind of like what theverge.com have.
What I need help with is that Im just trying to do a proof of concept mock up. I can do the specific styling fine myself but its literally just the structure I cant seem to figure out.
I cant seem to get the h1 to overlay the img.
I've tried creating a parent container div, and then containing both elements within separate div containers and giving the container with the h1 or "post info" absolute positioning.
But It never seems to work out quite right.
HTML:
<div class="container">
<div class="feat-img">
<img src="www.sample.com"/>
</div>
<div class="post-info">
<h1>Post Title</h1>
</div>
</div>
CSS:
.container: {width: 300px; height:300px;float:left;}
.feat-img img: {width:300px; height:300px; float:left;}
.post-info: {position:absolute;bottom:0px;}
Ok so I know there is a lot wrong with that style but I just did it off the top of my head there. It has the general jist of my train of thought.
Any help would be greatly appreciated as I havent found anything (Probably becuase I dont really know what Im searching for)
First, you need to know how an absolute div relates to a relative one.
Add
.feat-img {
position:relative;
height:300px;
width:300px;
}
to your CSS,
and place the .post-info div inside the .feat-img div:
<div class="feat-img">
<div class="post-info">
<h1>Post Title</h1>
</div>
<img src="image.jpg"/>
</div>
apply this CSS:
.post-info {
position:absolute;
bottom:0px; /* or whatever position */
left:0px; /* or whatever position */
}
Please have a look at this jsFiddle for a quick mockup: http://jsfiddle.net/ZJT6f/
Cheers,
Jeroen
look on this:
demo
html code:
<div class="container">
<div class="feat-img"><img src="http://lorempixel.com/300/300/"/></div>
<div class="post-info"><h1>Post Title</h1></div>
</div>
css code:
*{margin:0; padding:0}
.container: {width: 300px; height:300px; display:block; position: relative;}
.feat-img img: {width:300px; height:300px; position:absolute; top:0; left:0; display:block;}
.post-info{position:absolute; top:130px; left:0; display:block; width:300px; height:300px; text-align: center; color:#fff;}
Related
New member approaching.
I'm new at HTML and CSS. I'm a designer and i got amazed by HTML and CSS, i'm studying methods of position elements inside a div. And recently I came across some problems. I was trying to make the elements fit the spaces above, but it seems like there's a line dividing the two parts of the blocks. Here's an image to illustrate what I'm experiencing.
Problem
My markup is:
<section style="max-width:470px; background-color:green;">
<div class="bloco1">
hello
</div>
<div class="bloco2">
hi
</div>
<div class="bloco3">
hey
</div>
<div class="bloco3">
I want this elements to fit those spaces above
</div>
<div class="bloco1">
I want this elements to fit those spaces above
</div>
<div class="bloco2">
I want this elements to fit those spaces above
</div>
And CSS is:
.block1 {
display:inline-block;
width:150px;
height:200px;
background-color:gray;
}
.block2 {
display:inline-block;
width:150px;
height:100px;
background-color:yellow;
}
.block3 {
display:inline-block;
width:150px;
height:180px;
background-color:blue;
}
So, I've tried to mess with position but it didn't served well. What should i do?
I'm sure this is super simple, but I'm new to css. I'm essentially trying to position some rendered typography and make it stay centred no matter what the size of the browser is. I've tried using margins with percents, but that doesn't seem to work.
Here's my code.
html
<div class="weare">
<img src="image/textrenders/weare.png" />
</div>
<div class="shaftesburytv">
<img src="image/textrenders/Shaftesburytv.png" />
</div>
<div class="awebbasedstudio">
<img src="image/textrenders/awebbasedstudio.png" />
</div>
css
.weare {}
.shaftesburytv {}
.awebbasedstudio {}
I want the result to look something like this
Any help would be appreciated.
Simplify your content:
<div id="container">
<img src="http://placekitten.com/200/50">
<img src="http://placekitten.com/300/100">
<img src="http://placekitten.com/250/75">
</div>
Then ensure the container has the same width as the largest contained image, and apply margin:0 auto; to it to center. Finally put display:block on the images to make them all stack vertically:
#container {
margin:100px auto;
width:300px;
}
#container img {
display:block;
}
Sample here.
Alternatively, if you also want to center vertically, you can also use absolute positioning and then negative margins on the absolute size of the object - no problem for you since the image sizes are fixed:
#container {
margin-left:-150px;
margin-top:-112px;
left:50%;
position:absolute;
top:50%;
}
#container img {
display:block;
}
Sample of this approach here.
Since you're using images, you could
margin: 0 auto;
to them. For text, you could
text-align:center;
With divs, you could also center align them (in HTML).
You could also use center tags: http://jsfiddle.net/A33J2/
It can be verry simple.
If you do not split your image and gather all text of it into one.
html
<img id="my-whole-image" src="http://placekitten.com/300/250" />
css
#my-whole-image {
margin-left:-150px;
margin-top:-125px;
left:50%;
position:absolute;
top:50%;
display:block;
}
jsFiddled here
Just a tip, ence you're saying you are new to css, i presume you are new to html too : always use the minimum required to build your webpages. Those 3 images had to be merged into one for many reasons like server request, bandwidth, browser redraw, dom elements number.
I'm trying to create a website for a gaming community. The website is supposed to be very minimalist, with the Header/Navigation covering 100%x20% of the page and the primary content covering 100%x80%. I get the divs to show up, but I cant seem to make them position themselves correctly. Due to me having to use "position:absolute;" I cant really figure out how to make these things work right. Keep in mind I'm pretty new to HTML/CSS.
<!DOCTYPE html>
<html style="height:100%;">
<head>
<title>ESI | Missioners and Miners Haven</title>
<link rel="stylesheet" type="text/css" href="styles/styles.css" />
</head>
<body style="height:100%;">
<div id="contianer">
<div id="navbar">
<div id="logobox">
<p>ESI Official Site</p>
</div>
<ul>
<li>Sign Up!</li>
<li>ESI Blog</li>
<li>Corp Op's</li>
<li>Home</li>
</ul>
</div>
<div id="content">
</div>
</div>
</body>
</html>
Thats the HTML, heres the important part of the CSS:
#container
{
width:100%;
height:100%;
}
#navbar
{
width:100%;
height:20%;
background:#35291F;
list-style:none;
float:left;
position:absolute;
}
#content
{
margin:10%;
width:90%;
height:80%;
background:grey;
position:absolute;
}
I know this question has been asked many times but none of the solutions I've found on this site worked.
Appreciate any help, thanks.
margin:10% will give 10% to all (top bottom right left).
give margin-top
and there is no need of float if position is absolute.
You can't specify a height with a % that I know of. You'll likely want to use px for this. What you should be asking yourself is, what is 100% to you? Because the height of the window is just as variable as its with. When you specify percentages in CSS, those elements contain that percentage of their parent container. You're not going to want to position those elements absolutely. You also don't need to specify the height of the body or html elements. Without seeing what exactly you're trying to accomplish visually, it's difficult to tell you what your values should be, but start there.
If you want to use absolute positioning for this you need to set top appropriately to move #content down.
Couple of other things:
Don't set styles inline (your html and body tags). Use your stylesheet.
Why the float on #navbar? That element is already positioned absolute.
The margin on #content applies to all directions (also top and bottom).
JS Bin Demo
Is this what you wanted?
#content
{
margin-top:20%;
width:100%;
height:80%;
/* ... */
}
http://jsfiddle.net/WJrW3/
http://jsfiddle.net/rainthinks/t23bD/1/
full screen mode: http://jsfiddle.net/rainthinks/t23bD/1/embedded/result/
html, body, #container {
height:100%;
}
#navbar {
height:20%;
background:#35291F;
list-style:none;
}
#content {
height:80%;
background:grey;
}
I am trying to place a child div over the top of its parent div (including its content)
<div id="footer">
<div id="footer-container">
<div id="icon"></div>
</div>
</div>
#footer {
height:50px;
border-top:3px #666 solid;
margin-top:50px;
}
#footer-container {
height: 30px;
width: 300px;
margin-left: auto;
margin-right: auto;
margin-top: -15px;
}
#icon {
height:30px;
width:30px;
background-color:#666;
}
Now it works if the content of <div id="icon"> is text but if you place a background image in the div it does not. Is there any way to make this work? This maybe explains it better
http://jsfiddle.net/4QxL7/
EDIT
Apologies. It was working all along. I was using PNG's for the images which have 'white-space' in the middle which made the border (which is the same color) in the parent div look like it was going over the top of the child, its is in fact it is going behind.
Thanks for your help
I just tried two methods and they both worked using an oversized image from my site...
<div id="footer">
<div id="footer-container">
<div id="icon"><img src="image url here" width=30 height=30/></div>
</div>
</div>
http://jsfiddle.net/ZkxSM/
and
#icon {
height:30px;
width:30px;
background-color:#666; /*unnecessary now probably...*/
background:url('image url here');
}
http://jsfiddle.net/b6QyX/ (image needs to be resized before hand for this to work maybe... or width and height can be set in the html of the div)
There's nothing actually wrong with your jsfiddle..
Apologies. It was working all along. I was using PNG's for the images which has 'white-space' in the middle which made the border (which is the same color) in the parent div look like it was going over the top of the child, its is in fact it is going behind.
Thanks for your help
i'm stuck in a bit of a rut at the moment.
if you head over to my website...
http://dekkro.no-ip.org/
You will see an image, and a div underneath it. I have spent the past few hours trying to position these both in the middle of the page, with the bottom div over lapping the image.
I have failed and failed, what am I doing wrong here? I have used z-index, and positioning but its never centered.
Thanks!
All you need is this:
CSS:
#my_img {
margin:auto;
display:block;
}
#my_div {
margin:auto;
position:relative;
top:-100px;
z-index:2;
}
HTML:
<img id="my_img" src="http://dekkro.no-ip.org/images/testimage.png" />
<div id="my_div">whatever</div>
Here's a demo: http://jsfiddle.net/tX2JY/embedded/result/
Wow, that's alot of code going on for that page.
Without reviewing everything yet (will in a bit) - have you tried setting the main image to be a background image of a div, and then simply putting the log in form inside it?
CSS
#image-div { background-image:url('yourimage.jpg'); }
HTML:
<div id="image-div">
<div id="form-div">
form...
</div>
</div>
Lets say one div has width w1 and height h1 ; other w2 , h2
#div1 {
position:absolute; top:50%; left:50%; margin-left: - w1/2px; margin-top:-h1/2px;
}
#div2 {
position:absolute; top:50%; left:50%; margin-left: - w2/2px;
margin-top:-h2/2px; z-index:9999;
}