Make a Div vertically fill its parent [duplicate] - html

This question already has answers here:
Percentage Height HTML 5/CSS
(7 answers)
Closed 4 years ago.
I'm trying to get 2 divs to appear side by side in their parent and to both vertically fill the parent. I've tried setting height and min-height to 100% but I don't understand why its not working.
Here is my html:
<div class="calc-wrap clear">
<h2>Title</h2>
<div class="calc-content clear">
<div class="calc-form">
<form id="cost_calculator">
<fieldset>
<legend>
<h3>Find out how much your stuff costs you</h3>
</legend>
<ol>
<li class="one">
<label for="cost_of_pack">quantity</label>
<span class="pound-label">£</span>
<input type="number" id="cost_of_pack" name="cost_of_pack" value="0.0" min="0" step="0.10">
</li>
<li class="two">
<label for="quantity_smoked">How many per day?</label>
<input type="number" id="quantity_smoked" name="quantity_smoked" value="0">
</li>
</ol>
</fieldset>
</form>
</div>
<div class="calc-save">
<div class="calc-results-content clear">
<h3>Results</h3>
<div class="calc-results">
<div>
<p id="weekly_cost">£<span>0.00</span> per week</p>
<p id="monthly_cost">£<span>0.00</span> per month</p>
<p id="annual_cost" class="noborder">£<span>0.00</span> per year</p>
</div>
</div>
<div class="calc-quitnow">
<p>Well done</p>
</div>
</div>
</div>
</div>
</div>
And CSS:
.calc-content {
display: inline-block;
border: 3px solid blue;
width: 100%;
border-color: #87B7CD;
}
.calc-form,
.calc-save {
height:100%;
border: 1px solid red;
float: left;
}
.calc-form {
width: 60%;
}
.calc-save {
width: 39%;
background-color: #87B7CD;
}
.calc-results {
float: left;
width: 60%;
}
And a JS Fiddle
Any help would be much appreciated.

You can use "the Flex powers"! ;)
.calc-content {
display: flex;
/*display: inline-block;*/
border: 3px solid blue;
width: 100%;
border-color: #87B7CD;
}
.calc-form,
.calc-save {
border: 1px solid red;
/*float: left;*/
}
.calc-form {
width: 60%;
}
.calc-save {
width: 39%;
background-color: #87B7CD;
}
.calc-results {
float: left;
width: 60%;
}
<div class="calc-wrap clear">
<h2>Title</h2>
<div class="calc-content clear">
<div class="calc-form">
<form id="cost_calculator">
<fieldset>
<legend>
<h3>Find out how much your stuff costs you</h3>
</legend>
<ol>
<li class="one">
<label for="cost_of_pack">quantity</label>
<span class="pound-label">£</span>
<input type="number" id="cost_of_pack" name="cost_of_pack" value="0.0" min="0" step="0.10">
</li>
<li class="two">
<label for="quantity_smoked">How many per day?</label>
<input type="number" id="quantity_smoked" name="quantity_smoked" value="0">
</li>
</ol>
</fieldset>
</form>
</div>
<div class="calc-save">
<div class="calc-results-content clear">
<h3>Results</h3>
<div class="calc-results">
<div>
<p id="weekly_cost">£<span>0.00</span> per week</p>
<p id="monthly_cost">£<span>0.00</span> per month</p>
<p id="annual_cost" class="noborder">£<span>0.00</span> per year</p>
</div>
</div>
<div class="calc-quitnow">
<p>Well done</p>
</div>
</div>
</div>
</div>
</div>

This is because the parent <div> has no defined height, so obviously, the children elements can't fill 100% of the parents' height.
Add a height to your parent's class calc-content, e.g. height: 200px;.
calc-content {
display: inline-block;
border: 3px solid blue;
width: 100%;
height: 200px;
border-color: #87B7CD;
}

One way is to use display: flex or display: inline-flex in your .calc-content rule.

Use display: flex on calc-content and keep the form element inside the fieldset element like shown below. Also need for float left now.
.calc-content {
display: flex;
border: 3px solid blue;
width: 100%;
border-color: #87B7CD;
}
.calc-form,
.calc-save {
height: 100%;
border: 1px solid red;
}
.calc-form {
width: 60%;
}
fieldset {
width: 60%;
}
.calc-save {
width: 40%;
background-color: #87B7CD;
}
.calc-results {
float: left;
width: 60%;
}
<div class="calc-wrap clear">
<h2>Title</h2>
<div class="calc-content ">
<fieldset>
<legend>
<h3>Find out how much your stuff costs you</h3>
</legend>
<form id="cost_calculator">
<ol>
<li class="one">
<label for="cost_of_pack">quantity</label>
<span class="pound-label">£</span>
<input type="number" id="cost_of_pack" name="cost_of_pack" value="0.0" min="0" step="0.10">
</li>
<li class="two">
<label for="quantity_smoked">How many per day?</label>
<input type="number" id="quantity_smoked" name="quantity_smoked" value="0">
</li>
</ol>
</form>
</fieldset>
<div class="calc-save">
<div class="calc-results-content clear">
<h3>Results</h3>
<div class="calc-results">
<div>
<p id="weekly_cost">£<span>0.00</span> per week</p>
<p id="monthly_cost">£<span>0.00</span> per month</p>
<p id="annual_cost" class="noborder">£<span>0.00</span> per year</p>
</div>
</div>
<div class="calc-quitnow">
<p>Well done</p>
</div>
</div>
</div>
</div>
</div>

Are you trying to do like this:
Removed height from .calc-form, .calc-save & modified .calc-content with
display: flex;
.calc-content {
display: flex;
border: 3px solid blue;
width: 100%;
border-color: #87B7CD;
}
.calc-form,
.calc-save {
border: 1px solid red;
float: left;
}
.calc-form {
width: 60%;
}
.calc-save {
width: 39%;
background-color: #87B7CD;
}
.calc-results {
float: left;
width: 60%;
}
<div class="calc-wrap clear">
<h2>Title</h2>
<div class="calc-content clear">
<div class="calc-form">
<form id="cost_calculator">
<fieldset>
<legend>
<h3>Find out how much your stuff costs you</h3>
</legend>
<ol>
<li class="one">
<label for="cost_of_pack">quantity</label>
<span class="pound-label">£</span>
<input type="number" id="cost_of_pack" name="cost_of_pack" value="0.0" min="0" step="0.10">
</li>
<li class="two">
<label for="quantity_smoked">How many per day?</label>
<input type="number" id="quantity_smoked" name="quantity_smoked" value="0">
</li>
</ol>
</fieldset>
</form>
</div>
<div class="calc-save">
<div class="calc-results-content clear">
<h3>Results</h3>
<div class="calc-results">
<div>
<p id="weekly_cost">£<span>0.00</span> per week</p>
<p id="monthly_cost">£<span>0.00</span> per month</p>
<p id="annual_cost" class="noborder">£<span>0.00</span> per year</p>
</div>
</div>
<div class="calc-quitnow">
<p>Well done</p>
</div>
</div>
</div>
</div>
</div>

.parentDiv {
height: 250px;
background: black;
display:flex;
}
.childDiv {
width:50%;
background: grey;
padding:10px;
border:1px solid black
}
<div class="parentDiv">
<div class="childDiv">
text1
</div>
<div class="childDiv">
text2
</div>
</div>

Related

How to position a large number of elements using flexbox without resorting to position: absolute?

I'm doing a landing page for testing flexbox technology. I ran into a problem: I can't figure out how to position a large number of elements without resorting to crutches like this:
Also without resorting to position:absolute, because there will be inaccuracies in the margins or it will take a very long time to calculate these margins.
This is what I currently have:
On the second one, I indicated how I see one of the working implementation options: organize two columns in footer and make them flex-direction: column.
Here is my markup:
<footer class="footer">
<div class="footer__logo">
<img src="icons/Logo.svg" alt="logo" class="logo__img">
<p class="logo__text"> PETWORLD </p>
</div>
<div class="footer__input">
<label for="email" class="text">Updates right to your Inbox</label>
<input type="email" placeholder="Email Address" class="text">
<input type="submit" value="Send" class="submit__text text">
</div>
<div class="footer__privacy">
<p class="privacy__text"> Text</p>
<p class="privacy__text"> Text</p>
<p class="privacy__text"> Text</p>
</div>
<div class="footer__menu">
<!-- x3 колонки текста -->
<div class="menu__text">
<p class="text">Text</p>
<p class="text">Text</p>
<p class="text">Text</p>
</div>
</div>
<div class="footer__social">
<img src="icons/Socials icons.svg" alt="" class="logo__img">
</div>
</footer>
I decided to move away from BEM a bit and divided my footer into two visual components.
What came out in the end can be viewed below:
https://codepen.io/productomar/pen/ZEvNQjG
------------------HTML---------------------
<div class="container__footer">
<footer class="footer">
<div class="first__half">
<div class="footer__logo">
<img src="icons/Logo.svg" alt="logo" class="logo__img">
<p class="logo__text"> PETWORLD </p>
</div>
<div class="footer__input">
<label for="email" class="text">Updates right to your Inbox</label>
<input type="email" placeholder="Email Address" class="placeholder__text">
<input type="submit" value="Send" class="submit__text text">
</div>
<div class="footer__privacy">
<p class="privacy__text"> © PETWORLD 2022</p>
<p class="privacy__text"> Privacy policy</p>
<p class="privacy__text"> Terms of use</p>
</div>
</div>
<div class="second__half">
<div class="footer__menu">
<div class="menu__text">
<p class="text menu__b" style="font-weight: 600;">Our story</p>
<p class="text__item text">FAQ</p>
<p class="text__item text">Contact</p>
</div>
<div class="menu__text">
<p class="text menu__b" style="font-weight: 600;">Pet care</p>
<p class="text__item text">Treatments</p>
<p class="text__item text">Health</p>
<p class="text__item text">Hygiene </p>
</div>
<div class="menu__text">
<p class="text menu__b" style="font-weight: 600;">Shop</p>
<p class="text__item text">Pet Food</p>
<p class="text__item text">Toys</p>
<p class="text__item text">Accessories</p>
</div>
</div>
<div class="footer__social">
<img src="icons/Socials icons.svg" alt="logo__social" class="logo__social">
</div>
</div>
</footer>
</div>
------------------CSS---------------------
/* Первая часть футера */
.first__half {
display: flex;
flex-direction: column;
}
.footer {
display: flex;
padding: 70px 80.5px;
}
.footer__logo {
display: flex;
align-items: center;
}
.logo__text {
font-weight: bold;
}
.footer__input {
margin-top: 75px;
}
label {
display: block;
}
input[type="email"] {
border: 1px solid #D0D0D0;
border-radius: 10px;
margin-top: 12px;
}
.placeholder__text {
padding-top: 11px;
padding-bottom: 11px;
padding-left: 20px;
padding-right: 71px;
font-size: 18px;
color: #2D2D2D;
}
.submit__text {
padding: 11px 31.5px;
background-color: #3366FF;
color: white;
border-radius: 10px;
margin-left: 20px;
}
.footer__privacy {
display: flex;
margin-top: 33px;
}
.privacy__text {
margin-right: 30px;
font-weight: 600;
}
.privacy__text:nth-child(3) {
margin-right: 0px;
}
/* Вторая часть футера */
.second__half {
margin-left: 233px;
}
.footer__menu {
display: flex;
}
.menu__text {
margin-right: 60px;
}
.text__item {
padding-top: 16px;
}
.menu__text:nth-child(3) {
margin-right: 0px;
}
.footer__social {
margin-top: 99px;
}
.logo__social {
margin-left: 237px;
}

Positioning another div without breaking vertical alignment

I had encountered to an interesting CSS challenge. In the following code I was able to vertically aligned the text and input. The part I couldn't manage was, without breaking the vertical alignment (text - input) I need to put footer text under the input.
.container {
width: 300px;
}
.head {
display: inline-block;
vertical-align: middle;
width: 20%;
}
.col {
display: inline-block;
vertical-align: middle;
}
.col input {
width: 100px;
}
<div class="container">
<div class="head">
Text
</div>
<div class="col">
<input type="text" />
</div>
<div class="col">
<input type="text" />
</div>
<div class="head"></div>
<div class="col">
Footer Text
</div>
</div>
<div class="container">
<div class="head">
Three Line Text
</div>
<div class="col">
<input type="text" />
</div>
<div class="col">
<input type="text" />
</div>
<div class="head"></div>
<div class="col">
Footer Text
</div>
</div>
Also, container div height should effect from footer text as well. So using absolute will not work for this case.
I already aware some JavaScript or CSS hack solutions. But for this case, I want to proceed with a proper way. How can we achieve this properly?
UPDATE
I forgot to mention before. Footer text could be multiple lines as well. It should cover both inputs underneath.
Flexbox can do that but it requires restructuring the HTML and altering a class or two...oh, and a pseudo-element.
.container {
width: 300px;
display: flex;
margin: 1em;
border: 1px solid red;
}
.head {
width: 20%;
display: flex;
flex-direction: column;
justify-content: center;
}
.col,
.second {
display: flex;
flex-direction: column;
justify-content: center;
margin: 0 .25em;
}
input {
width: 100px;
}
.col:before {
content: "";
height: 1.2em; /* or whatever your line-height is */
}
<div class="container">
<div class="head">
Text
</div>
<div class="col">
<input type="text" />
<div class="foot">
Footer Text
</div>
</div>
<div class="second">
<input type="text" />
</div>
</div>
<div class="container">
<div class="head">
Three Line Text
</div>
<div class="col">
<input type="text" />
<div class="foot">
Footer Text
</div>
</div>
<div class="second">
<input type="text" />
</div>
</div>

Vertically aligning div using CSS and Bootstrap?

I am trying to build a simple survey with 10 questions which are stored in my database, looped through and the 10 questions are added to the page dynamically. Basically, I would like there to only be one question on the page at a time, but the page to be scrollable to see the other questions.
For example, question 1 would show up in the middle of the page, the user types their answer and clicks next, and the page scrolls to the next question. If the user scrolls up, it scrolls through the page up, and down scrolls down. For jumping to the next question I was planning on just having each question with an id and the next button just having a link to that id.
My HTML for populating the questions on the page looks like this:
<div class="container">
<div class="row wrapper">
<div class="question">
<p>What is your name?</p>
</div>
<div class="description">
<p>Used for...</p>
</div>
<div class="answer">
<input type="text" placeholder="">
</div>
</div>
</div>
My CSS for the above looks like this:
.question {
color: #232A33;
text-align: left;
font-size: 2em;
}
.description {
color: #FFCD3D;
text-align: left;
font-size: 1.25em;
}
.answer {
color: #232A33;
text-align: left;
font-size: 1.25em;
}
I managed to get the question centered on the page using the following code:
.wrapper {
width: 500px;
height: 150px;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
This works fine for one question, but if there are multiple questions they all overlap each other because the position is absolute.
Any tips on how I can achieve this design for the page?
Thanks in advance!
You're very close. This can be done by adding just a tad more css in (adjusting the row class slightly).
If you add the following:
.row {
width: 100%;
margin: 15% auto;
height: 100vh!important;
}
and adjust your wrapper to **relative** positioning, you should see a good result
.wrapper {
width: 500px;
height: auto;
position: relative;
}
See snippet or fiddle
.question {
color: #232A33;
text-align: left;
font-size: 2em;
}
.description {
color: #FFCD3D;
text-align: left;
font-size: 1.25em;
}
.answer {
color: #232A33;
text-align: left;
font-size: 1.25em;
}
.row {
width: 100%;
margin: 15% auto;
height: 100vh!important;
}
.wrapper {
width: 500px;
height: auto;
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row wrapper">
<div class="question">
<p>What is your name?</p>
</div>
<div class="description">
<p>Used for...</p>
</div>
<div class="answer">
<input type="text" placeholder="">
</div>
</div>
</div>
<div class="container">
<div class="row wrapper">
<div class="question">
<p>What is your name?</p>
</div>
<div class="description">
<p>Used for...</p>
</div>
<div class="answer">
<input type="text" placeholder="">
</div>
</div>
</div>
<div class="container">
<div class="row wrapper">
<div class="question">
<p>What is your name?</p>
</div>
<div class="description">
<p>Used for...</p>
</div>
<div class="answer">
<input type="text" placeholder="">
</div>
</div>
</div>
<div class="container">
<div class="row wrapper">
<div class="question">
<p>What is your name?</p>
</div>
<div class="description">
<p>Used for...</p>
</div>
<div class="answer">
<input type="text" placeholder="">
</div>
</div>
</div>

Align in same row

How to align the below image and box in same row? Image on left and box on right with some space between them.
<div id="slideshow" >
<div id="changeimage">
<img class="image" src="bafdf1f214f5a800c95ef301234c254a.iix"/>
</div>
<div id="newsbox">
<span class="label label-danger">Latest News</span>
<div class="group-box">
<div class="grey-group">
<span>Notes:This is a box</span>
</div>
</div>
</div>
</div>
Like #ntgCleaner said: "there's about 20 ways to skin that cat."
This might be one way to achieve this:
#slideshow {
display: flex;
}
.group-box {
margin-top: 50px;
}
<div id="slideshow" >
<div id="changeimage">
<img class="image" src="//thumb7.shutterstock.com/display_pic_with_logo/260389/347420135/stock-photo-top-view-of-architect-drawing-on-architectural-project-347420135.jpg"/>
</div>
<div id="newsbox"><span class="label label-danger">Latest News</span>
<div class="group-box">
<div class="grey-group">
<span>Notes:This is a box</span>
</div>
</div>
</div>
</div>
You can use "float" or "display: inline-block" css selectors to set your elements this way. I belive this simple example will help:
<style>
#slideshow{
border: 1px solid black;
width: 100%;
}
#changeimage{
float: left;
background: blue;
width: 40%;
}
#newsbox{
float: left;
background: red;
width: 40%;
}
#separator {
width: 20%;
height: 100px;
float: left;
background: green;
}
.clear{
clear: both;
}
</style>
<div id="slideshow" >
<div id="changeimage"><img class="image" src="http://www.w3schools.com/html/pic_mountain.jpg"/></div>
<div id="separator"></div>
<div id="newsbox"><span class="label label-danger">Latest News</span>
<div class="group-box">
<div class="grey-group">
<span>Notes:This is a box</span>
</div>
</div>
</div>
<div class="clear"> </div>
</div>
And here is working example:
https://jsfiddle.net/ej53b8x2/
If you want to set elements in row maybe you should use some framework like bootstrap or foundation?

Outer Div not expanding with Inner Div

I am developing the follow page:
The problem i'm having is the tabular structure. The outer div, my container is not expanding with the alternating heights of the tabs. See image of problem:
I would like the container to extend to the tabs heights.
HTML:
<section class="x_section_wide">
<div id="ambition_container">
<div id="x_tools">
<span id="a_name">name</span>
</div>
<div class="x_content">
</div>
<div id="a_progress_bar">
<span id="a_progress">
</span>
</div>
<div class="tabs">
<div class="tab">
<input type="radio" id="tab-4" name="tab-group-2" checked>
<label for="tab-4" id="a_tab_one">1</label>
<div id="tab_information" class="tab_content">
<div id="a_extra_info">
<div id="a_extra_info_inner">
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
</div>
</div>
</div>
</div>
<div class="tab">
<input type="radio" id="tab-5" name="tab-group-2">
<label for="tab-5" id="a_tab_two">2</label>
<div id="tab_evidence" class="tab_content">
<div id="a_evidence">
<div id="a_evidence_inner">
2
</div>
</div>
</div>
</div>
<div class="tab">
<input type="radio" id="tab-6" name="tab-group-2">
<label for="tab-6" id="a_tab_three">3</label>
<div id="tab_comments"class="tab_content">
<div id="a_comments">
<div id="a_comments_inner">
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
<p>3</p><br/>
</div>
</div>
</div>
</div>
<div class="tab">
<input type="radio" id="tab-7" name="tab-group-2">
<label for="tab-7" id="a_tab_four">4</label>
<div id="tab_supporters" class="tab_content">
<div id="a_supporters">
<div id="a_supporters_inner">
4
</div>
</div>
</div>
</div>
</div>
</div>
</div>
The CSS used for this particular page is:
#a_extra_info, #a_evidence, #a_comments, #a_supporters{
padding:30px;
}
#a_extra_info{
background-color: #E29FA4;
}
#a_evidence{
background-color: #B98489;
}
#a_comments{
background-color: #845F64;
}
#a_supporters{
background-color: #5E4549;
}
#a_extra_info_inner, #a_evidence_inner, #a_comments_inner, #a_supporters_inner{
background-color: #FFF;
/*padding: 10px;*/
}
#a_extra_info_inner span, #a_extra_info_inner label, #a_evidence_inner span, #a_evidence_inner label, #a_comments_inner span, #a_comments_inner label, #a_supporters_inner span, #a_supporters_inner label{
color: #000;
}
.tabs {
position: relative;
min-height: 276px; /* This part sucks */
clear: both;
}
.tab {
float:left;
width: 25%;
}
.tab > label {
text-align: center;
position: relative;
height:30px;
line-height: 30px;
font-weight: normal;
font-size: 100%;
cursor: pointer;
margin:0;
}
.tab [type=radio] {
display: none;
}
.tab_content {
position: absolute;
top: 28px;
left: 0;
right: 0;
bottom: 0;
}
[type=radio]:checked ~ label {
z-index: 2;
}
[type=radio]:checked ~ label ~ .tab_content {
z-index: 1;
}
#a_tab_one{
background-color: #E29FA4;
color:#000;
}
#a_tab_two{
background-color: #B98489;
color:#000;
}
#a_tab_three{
background-color: #845F64;
}
#a_tab_four{
background-color: #5E4549;
}
Is there a solution for this? Please see the link for a better view of the problem.
Thanks
set this style on your outer div id or class whatever you have
#ambition_container{
background-color: #461f20;
padding:20px 10px 10px 10px;
position:relative;
height:100%;
overflow-y:auto;
display:block
}
you must change like this
for tabs
<div class="tabs">
<div class="tab">
<div class="tab">
<div class="tab">
<div class="tab">
</div>
css of tabs
.tabs {
clear: both;
height: 30px;
position: relative;
}
and place content after tabs in content div
<div class="content" style="">
<div id="tab_information for tab1" class="tab_content" style="display: block;">
<div id="a_extra_info">
<div id="a_extra_info_inner">Place Content of tab 1</div>
</div>
</div>
<div id="tab_information for tab2" class="tab_content" style="display: none;">
<div id="a_extra_info">
<div id="a_extra_info_inner">Place Content of tab 2</div>
</div>
</div>
</div>
#ambition_container, #tabs{
overflow:hidden;
}
Add this rules on your CSS stuff.