I'm using a grid system with 3 columns in my SPA. The left and right list contain components that occupy 100 of the viewport height. The middle column contains a long list and would like to add a scrollbar just to the middle component. I've tried to wraw the middle component with several different scrollbar components but nothing works. I end up always with a main page scroll which leaves me only with the list component when scroll further down and left and right component are remaining remain to the top of the page.
Try adding overflow-y: scroll; on the middle component
const items = [...Array(100)].map((val, i) => `Item ${i}`);
const App = () => (
<div className="container">
<div className="left-col">
Left col
</div>
<div className="center-col">
<span>List</span>
<ul>
{items.map((item, i) => (<li key={`item_${i}`}>{ item }</li>))}
</ul>
</div>
<div className="right-col">
Right col
</div>
</div>
);
ReactDOM.render(<App />, document.getElementById('react'));
.container {
display: flex;
flex-direction: row;
height: 100vh;
}
.left-col {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
background: #ddd;
}
.center-col {
flex: 1;
background: #aaa;
overflow-y: scroll;
}
.right-col {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
background: #e7e7e7;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="react"></div>
Found the solution, need to set fixed height to left and right component and overflow:scroll to middle component.
Related
I'm asking my first question here, so I apologize in advance if I didn't explain something well.
I'm currently developing portfolio website (Angular 13 with Sass). I've come to a point where I have two divs placed next to each other inside of a flexbox. I use media query (mobile view) to achieve divs to place below each other. One div contains text and other image.
I'm using Sass for flex box
#mixin flex-container($flexdirection, $justifycontent: false) {
#if $justifycontent {
justify-content: $justifycontent;
} #else {
justify-content: center;
}
display: flex;
flex-wrap: wrap;
row-gap: 20px;
align-items: center;
flex-direction: $flexdirection;
}
HTML component looks like this
<div class="about" id="about" #about>
<div class="about__details">
<h1 class="about__title">{{title}}<span class="purple_highlight">A</span>ndjela. <span class="wave">👋</span>
</h1>
<p class="about__paragraph">{{paragraph}}
</p>
<a href="https://drive.google.com/file/d/1PK82zuN4g_Ly5nmNIgRpIsEW9j_gnyaS/view?usp=share_link" target="_blank">
<button class="about__cv_button">My resume</button>
</a>
</div>
<div class="about__img">
<img src="assets/img/profilePicture.png" alt="profile">
</div>
</div>
CSS looks like this
#include mq-between(xs, sm) {
.about {
#include flex-container(row);
height: 100vh;
&__img {
background-color: aqua;
bottom: 0;
right: 0;
flex-shrink: 0;
}
img {
max-width: 100%;
max-height: 100%;
}
&__details {
background-color: blanchedalmond;
left: 10%;
right: 10%;
}
}
}
Result looks like in picture
I've tried using flex-direction as column, flex-basis, max-width, max-height, padding and margin, but nothing seems to make changes for what I'm looking for.
What am I missing to show the divs beneath each other with the correct height?
So I wrapped my content in the layout component and all my other pages push the footer to the bottom because the content is bigger than the actual screen. However, I have one page that has less content and now my footer is awkwardly in the middle if I view it on a bigger height screen.
So here is my footer css
export const FooterContainer = styled.div`
background-color: #101522;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: absolute;
bottom: 0;
width: 100%;
`
My layout div css
.layout {
padding-bottom: 160px;
position: relative;
}
My layout component
return (
<>
<Sidebar isOpen={isOpen} toggle={toggle} />
<Navbar toggle={toggle} />
<div className="layout">
<main>{children}</main>
<Footer />
</div>
</>
)
}
Then in my page where my footer isn't sticking to the bottom this is my code
<Layout>
<div className="resource__container">
<h1 className="post__heading">
Here are some resources to learn web development
</h1>
<Posts posts={posts} key={posts.id} />
</div>
</Layout>
So here is a pic showing what is happening
The top black bar is the Navbar
The red section is my .resource__container
The white space below that is the padding-bottom: 160px from the .layout <div>
Then the last black space is the footer
After that it's just <main> but technically nothing there, so I don't why it isn't sticking to the bottom
Given a HTML structure like:
<body>
<header>…</header>
<main>…</main>
<footer>…</footer>
</body>
The solution to stick the footer at the bottom is using flexbox:
body {
display: flex;
min-height: 100vh;
flex-direction: column;
}
main {
flex: 1;
}
The key is the min-height: 100vh as well as flex: 1 of the main tag.
Useful resources:
https://dev.to/mokkapps/sticky-footer-in-gatsbyjs-using-flexbox-5162
https://css-tricks.com/couple-takes-sticky-footer/
https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/
I am trying to replicate stackoverflow-like design and ran into problem.
<div class="flex-grow-0 pd-around-m"> # line 1
<div class="flex-col fill-row mr-around-s"> # line 2
<div class="flex-row fill-row"> # line 3
<div class="flex-col justify-center mr-around-m"> # line 4
//Buttons
</div>
<span>
//Long Text!!
</span>
</div>
<div class="answer-bottom"></div>
</div>
</div>
.fill-row {
width: 100%
}
.flex-col {
display: flex;
flex-direction: column;
}
.flex-row {
display: flex;
flex-direction: row;
}
.mr-around-m {
margin: 1rem;
}
.justify-center {
justify-content: center;
}
When I enter long text in <span>, <div> in line 2, line 3 goes out of div box in line 1.
I tried adding white-space: pre-line to div in line 2 and directly at span but still text goes out of the box.
How can I keep the text inside parent div?
navigation bar on the left has property width:20% but gets squashed. Is this because of the textbox problem I asked above?
EDIT
https://jsfiddle.net/pzcu2yjn/
Here's a replication of my problem. if you make the text in span short enough, navbar and menu will have some empty space in the left maintaining 20% of the screen. however, if you leave the long text as it is, it gets squashed and 20% gets ignored
Few things:
On using flex it is good to provide width for left and right container since container will not know what it should when content increases.
Once you have the width assigned to the right container that is when you can use wrap functionality so the wrap works only for right container and it doesn't have no impact on less container. overflow-break-word;
NOTE:
I have removed unwanted code from the code, you can put it back it has no impact if those are needed.
.flex-row {
display: flex;
width: 100%;
}
.navbar {
display: flex;
flex-direction: column;
justify-content: flex-start;
border-right: 0.05rem solid var(--main-border-color);
align-items: flex-end;
width: 20%;
border: 1px solid red;
}
.pd-around-m {
width: 80%;
border: 1px solid blue;
display: inline-block;
overflow-wrap: break-word;
}
<div class="flex-row">
<div class="navbar">
<div>
menu1
</div>
</div>
<div class="flex-grow-0 pd-around-m">
<div class="flex-col fill-row mr-around-s">
<div class="fill-row">
<span>
AaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaa
</span>
</div>
<div class="answer-bottom"></div>
</div>
</div>
</div>
You can add overflow: hidden; to prevent the text from going outside of your div.
There's a very similar question with a precise answer:
<span> element going outside of <div> element
I am trying to build a flexbox type container with a search and filters and buttons. I am having trouble getting the desired behaviour. The toolbar-container class is the main flex container, which holds search-bar and filter-group as the top-level flex-items. I am getting confused as to how approach setting up filter-group as a nested flex-container to which the desired behavior is to push the buttons to the end of the row (far right, equivalent to float: right) and give the filters the largest amount of space, wrapping below as the window resizes but maintaining the positions of search and buttons on either side. I have tried using the below css the seperate the fitlers from the buttons but there filters and buttons stay grouped together like this:
search-bar-filters-buttons---------------------------------------------------
Below is desired layout, with only the filters wrapping to the space below when the space decreases.
search-bar-filters-----------------------------------------------------buttons
<div class="toolbar-container">
<div class="search-bar"></div>
<div class="filter-group">
<div class="filters"></div>
<div class="buttons"></div>
</div>
</div>
.toolbar-container {
display: flex;
}
.search {
}
.filter-group {
display: flex;
justify-content: space-between;
}
.filters {
flex-wrap: wrap;
}
.buttons {
}
You need to add flex: 1 for you .filter-group to take all remaining space. Demo:
.toolbar-container {
display: flex;
}
.filter-group {
display: flex;
flex: 1; /* new */
justify-content: space-between;
}
.filters {
flex-wrap: wrap;
}
<div class="toolbar-container">
<div class="search-bar">Search bar</div>
<div class="filter-group">
<div class="filters">Filters</div>
<div class="buttons">Buttons</div>
</div>
</div>
I'm having big trouble centering two elements in a flex container. What I would like to do is to center one element at the top of the flex container (using flex-start ?) and one element at the bottom of the container (flex-end ?). I have tried several options, but was unable to get what I want. Most of my tries ended in one element in the top left half of the container, and the other one in the bottom right one.....
Please have a look at: jsfiddle
<!DOCTYPE html>
<body>
<div class="flexcontainer1">
<div class="txt1"> Some text here </div>
</div>
<div class="flexcontainer2">
<div class="row">
<div class="txt2"> Some more text </div>
<div class="txt2"> Even more text </div>
</div>
</div>
</body>
Is this at all possible ?
Furthermore: I'm telling the elements to be centered, but it looks like the second line (Even more text) is not centered at all. Or is this just optical ?
Thanks,
Hans
The .row container between the flex container and the flex items is annoying, so get rid of it.
And then,
.flexcontainer1, .flexcontainer2 {
display: flex; /* Magic begins */
flex-direction: column; /* Vertical layout */
align-items: center; /* Center horizontally */
}
.flexcontainer1 { justify-content: space-around; }
.flexcontainer2 { justify-content: space-between; }
.flexcontainer1, .flexcontainer2 {
display: flex;
flex-direction: column;
align-items: center;
border: 1px solid black;
}
.flexcontainer1 {
justify-content: space-around;
height: 250px;
}
.flexcontainer2 {
justify-content: space-between;
height: 150px;
}
.txt1 {
font-size: 3.0vw;
}
.txt2 {
font-size: 2.2vw;
}
<div class="flexcontainer1">
<div class="txt1">Some text here</div>
</div>
<div class="flexcontainer2">
<div class="txt2">Some more text</div>
<div class="txt2">Even more text</div>
</div>