CSS set width of first element based on width of second element - css

I'm not sure exactly how to work what I'm trying to accomplish so bear with me...
I'm trying to set the width of an element, based on the width of a proceeding element. visually it would look like this:
container element
--------------------------------------------
| | |
| | |
|<--auto width for div1-->|<--div2 200px-->|
| | |
| | |
--------------------------------------------
So, essentially, div2 is set to float on the right of the page, and div1 should span up to the right edge of div2 automatically
I've started a fiddle at: http://jsfiddle.net/yUvJs/
Any help would be greatly appreciated... thanks in advance...

Just use tables. Get rid of the float, set the two box divs to display:table-cell and the container div to display:table. The first box should then adjust its width automatically based on the width of the second box.
Fiddle example

You can use the calc() function which is new in CSS 3, to allow the first <div> span 100% width of its containing block minus the 200px of the second <div> - just remember to account for the 2px of border on each child element:
#box1 {
display:inline-block;
border:1px dashed blue;
height:100%;
opacity:.8;
background:#ccc;
width: -webkit-calc(100% - 204px);
width: -moz-calc(100% - 204px);
width: calc(100% - 204px); /* 2px + 2px dotted border */
}
Example

I suggest implementing this in JavaScript. You would do something like this in JavaScript:
var cont = document.getElementById("container");
var box2 = document.getElementById("box2");
var box1Width = cont.offsetWidth - box2.offsetWidth;
document.getElementById("box1").style.width = box1Width + "px" ;
I implemented this on your fiddle with a couple minor changes to demonstrate that it works. Check it out: http://jsfiddle.net/yUvJs/26/

I'm not sure of all your requirements, but if you can reverse the html order:
<div id='container'>
<div class='box' id='box2'></div>
<div class='box' id='box1'></div>
</div>
And remove the display: inline-block (which should not be needed when floating), but apply a overflow: hidden (and its magic) to the box1 then you get what you want as seen in this fiddle.

I know this question is old, but for the above case, can use flex:
HTML:
<div id='container'>
<!-- If you want to give id/class is up to you, css selectors can do it pretty well too for generalization purposes -->
<div>...data...</div>
<div>...data...</div>
</div>
CSS:
#container{
display:flex; flex-direction:row;
width:700px; border:1px solid black; height:300px;
}
#container > div:nth-child(1){
flex:auto;
border:1px dashed blue; height:100%; opacity:.8; background:#ccc;
}
#container > div:nth-child(2){
flex:initial;
border:1px dashed red; width:200px; height:100%; opacity:.8; background:#eee;
}
Try it, and see how display flex solves your problem much more elegant

Related

CSS wrapper div's background to cover content

I have 2 divs inside a wrapper div and I was wondering if it's possible to bring the #wrapper div on top of the content (#outer and #inner).
<div id="wrapper">
<div id="outer">
<div id="inner"></div>
</div>
</div>
I want the #wrapper to add a transparent background without making any changes to the HTML. I have tried doing so using z-index without success.
See this fiddle: http://jsfiddle.net/nPpDE/
Any help is much appreciated.
Managed it using :after- http://jsfiddle.net/t6mMR/ -No extra html!
Like this:
#wrapper:after {
position:relative;
top:-200px;
left:0px;
content:"";
width:400px;
height:200px;
display:block;
background:rgba(255, 0, 0,0.5)
}
The pseudo-element is placed above the others, and a semi transparent background applied to it.
__
EDIT: A slightly different way of doing it- (see comment below) (using position:absolute
http://jsfiddle.net/t6mMR/1/
__
Note- To be able to "click through" the pseudo-element, add pointer-events: none; to it.
http://jsfiddle.net/t6mMR/1/
To get this to work in IE, see css 'pointer-events' property alternative for IE, it may help.
You can give the children position: relative and z-index: -1 (or otherwise negative value), but I'm not sure how buggy that is or what the browser support is.
some more info available here: http://philipwalton.com/articles/what-no-one-told-you-about-z-index/
Here's a quick example: http://codepen.io/Rykus0/full/jhwev
Otherwise, as others have said, you need to include a new element and position using either absolute or fixed
What you are asking is not possible.
However, it is possible when you add another div inside the #wrapper and position it with
position:absolute;
and give it a transparent color
http://jsfiddle.net/nPpDE/1/
EDIT: Harley's solution is better since the OP doesn't want to change the HTML
?? and what about having opacity colors on inner containers and regular color on main container:
#wrapper{
position:relative;
background:black;
width:400px;
height:200px;
}
#outer{
position:relative;
width:400px;
height:200px;
background: rgba(50,0,0,0.75);
}
#inner{
position:relative;
width:350px;
height:200px;
background:rgba(0,50,0,0.75);
margin: 0 auto;
}
fiddle that goes with it :) http://jsfiddle.net/nPpDE/2/

Set full width for the inner div

I have a parent div, Inside that div I have two levels of children div as follow,
<div class="grandParant">
<div class="parant1">test</div>
<div class="parant2">
<div class="child">Hello world this is a long test string</div>
<div class="child">12</div>
<div class="child">4545</div>
</div>
</div>
from the above sample code, I need to show the entire first "child" class content(Hello world this is a long test string) without any break, ie in a single line. The width of the "parant2" div should also be incremented with respect to the child width. So how could this be done with css? I am not posting my css since it is a little bit lengthy, but you can see it in jsfiddle.
EDIT
my expected output is more like the alphabet 'L'
| test |
| Hello world this is a long test string |
| 12 |
| 4545 |
my jsfiddle
If you remove the max-width take parant1 outside of it's grandparent, you'll get your desired result of non-wrapping:
DEMO: http://jsfiddle.net/9Sha7/18/
You can do that by removing "max-width" ...
Where ever if you give "max-width" it only get upto that extent only,beyond that width it will break the lines
Put css only like
min-width:100px;
Here is fiddel http://jsfiddle.net/9Sha7/8/
take out the padding
.grandParant{
margin:0 auto;
float:left;
min-width:100px;
max-width:120px;
height:150px;
position:relative;
border-left: 1px solid #000000;
border-right: 1px solid #000000;
background:#cccccc;
font-size:11px;
cursor:pointer;
}
.parant1{
width:auto;
margin-bottom:22px;
text-align:center;
background:blue;
}
.parant2{
text-align:left;
width:auto;
background:#f3f3f3;
}
.child{
width:100%;
position:static;
background:green;
}
}
Just get rid of all the width attributes and the max-width, then it should be fine according to your requirements: http://jsfiddle.net/9Sha7/13/
Remove max-width from your .grandParent class and give white-space:nowrap; to your .child class.
Working Fiddle

100% minus a textnode

I've got this simple markup:
<div id="parent">
<div id="static">
Hello Random
</div>
<div id="max">
100% of the rest
</div>
​</div>​
I want that the div with ID max should be 100% width as its parents minus the width of the element with ID static. static contains just a single textnode with some random words that I don't know.
I have tried this CSS but don't know exactly how to solve it:
#parent{
width:100%;
border:1px solid #FF0000;
float:left;
}
#static{
float:left;
border:1px solid #00FF00;
}
#max{
float:left;
width:90%; // It's not the same as minus so this will just fail...
}
​
This is my jsFiddle that I have tried with:
http://jsfiddle.net/WnceY/
I want to use pure CSS no JS. In this moment I don't care about IE either.
Just don't float everything.
http://jsfiddle.net/WnceY/4/
Only the #static needs to float. Then the rest will take care of itself.
I solved it with this CSS:
http://jsfiddle.net/WnceY/9/

Make <div> as wide as page

Let's say I have a <div> (#container) that's set to 960px for it's width. Inside that <div> , I want to create another <div> (#drawer) that's as wide as the page window. So basically, I would like to create a <div> within a <div> that's wider than its parent <div>:
<div id="container"> // Set at 960 px
<div id="drawer"> // I'd like this to be as wide as the window
</div>
</div>
CSS:
#content {
top:200px;
position:absolute;
width: 940px;
padding-bottom:100px;
}
#drawer {
????
}
---Update---
Hey Everyone,
Thanks for all the answers. I guess I should make my answer a little easier to follow. See below for some sort of visual description. I hope it helps!
Well you could set botht he left and right values if you make it absolutely positioned. This way you can still use padding directly on the #drawer if you want to.
#container {
top:200px;
width: 940px;
padding-bottom:100px;
background-color:rgb(255,0,0);
}
#drawer {
position:absolute;
right: 0px;
left:0px;
background-color:rgb(0,255,0);
}
I don't think it's possible for a child div to be wider then its parent. Maybe if you told us what you were trying to accomplish, we could help you.
i dunno what you're trying to do with that. But, i think this code works (by just removing "position : absolute" in #content :
<html>
<head>
<style type="text/css">
#content {
top:200px;
width: 940px;
padding-bottom:100px;
}
body {
margin:0px;
padding:0px;
}
#drawer {
background-color:blue;
top:0px;
position:absolute;
width:100%;
height:100%;
}
</style>
</head>
<body>
<div id="content">
<div id="drawer">
a
</div>
</div>
</body>
</html>
you can find out the width of the screen on pageload using javascript and then set the css width value to the same. this is a bad way of doing it....but its still a way. Why do you need to do this btw ?
I second rudeovski ze bear's comment. However, if you want to do this, you'll need to set the width explicitly (you can't rely on width: 100%, because it will always use the containing div for reference).
So you'll need something like:
#drawer
{
width: 1200px;
}
You can use a little jQuery to make this more dynamic:
$(function() {
var windowWidth = $(window).width();
$('#drawer').css('width', windowWidth);
});
You can use negative margins and calc() to calculate the 100vw - container width, all negated and divided by 2 for the left and right margin.
Because you know the width of your parent container, 940x in this case, the negative margins for the #drawer would be:
margin-left: calc(-100vw / 2 + 940px / 2);
margin-right: calc(-100vw / 2 + 940px / 2);
Tip!
To make it nicer, you can use a variable for 940px. If you use SASS, I'm sure you already know how to use variables there.
If you use CSS:
:root {
--container-width: 940px;
}
and then:
margin-left: calc(-100vw / 2 + var(--container-width) / 2);
margin-right: calc(-100vw / 2 + var(--container-width) / 2);
(Before using var, please ensure it is supported by the browsers you need: https://caniuse.com/css-variables)
You can watch it in action here, but please make sure your page is wider than 940px: https://stackblitz.com/edit/js-jscs4f

CSS height calculation doesn't add up

I have the following code:
<div id="headerwrap">
<div id="headertop">aaa</div>
<div id="headermiddle">abc</div>
<div id="headerbottom">def</div>
</div>
#headerwrap { position:fixed; top:0; left:0; }
#headertop { height:55px; margin:0 auto; }
#headermiddle { height:25px; margin:0 auto; }
#headerbottom { height:9px; margin:0 auto; }
I am trying to follow the header with a fixed position . When I do this I find an overlap. Checking with firebug I find the following:
headerwrap height - 91px
headertop height - 55px
headermiddle height - 25px
headerbottom height - 9px
Can anyone explain to me why the numbers don't add up? This is giving me position problems and I can't see what's wrong.
Why does 55+25+9 add up to 91?
I get 89px, here's a demo.
Are you sure that's all the CSS and that there aren't borders or extra styles on any of the elements?
EDIT: Just seen your comment on the other answer :)
Are you sure the content in #headerbottom isn't taller than 9px? You might need to use overflow-y: hidden;
By the way, with that code, headerwrap should have the same height as headertop, it shouldn't be the sum of the others, because 9 + 25 is not greater than 55.

Resources