Hello, i've lost half of my hair in 2 days, sorry for my english :)
If you resize the window the buttons in #header go outside of his container.
Here is the code:
#root {
background: grey;
display: flex;
flex-direction: column;
align-items: center;
}
#header {
background: red;
align-self: stretch; /* fill in main-axis */
flex: 0 0 65px; /* fixed height */
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
}
<div id="root">
<div id="header">
<button>PUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSH</button>
<button>BYE.</button>
</div>
<----- resize me ---->
</div>
I think the problem is #root.width is leaded by window/body.width ?
I have a solution with min-content:
#root { min-width: min-content; ... }, but i don't want use this value.
How configure #root correctly acting as a good and beautiful container for my layout ?
Here is a codepen for playing: https://codepen.io/papawa/pen/NLKbKR
Simply there is no problem, it's a logical result, unless you decide what do you expect to get in a smaller screens then that's what you will get and the reason is that you have a button with a long 'one word text' and the solution to fix this is just by wrapping the text itself and that's how you do it:
overflow-wrap: break-word;
or
word-wrap: break-word;
Here's the overall result:
#root {
display:flex;
flex-direction: column;
align-items:center;
background:grey;
overflow:hidden;
}
#header {
align-self:stretch;
flex: 0 0 65px;
display: flex;
justify-content: space-between;
align-items: center;
background: red;
padding: 10px;
}
.btn{
width: 50vw;
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
}
<div id="root">
<div id="header">
<button class="btn">PUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSH</button>
<button>BYE.</button>
</div>
<---- resize me ---->
</div>
FOR FURTHER READING: Handling Long Words and URLs (Forcing Breaks, Hyphenation, Ellipsis, etc)
Related
This is my current design when I change the screen width:
html:
<footer class='footer'>
<div *ngFor='let item of items'>
<h3 class='footer-item'>{{item.Description}}</h3>
</div>
</footer>
css:
.footer {
width: 100%;
height: fit-content;
display: flex;
flex-direction: row;
}
.footer-item {
margin: 15px;
}
I'm looking for a way to add a line breaker when the screen gets smaller, so the items will go in a new line instead of just making overflow-y.
You can use CSS flex wrap property to wrap items to next line
.footer {
width: 100%;
height: fit-content;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
I've started learning flexbox recently and tried doing some exercises to practice but I got stuck because the container for h1 is a lot bigger than it needs to be, even if margin and padding are 0, and it makes the whole other page uncentered.
main {
display: flex;
justify-content: center;
background-color: #cacaca;
height: 100vh;
flex-flow: row wrap;
}
h1 {
margin: 0;
padding: 0;
}
#container {
margin-top: 100px;
}
<main>
<h1>Here are some nice pics</h1>
<div id="container"> ... </div>
</main>
It's because default value of align-items is stretch which makes all flex items to stretch to full height of their parent. if you give align-item: flex-start to use only required height.
main {
height: 100vh;
display: flex;
justify-content: center;
align-items: flex-start;
}
h1 {
background: red;
}
<main>
<h1>Here are some nice pics</h1>
<div id="container">Container</div>
</main>
I'm having a problem with CSS flexbox. I had a working code yesterday yet today when I tested my solution it stopped working for some reason. It has to do with flexbox.
This is the result I want to have:
To be able to position the content with justify-content. This fails
Content should take all the available space so it has flex-grow: 1. This fails, as well.
The footer should be at the bottom since the content would push it down by taking all the available space thanks to flex-grow: 1. This fails.
It seems that whole flexbox stopped working correctly for me.
I believe the problem is that for some reason flexbox does not even respond correctly to this:
`justify-content: flex-start`
If I try any other values there like center, flex-end, etc nothing happens.
Funny thing is that yesterday flexbox was behaving correctly, I could position it around with justify-content and today I can't.
What am I missing here why is not at least justify-content: flex-end or justify-content: center doing behaving correctly and positioning the content?
If I fix the problem that causes justify-content to stop working I believe flex-grow will also work.
Does anyone have an idea why it's misbehaving?
I can get flex to behaving using this playground so I know my code should be working, My code above is exactly what I did here in the playground:
https://demos.scotch.io/visual-guide-to-css3-flexbox-flexbox-playground/demos/
.ikigai {
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.header, .footer {
height: 80px;
margin: 10px;
border: 1px dashed lightgray;
}
.content {
margin: 10px;
border: 1px dashed lightgray;
flex-grow: 1;
}
<div class="ikigai">
<div class="header">this is a header</div>
<div class="content">content</div>
<div class="footer">footer 12</div>
</div>
https://jsfiddle.net/re01pn2x/
Your flex container has no height defined.
Therefore, it defaults to height: auto (content-driven height).
Add this to your code:
.ikigai {
height: 100vh;
}
.ikigai {
display: flex;
flex-direction: column;
/* justify-content: flex-start; */ /* default value; not necessary */
height: 100vh;
}
.header, .footer {
height: 80px;
flex-shrink: 0; /* optional; if you don't want items to shrink vertically */
margin: 10px;
border: 1px dashed lightgray;
}
.content {
margin: 10px;
border: 1px dashed lightgray;
flex-grow: 1;
}
body {
margin: 0; /* override default margins; prevents vertical scrollbar */
}
<div class="ikigai">
<div class="header">this is a header</div>
<div class="content">content</div>
<div class="footer">footer 12</div>
</div>
More details: How to make div 100% height of the browser window?
justify-content
Note that justify-content wasn't working in your code because there was no free space available. This property works by distributing free space in the container. In this case, because the container was defaulting to height: auto, there was only enough space to accommodate the content.
justify-content & flex-grow
Also note that even with a height defined that creates extra space, justify-content will not work if you use flex-grow. Why? Because flex-grow will consume that free space, again leaving no space for justify-content to distribute.
You can fixed using height:100vh;
.ikigai {
display: flex;
flex-direction: column;
justify-content: flex-start;
height: 100vh;
}
.header, .footer {
height: 80px;
margin: 10px;
border: 1px dashed lightgray;
}
.content {
margin: 10px;
border: 1px dashed lightgray;
flex-grow: 1;
}
I want to center 4 boxes at the center of a page, i.e., they should be vertically centered and horizontally, one box should be at extreme left, one at extreme right and the left ones should be placed horizontally between the extreme ones.
I know that such a question have been asked before, but I am not getting the exact logic of the solution. Can someone please give a proper explanation for the same? Thanks a lot.
Here's the HTML code-
.cards div{
height: 200px;
width: 200px;
float: left;
text-align: center;
margin-left: 100px;
}
.card_1{
background-color: green;
}
.card_2{
background-color: blue;
}
.card_3{
background-color: yellow;
}
.card_4{
background-color: red;
}
<html>
<head>
<link rel="stylesheet" href="second.css">
</head>
<body>
<div class="cards">
<div class="card_1">
</div>
<div class="card_2">
</div>
<div class="card_3">
</div>
<div class="card_4">
</div>
</div>
</body>
</html>
Here is a solution using flexbox:
html, body {
height: 100%;
padding: 0;
margin: 0;
}
.cards {
width: 100%;
height: 100%;
align-items: center;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.cards div {
height: 200px;
width: 200px;
}
.card_1 {
background-color: green;
}
.card_2 {
background-color: blue;
}
.card_3 {
background-color: yellow;
}
.card_4 {
background-color: red;
}
More information about flexbox:
A Guide To Flexbox
Flexbox Froggy
JSFiddle
Edit
The secret to this solution is
align-items: center;
display: flex;
flex-direction: row;
justify-content: space-between;
display: flex; instructs the browser to use a flexbox layout when rendering your container element. flex-direction: row; renders all children of the container in a row. align-items: center; vertically centers the children of the container. And finally justify-content: space-between; spaces each child of the container with equal space in between each.
Flexbox is a powerful layout system. I would recommend learning more about it through SO or the provided links.
Keep in mind that flexbox is supported across all major browsers but IE 11 has limited support due to several bugs.
I've recently starting using flexbox and this is the first problem I've run into. I want my .wrp class below to remain display: inline-block; but one line seems to disable this value. That line is: flex-direction: column. When I remove that line my .wrp class starts behaving like an inline-block element again but then of course it loses it's flex-direction value. Is there a simple solution that doesn't require restructuring my HTML too much to keep the flex-direction behavior of flexbox but also keep the inline-block behavior on .wrp?
.contr {
display: flex;
flex-direction: column; /* this line seems to be breakig my display on .wrp */
justify-content: center;
height: 10rem;
background-color: #eee;
}
.wrp {
display: inline-block;
height: 5rem;
background-color: #ddd;
}
p {
width: 100%;
background-color: #ccc;
}
<div class="contr">
<div class="wrp">
<p>I want this paragraph to stretch to fit it's content. Not full width.</p>
</div>
</div>
You can't have an inline-block element within a flex. It looks like you may be looking for display: inline-table:
.contr {
display: flex;
flex-direction: column; /* this line seems to be breakig my display on .wrp */
justify-content: center;
height: 10rem;
background-color: #eee;
}
.wrp {
display: inline-table;
height: 5rem;
background-color: #ddd;
}
p {
width: 100%;
background-color: #ccc;
}
<div class="contr">
<div class="wrp">
<p>I want this paragraph to stretch to fit it's content. Not full width.</p>
</div>
</div>
Hope this helps! :)