I have a weird problem, I am pretty bad with CSS therefor I come up with this exercise where I want to build a image slider, which is inside a div called slider-div with width of 1000px, from a array, 10 images are loaded into the div and only 5 are visible so the user have to scroll.
To learn more I added two buttons which should scroll 100px (left or right),
I have a problem with those buttons, they stick to the parent slider-div / Background. I hoped they would always stay on the top and don't move. User can decide to scroll to the end or hit the buttons.
I am using display: flex; flex-direction: row; to get the images into a row.
How can I fix button to the visible edge of a div?
Only solution I worked out is:
position: fixed;
right: 33.6%;
but it is very unprofessional :( also bad when I changing the layout somewhere else.
Can you please look over and tell me where the mistake is what is missing?
Here is the link to codepen:
https://codepen.io/miomate/pen/pBQBya
Code:
.div-1 {
max-width: 1000px;
height: 200px;
overflow-x: scroll;
overflow-y: hidden;
position: absolute;
}
button{
/* position: absolute; */
top:35%;
}
.images {
display: flex;
flex-direction: row;
list-style: none;
margin: 0;
padding: 0;
}
.x {
position: absolute;
top: 30%;
}
.div-2-1{
}
.div-2-2{
right: 0;
}
The images aren't loaded but the result ist the same.
Thank you very much.
This is my solution using the "bottom" property of css.
body {margin: 0 0 0 0;}
.div-1 {
max-width: 1000px;
height: 300px;
overflow-x: scroll;
overflow-y: hidden;
position: relative;
}
.images {
display: flex;
flex-direction: row;
list-style: none;
margin: 0;
padding: 0;
}
.x {
position: absolute;
bottom: 0;
}
.div-2-1{
left:0;
}
.div-2-2{
right: 0;
}
Related
Basically I have a floating image, which has some arbitrary size based on the image being loaded (capped to some max-width %), with a caption below. (In the examples, I set a fixed size just for demonstration.)
Initially, I had a problem getting the figcaption to auto-fit to the size of the image above it. I used the display: table-caption style to get that to work in Firefox, but it breaks in Chrome.
<div>
<figure>
<img />
<figcaption>This is some text. This is some text. This is some text. This is some text. This is some text.</figcaption>
</figure>
</div>
div {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
figure {
position: relative;
max-width: 85%;
margin: auto;
display: block;
}
img {
width: 300px;
height: 150px;
background: #000;
}
figcaption {
display: table-caption;
caption-side: bottom;
width: 100%;
}
https://jsfiddle.net/45jk62s8/
In Chrome, for me, the figcaption looks all scrunched up, not the same width as the image.
If I change the figure element to display: table, it works in Chrome but not Firefox.
figure {
display: table;
}
https://jsfiddle.net/45jk62s8/1/
In Firefox, for me, the figure is no longer horizontally centered and the figcaption width is not constrained. I tried width: fit-content but that doesn't work since the figcaption is allowed to run long.
I assume it has something to do with the wrapper div I use to center the figure, but I can't figure out a cross-browser solution to this at the moment. The figure does need to be fixed and centered, such that even if the behind content is scrollable, the figure is always centered in the page.
This CSS seems to create the desired output in Firefox and Chrome. It uses table, table-cell and table-caption for all the components and removes the 100% width on the caption.
div {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow-x: hidden;
overflow-y: auto;
display: flex;
align-items: center;
justify-content: center;
}
figure {
position: relative;
max-width: 85%;
margin: auto;
display: table;
}
img {
width: 300px;
height: 150px;
background: #000;
display: table-cell;
}
figcaption {
display: table-caption;
caption-side: bottom;
}
I've a fixed side bar on the right side of the page (position: fixed)
But it's contents are not fully visible as it's not scrolling with the page scroll. I could have added overflow-y: scroll in the .sidebar{} css settings. But don't want a separate scroll bar for sidebar. Is there an option to make it scroll with the full page scroll.
Here is my css settings for sidebar :
.sidebar {
text-align: center;
padding: 2rem,1rem;
color: rgba(255,255,255,.5);
background-color: #202020;
top: 0;
bottom: 0;
}
If you want to debug to see what went wrong, here is it running live : https://pagefault.me
Thanks
Based on the answer I suggested in my comment, I was able to work in chrome to arrive at the css below.
1) Add some css to the .sidebar-nav component
nav.sidebar-nav {
position: absolute;
overflow-y: scroll;
top: 100px; /*100px to give some room for the sidebar heading (without this, absolute position will make the nav overlap)*/
left: 15px; /* you can make this zero and add `padding-left: 15px` */
bottom: 15px; /* leave some room for copyright section */
right: -17px; /*this may vary from browser to browser (i suggest using the width of the widest scrollbar, then adjust for padding-right)*/
padding-right: 15px; /*padding to prevent the text from flowing off screen*/
}
2) The .container class becomes
.sidebar .container{
max-width: 38rem;
padding-left: 1rem;
padding-right: 1rem;
margin-left: auto;
margin-right: auto;
height: 100%;
position: relative;
overflow: hidden;
}
3) Make sure the footer bit remains at the bottom after making .sidebar-nav absolute
.sidebar .container > p:last-of-type {
position: absolute;
bottom: -15px;
}
Of course as mentioned in the original solution, you have to test the scrollbar widths in different browsers to arrive at the right width to use in place of right: -17px in step 1.
Use absolute position instead of fixed as you want it to scroll it along with the page.
body {
margin: 0;
padding: 0;
position: relative;
}
main {
position: absolute;
top: 0;
left: 0;
width: 80%;
height: 300vh;
background: beige;
}
aside {
position: absolute;
top: 0;
right: 0;
width: 20%;
height: 300vh;
background: black;
color: white;
}
<main></main>
<aside><aside>
A flex box solution without positioning :
body {
margin: 0;
padding: 0;
display: flex;
}
main {
width: 80%;
height: 300vh;
background: beige;
}
aside {
width: 20%;
height: 300vh;
background: black;
color: white;
}
<main></main>
<aside></aside>
Page is here
I am using a sticky footer, which works fine everywhere but my portfolio page. The gallery is relatively positioned, and is overflowing from it's parent div. The footer is sticking to the bottom of the parent, rather than the page.
Help? Preferably in a way where I don't have to rewrite all my code?
Please try this one i hope it will helpful to you.
html, body {
height: 100%;
}
header {
background: #fff;
position: relative;
z-index: 100;
}
.content2 {
clear: both;
margin: 0 auto;
padding: 0;
text-align: center;
width: 70%;
}
.gallery {
display: table;
height: 100%;
margin-left: auto;
margin-right: auto;
margin-top: auto;
position: relative;
width: 100%;
}
#footer-block {
bottom: 0;
position: fixed;
width: 100%;
}
Thanks
Fixed it by coding body and html to 110% height, footer wrapper to 100% height, and adding page wrapper with 100% height.
i am trying to create a responsive fullscreen horizontal gallery scroll.
i have worked it out but the results are not okay.
Actuallt the image should be fully strecthed to an li. i's vertically scrolling.
secondly the li's are not shoing fine on firefox, but on chrome it works fine.
please help.
thanks.
my code snippet
ul {
display: inline-block;
height: 100px;
white-space: nowrap;
margin: 0;
padding: 0;
}
li {
display: inline-block;
max-height: 100%;
width: 25%;
}
here is the fiddle
FIDDLE
Thanks.
demo with li.image = 25%
Full li image demo
body {
width:100%;
margin: 0;
padding: 0;
}
you have missing width from body tag
Keep in mind that width: 100%; on slides class wont work unless the parent of this class has width defined in it (which is body here)!!
Since you li height is fixed, you can use img width as 100%
slides {
width: 100%;
overflow: auto;
margin: 0;
padding: 0;
}
ul {
display: inline-block;
height: 100px;
white-space: nowrap;
margin: 0;
padding: 0;
}
li {
display: inline-block;
height: 100px !imoprtant;
width: 25%;
}
li img{
width: 100%;
}
DEMO
i need to make a div position absolute and make it in center but i used does not make it happen. i have gone crazy trying to make it to the center.
i have tried using left and right value to 0. it should have made the div to the center automatically.
need to figure out what went wrong?
help please!
here is the code that i have tried and stuck
.slider-wrap {
width: 1000px;
height: 500px;
left: 0;
right: 0;
top: 100px;
background: #096;
z-index: 99;
margin: 0 auto;
}
You forgot to set the position to absolute
.slider-wrap {
width: 1000px;
height: 500px;
left:0; right:0;
top:100px;
background:#096;
z-index:99;
margin:0px auto;
}
Add position:absolute;
.slider-wrap {
position:absolute;
width: 1000px;
height: 500px;
left:0; right:0;
top:100px;
background:#096;
z-index:99;
margin:0px auto;
}
The proper way to center a div is as such:
.slider-wrap {
...
margin-right: auto;
margin-left: auto;
}
Also note that z-index will not work unless you set the position attribute. ie: position: absolute;
You need to add position: absolute; to your css for absolute positioning.
Note: Also add position: relative; to the parent element you whish to use as wrapper.
So for example:
.slider-container {
position: relative;
width: 100%;
height: 100%;
...
}
.slider-wrap {
position: absolute;
...
}
The width and height of 100% is optional, just added it in case you want the container to take up the whole remaining space or the whole page if it's right after the opening body tag...
To position the slider-wrap in the center of the screen (so both horizontal and vertical centered), try this:
html, body {
width: 100%;
height: 100%;
}
.slider-container {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
...
}
.slider-wrap {
position: absolute;
width: 1000px;
height: 500px;
top: 50%;
left: 50%;
margin-top: -250px; // half the height
margin-left: -500px; // half the width
...
}
If you're able to make the slider-wrap display inline with e.g. display: inline-block; (keep browser support in mind with this one), then you could use the following instead:
.slider-container {
position: relative;
width: 100%;
height: 100%;
text-align: center; // this one makes the slider-wrap center horizontally
...
}
.slider-wrap {
position: relative;
display: inline-block;
width: 1000px;
height: 500px;
vertical-align: middle; // this one does the vertical centering
...
}
Another option is using display: table-cell;. It's about the same as the previous one:
.slider-container {
position: relative;
display: table;
width: 100%;
height: 100%;
text-align: center;
...
}
.slider-wrap {
position: relative;
display: table-cell;
width: 1000px;
height: 500px;
vertical-align: middle;
...
}
Also line-height: ?px; will make vertical centering possible. But I think this answer is long enough now :-P
Give it a try. Fiddle around with this until you're happy with the result :-)
Try this it should work fine.
display:block;