I am experiencing some trouble while positioning an absolute div inside a relative one. I want my absolute div (inline-block) to grow until it reaches a given px-amount (max-width). This works as expected, until I add a width (smaller than the max-width of the absolutes div) to the relative div.
I want the text in the absolute-div to break at the max-width (400px) and not at the edge of the relative parent div (300px).
When giving white-space: nowrap, the words just flow over the absolute divs end.
Have anyone an idea how to solve this?
Thanks!
See:
http://codepen.io/anon/pen/KVJvmZ
html
<div class="relativeContainer">
<div class="absoluteContainer">
Hello you! This breaks on relativeContainers edge.. This is not what i want. It should just go further an further (until it reaches max-width of 400px).
</div>
</div>
<div class="relativeContainer">
<div class="absoluteContainer">
This should stay small.
</div>
</div>
css
.relativeContainer {
width: 300px;
height: 100px;
border: 1px solid black;
position: relative;
margin-bottom: 50px;
}
.absoluteContainer {
display: inline-block;
position: absolute;
top: 0;
left: 0;
max-width: 400px; /* Word-break should happen here. */
border: 1px solid red;
}
I am afraid it is not possible to solve this issue with your markup. But there is light at the end of the tunnel: You could change your markup or use javascript to achieve what you want.
Depending on your requirements, this could help you: http://codepen.io/anon/pen/eJXYOJ
html
<div class="relativeContainer">
<div class="absoluteContainer">
<div class="contentContainer">
Hello you! This breaks on relativeContainers edge.. This is not what i want. It should just go further an further (until it reaches max-width of 400px).
</div>
</div>
</div>
<div class="relativeContainer">
<div class="absoluteContainer">
<div class="contentContainer">
This should stay small.
</div>
</div>
</div>
css
.relativeContainer {
width: 300px;
height: 100px;
border: 1px solid black;
position: relative;
margin-bottom: 50px;
}
.absoluteContainer {
position: absolute;
width: 100vw; /* do a large number of px for ie8 compatibility*/
top: 0;
left: 0;
background-color: lightgray; /* just to show you what I've done*/
}
.contentContainer {
display: inline-block;
max-width: 400px; /* Word-break should happen here. */
border: 1px solid red;
}
Absolute container is directly related to the relative parent container.
There is no way to make an absolute container bigger (width or height) than a relative parent container.
If you want an absolute container bigger (width or height) than his parent the parent should not be relative.
Hope this help.
Have a good one
I don't think what you want to do is possible without using another class, or using JS. Here's how you can do it with css:
<div class="relativeContainer">
<div class="absoluteContainer bigger">
Hello you! This breaks on relativeContainers edge.. This is not what i want. It should just go further an further (until it reaches max-width of 400px).
</div>
</div>
<div class="relativeContainer">
<div class="absoluteContainer">
This should stay small.
</div>
</div>
.relativeContainer {
width: 300px;
height: 100px;
border: 1px solid black;
position: relative;
margin-bottom: 50px;
}
.absoluteContainer {
display: inline-block;
position: absolute;
top: 0;
left: 0;
max-width: 400px; /* Word-break should happen here. */
border: 1px solid red;
}
.absoluteContainer.bigger{
width: 400px;
}
I have looked at your example and I don't think you can do what you want if the absolute is inside the relative and you don't specify a width. Currently, with only a max-width, the inner absoluteContainer has no reason to go outside the relative container so it won't. Once you set a width, you get what you want but the small cannot stay small! You might be able to 'spoof' what you want by locating the absolute outside the relative but in the same location. This gives you something of what you want - but it won't (say) scroll the relative one if the absolute is bigger.
Example at: http://codepen.io/anon/pen/Nxovey
If you don't want to (or can't) identify longer text in CSS with extra classes then this is the best you can do without javascript.
Code:
<div class="spoofContainer">
<div class="absoluteContainer">
Hello you! This breaks on relativeContainers edge.. This is not what i want. It should just go further an further (until it reaches max-width of 400px).
</div>
</div>
<div class="relativeContainer">
</div>
<div class="spoofContainer">
<div class="absoluteContainer">
This should stay small.
</div>
</div>
<div class="relativeContainer">
</div>
CSS:
.spoofContainer {
width: 400px;
height: 0px;
overflow: visible;
position: relative;
}
.relativeContainer {
width: 300px;
height: 100px;
border: 1px solid black;
position: relative;
margin-bottom: 50px;
}
.absoluteContainer {
display: inline-block;
position: absolute;
top: 0;
left: 0;
max-width: 400px; /* Word-break should happen here. */
border: 1px solid red;
}
Related
I have tried a lot of things and searched online but I cannot figure out the solution to this problem.
I have a div container which has a max-height, min-height and also overflow: auto. When the inner content is larger than the max-height, a scrollbar appears as expected. But, inside the content there is a dropdown, which when clicked, the menu expands, and instead of being displayed outside the parent container, it is like changing the inner content height.
The only solution I found online and made sense to me, is to wrap the container to div with relative positioning and make the dropdown absolute, but there is a big drawback now, the dropdown stays fixed on scroll, as it is absolute positioned relative to the wrapper and not the content. Is there any common way to fix this or any other solution ?
I didn't post any code because I do not want the answer to rely on my code.
I just want a minimal example if possible with these properties:
Container has a max-height
If content is larger than the container's max-height then the container should display a scrollbar.
The content has a dropdown which should scroll with every other element of the content.
The menu options of the dropdown element are escaping the container / are displayed outside the boundaries of the container.
To illustrate on my comments on the question, here's an MCVE:
.scroll-container {
border: 3px dashed #eee;
height: 400px;
padding: 10px;
overflow: auto;
width: 400px;
}
.content {
background-color: #f0f0f0;
height: 600px;
position: relative;
}
.dropdown {
background-color: orange;
position: absolute;
height: 300px;
width: 300px;
left: 300px;
}
<div class="scroll-container">
<div class="content">
<div class="dropdown"></div>
</div>
</div>
As you can see, with absolute positioning based on the relative position of div.content the orange div.dropdown creates a horizontal overflow, which is what you don't want. To fix this scenario, you need to remove position: relative from div.content and use transform: translateX(300px); instead of left: 300px;:
.scroll-container {
border: 3px dashed #eee;
height: 400px;
padding: 10px;
overflow: auto;
width: 400px;
}
.content {
background-color: #f0f0f0;
height: 600px;
}
.dropdown {
background-color: orange;
position: absolute;
height: 300px;
width: 300px;
transform: translateX(300px);
}
<div class="scroll-container">
<div class="content">
<div class="dropdown"></div>
</div>
</div>
I have a fixed size, scrollable parent div containing two children: The first contains the content, and should determine the size of the scrollable area. The second is small overlay that is moved around dynamically, and should not affect the size of the scrollable area. That is, if the overlay overhangs the bottom edge of the content, and the user scrolls all the way down (assuming the content is larger than the parent div), scrolling should stop once they hit the bottom of the content, even if that means only part of the overlay is visible.
Is this possible to achieve?
E.g., with this HTML:
<div id="parent">
<div id="content">
<img src="640x423_image.jpg">
</div>
<div id="overlay"></div>
</div>
and this CSS:
#parent {
position: relative;
height: 400px;
width: 400px;
overflow: auto;
border: 1px solid red;
}
#overlay {
position: absolute;
height: 20px;
width: 20px;
background-color: lightblue;
top: 413px;
left: 15px;
}
(JSFiddle), the browser lets the user scroll past the bottom of the content until the entire overlay (the blue square) is visible, while I would like it to stop once the bottom of the image is visible, leaving the bottom 50% of the overlay still hidden.
For my use case, I am able to make adjustments to the HTML if necessary.
Put square box and img inside same container (#content in your case), and set overflow: hidden; on it.
#parent {
position: relative;
height: 400px;
width: 400px;
overflow: auto;
border: 1px solid red;
}
#content {
overflow: hidden;
position: relative;
}
#overlay {
position: absolute;
height: 20px;
width: 20px;
background-color: lightblue;
top: 413px;
left: 15px;
}
<div id="parent">
<div id="content">
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAFBQUFBVUFpkZFp9h3iHfbmqm5uquf/I18jXyP////////////////////////////////////////////////8BUFBQUFVQWmRkWn2HeId9uaqbm6q5/8jXyNfI///////////////////////////////////////////////////CABEIAacCgAMBIgACEQEDEQH/xAAYAAADAQEAAAAAAAAAAAAAAAAAAQIDBP/aAAgBAQAAAADnG4vowbzG0y3fNV4o16crpU2QkqdZuiWm+eL6JBMZDuJSw0DSiOd2QLTUmZllzchaxH0isptmOmGxSigQN80b6IBA41JM4z2ktXjkO04vSs1FICiL1wg03mqljdYXn0BDqUBc809NyyUMTJRk7RU6c8UrlBoOFQ5LUUZj6kiqAFA9QiqSBVHOutDQIjRCh4bKNAfOm6kqW3Fzcgo6DBGnVlcU3SM0rsSZSQzPF9kAIJLZOeRaWooyWsJ0yGTRtlRC1jMN2a522qrm0y6B5sYItYZ67xQIuHSMpw2I0pnPBrMMdJDTpOsNXgF6jb0G87wudWoZVZhb5J6xUMVZXUqIz3WHSkcobZXAOWFVGjmN8cx9VYWXaQ4J1pKWUkNvjrrhsps59KlHLsTn0xMYl3FPPSHFgJ1n0rlDTdQ9FQ6zx0Ww87TqUqefPr0Z0FO559GSc3ZnnlsljJrNCm7zSGqRXThgPXowudoubrLGq3UjToi2ckb6puabWNjnF65zO6z5x6xoKnlNDeW0xtvwo001lDm1dYwb2ZaSmxbOOJ9cUVJVRnak5d3MFmWZdjBqG87jQhamAb7Z6Z6rOzTKZvcWVIoVWZc3RZaAsyTM4jUeOtnLBprDHJDCo6IyvbkRfU8qNIh3eMTvSloBl0ck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kS+a4LhLNjI95Yus0NURFiyyybt4QssXKfWILQ3SePHhZ7kTEXhD7eYe8vEnzQ+CHhfR74MiXyseEy83ldCLRePkfIssssm8zlrEeuG0iJLsXY8IfbzHvF5f2L6m83lfUnwbyuV4vLYu8T5SehdDIj5Lv6n914v8AoPCy/uYu8Psjmyx5WXwXB/Q+C/418bw3i8L71wf2L7b5X9bf02XjReKKHhfQua/u1/XWHworNDwuvseVl/auNfWs3xvhvi+F8tl5eFyrhZY/tfJZv/hXi+Flll/1N/1LL+topi4ULNYpizrNFFcHmuFfTXFL94f9DT5bwvrrF539eyhrFcXrFor6nimUkXl/Q/o6eUWP7/R6KwiilijYi2PjZrKebwyy89vnZbKbxf2V/QWVzXLvLd5f/priudovlX0W/wCokltiwuV/V0a5rHRa4UihcL4b+my839lcq+yvrby8oVm7ymXfrlZfFGsaLNF4s0NYfF/2lzrksddZvFmsJ2MT5XwVfS8t8KX7/r64P7axeLviimW8Xy0f+PO7GWbLKy8rlf8Abv6VwsvhZYxF6O+V49YYiuK4b+l81/wKZvhZXG+LWGexl42PNZZviy8X/wAOjrFNf1ErKxWNI7zQjRZY+i8XzvNPNcv/xAAUEQEAAAAAAAAAAAAAAAAAAACg/9oACAECAQE/AAcf/8QAFBEBAAAAAAAAAAAAAAAAAAAAoP/aAAgBAwEBPwAHH//Z">
<div id="overlay"></div>
</div>
</div>
it's a known 'bug' that elements with fixed position loose their position if the container is translated. For example, if i've got a structure like this:
<div class="container">
<div class="fixed"></div>
</div>
and, say, the container is scrolled, when the conteiner gets transformed (say, translate(x,y), rotate(), or so..), then the fixed element behaves like it was positioned relative and it scrolls with the container. I can see it on the latest firefox, for example.
How can one fix this kind of problem? Is there any way?
This behaviour is not a bug. It's actually the specs recommended behaviour.
(See this post by Eric Meyer, or this question here on SO which accepted solution only provides a link to the same meyer's post)
For those who don't know this issue, and because you didn't provide a snippet into your question, here's one.
document.addEventListener('click', function() {
document.getElementById('container').classList.toggle('transformed')
}, false);
#bg {
border: 1px solid #AFA;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
#container {
border: 1px solid #FAF;
height: 50%;
width: 75%;
position: relative;
margin: 0 auto;
overflow: auto;
}
#content {
background: rgba(125, 175, 0, .7);
position: fixed;
width: 100%;
top: 0;
left: 0;
}
.transformed {
transform: translate(0, 5em);
}
<div id="bg">
<div id="container" class="transformed">
.<br>.<br>.<br>.<br>.<br>.<br>.
this is a scrollable paragraph
<br>.<br>the "fixed" content does scroll with the paragraph
<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.
you can click to toggle the transformation On/Off
<br>.<br>.<br>.<br>.<br>.
<span id="content">relatively fixed content</span>
</div>
</div>
However, I did find something that may help others facing the same issue.
It's not really a solution, since the "fixed" element will be only inside the container, (except for IE browsers where it will really be fixed to the document). But in my case, it's actually what I wanted and maybe it'll be fine for others too.
If you add a wrapper, set its height:100%; width:100%; and overflow:auto, then your "fixed" content won't scroll with the container.
Actually it's not you container which scrolls anymore, but the wrapper. So you might want to set the container's overflow:visible or hidden to avoid unwanted scrolling of the not so well "fixed" element.
Also, note that you need your wrapper be a block or inline-block element.
#bg {
border: 1px solid #AFA;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
#container {
border: 1px solid #FAF;
height: 50%;
width: 75%;
position: relative;
margin: 0 auto;
overflow: visible;
}
#wrapper {
height: 100%;
width: 100%;
overflow: auto;
}
#content {
background: rgba(125, 175, 0, .7);
position: fixed;
width: 100%;
top: 0;
left: 0;
}
.transformed {
transform: translate(0, 50%);
}
<div id="bg">
<div id="container" class="transformed">
<div id="wrapper">
.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.
<span id="content">relatively fixed content</span>
</div>
</div>
</div>
I am not familiar with this bug, but when you use positioned: fixed; the element is positioned relative to the browser window, so it doesn't really make any sense to put it inside a container.
This markup would be my recommendation:
<div class="fixed"></div>
<div class="container"></div>
Once you use position: fixed; on any element it is positioned relative to the view-port. Directly from page in MDN about position property.
fixed
Do not leave space for the element. Instead, position it at a specified position relative to the screen's viewport and don't move it when scrolled.
So what you are experiencing is a what it is actually supposed to work like and not a 'bug'.
Now if what you want is something that is positioned with relation to the .container div and translate with it than you will have to use absolute positioning here. Take a look at this fiddle. The important CSS is-
.container {
width: 200px;
height: 100px;
position: relative;
}
.absolute {
position: absolute;
width: 20px;
height: 10px;
top: 50px;
left: 50px;
}
Notice that with positioning the inner div as absolute I have also positioned the outer div as relative as the inner div takes its position in reference to the closest parent div positioned as anything different from static.
Well I have such simple divs structure:
<div id="wrapper">
<div id="1" class="block">
1
</div>
<div id="2" class="block">
2
</div>
</div>
JS Fiddle
Content of #1 can dynamically changed by javascript and its height can be changed depended of content. What I want is to make #2 the same height as #1. I understand that I can use javascript for this purpose but I suggest there must be some not such tricky way to make those divs' heights equal using only CSS and/or changing divs' positioning some how.
To expand on my comment, you can't do it semantically. You have to use a little trick to fake the 100% height. It's called faux columns, and you can read more here.
In your case, we can fake it by adding a few background divs:
<div class="background bg1"></div>
<div class="background bg2"></div>
Then changing your CSS like so:
#wrapper {
border: 1px solid black;
position: relative;
}
#wrapper:after {
display: block;
content: " ";
height: 0;
clear: both;
}
.block {
position: relative;
float: left;
vertical-align: top;
width: 200px;
text-align: left;
min-height: 200px;
padding-left: 20px;
z-index: 2;
}
.background {
position: absolute;
top: 0;
bottom: 0;
width: 200px;
z-index: 1;
}
.bg1 {
background-color: #eee;
}
.bg2 {
left: 200px;
background-color: #aaa;
}
Here's a working jsFiddle.
The jQuery Way: Use .height() to return the height of Div1 and then simply use .css() to set Div2 to Div1's height. When div one changes, you can use the resize event to trigger a function that would change div2's height.
The CSS way: Christian Varga's answer seems to work perfectly.
SO,
I've created a four-column fluid-width layout for a site, and I'm working on placing a fluid square DIV within one of my columns. There are a few techniques I've found to achieve this - namely, setting padding-bottom to the same percentage as the width - but none of these seem to work when the DIV contains content.
Is there a way to maintain a 1:1 (square) ratio on a fluid DIV when that DIV contains content?
Here's my HTML:
<div id="leftmostcolumn">
<div id="logo"></div>
</div>
<div id="leftcolumn"></div>
<div id="rightcolumn"></div>
<div id="rightmostcolumn"></div>
And my CSS:
body {
width: 100%;
height: 100%;
background-color: red;
}
#leftmostcolumn {
position: absolute;
top: 0;
left: 0;
width: 25%;
height: 100%;
background-color: blue;
}
#leftcolumn {
position: absolute;
top: 0;
left: 25%;
width: 25%;
height: 100%;
background-color: green;
}
#rightcolumn {
position: absolute;
top: 0;
left: 50%;
width: 25%;
height: 100%;
background-color: yellow;
}
#rightmostcolumn {
position: absolute;
top: 0;
left: 75%;
width: 25%;
height: 100%;
background-color: gray;
}
#logo {
width:100%;
padding-bottom:100%;
background-color: #aa2d2d;
color: white;
}
And here's a JsFiddle.
The DIV "logo" is the one I'm trying to maintain as a square. Right now, I've used the padding-bottom approach but that doesn't do the trick when there's content in the DIV. Any input is greatly appreciated!
Marca
EDIT:
Getting there...I'm adapting a script I found to find the width of the DIV and then apply that value to the height to keep it a square. However, as it stands now the script doesn't constantly resize the DIV, and it won't allow it to shrink below a certain size. Any thoughts on how to correct either of these issues?
HTML:
<div id="box"></div>
CSS:
#box { width: 75%; height: 50px; background-color: black; }
JQUERY:
$("#box").css("height", function() {
return $(this).width();
});
JsFiddle is here.
This is something I've actually been messing around with for a while, and have come up with a quasi (but not entirely) hacky, CSS-only solution that seems to work on most browsers in the past decade. The trick is to use images, and positioning in a tricky fashion. Consider the following (simplification) of your code.
Markup:
<div class="sqr_box">
your content goes here!
</div>
CSS:
.sqr_box
{
width: 50%; /* or 100px, or 20em, or whatever you want */
border: solid 2px pink;
background-color: grey;
color: white;
}
Now, we can't set the height in terms of percent, so we won't; instead, first we'll go into Photoshop, and make an image that is 2x2 px, transparent, or background-colored. Next we'll add the following to your markup:
<div class="sqr_box">
<img src="images/sizers/2x2.png" class="sizer">
<div class="content">your content goes here!</div>
</div>
and THIS to your CSS:
.sqr_box
{
width: 50%; /* or 100px, or 20em, or whatever you want */
position: relative; /* static positioning is less than ideal for this scenario */
}
.sqr_box > img.sizer
{
display: block; /* images default to an inline-block like thing */
width: 100%;
height: auto; /* CLUTCH!!! this ensures that the image's height changes to maintain proportions with it's width */
visibility: hidden;
}
.sqr_box > .content
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%; /* Our parent element now has a dynamically assigned height, this will work */
border: solid 2px pink;
background-color: grey;
color: white;
}
Best of all, this will work for any sized ratio of box you'd want! Just change the proportions of the image!
Hope this is all still relevant to you, 3 months later.
-Sandy
Put all four columns in one div. set that div to 100% width and set the font size to 100em
Have each of your four columns have a width of 25em instead of 25%
Have your logo width and height set to 25em each