CSS divs next to each other that affect each others width - html

I have this problem that I can't seem to figure out. After researching I can't seem to find anyone that has solved it. I'm not sure that it can be solved in pure CSS, but I have to ask:
Consider a situation where there are two divs next to each other. The divs have a known width.
The size of the content, however, is not known. When the content of one div becomes very large, that div should take space from the other, as such:
This width should only grow if the known width can't accommodate the content.
Likewise, the blue div should grow if its content is very large:
Is this possible to solve with only CSS, or would I need JS?

flexbox and min-width can do that:
.wrap {
width: 50%;
margin: 1em auto;
border: 1px solid grey;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.wrap div {
flex: 1 0 auto;
max-width: 70%;
}
.left {
background: rgba(255, 0, 0, 0.5);
min-width: 30%;
}
.right {
background: rgba(0, 0, 255, 0.5);
min-width: 30%;
}
<div class="wrap">
<div class="left">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur quas incidunt dolorem doloribus asperiores, iure esse voluptatibus dolore cum sint exercitationem minus aspernatur explicabo perspiciatis distinctio expedita.</div>
<div class="right"></div>
</div>
<div class="wrap">
<div class="left"></div>
<div class="right">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fuga, accusantium libero omnis, quod ea pariatur porro asperiores enim officia minima!</div>
</div>

if you set a fixed size to div, content will flow in multiple lines instead of div expanding to fit the content.
Instead you need to set minimum width to the div then it can expand, but you will have to set max-width that it can occupy as well so that it wont take full space pushing the second div away.

Related

Why isn't flex box centering my div properly

I was watching a video that used align items and justify center to put a div on top of the page
why is it only at the top not at in the middle of the page?
.wrapper {
display: flex;
width: 100%;
justify-content: center;
align-items: center;
padding: 0 10%;
overflow: hidden;
height: 100%;
}
<div class="wrapper">
<div class="cols cols0">
<span class="topline">Hello</span>
<h1> I'm <span class="multiText">Coder</span></h1>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Possimus, sed nam quia autem voluptatum quae omnis maiores dolorem hic dolores sint quisquam a. Eaque expedita laborum debitis, dolores fugit assumenda!</p>
<div class="btns"> <button> download CV</button>
<button> hire me</button>
</div>
</div>
<div class="cols cols0"></div>
</div>
The issue is that height: 100% means 100% of the parent's height. The parent's height however is by default set to fit-content means the height is undefined. 100% of undefined is also undefined!
In order to vertically center content within an element, the height of the element must be higher than the content itself!
The simple solution is to give it a min-height of 100vh (the height of your content frame within the browser.
To remove the default vertical scrollbar you have to reset the default body margin and set the box-sizing of the element to border-box as otherwise, the padding will add height on top.
/* need to add this body reset + min-height */
body {
margin: 0;
}
.wrapper {
min-height: 100vh;
box-sizing: border-box;
}
/* original css */
.wrapper {
display: flex;
width: 100%;
justify-content: center;
align-items: center;
padding: 0 10%;
overflow: hidden;
height: 100%;
}
<div class="wrapper">
<div class="cols cols0">
<span class="topline">Hello</span>
<h1> I'm <span class="multiText">Coder</span></h1>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Possimus, sed nam quia autem voluptatum quae omnis maiores dolorem hic dolores sint quisquam a. Eaque expedita laborum debitis, dolores fugit assumenda!</p>
<div class="btns"> <button> download CV</button>
<button> hire me</button>
</div>
</div>
<div class="cols cols0"></div>
</div>
The .wrapper div is not spread over the full height.
You can set its height to 100vh
try to remove second block
<div class="cols cols0"></div>

Align two elements on top of each other without a container with a width

I have two elements that I would like to be aligned in a column-like way. These elements also need to be floated to the right of the page. If I float the container containing these two elements to the right, they automatically align in a row-like way. My immediate thought is to specify a width of the container so that they will be forced to move downward. The issue with this is that the two elements are different widths.
<div style="float: right; width: 100px;">
<div style="width: 110px; height:50px; background-color: blue;">
Element 1
</div>
<div style="width: 60px; height:50px; background-color: red;
float:right;">
Element 2
</div>
</div>
paragraph text that will not flow into the bottom element because the container's width prevents it. Filler text.....
If the bottom element is not as wide as the top element, the width of the container makes it wider. This is an issue because I have other text / elements that I would like flow around these side elements, and it looks weird because of the whitespace created by the different in widths.
If I try something like making the parent absolute, as not to effect the other elements on the page, the children don't either.
How can I create elements that are floated in a container without the width of the container affecting the other elements on the page as well? Thanks, Levi
One approach is as below, taking advantage of display: contents comments in the code itself:
/* basic CSS reset to reduce all elements to the same
box-sizing, font, margin and padding: */
*,
::before,
::after {
box-sizing: border-box;
font: normal 1rem / 1.5 sans-serif;
margin: 0;
padding: 0;
}
/* a wrapping element to allow for some dynamic sizing of the
contents: */
main {
/* the width of the <main> element is 80vw (viewport-width units)
unless that is less than 30em (the minimum size it will be) or
unless that size exceeds 1000px (at which point it will take
a width of 1000px maximum): */
width: clamp(30em, 80vw, 1000px);
/* a margin of 1em on the top and bottom top-to-bottom languages: */
margin-block: 1em;
/* a margin of auto on the inline axis, left (start) and right (end)
in left-to-right languages, such as English: */
margin-inline: auto;
}
div.wrapper {
/* for those browsers that are yet to implement
logical properties: */
float: right;
/* equivalent to "float: right" in left-to-right
languages, such as English: */
float: inset-inline-end;
width: 100px;
/* effectively removes this element from the
layout, exposing its contents to the layout
engine: */
display: contents;
}
div.wrapper > div {
/* ensuring that the "display: contents" is
unset, which takes it back to the default
display model: */
display: unset;
/* for those browsers that are yet to implement
logical properties: */
float: right;
/* as above, equivalent to "float: right" in
left-to-right languages, such as English: */
float: inset-inline-end;
height: 50px;
/* forces each element to clear the float of its
siblings: */
clear: both;
/* to hide the overflowing text: */
overflow: hidden;
text-overflow: ellipsis;
}
.wrapper > div:first-child {
background-color: blue;
width: 110px;
}
.wrapper > div:last-child {
background-color: red;
width: 60px;
}
<main>
<div class="wrapper">
<div>
Element 1
</div>
<div>
Element 2
</div>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur perferendis corporis itaque, sequi quod commodi explicabo dolore, totam, libero architecto doloremque nisi illo iste quae ea, laboriosam reprehenderit nemo animi! Lorem ipsum dolor
sit amet, consectetur adipisicing elit. Quia reiciendis sapiente blanditiis provident ad ullam consequatur, temporibus ex accusamus est nihil voluptatum totam cupiditate. Ducimus sit deserunt nostrum, dolorem doloremque. Lorem ipsum dolor sit amet,
consectetur adipisicing elit. Consectetur perferendis corporis itaque, sequi quod commodi explicabo dolore, totam, libero architecto doloremque nisi illo iste quae ea, laboriosam reprehenderit nemo animi! Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Quia reiciendis sapiente blanditiis provident ad ullam consequatur, temporibus ex accusamus est nihil voluptatum totam cupiditate. Ducimus sit deserunt nostrum, dolorem doloremque.</p>
</main>
JS Fiddle demo.
References:
CSS logical properties.
display.
float.
Bibliography:
Compatibility of CSS logical properties, from "Can I Use."

margin lost when use custom-component in Angular

Hello I have a parent flex box and 2 childs with 100% width.
<div class="wrapper">
<app-user></app-user>
<div class="text">
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Delectus
laboriosam incidunt necessitatibus optio id cumque velit nam deserunt
dolorem. Dolorum asperiores corporis reiciendis veniam, porro temporibus
obcaecati distinctio illo. Nihil.
</div>
</div>
My margin in <app-user> not working , due to the 100% width of parent. I need to fix the 100% width of my .sidebar element to keep the width and also I need a margin to take .text little bit away from my first element
link to code
app-user
<div class="sidebar">
sidebar
<img
src="https://cdn.oneesports.gg/wp-content/uploads/2020/07/Dota2_InvokerHeader-1024x683.jpg"
alt=""
/>
</div>
css
.wrapper {
display: flex;
}
.sidebar {
max-width: 400px;
width: 100%;
margin-right: 32px;
}
.sidebar img {
width: 100%;
height: auto;
}
UPD 1
If I move styles from .sidebar directly to app-user in browser it works perfect , but I dont want to use :host styles in css. As in produciton I have a big project
You can use calc() function documented here :
https://developer.mozilla.org/fr/docs/Web/CSS/calc()
So you can remove the margin from your .sidebar class
and add this css lines to your app.component.css :
.wrapper .text { width: calc(100% - 32px); margin-left: 32px; }
And here is your link code that I modified

Width of flex-container doesn't expand when flex item contains a variable width image

The problem arises when there is a variable width image set to max-height: 100%; in a flex-item. When the page loads flex box successfully gets the width for the current size of the flex-item, however if you resize the browser which forces the image to decrease in size (or increase), the outer container does not follow the new width.
If you run the code snippet below in full screen you'll see on initial load the image is fully surrounded by the pink box, but when you resize (make the height smaller) the image shrinks and the flex container stays at the same width.
.flex {
display: inline-flex;
background-color: deeppink;
height: calc(100vh - 20px);
border: 10px solid deeppink;
}
.flex-left {
width: 250px;
}
.flex-right img {
width: auto;
min-width: 0;
max-height: 100%;
}
<div class="flex">
<div class="flex-left">
<h2>Testing headline</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus quia nemo qui ipsam? Temporibus sint necessitatibus expedita, eum quae tempora voluptas dolore facere voluptate! Possimus molestias non commodi. Officiis, iste?</p>
</div>
<div class="flex-right">
<img src="http://unsplash.it/1000/650" alt="">
</div>
</div>
Try adding a percentage height to the image container.
The image is already flexible, because its dimensions are set in percentages. With a percentage height on the container, it becomes flexible, as well.
jsfiddle demo
.flex {
display: inline-flex;
background-color: deeppink;
height: calc(100vh - 20px);
border: 10px solid deeppink;
}
.flex-left {
width: 250px;
}
.flex-right {
height: 100%;
}
img {
max-width: 100%;
max-height: 100%;
}
<div class="flex">
<div class="flex-left">
<h2>Testing headline</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus quia nemo qui ipsam? Temporibus sint necessitatibus expedita, eum quae tempora voluptas dolore facere voluptate! Possimus molestias non commodi. Officiis, iste?</p>
</div>
<div class="flex-right">
<img src="http://unsplash.it/1000/650" alt="">
</div>
</div>

Make flex item take 100% width of new line

I have HTML structure like:
<div class="container">
<div class="btn-1"></div>
<div class="btn-2"></div>
<div class="btn-3"></div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. A veritatis harum illum assumenda odio ut, quos, ipsam molestias et sint nemo, saepe! Soluta a, quasi sequi, ut corrupti eius molestias.
</div>
</div>
btn-1 should be aligned to the top left, all other buttons (btn-2, btn-3...) should be aligned to the top right.
The text after all these buttons should be 100% width.
Quick mockup:
I figured out the first part (buttons) with:
.container {
display: flex;
justify-content: flex-end;
}
.btn-1 {
/* align first button to the left */
margin-right: auto;
}
Bu not matter what I do, the text doesn't flow to the next line...
Here's my JSFiddle https://jsfiddle.net/an0o7taq/
Thanks for any help!
You need to add flex-wrap: wrap to the container.
By default, flex containers are set to flex-wrap: nowrap, forcing items to remain on a single line.
revised jsfiddle
Spec reference:
5.2. Flex Line Wrapping: the flex-wrap property
You need more container with different flex flows and styles. Tip: learn most important flex props: align-items, flex-flow, justify-content. They all apply to the direct children of the container. So when you want your layout you need more container with different flex flows.
This guide helped me a lot. They also have great examples:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
.container {
display: flex;
flex-flow: column nowrap;
}
.header {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.header-left, .header-right {
display: flex;
flex-flow: row nowrap;
}
.btn {
width: 40px;
height: 40px;
border: 1px solid #ddd;
background-color: #eee;
}
.text {
width: 100%;
}
<div class="container">
<div class="header">
<div class="header-left">
<div class="btn">btn1</div>
</div>
<div class="header-right">
<div class="btn">btn2</div>
<div class="btn">btn3</div>
</div>
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. A veritatis harum illum assumenda odio ut, quos, ipsam molestias et sint nemo, saepe! Soluta a, quasi sequi, ut corrupti eius molestias.
</div>
</div>