I would like a , tags which take 100% of the page and my cards to be scrollable when their contents are overflowing. But I can't handle the cards overflow the right way: the scrollbar isn't scrollable. All my containers use flexbox rules.
I've created a codepen to show you the issue:
https://codepen.io/YseopCSS/pen/dBGPYb
Can you help me please ?
<main class="edit-report">
<header class=header>
header
</header>
<section class="main__content">
<div class="global-filter">global-filter</div>
<div class="edit-report__edition">
<div class="edit-report__card data">data</div>
<div class="edit-report__card table">table</div>
<div class="edit-report__card report">
Report<br>
Report<br>
Report<br>
Report<br>
Report<br>
Report<br>
Report<br>
Report<br>
Report<br>
Report<br>
</div>
</div>
</section>
</main>
html, body {
height:100%;
margin: 0;
padding: 0;
}
.edit-report {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
display: flex;
flex-direction: column;
width: 100%;
}
.header {
padding: 15px 50px;
background:green;
}
.main {
&__content {
display: flex;
flex-direction: column;
flex-grow: 1;
overflow: hidden;
background: yellow;
}
}
.global-filter {
height: 100px;
background: purple;
}
.edit-report {
&__edition {
display: flex;
align-items: flex-start;
justify-content: space-between;
flex-grow: 1;
}
&__card {
flex: 1 1 25%;
min-height: 100px;
&.data {
background: pink;
}
&.table {
background: violet;
}
&.report {
background: cyan;
overflow-y: scroll;
}
}
}
After checking your code, I realized the problem was the height on the edit-report__card.report, it does not have a height defined, thanks to that, the current height is "2106px", hence the scroll is not working, even when, you force it with "overflow-y: scroll;", in order to fix it just add this line:
.edit-report__card.report {
background: cyan;
overflow-y: scroll;
height: 100%;
}
CSS,
This pen maybe able to help you.
<ul>
<li>
<img src="http://placekitten.com/280/330"/>
</li>
<li>
<img src="http://placekitten.com/220/300"/>
</li>
<li>
<img src="http://placekitten.com/140/210"/>
</li>
<li>
<img src="http://placekitten.com/140/210"/>
</li>
<li>
<img src="http://placekitten.com/140/210"/>
</li>
<li>
<img src="http://placekitten.com/140/210"/>
</li>
<li>
<img src="http://placekitten.com/140/210"/>
</li>
ul {
display:flex;
flex-flow: row wrap;
/* allow scrolling of flex container */
overflow-x:auto;
/* overwrite list container styles */
margin:2px;
padding:1em;
/* color for some visual of the spacing */
box-shadow:0 0 10px black inset;
}
li {
/* remove list style bullets */
list-style-type:none;
width: 40%;
/* vertical center with 10px on left and right */
width: 23%;
padding: 1%
}
li img {
width: 100%;
}
https://codepen.io/rusticblonde/pen/QXyjKy
Related
Inside my HTML:
<div class="certificates-container">
<button class="certificates-exit-btn">⇐</button>
<div class="carousel">
<ul class="carousel-list">
<li class="slide" data-active>
<img src="images/img1.png" alt="Certificate Image #1">
<p>lorem10</p>
<p>2022</p>
</li>
<li class="slide">
<img src="images/img2.png" alt="Certificate Image #2">
<p>Lorem10</p>
<p>2022</p>
</li>
<li class="slide">
<img src="images/img3.png" alt="Certificate Image #3">
<p>lorem10</p>
<p>2022</p>
</li>
</ul>
</div>
</div>
Inside my scss;
.certificates-container{
position: fixed;
width: 100vw;
height: 100vh;
z-index: 99999;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
.certificates-exit-btn{
position: absolute;
top: 20px;
right: 20px;
font-size: 1em;
padding: 0.5em 1em;
}
.carousel{
display: block;
.carousel-list{
border: 1px solid blue;
padding: 2em;
display: flex;
overflow-y: scroll;
.slide{
border: 1px solid green;
width: 400px;
img{
display: block;
width: 100%;
}
p{
text-align: center;
}
}
}
}
}
I want .carousel-list to be horizontally scrollable. But green bordered .slide elements overflows the container and become invisible and looks like this;
image It does not become horizontally scrollable they just overflows the container and i can't put certain pixel width to them. How should I do that?
overflow-y property specifies whether to clip the content, add a scroll bar, or display overflow content of a block-level element when it overflows at the top and bottom edges. You need to use overflow-x for this.
Add this
.carousel-list {
overflow-x: scroll;
}
Try to align the div in the center of the link, not working. still align left.
a {
display: block;
text-align: center;
background: green;
}
div {
width: 100px;
background: red;
}
<a>
<div>
Hello World
</div>
</a>
Set the margin to auto on the div and remove the text-align rule on the anchor:
a {
display: block;
background: green;
}
div {
width: 100px;
background: red;
margin: auto;
}
<a>
<div>
Hello World
</div>
</a>
Use display: inline-block; on the div to allow that:
a {
display: block;
text-align: center;
background: green;
}
div {
width: 100px;
background: red;
display: inline-block;
}
<a>
<div>
Hello World
</div>
</a>
Set div style Margin 0 auto
div {
width: 100px;
background: red;
margin: 0 auto;
}
a {
display: block;
text-align: center;
background: green;
}
<a>
<div>
Hello World
</div>
</a>
There are quite a few ways in which this can be achieved; the following code snippet shows a few of those approaches with explanatory comments:
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
main {
display: grid;
gap: 1em;
margin-block: 1em;
margin-inline: auto;
width: clamp(10em, 70vw, 1000px);
}
/* styling all <a> elements: */
a {
border: 1px solid lightblue;
padding-block: 0.25em;
}
/* styling all <div> elements: */
div {
border: 1px solid palegreen;
text-align: start;
}
/* using the ::before pseudo-element to
show the 'Hello World string: */
div::before {
content: "Hello World";
display: block;
text-align: center;
}
/* styling all <code> elements: */
code {
/* display: block purely to allow for
padding to be applied on the inline
axis: */
display: block;
/* to show the code as it was written,
including line-breaks: */
white-space: pre;
padding-inline: 1em 0.5em;
}
a.withMarginInline div {
/* using the margin-inline of 'auto' to move the <div>
to the center of its parent: */
margin-inline: auto;
/* setting the width of the <div> to a width which fits its
content, allowing lines to wrap as needed: */
width: fit-content;
}
/* retaining the 'text-align' property of 'center': */
a.withDisplayInlineBlock {
text-align: center;
}
/* displaying the <div> as an inline-block, which allows it
to be positioned as an inline element, but also allows it
to have specified width, height, margin...: */
a.withDisplayInlineBlock div {
display: inline-block;
}
/* setting the <a> parent to flex-box layout: */
a.withDisplayFlex {
display: flex;
/* centering the content of that element (horizontally),
if vertical centering is required then
'align-content: center' could be added, or
'place-content: center' used in place of both: */
justify-content: center;
}
a.withDisplayGrid {
/* using grid layout: */
display: grid;
/* positioning the content vertically and horizontally
in the centre: */
place-content: center;
}
<main>
<a href="#" class="withMarginInline">
<div>
<code>
childElement {
margin-inline: auto;
width: fit-content;
}
</code>
</div>
</a>
<a href="#" class="withDisplayInlineBlock">
<div>
<code>
parentElement {
text-align: center;
}
childElement {
display: inline-block;
}
</code>
</div>
</a>
<a href="#" class="withDisplayFlex">
<div>
<code>
parentElement {
display: flex;
justify-content: center;
}
</code>
</div>
</a>
<a href="#" class="withDisplayGrid">
<div>
<code>
parentElement {
display: grid;
place-content: center;
}
</code>
</div>
</a>
</main>
JS Fiddle demo.
References:
align-content.
border.
box-sizing.
clamp.
content.
display.
justify-content.
margin.
margin-block.
margin-inline.
padding.
padding-block.
padding-inline.
place-content.
text-align.
white-space.
width.
This question already has answers here:
Make a div fill the height of the remaining screen space
(41 answers)
Closed 4 years ago.
I've got a div, I need the bottom div to fill the remainder of the screen and show a scroll bar. The bottom div is not showing a scroll bar.
JSFiddle
.page {
position: fixed;
top: 0;
width: 100%;
height: 100%;
margin: 0;
padding: $menu-height 0 0 0;
box-sizing: border-box;
}
.sidebar {
width: 500px;
float: left;
height: 100%;
box-sizing: border-box;
background: #ddd;
border-right: 1px solid red;
}
.top {
height: 200px;
background: #eee;
}
.bottom {
background: #ccc;
overflow-y: auto;
}
.filler-content {
height: 2000px;
}
<div class="page">
<div class="sidebar">
<div class="top">
top
</div>
<div class="bottom">
<div class="filler-content">
bottom
</div>
</div>
</div>
</div>
If I understood your problem correctly, display: flex is your friend.
Add display: flex; flex-direction: column; to your .sidebar and flex: 1; to your .bottom and that should do it. If I misunderstood, just let me know in a comment and I'll try to help otherwise
Demo: http://jsfiddle.net/qy5fL29t/23/
I would use a flexbox solution as it will make it a lot simpler and get rid of the need for using floats (we shouldn't be abusing them in the day of css3)
html,
body {
height: 100%;
margin: 0;
}
.page {
height: 100%;
display: flex; /* this one is so that you don't need to float the sidebar and can insert a main area that will take up the rest of the width */
flex-direction: column;
flex-wrap: wrap;
}
.sidebar {
width: 500px;
height: 100%;
display: flex; /* this is so we can get bottom to take any height top doesn't need */
flex-direction: column;
background: #ddd;
border-right: 1px solid red;
}
.top {
flex-basis:200px;
min-height: 200px; /* these two are to force top to be 200px otherwise flex may recalculate based on available space */
max-height: 200px;
background: #eee;
}
.bottom {
flex-grow: 1; /* this forces bottom to grow to fill the space top doesn't take */
overflow-y: auto;
}
/* test and example below */
.filler-content {
height:1000px;
}
.main {
flex-grow: 1;
background: white;
}
<div class="page">
<div class="sidebar">
<div class="top">
top
</div>
<div class="bottom">
<div class="filler-content">
bottom
</div>
</div>
</div>
<div class="main">
</div>
</div>
Replace your css with this
.sidebar {
width: 500px;
float: left;
height: 100%;
box-sizing: border-box;
background: #ddd;
border-right: 1px solid red;
}
.top {
height: 200px;
background: #eee;
}
.bottom {
background: #ccc;
overflow-y: scroll;
height:200px
}
.filler-content {
height:2000px;
}
<html>
<body>
<div class="page">
<div class="sidebar">
<div class="top">
top
</div>
<div class="bottom">
<div class="filler-content">
bottom
</div>
</div>
</div>
</body>
</div>
</html>
You can use this code for bottom div srollbar.
.bottom {
background: #ccc;
overflow-y: auto;
height:200px;
}
I would like to center an element vertically and horizontally. The catch is that any sibling elements should maintain their position relative to the centered element. If the siblings are large enough, they may overflow the viewport. The siblings have variable heights.
I've started a code sample here: https://jsfiddle.net/hqmoz9xy/2/
html,
body {
height: 100%;
}
body {
margin: 0;
padding: 1em;
}
body,
.container {
box-sizing: border-box;
}
.container {
border: 1px solid red;
padding: 1em;
width: 100%;
height: 100%;
}
.main-display {
width: 100px;
height: 100px;
background-color: #999;
padding: 1em;
}
<div class="container">
<div class="main-display">
Main box: this box should be at the center of the container.
</div>
<ul class="extra-info">
<li>These items should naturally follow the main box and not care about vertical centering.</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
This is easily accomplished using JS and negative margins but I would like to do it only with CSS. Is there a way to do this using flex?
You can use flexbox:
.container {
display: flex; /* Magic begins */
flex-direction: column;
}
.before, .after {
flex: 1; /* If possible, center .main vertically */
min-height: 0; /* Really, don't care about overflow, just center .main vertically */
}
.main {
align-self: center; /* Center .main horizontally */
}
html,
body {
height: 100%;
}
body {
margin: 0;
padding: 1em;
}
body,
.container {
box-sizing: border-box;
}
.container {
border: 1px solid red;
padding: 1em;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.before,
.after {
flex: 1;
min-height: 0;
}
.main {
width: 100px;
height: 100px;
background-color: #999;
padding: 1em;
align-self: center;
}
<div class="container">
<div class="before"></div>
<div class="main">
Main box: this box should be at the center of the container.
</div>
<div class="after">
<ul class="extra-info">
<li>These items should naturally follow the main box and not care about vertical centering.</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</div>
setting negative margin to virtually reduce width down to zero for extra-info, using display:table to shrink on content and and center, you could do something like this :
html,
{
height: 100%;
}
body {
min-height:100%;
border: 1px solid red;
}
body,
.container {
box-sizing: border-box;
}
.container {
padding: 1em;
display: table;
margin: auto;
}
.main-display {
width: 100px;
height: 100px;
background-color: #999;
padding: 1em;
}
.extra-info {
padding:0;
margin:0;
background:lightgray;
margin-right: -50vw;
max-width: 50vw
/* size virtually reduce to zero with equal negative margin value*/
}
<div class="container">
<div class="main-display">
Main box: this box should be at the center of the container.
</div>
<ul class="extra-info">
<li>These items should naturally follow the main box and not care about vertical centering.</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
I am not sure I quite understand what do you want but maybe ?
#main {
width: 100%;
height: 100px;
border: 0px;
display: -webkit-flex;
display: flex;
}
#main div {
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
}
<div id="main">
<div style="background-color:white;">empty</div>
<div style="background-color:Blue;">Your box here</div>
<div style="background-color:white;">empty</div>
</div>
You also can do it with margin only by setting left margin to 40% or 35% depending on your box width
Is there a way for stickies to take into account other stickes on the page?
For example:
body {
display: flex;
min-height: 2000px;
flex-direction: column;
}
#header {
height: 40px;
flex: 0 0 auto;
position: sticky;
top: 0;
background: yellow;
}
#footer {
flex: 0 0 auto;
height: 20px;
}
#main {
display: flex;
flex: auto;
background: blue;
}
#side {
width: 200px;
background: red;
}
#side > div {
position: sticky;
top: 0px;
}
<div id="header">header</div>
<div id="main">
<div id="side">
<div>side</div>
</div>
<div id="content">
content
</div>
</div>
<div id="footer">footer</div>
Notice that if I scroll down the header will overlap the sidebar because they have the same top position.
To fix I have to make the top position of the sidebar take the value of the header height
body {
display: flex;
min-height: 2000px;
flex-direction: column;
}
#header {
height: 40px;
flex: 0 0 auto;
position: sticky;
top: 0;
background: yellow;
}
#footer {
flex: 0 0 auto;
height: 20px;
}
#main {
display: flex;
flex: auto;
background: blue;
}
#side {
width: 200px;
background: red;
}
#side > div {
position: sticky;
top: 40px;
}
<div id="header">header</div>
<div id="main">
<div id="side">
<div>side</div>
</div>
<div id="content">
content
</div>
</div>
<div id="footer">footer</div>
But what if the header has variable height? Can I tell the browser somehow to position the stickies so they dont overlap others?
I think you would need to use javascript for this. First to get the height of the header and then set the top position of your side div using that value. I am not aware of any pure css way of doing it I am afraid.
If you are using jQuery it is simply using the .height() method if not you can use this:
var clientHeight = document.getElementById('myDiv').clientHeight;
var offsetHeight = document.getElementById('myDiv').offsetHeight;
The offset method gets the height with any padding and borders.