What is the easiest way (using css) to position several separate text items over the top of a single image?
I've got the following:-
<div>
<img src="image.jpg"/>
<p>One</p>
<p>Two</p>
<p>Three</p>
</div>
with the CSS:-
div {
position: relative;
}
div p {
position: absolute;
top: 0;
left: 0;
}
But that doesn't work because all of the text items are on top of one another.
Is the best way to do it with a list? Or is there another way?
Something like this:
div { position:relative; }
div img { position:absolute; top: 0; left: 0; }
div p { position:relative; z-index: 2; }
But this way the div will take the height of the text, not the image.
DEMO
div {
position: relative;
}
div img {
position:absolute
}
div p {
position: relative;
top: 0;
left: 0;
color:#fff;
}
UPDATE
OK, that seems to work - but how do I make sure the text items are all
contained within the confines of the image? Is that even possible?
Ideally, I'd like to make the text items appear in two parallel lists.
DEMO
You'd need to set the size of the container div to that of the img. From then, you can control how you want to handle the overflow.
You can use float:left; width:50%; on your p so they display in two columns.
Related
Here's my scenario: I have a few divs that must stack on top of each other. Each div will have a background color (or texture.) Each of the divs has another div nested inside of it. The parent div's color or texture extends the entire width of the screen.
Problem: When adding the second div, it appears above the first.
See what I'm talking about at: http://staging.ontempoideas.com/bvcil
It looks something like this...
HTML:
<div id="1P">
<div id="1C">
</div>
</div>
<div id="2P">
<div id="2C">
</div>
</div>
CSS:
#1P {
position: absolute;
left: 0;
right: 0;
background-color: blue;
}
#1C {
width: 920px;
margin-left: auto;
margin-right: auto;
}
#2P {
position: absolute;
left: 0;
right: 0;
background-color: green;
}
Any thoughts?
To be honest, I'm not sure exactly what you're after from your description, but I put some code here that may help: https://jsfiddle.net/JTBennett/hksxgncv/1/
These are the positions you want to be using here:
position:relative;
position:inherit;
Your two parent divs are set at the same exact absolute position. If you want to clarify anything, I can update it for you.
I'm trying to contain 2 images and some text to a div. I have it positioned the way I'd like, but when adding it to my site it's positioned in the top left corner.
How do I get it so it sits in a div by itself under the rest of my content and doesn't move to the top left of my website?
I created a fiddle with my code: http://jsfiddle.net/43qahfsn/2/
Would using percentages instead of pixels make a difference? Or is there some better way to do this?
#box {
width:1200px;
height:700px;
}
.text, .stripe, .photo {
position: absolute;
text-align: center;
}
.text {
color:#000;
top: 50px;
left: 250px;
}
.stripe {
z-index: 1;
top: 0px;
left: 0px;
}
.photo {
top: 400px;
left: 600px;
}
You need to make the positioning of the absolutely positioned elements relative to their parent. In your case wrap them in a div and apply position:relative; to it.
.container {
position:relative;
}
<div class="container">
<!-- your current html --->
</div>
Demo http://jsfiddle.net/43qahfsn/5/
I have a parent-child div relationship with the child intended as a horizontal scrollbar attached to the bottom edge of the parent.
The parent is a vertically growable/shrinkable container for rectangular strips that are added/deleted by the user interactively.
How do I force the scrollbar to adhere to the parent's bottom edge?
Here is my current css situation:
.parent-div {
position: absolute;
left: 0;
top:80px;
right: 0;
}
.horizontal-scrollbar-div {
position: absolute;
top: 0;
left: 0;
bottom:-50px;
height:50px;
width: 100%;
z-index: 500;
}
This is not working correctly. At runtime strips get added the scrollbar remains at the top edge of the parent (initially there are no horizontal strips so the parent has 0 height).
What are the css changes I need to make here?
Thanks,
Doug
Marc's answer is right, but in order for it to work you need to add "position: relative;" on the ".parent-div". Otherwise the ".horizontal-scrollbar-div" would position itself according to the body element instead of ".parent-div".
Here is how I would do it. You can change the height of parent and the scrollbar will always stay at bottom of the parent-div.
.parent-div {
position: relative;
height:200px;
background-color:#aaa;
}
.horizontal-scrollbar-div {
position: absolute;
bottom: 0;
height:50px;
width: 100%;
background-color:yellow;
}
<div class="parent-div">
<div class="horizontal-scrollbar-div"></div>
</div>
I would try the following:
.horizontal-scrollbar-div {
position: absolute;
left: 0;
bottom: 0;
height:50px;
width: 100%;
z-index: 500;
}
You want the bottom edge of .horizontal-scrollbar-div to be pinned to the bottom edge
of the parent container.
Note: You may not need the z-index property.
Also: You may need a minimum height to .parent-div.
I have a centered div and was wondering how can i attach a div on its right,
there is a title DIV on top, then the yellow centered DIV and this SOCIAL SHARING DIV I'd like to attach on the right.
Thank you!!!
Add it inside the yellow div, and position it as follows:
#yellowdiv { position: relative; }
#sidebar { position: absolute; left: 790px; top: 10px; }
It would be perfectly feasible to use the yellow div as the parent element for the brown div; the social data is all relevant info to the video. In that case, if you want, use the following:
#video {
position: relative;
}
#brown {
position: absolute; top: 0; left: 100%; /* this guarantees that it'll line up at the very end of #video */
}
Demo
http://jsfiddle.net/KXvpV/1/
Code
HTML
<div id="one"></div>
<div id="two">
<div id="social"></div>
</div>
CSS
#social { position: relative; top: 20px; right: -201px; }
Try making the yellow div position:relative, put the sidebar div inside it and make it position:absolute with values of top:0 and right:-XXX where XXX is the width of the sidebar plus the margin you require.
I want to position divs on top of one another. I have created a fiddle. I have three divs. Each div contains an image. I want to position divs so that the div having the largest image is at the bottom and the one having smallest image is on top.
http://jsfiddle.net/bobbyfrancisjoseph/7spcZ/2/
http://jsfiddle.net/bobbyfrancisjoseph/7spcZ/2/show/
Just use this:
#page1 div {
position:absolute;
}
Demo: http://jsfiddle.net/ZwyTY/
Use position absolute with z-index.
use below css
#page-left {
float: left;
margin:5px;
}
#page-right {
float: left;
margin:5px;
}
#largest {
position: absolute;
z-index: 1;
}
#medium {
position: absolute;
z-index: 2;
}
#smallest {
position: absolute;
z-index: 3;
}
http://jsfiddle.net/sannankhalid/sc8qE/
Just use position : absolute;
#largest, #medium, #smallest{
position: absolute;
}
http://jsfiddle.net/w4WBk/
If you want to change the order of what comes on top, you can use z-index
You can use z-index & position: absolute to treat the elements as layers. But as per your requirement and element order, a simple position: absolute is enough
#page1 div {
position:absolute;
}
Couldn't you just change the order of the div's? Either that or target each one with position relative.
you can only define the absolute position into your main div that will automatically call the images as orderd or coded in your HTML here is no-need to use z-index in your css as per current your requirement.
CSS
#page1 div {
position:absolute;
}
But if you will change the order of #largest,#medium,#smallest div's in your HTML code than you will have to define the z-index property for cover up the images as:-
HTML
<div id ="page1">
<div id = "smallest">
<img src="http://ssl.gstatic.com/android/market/pongoproductions.Brad_Pitt_100100/hi-256-0-acd81f6f98299f7b230fc5014a9eb18c536adc8a"/>
</div>
<div id = "medium">
<img src="http://www.picgifs.com/celebrities/b/brad-pitt/celebrities-brad-pitt-179755.jpg"/>
</div>
<div id = "largest">
<img src="http://www.moviespad.com/photos/brad-pitt-poster-392f6.jpg"/>
</div>
</div>
CSS
#page1 div {
position: absolute;
}
#largest {
z-index: 1;
}
#medium {
z-index: 2;
}
#smallest {
z-index: 3;
}
http://jsfiddle.net/9cg3h/