I have this code:
<div id="main_wrapper" style="width: 80vw; overflow: hidden;">
<div id="inner_wrapper" style="position: relative;">
<div id="red" style="background-color: red;width: 20vw;height: 100px;position: absolute;left: 80vw;"></div>
<div id="blue" style="background-color:blue; height:100px; margin: 0px 1% 0px 1%;"></div>
</div>
</div>
Here is the js-fiddle: https://jsfiddle.net/omriman12/msrpwnym/1/
Basically the main_wrapper over-hidden's inner_wrapper, and what i want to do is to align red to the right of blue element.
My problem is that red gets over-hidden when i use relative.
I must keep it relative! This is part of the question, dont change the structure!
Here is what i am actually trying to create:
Try this. The problem is that you set #main_wrapper to width: 80vw and overflow-x: hidden. That caused any child will be hidden if those children are wider than 80vw. You have to wide #main_wrapper to 100vw and set the width #inner_wrapper with 80vw for displaying child of #main_wrapper.
By the way, you need to dynamically resize the width of #main_wrapper when each row is clicked as below snippet.
$('.row').click(function() {
$(this).toggleClass('selected');
if($(this).hasClass('selected')) {
$('#main_wrapper').width('100vw');
} else {
$('#main_wrapper').width('80vw');
}
});
#main_wrapper{
width: 80vw;
overflow-y: scroll;
overflow-x:hidden;
}
#inner_wrapper{
position: relative;
width: 80vw;
}
#blue{
background-color:blue;
height:100px;
margin: 0px 1% 0px 1%;
}
#red{
background-color: red;
width: 20vw;
height: 100px;
position: absolute;
left: 80vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="main_wrapper">
<div id="inner_wrapper" class="row">
<div id="red" ></div>
<div id="blue"></div>
</div>
</div>
Related
I want the third div down, "contents", to fill its container but leave exactly 200px of space on the right side to fit the fixedWidthButtons.
So far, no matter what I set right to, it doesn't affect the width of the div.
If I set its display:block; it fills the container completely and the buttons get pushed out of the container.
If I set the display:inline-block;, the container becomes 181.344 px wide and won't resize no matter what I set right to.
<div class="container" style="left:0; right:0; margin-bottom: 10px; height: 65px; display: block;">
<div class="panel" style="width:100%; display: block;">
<div class="contents" style="display:inline-block; position:relative; left: 0px; right: 200px;">
<div class="buttonTextAndCounterContainer" style="width:100%; display:block">
<div class="button" style="float:left; display:none;"></div>
<div class="textAndCounterContainer" style="display:block;">
<div class="counter" style="float:right; display:block;"></div>
<div class="text" style="width:100%; position:relative; left:0px; vertical-align:top; display:inline-block;"></div>
</div>
</div>
</div>
<div class="fixedWidthButtons" style="display:inline-block; float:right;"></div>
</div>
</div>
You need to read up on some of properties you're dealing with. Position is for a point of origin not width.
You also express some issues with block versus inline-block these are easily googled. That said a solution to your problem is to change the css:
.content {
display: inline-block;
width: calc(100% - 200px);
}
Few suggestions:
position: absolute works relative to where its relative is (parent who is positioned relative is).
position: relative informs that the element is not positioned (without changing the layout at all) and make it's children if set to absolute position behave relative to it's parent
Setting inline-block also give us the provision of setting width and height which it would adjust to; if that is not needed, better off to go with inline.
It is good to remove the inline-styles - sample snippet below
.container {
margin-bottom: 10px;
height: 65px;
border: 1px solid black;
position: relative;
}
.contents {
border: 1px solid grey;
position: absolute;
display: inline-block;
width: 300px;
left: 0;
}
.fixedWidthButtons {
display: inline-block;
width: 200px;
position: absolute;
right: 0;
border: 1px solid red;
}
<div class="container">
<div class="panel">
<div class="contents">
<div class="buttonTextAndCounterContainer">
<div class="button">button</div>
<div class="textAndCounterContainer">
<div class="counter">counter</div>
<div class="text">text</div>
</div>
</div>
</div>
<div class="fixedWidthButtons">For my buttons</div>
</div>
</div>
Solution:
To set a div's width by using only left and right, you must set the div's position: absolute;
Source: http://alistapart.com/article/conflictingabsolutepositions
I'm trying to put two images side by side and make them responsive. The problem now is, that the second image wraps first and then reacts to the size of the browser.
I want them to stay on the same line (level) and change their size automatically and wrap at a certain point (this part isn't the problem)....
The html:
<div id="wrapper">
<div id="outer">
<div class="itemwrapper">
<img src="item2.jpg" alt="bag" />
</div>
<div class="itemwrapper">
<img src="item3.jpg" alt="pen" />
</div>
</div>
</div>
The css:
#wrapper {
max-width: 1050px;
margin: 60px auto 60px auto;
background-color: #DDD
}
.itemwrapper {
display: inline;
}
img {
max-width: 100%;
height: auto;
}
use display table to set it side by side and keep it side by side and responsive.
display: table; with table-layout: fixed; will create a fluid layout for child elements with display: table-cell;
this will not only keep them the same width but also keep the containers the same height.
vertical-align: top; will keep them aligned to the top alternatively you can change the vertical position to middle and bottom plus some others.
Any questions just fire away.
#wrapper {
max-width: 1050px;
margin: 60px auto 60px auto;
background-color: #DDD
}
#outer {
display: table;
width: 100%;
table-layout: fixed;
}
.itemwrapper {
display: table-cell;
vertical-align: top;
width: 100%;
}
img {
max-width: 100%;
height: auto;
}
<div id="wrapper">
<div id="outer">
<div class="itemwrapper">
<img src="item2.jpg" alt="bag" />
</div>
<div class="itemwrapper">
<img src="item3.jpg" alt="pen" />
</div>
</div>
</div>
if image are same size or same ratio, you may use flex , width and min-width to set a break point:
#outer {
width:70%;/* demo*/
margin:auto;/* demo*/
display:flex;
flex-wrap:wrap;
}
#outer>div {flex:1;}
#outer>div>img {
width:100%;
min-width:200px;/* demo*/
}
<div id="wrapper">
<div id="outer">
<div class="itemwrapper">
<img src="http://lorempixel.com/200/100" alt="bag" />
</div>
<div class="itemwrapper">
<img src="http://lorempixel.com/400/200" alt="pen" />
</div>
</div>
</div>
remove or reset to your needs the rules commented with demo.
Thanks for the help, but I'm doing it with a different solution now, whicha friend suggested:
#outer {
position: relative;
width: 50%;
height: 0;
margin: 30px auto 0 auto;
padding-top: 25%;
background-color: #999;
}
.itemwrapper {
position: absolute;
width: 50%;
height: 100%;
top: 0;
}
.item2 {
left: 50%;
}
#outer img {
height: 100%;
max-width: 100%;
}
<div id="wrapper">
<div id="outer">
<div class="itemwrapper">
<img src="http://lorempixel.com/200/100" alt="bag" />
</div>
<div class="itemwrapper item2">
<img src="http://lorempixel.com/400/200" alt="pen" />
</div>
</div>
</div>
This evokes another problem though. The images arent filling the itemwrappers. I think i need to write some js for this :S.
I am making a data visualisation(vis) and I want the vis to have a key (like a sidebar) for the different colors. I have been doing this by making a div and floating it left and the vis floated to the right:
HTML
<div id="Container">
<div id="key"></div>
<div id="visContainer">
<div id="vis"></div>
</div>
</div>
CSS
#key{
float:left;
width:5%;
}
#visContainer{
float: right;
width: 95%;
}
#vis{
outline: solid;
outline-color: black;
}
But obviously the vis div will not always be centered on the screen. Is there a way to make this possible (i.e. have a centered vis that has a sidediv that does not impede on the centrality of the vis div)?
p.s. If possible I want to avoid using absolute positions as id like this to scale with the page size.
You can use percentages to calculate the the center of the screen in the vis container element:
50% * 95% = 47.5%
That's where the center of your vis element should be. Now just subtract half of its width and you're good to go:
47.5% - (10% / 2) = 42.5%
See it here:
#key{
float:left;
width:5%;
height: 30px;
background: yellow;
}
#visContainer{
float: right;
width: 95%;
height: 30px;
background: cyan;
position: relative;
}
#vis{
outline: solid;
outline-color: black;
width: 10%;
margin-left: 42.5%;
}
<div id="Container">
<div id="key"></div>
<div id="visContainer">
<div id="vis"></div>
</div>
</div>
Of course, you can avoid all that math by using absolute position on the sidebar (it will still scale). Beware that now the 10% width of the vis element is 10% of the full body width since the container is 100% wide (going "under" the #key sidebar").
See it here:
#key{
position:absolute;
z-index: 1;
width:5%;
height: 30px;
background: yellow;
}
#visContainer{
height: 30px;
background: cyan;
position: relative;
}
#vis{
outline: solid;
outline-color: black;
width: 10%;
margin: auto;
}
<div id="Container">
<div id="key"></div>
<div id="visContainer">
<div id="vis"></div>
</div>
</div>
HTML
<div class="main-block">
<div id="Container">
<div id="key">ddd</div>
<div id="visContainer">
<div id="vis">sss</div>
</div>
</div>
</div>
CSS
.main-block{
display: inline-block;
width: 100%;
text-align: center;
}
#container{
width: 50%;
display: inline-block;
}
My code structure looks like this
<div style="height: 100px;
Width: 200px;"> <!-- Container -->
<div style="float: left;
height: 50px;
Width: 100px;
background-color: red;">
</div>
<div style="float: left;
height: 50px;
Width: 100px;
background-color: blue;">
</div>
<div style="float: right;
height: 50px;
Width: 100px;
background-color: green;">
</div>
</div>
But the right position of elements should look like this:
┌──────┬──────┐
│ red │green │
├──────┼──────┘
│ blue │
└──────┘
I cannot change or add any additional code, the only way is with CSS.
How should I float the divs to be in the right order as I mentioned above?
Edit: My code doesn't and can't contain div with clear.
you dont need floating for that. disable all floating using !important to override the inline styles, and then use :nth-of-type() to select the green div and position it absolutely with right and top equal 0;
div {
position: relative;
}
div > div{
float: none !important;
}
div > div:nth-of-type(3) {
position: absolute;
right: 0;
top:0;
}
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;">
</div>
<div style="float:left; height: 50px; Width:100px; background-color:blue;">
</div>
<div style="float:right; height: 50px; Width:100px; background-color:green;">
</div>
</div>
You can use clear: left on the blue box to push it down and then use negative margin on the green box to push it up.
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left;height: 50px;
width:100px; background-color:red;">
</div>
<div style="float:left;clear:left;
height: 50px; Width:100px; background-color:blue;">
</div>
<div style="float:left; height:50px;
width:100px; background-color:green;margin-top:-50px;">
</div>
</div>
Well this is more like a puzzle instead of a legit question but here goes.
With the proper use of margins and positions in addition to assigning null to clear property one can accomplish your scenario.
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;"></div>
<div style="float: right; height: 50px; margin-top: 50px;Width:100px; background-color:blue;position: absolute;"></div>
<div style="clear: none;"></div>
<div style=" height: 50px; margin-left: 100px;margin-bottom: 50px;Width:100px; background-color:green;"></div>
</div>
</div>
Keeping the same HTML structure, you could select the divs in CSS using :nth-child(N). In this case you'd just need to update the blue (2) and green (4) boxes, and the one with the clear:both style (3):
div > div:nth-child(2) {
margin-top: 50px;
}
div > div:nth-child(3) {
display: none;
}
div > div:nth-child(4) {
margin-top: -100px;
}
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;">
</div>
<div style="float:left; height: 50px; Width:100px; background-color:blue;">
</div>
<div style="clear:both;"></div>
<div style="float:right; height: 50px; Width:100px; background-color:green;">
</div>
</div>
Notice that this will work for this particular example. It would be ideal if the container div had an id and use that instead of div >.
For a more generic solution that would work independently of the height of the boxes, you could use transform:translate() like this:
div > div:nth-child(2) {
transform:translate(0%, 100%);
}
div > div:nth-child(3) {
display:none;
}
div > div:nth-child(4) {
transform:translate(0%, -100%);
}
As you can see on this JSFiddle: http://jsfiddle.net/eekhjv3n/1/
I want to have one <div> with id that has horizontal scroll, but the problem is it has to be responsive, not with fixed width.
html, body {margin: 0; padding: 0;}
#myWorkContent{
width:530px;
height:210px;
border: 13px solid #bed5cd;
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
}
#myWorkContent a {
display: inline-block;
vertical-align: middle;
}
#myWorkContent img {border: 0;}
<div id="myWorkContent">
<img src="http://placekitten.com/200/200/" height="190">
<img src="http://placekitten.com/120/120/"/>
<img src="http://placekitten.com/90/90/" height="90" width="90">
<img src="http://placekitten.com/50/50/" height="190">
<img src="http://placekitten.com/100/100/">
<img src="http://placekitten.com/200/200/" height="190">
</div><!-- end myWorkContent -->
Thanks to http://jsfiddle.net/clairesuzy/FPBWr/
The problem is with that 530px. I would like to use 100% instead. But then I got page scroll and scroll of the DIV goes right, can not get it, any idea?
Here is article in Serbian about solution
http://www.blog.play2web.com/index.php?id=18
Just set your width to auto:
#myWorkContent{
width: auto;
height:210px;
border: 13px solid #bed5cd;
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
}
This way your div can be as wide as possible, so you can add as many kitty images as possible ;3
Your div's width will expand based on the child elements it contains.
jsFiddle
Below worked for me.
Height & width are taken to show that, if you 2 such children, it will scroll horizontally, since height of child is greater than height of parent scroll vertically.
Parent CSS:
.divParentClass {
width: 200px;
height: 100px;
overflow: scroll;
white-space: nowrap;
}
Children CSS:
.divChildClass {
width: 110px;
height: 200px;
display: inline-block;
}
To scroll horizontally only:
overflow-x: scroll;
overflow-y: hidden;
To scroll vertically only:
overflow-x: hidden;
overflow-y: scroll;
Just make sure you add box-sizing:border-box; to your #myWorkContent.
http://jsfiddle.net/FPBWr/160/
use max-width instead of width
max-width:530px;
demo:http://jsfiddle.net/FPBWr/161/
I figured it this way:
* { padding: 0; margin: 0 }
body { height: 100%; white-space: nowrap }
html { height: 100% }
.red { background: red }
.blue { background: blue }
.yellow { background: yellow }
.header { width: 100%; height: 10%; position: fixed }
.wrapper { width: 1000%; height: 100%; background: green }
.page { width: 10%; height: 100%; float: left }
<div class="header red"></div>
<div class="wrapper">
<div class="page yellow"></div>
<div class="page blue"></div>
<div class="page yellow"></div>
<div class="page blue"></div>
<div class="page yellow"></div>
<div class="page blue"></div>
<div class="page yellow"></div>
<div class="page blue"></div>
<div class="page yellow"></div>
<div class="page blue"></div>
</div>
I have the wrapper at 1000% and ten pages at 10% each. I set mine up to still have "pages" with each being 100% of the window (color coded). You can do eight pages with an 800% wrapper. I guess you can leave out the colors and have on continues page. I also set up a fixed header, but that's not necessary. Hope this helps.