CSS positioning relative to div - html

I have two images inside a div. I'd like to position these images using percent relatively to the parent div.
Here's a fiddle to understand: http://jsfiddle.net/b9ce626s/
I tried to set position: absolute; on the image but it uses window width.
I need the image on the very right be positioned at 95% of the red div, and not the window. I also don't want the left image impacts the positionning of the right one.

Add position: relative on #main so the position of the images are both based on that element (and not on the root element).
Example: http://jsfiddle.net/b9ce626s/1/
A page element with relative positioning gives you the control to absolutely position children elements inside of it.
https://css-tricks.com/absolute-positioning-inside-relative-positioning/
As a side note, if you assign a width with a percentage value to the images, it will be now based on the parent element width.

Try this..
Html
<div id="main">
<img id="card1" src="http://dynamic-projets.fr/wp-content/uploads/2012/08/attach_image.png" alt="KH" />
<img id="card2" src="http://www.rotaryd1650.org/images/main/IconesCollectionPro/128x128/image_gimp.png" alt="9H" />
</div>
Css
body, html {
width: 100%;
height: 100%;
margin: 0;
}
#main {
display: block;
width: 50%;
height: 50%;
background-color: red;
position:relative;
}
img {
position: absolute;
width: 5%;
}
#card1 {
left:5%;
}
#card2 {
right: 5%;
}
Fiddle Sample

#main {
display: block;
width: 50%;
height: 50%;
background-color: red;
position: relative;
}

Give main position: relative; like so:
#main {
display: block;
width: 50%;
height: 50%;
background-color: red;
position:relative;
}
This keyword lays out all elements as though the element were not positioned, and then adjust the element's position, without changing layout (and thus leaving a gap for the element where it would have been had it not been positioned). The effect of position:relative on table-*-group, table-row, table-column, table-cell, and table-caption elements is undefined.
JSFiddle Demo

Related

absolute div inside absolute div cuts off with respect to relative position

I have 3 divs on top of each other having following css.
.d1 {
position: relative;
background-color: yellow;
height: 50px;
width: 100px;
overflow: hidden;
}
.d2 {
position: absolute;
background-color: green;
height: 25px;
width: 50px;
}
.d3 {
position: absolute;
left: 83px;
}
and the divs that have classes are as follows:
<div class="d1">
<div class="d2">
<div class="d3">text</div>
</div>
</div>
and as a result I see content of d3 cut off because of overflow:hidden in d1.
How can I avoid cut off content of d3 without modifying d1?
Getting around the overflow..
An element can overflow from a relative or absolute positioned parent by setting its position to fixed. An element that has position: fixed will have the default left,right,top, and bottom styles set as auto. This will position .d3 to the top-left of .d2, and then the left: 83px style will push it to the left from there.
Making up the additional space..
However, to get that additional movement to the right as the original markup, you will need to add margin-left: 8px, which will make-up the additional ~8px needed to replicate the original. Further adjustments to the position of .d3 will need to be done by setting the margin style (see below).
Your updated code should look like this..
.d1 {
position: relative;
background-color: yellow;
height: 50px;
width: 100px;
overflow: hidden;
}
.d2 {
position: absolute;
background-color: green;
height: 25px;
width: 50px;
}
.d3 {
position: fixed;
margin-left: 8px;
left: 83px;
}
Some considerations and caveats..
As a previous commenter mentioned, best practice would be to fix your html markup because this solution could cause issues if you ever need to move the position of .d3. For example, setting left,right,top, or bottom will cause the default setting of this style, auto, from being unset, and the element will be positioned relative to the viewport rather than the parent relative or absolute element.

Making a div inside a div responsive

I am beginner in web development, and i am trying to develop a responsive website. While developing i made one div element that has an image, then inside that i have one box with content "BUILD THAT MATTERS", which is inside a div with a class "firstBlock", the div "firstBlock" moves down when i click the hover button of navigation bar, but the box inside "BUILD THAT MATTERS", doesn't move.
I tried to make the image as a background image of the the div firstBlock, but that made the fitting of image in such a way that it got cropped, so is there any way so that the box inside the div moves with the navigation bar drop down menu, as of now only the firstBlock, that is the image moves, but inner box "BUILD THAT MATTERS" remains fixed in its position.
<-- html-->
<div id="firstBlock">
<img id="img1" src="image1.jpg">
<div class="box1">
<h1 class="text1">BUILD THAT MATTERS</h1>
</div>
</div>
/*CSS*/
#firstBlock {
display: flex;
height: 90%;
width: 100%;
}
#img1 {
width: 100%;
height: 100%;
object-fit: cover;
}
.box1 {
display: flex;
border: 5px solid black;
position: absolute;
top: 70%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
So, the box with content "BUILD THAT MATTERS" should move down with the first block when navigation drop down scrolls down.
fiddle link: https://jsfiddle.net/gj7v8huc/
git link: https://ayush-flash.github.io/StartupLandingPage/StartUpLandingPage/
Very simple rule:
make the parent of the absolute element relative
Your class:
.box1 {
display: flex;
border: 5px solid black;
position: absolute; # parent of this should be relative
top: 40%; # I set this to 40%
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
the parent:
#firstBlock {
display: flex;
height: 90%;
width: 100%;
position: relative; # take care of child with absolute position
}
What is this rule for?
When we have an element that has position: absolute it is removed from current flow of document. For bringing it back to the document's flow but meanwhile keeping it according to the need of absolute position, we can make the parent's position relative
It is all about parent and child element for using position:absolute. Parent element should have relative position so that the absolute child element will do the calculation of top and left from its expected parent. So apply position:relative to your firstBlock div.
#firstBlock {
display: flex;
height: 90%;
width: 100%;
position:relative;
}
Also change your box1 div top and left position as per your parent alignment. I have applied 50% like below.
.box1 {
display: flex;
border: 5px solid black;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
As I have already posted in the comment, below is the JSFiddle link.
DEMO

Absolute positioning of element in responsive header

I'm trying to get exactly the same effect as here: Responsive images positioned over image , so I want my absolute positioned elements to stay in the same place but get smaller or bigger when the browser is being sized. I tried many different possibilites, before I worked with this code, but I don't understand why my wrapping container (id="wrapper") is placed under the image. And to get the question mark on the image I would need to use minus percentage. In the example I copied from another question in stackoverflow there are bootstrap styles. I don't know and I don't want to use bootstrap though. I will be very grateful for all suggiestions what I'm doing wrong.
#wrapper {
position: relative;
display: inline;
}
#questionMark {
position:absolute;
left:33%;
top:-43%;
max-width: 10%;
}
This is my fiddle: https://jsfiddle.net/8obzf2c8/2/
inline elements doesn't get the height of the elements inside them.
You should remove the display: inline from the #wrapper element.
#wrapper {
position: relative;
margin-top: 150px;
}
#questionMark {
position:absolute;
left:33%;
top:-43%;
max-width: 10%;
}
<div id=wrapper>
<img src="http://e.allegroimg.pl/s400/19/53/89/71/60/5389716014"/>
<img id="questionMark" src="https://image.freepik.com/free-icon/speech-bubble-with-question-mark_318-78800.png"/>
</div>
I also set margin-top to make sure the question mark image is in the viewport.
Your wrapper has property display: inline; so it behave like a fe. span element, it is not a block.
Change display: inline; to display: inline-block; so the wrapper behaves like inline container. You will also need to change this weird top: -43%; to fe. top: 43% as things will get more normal and predictable.
Use display:inline-block; instead of inline
#wrapper {
position: relative;
display: inline-block;
}
#questionMark {
position:absolute;
left:33%;
top:43%;
max-width: 10%;
}
This will make the image placed in the center and will also be responsive with all screen sizes.
#wrapper {
position: relative;
display: inline-block;
}
img{
width: 100%;
height: auto;
}
#questionMark {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
max-width: 10%;
}
<div id=wrapper>
<img src="http://e.allegroimg.pl/s400/19/53/89/71/60/5389716014"/>
<img id="questionMark" src="https://image.freepik.com/free-icon/speech-bubble-with-question-mark_318-78800.png"/>
</div>
Hope this helps,

Position div tag at the bottom right of an image

I am trying to position an div element at the bottom right of an image, that is inside a container element. I set position relative to the container, and position absolute to the inner div, but it does not work. Here is the (http://jsfiddle.net/ZC84G/). Please, help.
<div class="container">
<div class="icon"></div>
<img src="/images/someImage.png" />
</div>
CSS:
body {
background-color: black;
}
.container {
position: relative;
}
.container img {
max-width: 75%;
max-height: 80%;
}
.icon{
background-image: url('http://icons.iconarchive.com/icons/iconfactory/star-wars-lego/32/Biggs-No-Helmet-icon.png');
width: 31px;
height: 31px;
position: absolute;
bottom: 5px;
right: 5px;
}
This is because by default div has block display mode, and it's width is 100% of the parent container. Try to add display: inline to .container
.container {
position: relative;
display: inline;
}
Here's the corrected jsfiddle: http://jsfiddle.net/ZC84G/4/
Your container div has no width and height set. And since a <div> is a block-level element by default, it will be set to 100% width ie expand to however much horizontal space is left.
Plus, you're also constraining your image size:
max-width: 75%;
max-height: 80%;
If you replace the img CSS with:
max-width: 75%;
max-height: 80%;
It works fine, and as expected: http://jsfiddle.net/ZC84G/3/
I've modified your CSS on the image a bit.
Basically, I set it to scale properly to the size of its container, and now it sits where I think you wanted it. The way you could find this yourself in the future would be to inspect the element by using right click from your browser, and looking at the size of the different elements to see what was expanding larger/smaller than it should.
.container img {
max-width: 100%;
height: auto;
}

Relatively-positioned parent with absolutely-positioned children collapses the later elements

I have the following HTML:
<div id="contents">
<div id="inner">Inner Div</div>
</div>
<div id="footer">Footer</div>​
Applying the following CSS:
#contents {
position: relative;
}
#inner {
position: absolute;
background-color: green;
width: 100%;
}
#footer {
background-color: red;
}​
Problem is that the #footer gets collapsed and under the #contents. You can check here on jsfiddle also http://jsfiddle.net/MAhmadZ/YkJMH/1/
Note: This is just an abstract from a larger page. I have no option but to use position properties. I have multiple div inside #contents all absolutely positions and only 1 will be showed at a time. I can't be sure of the height
Your #contents element is empty after #inner is taken out of the flow by absolute positioning, so it has zero height and as a result #footer collapses under it.
If you give #contents a height or some vertical padding, it should prevent #footer from sliding underneath your absolutely-positioned element.
This should fix it:
#contents {
position: relative;
}
#inner {
position: absolute;
background-color: green;
width: 100%;
}
#footer {
background-color: red;
position: absolute;
width: 100%;
bottom: 0;
}​
Your problem is #inner is positioned absolutely. This makes it invisible to the STATIC formatting of HTML layout.
Not quite sure what you want to achieve, but you can set the footer to use position: fixed and bottom: 0px to keep it at the bottom of the page.
I have myself come up with the following solution:
#footer:before {
content: '.';
display: block;
width: 100%;
}
Check it live: http://jsfiddle.net/MAhmadZ/YkJMH/5/