Relative parent DIV to inherit the width of absolute child DIV - css

I am trying to position a child DIV at the bottom of a parent DIV, but I would also like the contents of the child DIV to help dictate the dimensions of the parent DIV. As I have it right now, the child DIV doesn't affect the width/height of the parent DIV.
Here is a sample of my HTML/CSS code:
//HTML code:
<div id="parent">
<h3>Top Aligned Title</h3>
<div id="child"></div>
</div>
//CSS code:
#parent {
background-color:#222;
position: relative;
height: 500px;
}
#child {
background-color:#444;
position: absolute;
bottom: 0px;
width: 100px;
height: 200px;
}
What do I need to do it achieve what I am trying to do? I could forgo the absolute/relative CSS rules and simply create a table within the parent DIV which would allow me to achieve both bottom alignment and content that dictates the parent's dimensions.
However, I'd like to know if there a way to do this in CSS and without having to set the width of the parent DIV.
thanks in advance!

The short answer is that what you are asking basically can't be done with pure CSS / HTML. (at least without tables) You'd need Javascript that would read #child's width/height and then do the calculation you want to do (I don't know) and set a new height/width to #parent.
Otherwise, if you mean that you want #child's height/width to change according to its content, of course this is native CSS, just set it's height/width to auto and then start adding text inside it you'll see it will start growing to fit your content inside.
As the #child is positioned absolute, then it is taken OUT of the normal flow of the document, therefore it will not affect the #parent.

With modern CSS, this is doable.
HTML:
<div id="parent">
<h3>Top Aligned Title</h3>
<div id="child">
<p>CHILD ELEMENT</p>
</div>
</div>
CSS:
#parent {
background:red;
height: 500px;
position:relative;
}
#child {
background:green;
position: absolute;
top:100%;
-webkit-transform: translateY(-100%);
-ms-transform: translateY(-100%);
transform: translateY(-100%);
width: 100px;
}
http://jsfiddle.net/Bushwazi/bpe5s6x3/
transform:translateY(-100%); is the trick. It's math is based on the element's box-model.
You could also combine top:50%; with transform:translateY(-50%); to center it.
You can swap top for left and translateY for translateX to position the element horizontally.

Here you go
HTML:
<main id="parent">
<div class="popup">Top Aligned Title
<div class="content">
Content
</div>
</div>
</main>
CSS:
#parent {
width: 120px;
}
.popup {
position: relative;
margin-top: 48px;
}
.content {
left: 0;
position: absolute;
background: green;
width: 100%;
}
http://jsfiddle.net/8L9votay/

You can play around with flex and zero-width/height.
I've recently come up with the following solution (for width):
#parent {
display: inline-flex;
flex-direction: column;
background: #518cff;
color: #fff;
}
#child-wrapper {
height: 0; /* This can also be max-height, but height is just enough */
}
#child {
transform: translateY(-100%); /* If you need to align child to the bottom */
background: #b40000;
color: #fff;
}
<div id="parent">
<h3>Top Aligned Title</h3>
<div id="child-wrapper"> <!-- This is the solution -->
<div id="child">
Child's content that is longer than parent's
</div>
</div>
</div>

Related

Place two images side by side

I am trying to place two images side by side. I want them to be full width and responsive. However, I can't figure out how to get them on the same line. Does anyone have solutions? Here is a fiddle - https://jsfiddle.net/0je558ex/
<div class="food-featured-posts">
<div class="food-featured-posts-first">
<img src="https://static.pexels.com/photos/2855/landscape-mountains-nature-lake.jpg"/ >
</div>
<div class="food-featured-posts-second">
<img src="https://static.pexels.com/photos/4164/landscape-mountains-nature-mountain.jpeg"/ >
</div>
</div>
food-featured-posts {
width: 100%;
margin-bottom: 100px;
}
.food-featured-posts-first img {
width: 50%;
height: 50%;
display:inline-block
}
.food-featured-posts-second img {
width: 50%;
height: 50%;
display:inline-block
}
You have two problems actually.
First, you're setting the styling of the img, but the div which wraps them are implicitly styled to basically be: display:block;width:100%;.
Simply remove the divs.
Second, and slightly more interestingly, your img elements still will not render next to each other at 50% because any whitespace between two display:inline-block elements means that the total size is greater than 100%, so the second element is kicked to the second line.
You therefore need to put the img tags on the same line—frustrating, I know.
See this question: CSS two div width 50% in one line with line break in file
<div class="food-featured-posts">
<!-- Note these are on the same line -->
<img src="https://static.pexels.com/photos/2855/landscape-mountains-nature-lake.jpg"/ ><img src="https://static.pexels.com/photos/4164/landscape-mountains-nature-mountain.jpeg"/ >
</div>
food-featured-posts {
width: 100%;
margin-bottom: 100px;
}
.food-featured-posts img {
width: 50%;
display:inline-block;
}
Set the divs that wrap the image to width: 50%; display: inline-block; and set the img tags to width: 100%; so they will take up the entire div, then remove the space between the inline-block div elements in your HTML since spaces on inline elements take up space and the space will exceed 100% width (since each div takes up 50%).
img {
width: 100%;
}
.food-featured-posts > div {
width: 50%;
display: inline-block;
}
<div class="food-featured-posts">
<div class="food-featured-posts-first">
<img src="https://static.pexels.com/photos/2855/landscape-mountains-nature-lake.jpg"/ >
</div><div class="food-featured-posts-second">
<img src="https://static.pexels.com/photos/4164/landscape-mountains-nature-mountain.jpeg"/ >
</div>
</div>

How to use CSS position(relative, absolute) with percentage (height, width) dimension? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So many question & answers are there related to this topic but no where i found the like following mentioned case. All describes If you want to set the height and width in percentage, set the parent height in percentage first but still I did not get clarity.
For example: Following ID and class of block of drupal 7, and my code which starts from the "box1" which I added using body(Editor) of that block.
<div id="block-block-1">
<div class="block-inner">
<div class="block-content">
<div class="box1">
<a class="box2">
<span class="box3">For Title</span>
<span class="box4">For Text</span>
<span class="box5">For Image</span>
</a>
</div>
</div>
</div>
</div>
**So, for which part of code is parent for which one.
Height & width Aspect: Do I have to make the #block-block-1 {height: 100%, width: 100%}, and it will be apply to every one. Or do I have to add at each stage, but if I do that, then automatically all div, a sections will be 100%, that not make any sense.
Position Aspect: Now The other aspect of positioning of Div with combination of Position:relative and Position:absolute, where, it states that parent div should be relative and inside that all div should be absolute and set its position using top, right and left. But same question one's Relative position is other's absolute position, so again it creates contradictory same as height and width.
So, what is the right way to use this height/weight(in percent) and position aspect simultaneously?
A few examples to get you some insight in position and more, hope it helps to bring your css-skills to a next level.
Important read html code first and look what happen in result
First of all, understand block-level/inline elements.
* {
outline: 1px solid
}
div.iHaveKids {
padding: 25px
}
/* all childs of div*/
div > * {
background-color: red
}
b {
background-color: yellow;
width: 100%;
}
b.running {
margin-left: 40px
}
span {
background-color: pink
}
div.clearMe {
width: 50px;
}
<div>This is a beatifull block-level element</div>
<span>This is inline element</span>
<div>a block-level element width is, if not set, 100%</div>
<div class="iHaveKids">a block-level element height is, if not set, designed to fit hes childeren
<div>I'm a child</div>
<div>Me tooooooo</div>
<div><b>A block element can have margins and/or paddings but i'm a inline element</b>
</div>
<div><b>Even when i have a width set i just ignore them, because i'm a inline element</b>
</div>
<div><b class="running away">however i can set margin-left and/or margin-right</b>
</div>
</div>
<span>i'm an inline element<span>me toooo and i'm inside an inline element</span>
<div class="clearMe">block-level element</div>
<div>block-level elements start at a new-line in the document even if there is enough room</div>
</span>
Now position:
* {
outline: 1px solid;
}
div.example {
background: red;
}
.example.ex1,
.example.ex2,
.example.ex3,
.example.ex4,
.example.ex5,
.example.ex10 {
position: absolute;
}
span {
background-color: yellow
}
div.ex4 {
z-index: 2500;
}
.example.ex6,
.example.ex7,
.example.ex8,
.example.ex9 {
position: relative;
}
.ex8,
.ex9 {
top: 40px;
}
.ex9 {
width: 500px;
height: 500px;
}
.ex10 {
bottom: 0;
}
<div>im a block-level element, i have by default position:static</div>
<div class="example ex1">im a absolute element, i have position:absolute
<span>my parent has an absolute position i'm just an inline element</span>
</div>
<div class="example ex2">im a absolute element, i have position:absolute
<span>hm hm hm hm hm my parent has also position:absolute, if no top,left,bottom,right is defined than i will place my self in the normal document flow without looking to position:absolute elements, floated elementes, position:fixed elements, position, relative elements</span>
<span>if two or more position:absolute elements are in the same place without a stacking-order than is the one that was latest in document-flow who get higher stakcing-order</span>
</div>
<div>
<div>make</div>
<div>some</div>
<div>room</div>
<div>for example 3</div>
<div>for example 4</div>
<div>for example 5</div>
</div>
<div class="example ex3">Example 3</div>
<div class="example ex4">Example 4:: ex3, ex5 has no stacking order but ex4 does, autor-define stacking are higher than browser setted stackings</div>
<div class="example ex5">Example 5</div>
<div>
<div>make</div>
<div>some</div>
<div>room</div>
<div>for example 6</div>
<div>for example 7</div>
<div>for example 8</div>
</div>
<div class="example ex6">Example 6, i have position: relative</div>
<div class="example ex7">Example 7, i have position: relative<span>hm hm hm hm hm my parent has also position:relative, if no top,left,bottom,right is defined than i will place my self in the normal document flow</span>top
</div>
<div class="example ex8">Example 8, i have position: relative and bottom: 40px;<span>meaning i will move myself 40px away from the top where the document-flow woulkd place me</span>
</div>
<div class="example ex9">I have position:relative i'm also a block-element and have a width of 500px and an height of 500px; i also have top: 40px;
<div class="example ex10">i'm a block-level element with position:absolute inside of an element with position:relative i also have bottom: 0 so i place myself 0px away from the bottom line of my <b>nearest positioned ancestor</b> if i have no nearest positioned ancestor i take
the document root</div>
</div>
Than finally % width and height
div {
position: relative;
}
div > div {
position: absolute;
}
div.parent {
top: 10px;
height: 400px;
background-color: red;
}
div.child {
background-color: yellow;
top: 10%;
bottom: 10%;
}
<div class="parent">
i'm position:realtive;
<div class="child">
i'm position:absolute;
<br>i first look top, right, bottom, left
<br>than i look to my content and fit even if i'm a block-level element
<br>to calculate percentage i look to my the parent
<br>my parent is height: 400px
<br>i use top: 10%;
<br>what means i will set 40px
<br>
</div>
</div>
To finally answer your question:
So, what is the right way to use this height/weight(in percent) and position aspect simultaneously?
Percentages are based on maximum available space, in last example top: 400px; so 10% == 40px once you understand how things behave ( root, parent, child, siblings, ancestor,.. ) you find this kind of work too so easy for you. Read, read and read even more to learn about difference between any ground of elements.
Do I have to make the #block-block-1 {height: 100%, width: 100%}, and it will be apply to every one.
Like in examples said, apply 'width: 100%`; to an block-level element has no use. If you ask how child element behave on dimension settings (width, height, top, right, bottom, left, margin, padding,..) you need to know what kind of element it is (block, inline, grid, flex, table, list, replaced,... element) and know how it behaves.
But same question one's Relative position is other's absolute position, so again it creates contradictory same as height and width.
An element with position: absolute; looks for the nearest positioned ancestor element, if not set i will look to document.root
useful resources:
Percentage on Mozilla
Lenght properties on css-tricks
The Difference Between “Block” and “Inline” on impressivewebs
Here is an example so you can understand how it works.
The parent block .box2 100px from the left edge because of the margin.
If you want to position elements with absolute, but with the .box2 as a reference (relatively to it), you have to add position: relative on the parent element.
.box3 is 0px from the left edge of .box2
.box4 is 100px from the left edge of .box2
.box5 is 100px from the right edge of .box2
You can also use top and bottom properties, of course.
.box2 {
border: 1px solid red;
display: block;
height: 2em;
margin-left: 100px;
position: relative;
width: 400px;
}
.box2 span {
border: 1px solid blue;
position: absolute;
}
.box3 {
left: 0;
}
.box4 {
left: 100px;
}
.box5 {
right: 100px;
}
<a class="box2">
<span class="box3">For Title</span>
<span class="box4">For Text</span>
<span class="box5">For Image</span>
</a>

Stretching an element outside of its parent

I have 3 divs in my body: a container, a parent, and a child.
I'm trying to get the child to extend outside of its parent on the left side.
But if I do so with position: absolute, the parent will not stretch to the desired height...
position: static
position: absolute
Using a margin-left: -20px will not do either: ultimately, i'll have other nested parents, and need all the children to extend to the outer left.
Hers's my code so far:
#container {
position: absolute;
width: 300px;
}
.parent {
margin-left: 20px;
}
.child {
padding: 30px;
}
Any way to do this in pure css?
Edit: Here's my html code so you can see how the parents will be nested in each other:
<div id="container">
<div class="parent">
<div class="child"></div>
<div class="parent">
<div class="child"></div>
</div>
</div>
<div class="parent">
<div class="child"></div>
</div>
</div>
Edit 2: I have to point out there are multiple (infinite) levels of nesting in my code. The html sample above is just a fragment.
Why not simply use
Demo Fiddle
.child {
height: 60px;
position:relative;
left:-20px;
}
You can use position:relative to also justify content beyond the borders of a parent, so long as overflow:hidden isnt set on the parent.
Use this style:
.stretch-block {
position: relative;
left: 50%;
margin-left: -50vw;
width: 100vw;
}
It pushes the element to 50% width of the parent container from the left, then pulls it to the viewport's left edge with a negative margin that's 50% of viewport width. Then the element gets a width that's 100% of viewport width.

fixed position div inside div container

I am trying to create fixed position div inside relative container. I am using bootstrap css framework. I am trying to create a fixed position cart. So whenever user scroll page it will show cart contents. but now problem is, it ran outside that container div.
This has to work in responsive mode.
Here my try:
.wrapper {
width: 100%
}
.container {
width: 300px;
margin: 0 auto;
height: 500px;
background: #ccc;
}
.element {
background: #f2f2f2;
position: fixed;
width: 50px;
height: 70px;
top: 50px;
right: 0px;
border: 1px solid #d6d6d6;
}
<div class="wrapper">
<div class="container">
<div class="element">
fixed
</div>
</div>
</div>
Screenshot:
This is how position: fixed; behaves:
MDN link
Do not leave space for the element. Instead, position it at a
specified position relative to the screen's viewport and doesn't move
when scrolled. When printing, position it at that fixed position on
every page.
Hence, to get what you want you have to use something more than fixed positioning:
Probably this:
.wrapper {
width: 100%
}
.container {
width: 300px;
margin: 0 auto;
height: 500px;
background: #ccc;
}
.element {
background: #f2f2f2;
position: fixed;
width: 50px;
height: 70px;
margin-left: 250px;
border: 0px solid #d6d6d6;
}
<div class="wrapper">
<div class="container">
<div class="element">
fixed
</div>
</div>
</div>
Make the element's parent container have position: relative
Instead of using top or left use margin-top and/or margin-left
If you only use top that will position the element based on the window, but if you use margin-top that will position based on the parent element. Same goes for left or right
<div class="parent">
<div class="child"></div>
</div>
.parent {
position: relative;
}
.child {
position: fixed;
margin-top: 30px;
margin-left: 30px;
}
I found the answer to that :
<div class="container">
<div class="inContainer">
<p> coucou </p>
</div>
<div>
<p> other thing</p>
</div>
</div>
You want that class="inContainer" are in class="Container" in fixed position but if you scroll with the navigator scroll you don't want that the class="inContainer" move outside the class="container"
So you can make something like that
.container{
height: 500px;
width: 500px;
overflow-y:scroll;
}
.inContainer {
position: absolute;
}
So class=inContainer will be always on the top of you're class=Container and move with you're class=container if you scroll with navigator scroll =)
(tested only with chrome)
No it's impossible because fixed property throws the element out of the flow so it doesn't depend to anything on the document and yes it is no more contained in your container : )
Yes, you can do it, just use margin-top property instead of top property.
When you use position: fixed and specify a top and or left position,
you'll find that the element will be fixed relative to the window, and
not to any other element of position: relative.
There is a way around this and that is not to specify top and left
positions but instead to use margin-left and margin-top on the
position: fixed element.
Source: https://www.gravitywell.co.uk/latest/design/posts/css-tip-fixed-positioning-inside-a-relative-container/
The behavior of the positioning is mentioned in the AdityaSaxena's answer https://stackoverflow.com/a/18285591/5746301
For creating a fixed position cart, you can also do it with using the jquery.
If we apply the Left or right value or margin, we may face some issue while responsive.
In the below snippet, I have placed the fixed element at the right of the container.
Even if the width of the container increased the fixed element placed accordingly.
Here is the jsfiddle Demo URL
//Jquery
$(document).ready(function(){
var containerWidth = $(".container").outerWidth();
var elementWidth = $(".element").outerWidth();
var containerOffsetLeft = $(".container").offset().left;
var containerOffsetRight = containerOffsetLeft + containerWidth - elementWidth;
$(".element").css("left", containerOffsetRight);
});
//CSS
.wrapper {
width:100%
}
.container {
width:300px;
margin:0 auto;
height:900px;
background:#ccc;
}
.element {
background:#f2f2f2;
position:fixed;
width:50px;
height:70px;
top:50px;
border:1px solid #d6d6d6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="container">
<div class="element">
fixed
</div>
</div>
</div>
Hope this may help you.
Thanks
If you are looking to show the cart even when the user scrolls that is fixed then you should use position:fixed for the cart (if .container is your cart), because it should be shown with respect to screen/viewport. Your current code will only show the element which is positioned absolutely inside the container. If you want it to be like that then give :
.container {
position:relative;
}
.element {
position:absolute;
top:50px;
right:0px;
}
<div style="position: fixed;bottom: 0;width: 100%;">
<div class="container" style="position: relative;">
<div style="position: absolute;right: 40px;bottom: 40px;background:#6cb975;border-radius: 50%;width: 40px;text-align: center;height: 50px;color: #fff;line-height: 50px;">
F
</div>
</div>
</div>
You can just add
.element {
left:368px;
}
Fiddle: http://jsfiddle.net/UUgG4/

CSS Absolute positioning 100% height less padding without JS

The following code has a DIV that needs to be positioned at the top of the container, another at the bottom and then the content needs to come through in the middle.
<div style="position:absolute; top:0; width:100%; height:40px"></div>
<div class="howto"></div>
<div style="position:absolute; bottom:0; width:100%; height:40px"></div>
So we don't know the height of the containing DIV. How without JS can the div with class howto have the height of the container DIV less the height of the absolute positioned div at the top and bottom so as to contain content between these 2 DIVs.
For what you wish to accomplish, this is one possible solution:
#tinkerbin: http://tinkerbin.com/QsaCPgR6
HTML:
<div class="container">
<div class="header"></div>
<div class="howto">
Has height set to auto. You may change that if you want to.
</div>
<div class="footer"></div>
</div>
CSS:
.container {
position: relative;
padding: 40px 0; /* top and bottom padding = .header and .footer padding*/
}
.header,
.footer {
position: absolute;
height: 40px;
width: 100%;
}
.header {
top: 0;
}
.footer {
bottom: 0;
}
.howto {
height: /*specifiy one if you wish to*/;
}
As far as I know there isn't a pure CSS way to do what you're trying to do without JS.
See this previous post on SA:
Make a div fill the height of the remaining screen space

Resources