Make div fit next to another div at all times - html

I have 3 divs. One of them is functioning as a wrapper for the other two.
Let's call them div1 and div2. Div1 has a fixed width. The width of the wrapper is variable but never less then the width of div1.
Now, how do I make div2 always have the width (width of wrapper - width of div1)?
Here is what I got:
.wrapper {
width: 420px; /*Variable but not less then width of div1*/
height: 500px;
border: 2px solid #0000FF;
}
.div1 {
width: 200px;
height: 200px; /*Fixed*/
border: 2px solid #FF0000;
display: inline-block;
}
.div2 {
position: absolute;
width: 100%; /*Should be width of wrapper - width of div1*/
height: 200px;
border: 2px solid #00FF00;
display: inline-block;
}
https://jsfiddle.net/kjhnhtny/10/

If I have not mistaken your question, you can use a pure css approach.
.wrapper {
width: 420px; /*Variable but not less then width of div1*/
height: 500px;
border: 2px solid #0000FF;
box-sizing: border-box;
}
.div1 {
width: 200px;
height: 200px; /*Fixed*/
border: 2px solid #FF0000;
float: left;
box-sizing: border-box;
}
.div2 {
width: 100%; /*Should be width of wrapper - width of div1*/
height: 200px;
border: 2px solid #00FF00;
box-sizing: border-box;
}

You're looking for two things:
To set float: left on .div2
The CSS calc() function, which can handle subtraction. Specifically, you're looking for width: calc(100% - (200px + (2px * 2) + (2px * 2))), which is 100% of the.wrapper, minus the width of .div1, along with both sides of both element's border width.
This can be seen in the following:
.wrapper {
width: 420px; /*Variable but not less then width of div1*/
height: 500px;
border: 2px solid #0000FF;
}
.div1 {
width: 200px;
height: 200px; /*Fixed*/
border: 2px solid #FF0000;
display: inline-block;
}
.div2 {
float: left;
width: calc(100% - (200px + (2px * 2) + (2px * 2))); /*Should be width of wrapper - width of div1*/
height: 200px;
border: 2px solid #00FF00;
display: inline-block;
}
<div class="wrapper">
<div class="div1">
</div>
<div class="div2">
</div>
</div>
Note that you could make use of CSS variables so that you only need to modify one property's value (with all elements being automatically adjusted), by setting a variable in :root and referencing it with var().
Having said that, CSS variables would probably be a bit overkill, but I'll show you how can use them in case you opt for this approach. Try adjusting the --border-width in the following, and you'll see that all elements update and resize appropriately :)
:root {
--width: 200px;
--border-width: 2px;
}
.wrapper {
width: 420px; /*Variable but not less then width of div1*/
height: 500px;
border: var(--border-width) solid #0000FF;
}
.div1 {
width: var(--width);
height: 200px; /*Fixed*/
border: var(--border-width) solid #FF0000;
display: inline-block;
}
.div2 {
float: left;
width: calc(100% - (var(--width) + (var(--border-width) * 2) + (var(--border-width) * 2))); /*Should be width of wrapper - width of div1*/
height: 200px;
border: var(--border-width) solid #00FF00;
display: inline-block;
}
<div class="wrapper">
<div class="div1">
</div>
<div class="div2">
</div>
</div>

Related

Why can't I make a circle with border-radius?

* {
margin: 0;
}
div {
background-color: green;
height: 900px;
width: 50%;
margin: auto;
border-radius: 50px;
overflow: hidden;
padding: 20px;
border: 4px solid red;
box-sizing: border-box;
}
div:hover {
box-shadow: 5px 5px 5px 1px blue;
}
#media screen and (max-width:600px) {
div {
background-color: aqua;
color: red;
width: 100%;
height: 10%;
}
body {
background-color: chocolate
}
}
.divas {
background-color: yellow;
position: relative;
margin-top: 20%;
height: 300px;
border-radius: 50%;
}
<div>This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . </div>
<div class="divas"></div>
Hello I would like to make a perfectly rounded circle. I tried creating it with border-radius , but it doesn't allow me to create a perfect circle. Can someone explain me why? I tried changing padding and such but it doesn't work anyways.? Thanks in advance.
If you want to create circle with border-radius then height and width should be same for the div which you are applying border-radius
Then only border-radius will look like circle
Example
#circle{
width: 100px;
height: 100px;
border-radius: 50%;
background: #000;
}
<div id="circle"></div>
For a perfect circle you need an element with the same height and width. You also only have a border-radius:50px instead of border-radius:50% defined.
* {
margin: 0;
}
div {
background-color: green;
height: 500px;
width: 500px;
margin: auto;
border-radius: 50%;
overflow: hidden;
padding: 25px 28px 28px;
text-align: center;
border: 4px solid red;
box-sizing: border-box;
display: flex;
align-items: center;
font-size: 60px;
}
div:hover {
box-shadow: 5px 5px 5px 1px blue;
}
#media screen and (max-width:600px) {
div {
background-color: aqua;
color: red;
width: 100%;
height: 10%;
}
body {
background-color: chocolate
}
}
.divas {
background-color: yellow;
position: relative;
margin-top: 20%;
height: 300px;
width: 300px;
border-radius: 50%;
}
<div>
This is text.<br> This is text. This is text. This is text. This is text. This is text.
</div>
<div class="divas"></div>
Div with same height and width
border-radius is half the width gives a circle.
.mycircle {
background-color: green;
height: 200px;
width: 200px;
border-radius: 100px;
}
<div class="mycircle"></div>
For Creating a circle in css object should have same width and height
add width:300px; to .divas class.
You want to create a circle with border-radius then height and width should be the same for the element which you are applying border-radius:100%
* {
margin: 0;
}
div {
background-color: green;
height: 600px;
width: 600px;
margin: auto;
border-radius: 100%;
overflow: hidden;
padding: 90px;
border: 4px solid red;
box-sizing: border-box;
}
div:hover {
box-shadow: 5px 5px 5px 1px blue;
}
#media screen and (max-width:600px) {
div {
background-color: aqua;
color: red;
width: 100%;
height: 10%;
}
body {
background-color: chocolate
}
}
.divas {
background-color: yellow;
position: relative;
margin-top: 20%;
height: 300px;
width: 300px;
border-radius: 100%;
}
<div>This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . </div>
<div class="divas"></div>
In order to make a perfect circle, you need to set width and height equal. If you do so by CSS you need to define them both in 'px', rather than '%' since the window width & height may vary from device to device. Now set border-radius as 50%. Below is the CSS:
div {
background-color: green;
height: 900px;
width: 900px;
margin: auto;
border-radius: 50%;
overflow: hidden;
padding: 20px;
border: 4px solid red;
box-sizing: border-box;
}
In case your with is dependent on the window's width (as you have used %), you can set height equal to width using javascript. Here is how to do it.
HTML
<div></div>
JavaScript
var requiredWidth = window.innerWidth * (0.5); //window width excluding scrollbar.
var div = document.querySelector('div');
div.style.width = requiredWidth + 'px';
div.style.height = requiredWidth + 'px';
Now that you have defined height and width using javascript, set border-radius to 50% in your CSS.
CSS
div {
border-radius: 50%;
background-color: red;
}
Use the aspect-ratio CSS' property. Compatible with all major browsers...
.fixed-units {
width: 150px;
aspect-ratio: 1 / 1;
border: 1px solid #000;
background-color: #bada55;
border-radius: 50%;
}
.dynamic-units {
width: 50%;
aspect-ratio: 1 / 1;
border: 1px solid #000;
background-color: #daba55;
border-radius: 50%;
}
<label>For Fixed Units</label>
<div class="fixed-units"></div>
<br>
<label>For Dynamic Units</label>
<div class="dynamic-units"></div>
You need to use border-radius: 100% and have your height and width be equal.

Set the child's width relative to parent with presence of margin

I want to set the width of a header realtive to conatiner width with taking into account header's margin
div.container {
width: 100%;
height: 300px;
border: 1px solid red;
position:relative;
}
header{
width: 100%;
border: 1px solid green;
height: 20px;
margin: 10px;
}
<div class="container">
<header></header>
</div>
but header element gets out from the border of container on a few pixels on the right side.
Also tried to add box-sizing: border-box; to header's style, nothing happened. Why?
Set width: calc(100% - 22px);for header. That's 100% minus twice the border (2 * 1px) minus twice the margin (2*10px), adding up to 22px.
div.container {
width: 100%;
height: 300px;
border: 1px solid red;
position:relative;
}
header{
width: calc(100% - 22px);
border: 1px solid green;
height: 20px;
margin: 10px;
}
<div class="container">
<header></header>
</div>
I sugest instead of using margins on child div - use padding:10px on parent div. I've updated your code snippet.
div.container {
width: 100%;
height: 300px;
border: 1px solid red;
position:relative;
padding:10px;
}
header{
width: 100%;
border: 1px solid green;
height: 20px;
}
<div class="container">
<header></header>
</div>

Html css with jsfiddle ex: not working: vertical align and using full width based on width percentage of two child containers

https://jsfiddle.net/3Lthpf72/5/
Html css with jsfiddle ex: not working: vertical align and using full width based on width percentage of two child containers
When I make the two child containers add up to the parent width percentage, it folds down. Also the vertical align middle is at the bottom, not the middle.
Any thoughts?
.payee.list-item {
border: 1px solid red;
height: 100%;
width: 300px;
}
.list-item-content {
display: inline-block;
border: 1px solid blue;
width: 80%
}
.payee.list-item>img {
border: 1px solid green;
height: 45px;
display: inline-block;
width: 17%;
vertical-align: middle;
}
<div class="payee list-item">
<img src="/Image/PayeeBillPayAccountPortrait/832">
<div class="list-item-content">
<h4>Colonel Sanders!</h4>
<h3>Colonel Sanders</h3>
</div>
</div>
Are you trying to do something like that?
.payee.list-item {
border: 1px solid red;
height: 100%;
width: 300px;
display: inline-block;
position: relative;
}
.list-item-content {
float: right;
border: 1px solid blue;
width: 80%
}
h3, h4 {
width: 50%;
float: left;
box-sizing: border-box;
padding: 6px;
}
h3{background: lightgray;}
h4{background: gray;}
.payee.list-item>img {
border: 1px solid green;
max-height: 45px;
width: 17%;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
<div class="payee list-item">
<img src="https://static.pexels.com/photos/6555/nature-sunset-person-woman.jpg">
<div class="list-item-content">
<h3>Colonel Sanders</h3>
<h4>Colonel Sanders!</h4>
</div>
</div>

How to manage textarea right side overflow in css?

I have to create two <textarea>s in two different <div>s and both are have to come in single line. And both <textarea>s have to occupy 100% width (50% by each) in all types of screen.
However, when I am trying the second <textarea>, the right side is overflowing and even I am not able to manage right margin (in CSS) for <textarea>. How can I avoid right overflow for <textarea>?
.container {
background-color: lightblue;
border: 5px solid black;
min-height: 500px;
}
textarea {
width: 100%;
height: 100%;
border: 3px none #cccccc;
margin: 10px 10px 10px 10px;
border: 1px solid black;
}
.left {
float: left;
width: 50%;
}
.right {
float: left;
width: 50%;
}
<div class='left'>
<textarea>left </textarea>
</div>
<div class='right'>
<textarea>right</textarea>
</div>
Note the change in margin to textarea. That should do it!
.container {
background-color: lightblue;
border: 5px solid black;
min-height: 500px;
}
textarea {
width: 100%;
height: 100%;
border: 3px none #cccccc;
margin: 10px 0px 10px 0px;
border: 1px solid black;
}
.left {
float: left;
width: 50%;
}
.right {
float: left;
width: 50%;
}
<div class='left'>
<textarea>left</textarea>
</div>
<div class='right'>
<textarea>right</textarea>
</div>
you have to remove margin from your textarea because margin calculated form the outer width of the element , you can use padding to .conatiner instead.
and add a box-sizing attribute to remove the border width from the calculate width
html,body,.container{
height:100%;
margin:0;
}
.container{
background-color: lightblue;
border: 5px solid black;
padding:10px;
display: table;
width: 100%;
box-sizing: border-box;
}
textarea {
width: 100%;
height: 100%;
border: 3px none #cccccc;
border: 1px solid black;
box-sizing: border-box;
}
.left{
display: table-cell;
width:50%;
height: 100%;
}
.right{
display: table-cell;
width:50%;
height: 100%;
}
<html>
<body>
<div class="container">
<div class='left'>
<textarea>left </textarea>
</div>
<div class='right'>
<textarea>right</textarea>
</div>
</div>
</body>
</html>
Remove margin from your textarea because margin calculated form the outer width of the element, and give display: table; to container.
Remove margin. Because you are assigning 50% to each left and right textarea. so your total width will be 100%+10px; so it will overflow on x-axis
textarea {
width: 100%;
height: 100%;
border: 3px none #cccccc;
border: 1px solid black;
}
You can use iframes for that. If you use iframes you can fit the overflow to hidden both left and right side

How do prevent my div from spilling outside its parent container?

Here is my code taken from the codepen: http://codepen.io/rags4developer/pen/ONoBpm
Please help me to fix these problems.
How do I prevent the the main div & footer from spilling out of the container div ? overflow: hidden for container will not always work !
How do I make the container div height equal to page height without setting its height to a fixed percentage ?
HTML:
<body>
<div id="container">
<div id="nav">nav links 1,2,3 etc</div>
<div id="main">
<!--no text here-->
<div id="left">left panel</div>
<div id="right">right panel</div>
</div>
<div id="footer">footer</div>
</div>
</body>
CSS:
* {box-sizing: border-box;}
html {height: 100%;}
body {height: 100%;}
#container {
border: 8px solid yellow;
height: 100%;
width: 80%;
margin: 0 auto;
}
#nav {
border: 4px solid red;
height: 15%;
}
#main {
border: 4px solid black;
height: 100%;
background: gray;
}
#left {
border-top: 4px solid green;
border-left: 4px solid green;
border-bottom: 4px solid green;
float: left;
width: 15%;
height:100%;
/*I will make this gradient later*/
background: #9e9999;
}
#right {
border: 4px solid blue;
float: right;
width: 85%;
height: 100%;
border-radius: 20px 0 0 0;
background: white;
}
#footer {
border: 4px solid pink;
clear: both;
}
I am not completely sure if I understand you correctly, but your heights (i.e. the heights within the #container div) add up to 15% + 100% + the height of the footer = at least 115% of the #container height plus the footer height, which causes the "spilling over".
I changed the #content height to 80% and added height: 5%; to the footer in this fork of your codepen: http://codepen.io/anon/pen/EKeOdm
Now everything remains within the #container. Is this what you want?
The clearfix solution still works well for floated elements, IMO. Try removing the height styles and add this:
#main:before,
#main:after {
display: table;
content: "";
}
#main:after {
clear: both;
}
Further: http://nicolasgallagher.com/micro-clearfix-hack/
Using display table should fix this.
#container {
border: 8px solid yellow;
height: 100%;
width: 80%;
margin: 0 auto;
**display: table;**
}
#content {
border: 4px solid black;
background: gray;
height: 100%;/*Not sure 100% of what ? Parent ???*/
**display: table-row;**
}