I can't get max-width to work when using absolute positioning. In https://jsfiddle.net/jn2bs6ax/ the item should take as little width as possible, up to the max-width of 1000px. But it's taking a width of about 50% and wrapping the text. How to make it work? If I set the max-width to something small like 300px it works, but anything larger than what it's width currently is doesn't cause it to expand.
This example just shows an item that's been absolutely positioned to the center below an item, but I use transform, top, bottom, left and right to position things in all 4 directions relative to an item. The solution should work with all 4 cases.
There's a similar question CSS (position:absolute + left:50% = max-width:50%)? but the answer only works with centering below but not for all cases like positioning to right of an item vertically centered.
<div class="relative">
text
<div class="absolute">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim recusandae doloribus nesciunt unde vitae quis aliquid laborum adipisci ipsa, dolorem repellendus nulla iure atque minus fuga sunt rem eaque animi.</div>
text
</div>
css
.relative {
position: relative;
}
.absolute {
position: absolute;
background-color: grey;
max-width: 100%;
top: 100%;
left: 50%;
transform: translateX(-50%);
}
Try setting display: inline-table; or display: table;
.relative {
position: relative;
}
.absolute {
position: absolute;
background-color: grey;
max-width: 1000px;
top: 100%;
left: 50%;
transform: translateX(-50%);
display: inline-table;
}
<div class="relative">
text
<div class="absolute">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim recusandae doloribus nesciunt unde vitae quis aliquid laborum adipisci ipsa, dolorem repellendus nulla iure atque minus fuga sunt rem eaque animi.</div>
text
</div>
Try add width: 100%; for your class along max-width in any place.
.relative {
position: relative;
}
.absolute {
position: absolute;
background-color: grey;
max-width: 1000px;
top: 100%;
left: 50%;
transform: translateX(-50%);
width: 100%;
}
<div class="relative">
text
<div class="absolute">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim recusandae doloribus nesciunt unde vitae quis aliquid laborum adipisci ipsa, dolorem repellendus nulla iure atque minus fuga sunt rem eaque animi.</div>
text
</div>
As you already noticed, the issue is related to left:50% that will restrict the width of the element to 50% of the parent element.
One hacky way to overcome this is to increase the width of the parent element by adding more padding since absolute element will consider the padding-box. Then you add negative margin to rectify the added padding. You should simply pay attention to overflow:
.relative {
position: relative;
padding:0 50%;
margin:0 -50%;
}
.absolute {
position: absolute;
background-color: grey;
max-width: 800px;
top: 100%;
left: 50%;
transform: translateX(-50%);
}
.hide {
overflow:hidden;
height:500px;
}
<div class="hide">
<div class="relative">
text
<div class="absolute">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim recusandae doloribus nesciunt unde vitae quis aliquid laborum adipisci ipsa, dolorem repellendus nulla iure atque minus fuga sunt rem eaque animi.</div>
text
<div class="absolute" style="top:200px;">Lorem ipsum dolor sit amet</div>
</div>
</div>
Related
I have two <div> with the position: absolute. One is at the top and the other is at the bottom the page. The one at the bottom of the page goes lower than the last element (footer). My problem is that even if my <div> is in position: absolute and should be removed from the flow, my page extends to fit the <div> that is "overflowing". How can I make the page crop everything that exceeds my footer?
Here's what I'm talking about:
body{
position: relative;
}
p{
width: 80%;
font-size: 50px;
margin: 0;
}
footer{
margin-top: 200px;
position: relative;
}
.bg_gradient.first{
position: absolute;
top: 0;
left: 0;
width: 1000px;
height: 1000px;
transform: translate(-400px, -400px);
background: radial-gradient(circle, rgba(254,73,70,1) 0%, rgba(254,73,70,0) 70%);
z-index: -1;
}
.bg_gradient.last{
position: absolute;
bottom: 0;
left: 0;
width: 1000px;
height: 1000px;
transform: translate(-400px, 400px);
background: radial-gradient(circle, rgba(254,73,70,1) 0%, rgba(254,73,70,0) 70%);
z-index: -1;
}
<body>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eum voluptas incidunt nulla necessitatibus rerum illum provident ea earum neque officia nam deserunt animi nostrum iusto velit distinctio, dolor eveniet voluptates.</p>
<div class="bg_gradient first"></div>
<div class="bg_gradient last"></div>
<footer>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Facere eligendi maiores dolore mollitia animi a fugit saepe perferendis unde, sequi debitis sint ratione, recusandae tempora quis culpa vitae sed assumenda!</p>
</footer>
</body>
Why the bottom of the page is not cropping my absolute positioned element
It's not supposed to be cropping it. That's how it is supposed to work. Just because an element is absolutely positioned, does not mean the parent container won't stretch to accommodate it.
Thanks to #Mahatmasamatman, the things is this is the normal behavior.
Just because an element is absolutely positioned, does not mean the parent container won't stretch to accommodate it.
- #Mahatmasamatman
My solution is to create a div that make the same width and height than my body with position: absolute and set overflow: hidden so I would get the behavior I wanted.
Here's a jsfiddle with the solution
https://jsfiddle.net/d3yns9b6/ shows how max-width doesn't work when I want to set it to something larger than the containing element.
Since it's absolutely positioned it should be able to extend outside the containing element. If I set an exact value using width it works but then both pieces of text in the example are exactly that width.
I want them both to take up as little width as they need, up to a maximum of the amount I set (even if it exceeds the parent container).
.out {
position: relative;
width: 200px;
height: 400px;
background-color: blue;
}
.in {
position: absolute;
max-width: 600px;
background-color: yellow;
}
<div class="out">
<div class="in">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam commodi saepe, magnam aliquid quisquam cum ex corrupti sequi aut eius harum animi vitae, exercitationem eaque tempore culpa at itaque explicabo.
</div>
<div class="in" style="margin-top:300px">
Lorem
</div>
</div>
Well, when no width is provided, it will fall back to auto, meaning it will use the width given by the parent element, regardless of absolute positioning or any max-width. So you need to specify any width, using percentage or relative units like vh or vw.
.out {
position: relative;
width: 200px;
height: 400px;
background-color: blue;
}
.in {
position: absolute;
width: 500%;
max-width: 600px;
}
.in > span {
background-color: yellow;
display: inline-block;
}
<div class="out">
<div class="in">
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam commodi saepe, magnam aliquid quisquam cum ex corrupti sequi aut eius harum animi vitae, exercitationem eaque tempore culpa at itaque explicabo.</span>
</div>
<div class="in" style="margin-top:300px">
<span>Lorem</span>
</div>
</div>
You need to set the width with width and constrain it with max-width.
Something like this:
.in {
position: absolute;
width: 100vw;
max-width: 600px;
background-color: yellow;
}
This is the logical behavior of absolute element where their width obey to the shrink-to-fit algorithm thus they cannot exceed the available width of their containing block.
the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width). ref
One idea is to increase the available width by increasing the padding since absolute element consider the padding-box then you can apply negative margin to compensate the padding added.
.out {
position: relative;
width: 200px;
padding-right:400px; /*width + padding = 600px (equal to max-width)*/
margin-right:-400px;
height: 400px;
background-color: blue;
background-clip:content-box; /* We don't show the background on the padding*/
}
.in {
position: absolute;
max-width: 600px;
background-color: yellow;
}
<div class="out">
<div class="in">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam commodi saepe, magnam aliquid quisquam cum ex corrupti sequi aut eius harum animi vitae, exercitationem eaque tempore culpa at itaque explicabo.
</div>
<div class="in" style="margin-top:300px">
Lorem
</div>
</div>
In this case the padding is not really needed since it's a block element but it can be useful when dealing with inline elements.
Example:
.out {
position: relative;
display:inline-block;
width: 200px;
padding-right:400px; /*width + padding = 600px (equal to max-width)*/
margin-right:-400px;
height: 300px;
background-color: blue;
background-clip:content-box; /* We don't show the background on the padding*/
}
.in {
position: absolute;
max-width: 600px;
background-color: yellow;
}
.extra {
display:inline-block;
background:red;
vertical-align:top;
margin-top:100px;
}
<div class="out">
<div class="in">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam commodi saepe, magnam aliquid quisquam cum ex corrupti sequi aut eius harum animi vitae, exercitationem eaque tempore culpa at itaque explicabo.
</div>
<div class="in" style="margin-top:200px">
Lorem
</div>
</div>
<div class="extra">
some content here
</div>
I've been trying different ways but couldn't achieve what I want.
<div id="parent">
<div id="child-1"></div>
<div id="child-2"></div>
<div id="child-3"></div>
</div>
So I have the #parent at height: 100vh.
#child-1 should have height: 100% of parent.
#child-2 and #child-3 should have width: 100% and height: auto and they should be stacked on top of each other at position bottom: 0.
I've been trying to set parent relative and two childs absolute but the first child's height gets ignored.. I tried with display flex but first child's height is not 100% of parent.. I'm very confused how to do this.
Can someone help?
Here is what I'm trying to achieve: jsfiddle.net
You have to first get the bottom value of #child-2 dynamically as you said it should be on the top of #child-3.
You need to apply jQuery to get the height of #child-3 dynamically and then applying the height value of #child-3 to the bottom value of child-2, just like
#child-2 {
bottom: height-of-child-3;
}
Look at this Codepen
Or look at the snippet below:
height_child_three = $('#child-3').height();
$('#child-2').css({
position: 'absolute',
bottom: height_child_three
});
#parent {
width: 100vw;
height: 100vh;
background: #000;
position: relative;
}
#child-1 {
width: 100%;
height: 100%;
background: #eee;
}
#child-2 {
width: 100%;
background: #a0ea0e;
}
#child-3 {
position: absolute;
bottom: 0;
width: 100%;
background: #30e30e;
}
body { margin: 0; } /* A small reset */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
<div id="child-1">
<strong>I'm child 1</strong>
</div>
<div id="child-2">
<strong>I'm child 2</strong>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione deleniti voluptate commodi distinctio, repellendus qui, placeat laboriosam eligendi! Ducimus reiciendis officiis debitis placeat adipisci quae hic tempore vitae suscipit nemo.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam sed aliquid, laborum nisi quos excepturi hic! Molestias hic consectetur dolor! Perferendis iste, quisquam quaerat ab, odio ducimus! Odio, minima error?</p>
</div>
<div id="child-3">
<strong>I'm child 3</strong>
</div>
</div>
Hope this helps!
Is this what you need?
HTML:
<div id="parent">
<div class="child-1"></div>
<div class="child-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error voluptatum necessitatibus dolorem soluta laudantium cupiditate maiores neque, aliquid accusamus autem saepe tempora, itaque possimus, eaque deleniti odio atque enim omnis.</div>
<div class="child-3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa, illo est dolor dolores placeat deleniti quae consequuntur eum ipsum blanditiis laboriosam quod repellendus fugit! Odio quis rem vel a dolores.</div>
</div>
CSS:
html,
body,
div {
margin: 0;
padding: 0;
}
*,
*:after,
*:before {
box-sizing: border-box;
}
#parent {
position: relative;
width: 100vw;
height: 100vh;
background: #ccc;
}
.child-1 {
width: 100%;
height: 100%;
background: red;
}
.child-2 {
width: 100%;
height: auto;
padding: 30px;
background: blue;
}
.child-3 {
width: 100%;
height: auto;
padding: 30px;
background: green;
}
Here you can see a solution just using plain CSS. CODEPEN
So I have 2 divs in a container. One floated left and one floated right. The one on the right is text. The one on the left is an image. How can I set the image height so it's equal to the unknown height of the right text?
The container of both divs should also be the same height as the text div. The image's height should not be taller or shorter than the text.
The div containing the text should be as big as the amount of text in it.
Here's an example on JSFiddle
HTML:
<div class="body">
<div class="imgContainer"><img src="http://www.appleinspires.me/wp-content/uploads/2013/05/mzl.bneaekit.512x512-75.jpg" alt=""></div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime ducimus, excepturi ad! Porro officia, est omnis eum modi reiciendis, velit aliquid dolores tempore odit ipsa temporibus ullam. Adipisci, optio, neque?<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure deserunt amet aspernatur nisi, voluptatem consequuntur vel saepe dolorem odio atque, porro architecto alias aliquid. Atque ea soluta, obcaecati sapiente mollitia!</div>
</div>
CSS:
.body {
display: table;
background-color: grey;
margin-bottom: 10.38vw;
margin-left: 5.19vw;
margin-right: 5.19vw;
}
.imgContainer {
float: left;
position: relative;
height: 100%;
width: 40%;
background-color: green;
}
img {
height: 100%;
width: 100%;
display: block;
border-radius:100%;
margin-left: auto;
margin-right: auto;
}
.text {
background-color:blue;
float: right;
width: 60%;
font-size: 2.26vw;
text-align: left;
display: table-cell;
}
On the JSFiddle, the image's height appears to be taller than the height of the div with text, but this is not what I want. I'd like the image to be shrunken to the height of the right div. Since my container is displayed as a table and the children divs should be displayed as table-cells, the container's height is supposed to be the same height as the text, which is what I'm aiming for. The image should also be horizontally centered in its div once it's shrunk, but my current code should allow that to work.
Another thing: The point of this is because I'm trying to go for a responsive design. The image should stay the same height as the text as the browser window is resized.
And just to clarify, I do not want the height of the text div to enlarge to the height of the image; I want the image's height to be shrunken to the height of the text.
If anyone has any suggestion or solutions, please let me know. Thanks!
You can just change the parent element .body to a flexbox using display: flex like this:
.body {
display: flex;
}
The result of this is that your items will all line up in a row, using the size of the content as their size in the main axis. If some items are taller than others, all items will stretch along the cross axis to fill its full size.
Check this JSFiddle or run the Code Snippet below for a practical example of the above code:
.body {
display: flex;
background-color: grey;
margin: 0 auto;
}
.imgContainer {
background-color: green;
}
img {
height: 100%;
width: 100%;
border-radius: 100%;
}
.text {
background-color: blue;
font-size: 14px;
text-align: left;
}
.text2 {
background-color: red;
font-size: 14px;
text-align: right;
}
<div class="body">
<div class="imgContainer">
<img src="https://picsum.photos/512/512" alt="">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime ducimus, excepturi ad! Porro officia, est omnis eum modi reiciendis, velit aliquid dolores tempore odit ipsa temporibus ullam.!
</div>
<div class="text2">
Just some short sentence.
</div>
</div>
You can
Remove floats in order to use a tabular layout, which will ensure both elements have the same height.
Remove the image from the normal flow of the document using absolute positioning. This way .imgContainer will be as short as possible.
Make the image grow to fill .imgContainer.
.body {
display: table;
}
.imgContainer,
.text {
display: table-cell;
vertical-align: top;
}
.imgContainer {
position: relative;
width: 40%;
background-color: green;
}
img {
position: absolute;
height: 100%;
width: 100%;
border-radius: 100%;
}
.text {
background-color: blue;
width: 60%;
display: table-cell;
}
<div class="body">
<div class="imgContainer">
<img src="http://www.appleinspires.me/wp-content/uploads/2013/05/mzl.bneaekit.512x512-75.jpg" alt="">
</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime ducimus, excepturi ad! Porro officia, est omnis eum modi reiciendis, velit aliquid dolores tempore odit ipsa temporibus ullam. Adipisci, optio, neque?<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure deserunt amet aspernatur nisi, voluptatem consequuntur vel saepe dolorem odio atque, porro architecto alias aliquid. Atque ea soluta, obcaecati sapiente mollitia!</div>
</div>
I try to achieve a Layout with nested min-height divs and a sliding footer.
The problem of course is that die inner min-height div is not expanding to the full heights of the outer div because the outer divs height is set with min-height.
here is the html:
<div class="container">
<section class="pos-container">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus, voluptates, qui eos dignissimos quae nobis at provident voluptatum dicta nesciunt possimus iusto vitae nihil hic assumenda aspernatur quos vel necessitatibus.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus, voluptates, qui eos dignissimos quae nobis at provident voluptatum dicta nesciunt possimus iusto vitae nihil hic assumenda aspernatur quos vel necessitatibus.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus, voluptates, qui eos dignissimos quae nobis at provident voluptatum dicta nesciunt possimus iusto vitae nihil hic assumenda aspernatur quos vel necessitatibus.</p>
</section>
</div>
<footer>
i'm footer
</footer>
and the css:
body {
background-color: grey;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow-y: scroll;
}
.container {
z-index: 1;
position: relative;
width: 100%;
min-height: 100%;
background-color: pink;
margin-bottom: 6em;
}
.pos-container {
position: relative;
width: 50em;
min-height: 100%;
margin: auto;
background-color: green;
}
footer {
z-index: 0;
position: fixed;
bottom: 0;
width: 100%;
height: 6em;
}
FIDDLE #1
In this Fiddle the height of the inner div (green) is not expanding to the height of the outer div(pink).
FIDDLE #2
Seems fixed if i set the height of the outer div from min-height to height but there is another problem if the height of the inner div is more than 100% as you can see in FIDDLE #3
Is there any pure css solution for this problem?
Thanks in advance!
Remove the height for body or make it height:auto;
Check this FIDDLE
CSS change
body{
height:100%; // remove this and add below line
height:auto;
}