I would like to set a height of a second DIV based on the height of its sibling which comes above it and also the parent container which has both of these DIVs.
<div class="panel">
<div class="box-one">
<label>
<span class="label-text">Name:</span>
<input type="text" />
</label>
<label>
<span class="label-text">Description:</span>
<textarea name="" id="" cols="30" rows="5"></textarea>
</label>
</div>
<div class="box-two">
<div class="content">....</div>
</div>
</div>
Here is my SCSS code
.panel {
height: 300px;
border: solid 2px black;
background-color: #ccc;
display: table;
width: 50%;
padding: 20px;
}
.panel label {
display: block;
margin-bottom: 1em;
}
.panel label .label-text {
display: block;
}
.panel label input, .panel label textarea {
width: 90%;
}
.panel .box-one, .panel .box-two {
display: table-row;
}
.panel .box-two {
height: 100%;
border: solid 2px black;
overflow: auto;
background-color: #fff;
padding: 10px;
box-sizing: border-box;
}
.panel .box-two .content {
height: 100%;
overflow: auto;
}
Some how I kind of figured a solution with this CSS tables approach, but is there a better approach rather than this? Because this approach needs addtional DIVs to be wrapped around it.
Here is my codepen
http://codepen.io/nirmalkc/pen/jPRWKK?editors=110
BTW, I dont want to go with any javascript based approach.
If anyone, has an alternate for the above with a better apporach, that will be great.
flexbox can do that.
.panel {
height: 300px;
width: 50%;
margin: 10px auto;
border: 1px solid grey;
display: flex;
flex-direction: column;
}
.panel [class*="box"] {
flex: 1 0 auto;
}
.box-one {
background: lightblue;
}
.box-two {
background: lightgreen;
border-top: 1px solid red;
}
.tall {
height: 200px;
}
<div class="panel">
<div class="box-one">
</div>
<div class="box-two">
</div>
</div>
<div class="panel">
<div class="box-one tall">
</div>
<div class="box-two">
</div>
</div>
Related
I would like the button to be positioned at the bottom right of the red colored div. I used padding-bottom and margin-bottom properties but that does not seem to work. Could anyone please help?
.container {
display: flex;
flex-direction: column;
width: 300px;
height: 200px;
border: 1px solid blue;
padding: 8px;
}
.box {
width: 300px;
height: 150px;
border: 1px solid red;
}
.button {
float: right;
}
<div class="container">
<div class="box">
</div>
<div>
<button class="button">Click</button>
</div>
</div>
.button {
float: right;
position:relative;
transform:translate(-5px,-25px); //x and y controls
}
I have just answered the same thing to other question. ... Use position:relative. I see the point why people refrain from using it. But really ain't no shame. Especially when there isn't a parent-child relation between the elements.
.container {
display: flex;
flex-direction: column;
width: 300px;
height: 200px;
border: 1px solid blue;
padding: 8px;
}
.box {
width: 300px;
height: 150px;
border: 1px solid red;
}
.button {
float: right;
position:relative;
top: -22px;
}
<div class="container">
<div class="box">
</div>
<div>
<button class="button">Click</button>
</div>
</div>
An alternative to the other answers using display: grid instead. This is easier for the browser than using position absolute or float!!
/* ignore */ body { margin: 0 } * { box-sizing: border-box } /* ignore */
.container {
display: grid;
width: 50vw;
height: 100vh;
border: 1px solid blue;
padding: 8px;
}
.box, .button { grid-area: 1/1/-1/-1 }
.box { border: 1px solid red }
.button { margin: auto 0 0 auto }
<div class="container">
<div class="box">
</div>
<div class="button">
<button>Click</button>
</div>
</div>
I'm trying to center two buttons within the center of their parent element. The problem is that the first element has content in the beginning and end, which causes flex to center to that, messing up the alignment of the whole.
#container {
background-color: green;
width: 100%;
}
#d1 {
height: 50px;
display: flex;
}
#d2 {
background-color: blue;
width: 50px;
height: 50px;
}
#d3 {
background-color: red;
}
<div id="container">
<div id="d1">
<p>This is some text</p>
<input type="button">
<div id="d2"></div>
</div>
<div id="d3">
<input type="button">
</div>
</div>
Add a display: flex property on #container
#container {
background-color: green;
width: 100%;
display:flex
}
#d1 {
height: 50px;
display: flex;
}
#d2 {
background-color: blue;
width: 50px;
height: 50px;
}
#d3 {
background-color: red;
}
<div id="container">
<div id="d1">
<p>This is some text</p>
<input type="button">
<div id="d2"></div>
</div>
<div id="d3">
<input type="button">
</div>
</div>
You need justify-content: center; along with display:flex; on parent div.
#container {
background-color: green;
width: 100%;
display: flex;
justify-content: center;
}
#d1 {
height: 50px;
display: flex;
}
#d2 {
background-color: blue;
width: 50px;
height: 50px;
display: flex;
}
#d3 {
background-color: red;
}
<div id="container">
<div id="d1">
<p>This is some text</p>
<input type="button">
<div id="d2"></div>
</div>
<div id="d3">
<input type="button">
</div>
</div>
I don't know how much this code will be help you. Just try to give it more better view so that you can control the alignment properly along with button height and width. Just hope it helps you.
#container {
background-color: green;
width: 100%;
display: flex;
}
#d1 {
width: 33.33%;
display: flex;
padding: 0 10px 0 10px
}
#d2 {
width: 33.33%;
display: flex;
padding: 0 10px 0 10px text-align:center;
}
#d3 {
width: 33.33%;
display: flex;
padding: 0 10px 0 10px
}
#d2>input {
background-color: blue;
width: 100%;
vertical-align: middle;
}
#d3>input {
background-color: red;
vertical-align: middle;
width: 100%;
padding: 0 10px 0 10px
}
<div id="container">
<div id="d1">
<p>This is some text</p>
</div>
<div id="d2"><input type="button"></div>
<div id="d3">
<input type="button">
</div>
</div>
I have border-radius and overflow: hidden on the parent element to hide anything overflowing inner.
It should looks like this:
It works everywhere except IE and Edge. In these browsers, it looks like this:
HTML:
<div class="table">
<div class="col1"></div>
<div class="col2"></div>
</div>
CSS:
.table {
border-radius: 10px;
border: 1px solid black;
overflow: hidden;
display: table;
width: 100%;
}
.col1 {
background: pink;
display: table-cell;
width:50px;
}
.col2 {
background: orange;
display: table-cell;
height: 200px;
}
Over flow has some issues non-block elements. So try adding a wrapping div for ".table" ans apply overflow: hidden for that wrapper. See the sample. below
.table-wrapper{
border-radius: 30px;
background: #ccc;
overflow: hidden;
display: block;
width: 200px;
}
.table {
display: table;
width: 200px;
}
.col1 {
background: rgba(255,0,0,.3);
display: table-cell;
width:50px;
}
.col2 {
background: rgba(0,255,0,.2);
display: table-cell;
height: 200px;
}
<div class="table-wrapper">
<div class="table">
<div class="col1"></div>
<div class="col2"></div>
</div>
</div>
Just set border-radius on the .col1 and .col2
.table {
border-radius: 10px;
border: 1px solid black;
overflow: hidden;
display: table;
width: 100%;
}
.col1 {
background: pink;
display: table-cell;
width:50px;
border-radius: 10px 0px 0px 10px;
}
.col2 {
background: orange;
display: table-cell;
height: 200px;
border-radius: 0px 10px 10px 0px;
}
<div class="table">
<div class="col1"></div>
<div class="col2"></div>
</div>
How would I make an input element 100% width that is available? So, 100% width minus the width of any other elements on the same line (not knowing the width of those elements)?
.bg {
padding: 20px;
background: #f5f5f5;
margin-bottom: 25px;
}
input {
width: 100%;
padding: 0.4em;
height: 25px;
border: 1px solid #ccc;
}
.button {
height: 25px;
padding: 0.4em;
background: #ccc;
border: 1px solid #ccc;
text-decoration: none;
}
<div class="bg">
<input type="text" placeholder="100% width - button" /><a class="button" href="#">Click</a>
</div>
<div class="bg">
<input type="text" placeholder="100% width" />
</div>
2 ways do that:
use flexbox, applying display:flex in .bg (.bg2 for demo) and flex:1 (or just flex-grow:1) in input
.bg {
padding: 20px;
background: #f5f5f5;
margin-bottom: 25px;
}
.bg2 {
display: flex;
border: solid red
}
.bg2 input {
flex: 1;
}
input {
width: 100%;
padding: 0.4em;
height: 25px;
border: 1px solid #ccc;
}
.button {
height: 25px;
padding: 0.4em;
background: #ccc;
border: 1px solid #ccc;
text-decoration: none;
}
<div class="bg bg2">
<input type="text" placeholder="100% width - button" /><a class="button" href="#">Click</a>
</div>
<div class="bg">
<input type="text" placeholder="100% width" />
</div>
For older versions of IE, use CSS tables
* {
box-sizing: border-box
}
.bg {
padding: 20px;
background: #f5f5f5;
margin-bottom: 25px;
display: table;
width: 100%;
}
input {
display: table-cell;
width: 100%;
padding: 0.4em;
height: 25px;
border: 1px solid #ccc;
}
.button {
display: table-cell;
height: 25px;
padding: 0.4em;
background: #ccc;
border: 1px solid #ccc;
text-decoration: none;
}
<div class="bg bg2">
<input type="text" placeholder="100% width - button" /><a class="button" href="#">Click</a>
</div>
<div class="bg">
<input type="text" placeholder="100% width" />
</div>
Adding display:flex to the .bg-class works. Flexbox tries to evenly distribute it's child-elements, but since one of the elements has 100% width and there's still another child, the width is adjusted, so everything fits in one row. There is a great guideline for the flex-attribute here.
If a user is signed up to my site, in their login area I have 3 divs as follows:
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
These divs all have a width of 32% and sit inline with each other.
#psts-cancel-link {
background: white;
border-left: 3px solid #ccc;
padding: 1em;
width: 32%;
min-height: 270px;
float: left;
}
.psts-receipt-link {
background: white;
border-left: 3px solid #ccc;
min-height: 270px;
float: left;
width: 32%;
margin: 0 10px;
padding: 20px;
}
#psts-signup-another {
background: white;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
width: 32%;
min-height: 270px;
float: left;
}
When a user is not signed up, only one of the divs displays:
<div id="psts-signup-another"></div>
Is it possible to change the styling of this so that it's width is 100% when div1 and div2 aren't displayed?
So far I have tried this, but with no success:
#psts-cancel-link ~ .psts-receipt-link ~ #psts_existing_info #psts-signup-another {
width:100%;
}
Table Layout Implementation
Use a table layout. Specify display: table on the parent and display: table-cell on the child elements.
#psts-cancel-link {
background: tomato;
border-left: 3px solid #ccc;
padding: 1em;
min-height: 270px;
display: table-cell;
word-wrap: break-word;
}
.psts-receipt-link {
background: lightblue;
border-left: 3px solid #ccc;
min-height: 270px;
margin: 0 10px;
padding: 20px;
display: table-cell;
word-wrap: break-word;
}
#psts-signup-another {
background: tomato;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
min-height: 270px;
display: table-cell;
word-wrap: break-word;
}
.container {
display: table;
width: 100%;
table-layout: fixed;
}
Logged in
<div class="container">
<div id="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logged out
<div class="container">
<div id="psts-signup-another"></div>
</div>
Flexbox Layout Implementation
You can also use flexbox which expands and shrinks the child items according to the parent container.
#psts-cancel-link {
background: tomato;
border-left: 3px solid #ccc;
padding: 1em;
min-height: 270px;
flex: 1;
}
.psts-receipt-link {
background: lightblue;
border-left: 3px solid #ccc;
min-height: 270px;
margin: 0 10px;
padding: 20px;
flex: 1;
}
#psts-signup-another {
background: tomato;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
min-height: 270px;
flex: 1;
}
.container {
display: flex;
}
Logged in
<div class="container">
<div id="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logged out
<div class="container">
<div id="psts-signup-another"></div>
</div>
You could simply use :first-child if it's indeed the only child in the second case.
#psts-signup-another:first-child {}
You can use the adjacent selector. Have a look at the following snippet:
#psts-signup-another {padding: 5px; background: #f99;}
div + div + #psts-signup-another {padding: 5px; background: #99f;}
<h2>Div when three divs are present</h2>
<div class="theDivs">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
<h2>Div when three divs are not present</h2>
<div class="theDivs">
<div id="psts-signup-another"></div>
</div>
i think you should use another container div with a new class when user logout.
Logged:
<div class="container">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logout:
<div class="container logout">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
CSS:
.container.logout > div {
display:none;
}
.container.logout > .psts-signup-another {
display:block;
}