Why isn't flex box centering my div properly - html

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>

Related

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

i need help on how to make a div occupy 100% of parent div verticallt

i need a solution guys, i want the two boxes in pink and white to have same height as its parent container in blue box (that is to stop where that blue box stopped) without depending on the content of the div in pink and white boxes.
here is my fiddle code
https://jsfiddle.net/dcq4bufa/3/
my HTML CODE
<head>
<title>eco</title>
</head>
<body>
<header class="header">
<ul class="nav">
<li class="nav-items">HOME</li>
<li class="nav-items">APP</li>
<li class="nav-items">STORE</li>
</ul>
<div class="home">
<div class="home__right">
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente nemo
aliquid saepe tempora doloribus dicta quas aperiam, eius odio ipsa culpa ipsam rerum quam vero
itaque, recusandae sint perferendis ipsum.</p>
<h1 class="home__right--sub"> tenetur quidem ducimus quod odit totam, dolor</h1>
</div>
<home class="home__left">
<h1 class="paragraph">Lorem ipsum dolor sit amet, consectetur ad</h1>
</home>
</div>
</header>
</body>
First you must outline the height of the parent container(blue),then set the two children elements(pink and white)height of 100% since they are in a container that is 100 of your device height.Try this:
.home {
display: flex;
height:100vh;
&__right {
background-color: pink;
height:100%;
flex-basis: 50;
}
&__left {
background-color: white;
height:100%;
flex: 1;
}
}
You can adjust the height of .home if you are not comfortable with the overflow
If you can use flexbox, you may do the following:
.header {
height: 80vh;
background-color: blue;
/* Add flexbox */
display: flex;
flex-direction: column;
}
.home {
display: flex;
flex: 1; /* Set flex-grow: 1 */
...

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>

CSS divs next to each other that affect each others width

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.