I will make a carousel and i try to align left 3 elements with width 100% each but it is not working.
Can someone tell me how to align 3,4 elements on left to get out of the screens witdh so that i can change the image with the transform css property ?
it worked when i delete from the top of the page but when i insert it,everything is ruined.
.carousel {
width: 100%;
margin: 0;
background-color: #ccc;
position: relative;
overflow-x: hidden;
}
.carousel--list {
margin: 0;
padding: 0;
}
.carousel--slide {
background: #000;
text-align: center;
color:#fff;
width:100%;
display: inline-block;
overflow: hidden;
height: 400px;
padding: 0;
}
.carousel--track{
width: auto;
-webkit-transition: transform 2s;
transition: transform 2s;
}
.carousel--buttons {
content: "";
clear: both;
display: table;
}
.slide--image {
width: 100%;
}
.red{
background: red;
}
.blue{
background: blue;
}
.yellow{
background: yellow;
}
/* transform .*/
<section class="carousel">
<div class="carousel--track">
<div class="carousel--list">
<div class="carousel--slide red" data-index="0">
<img class="slide--image" src="./images/carousel/slider-bg.png">
</div>
<div class="carousel--slide blue" data-index="1">
<img class="slide--image" src="./images/carousel/slider-bg.png">
</div>
<div class="carousel--slide yellow" data-index="2">
<img class="slide--image" src="./images/carousel/slider-bg.png">
</div>
</div>
</div>
<div class="carousel--buttons">
<button onclick="prevSlide()" class="btn--prev btn">Prev</button>
<button onclick="nextSlide()" class="btn--next btn">Next</button>
</div>
</section>
Thanks in advance !
You could use flexbox or bootstrap. With those you will be able to edit your code as a gride and place elements where you wan't.
Flexbox is a css engine. Maybe what you need !
Example for bootstrap :
And flexbox :
Apply white-space: nowrap to the parent .carousel--list class...It will wrap all the inner inline-block items in a single row.
Note: I have applied transform(-30px) to the .carousel--list to give you a visual
Stack Snippet
.carousel {
width: 100%;
margin: 0;
background-color: #ccc;
position: relative;
overflow-x: hidden;
}
.carousel--list {
margin: 0;
padding: 0;
white-space: nowrap;
transform: translateX(-30px);
}
.carousel--slide {
background: #000;
text-align: center;
color: #fff;
width: 100%;
display: inline-block;
overflow: hidden;
height: 150px;
padding: 0;
}
.carousel--track {
width: auto;
-webkit-transition: transform 2s;
transition: transform 2s;
}
.carousel--buttons {
content: "";
clear: both;
display: table;
}
.slide--image {
width: 100%;
}
.red {
background: red;
}
.blue {
background: blue;
}
.yellow {
background: yellow;
}
/* transform .*/
<section class="carousel">
<div class="carousel--track">
<div class="carousel--list">
<div class="carousel--slide red" data-index="0">
<img class="slide--image" src="http://via.placeholder.com/350x150/f00">
</div>
<div class="carousel--slide blue" data-index="1">
<img class="slide--image" src="http://via.placeholder.com/350x150/00f">
</div>
<div class="carousel--slide yellow" data-index="2">
<img class="slide--image" src="http://via.placeholder.com/350x150/f0f">
</div>
</div>
</div>
<div class="carousel--buttons">
<button onclick="prevSlide()" class="btn--prev btn">Prev</button>
<button onclick="nextSlide()" class="btn--next btn">Next</button>
</div>
</section>
Related
I'm building a customised horizontal carousel, where in I want to display some items which are vertically scroll-able.
Code I've tried so far:
html
<div class="carousel">
<div class="c-item">Item-1</div>
<!-- to be displayed vertically -->
<div class="abs">
<div class="a-item">Abs Item-1.1</div>
<div class="a-item">Abs Item-1.2</div>
<div class="a-item">Abs Item-1.3</div>
</div>
<div class="c-item margin">Item-2</div>
<!-- to be displayed vertically -->
<div class="abs">
<div class="a-item">Abs Item-2.1</div>
<div class="a-item">Abs Item-2.2</div>
<div class="a-item">Abs Item-2.3</div>
</div>
</div>
<div class="other">
Other div
</div>
css
.carousel{
color: #FFF;
white-space: nowrap;
overflow-x: auto;
overflow-y: hidden;
position: initial;
.c-item{
display: inline-block;
width: 35%;
background: #000;
height: 100px;
&.margin{
//margin-left: 35%;
}
}
.abs{
background: #444;
display: inline-block;
vertical-align: top;
width: 35%;
max-height: 180px;
overflow-y: auto;
.a-item{
height: 100px;
border: 1px solid #000;
}
}
}
.other{
background: yellow;
}
Result:
(codepen)
The problem here is: I want the other div to start just below the item-1; meaning that the vertically scrolled div should be overlapping the other div and the carousel height should be fixed at 100px. I tried using position: absolute for the .abs div but then that div doesn't move on scrolling the carousel.
Desired output will look like this:
A flexbox solution
Each item is 33.33% wide and 100px high. The items inside .multiple are also 100px high.
.multiple has position: relative and overflow-y: auto. The items inside have position: absolute.
Hint: Container -> position: relative, items inside -> position: absolute. That's how it works.
top: (100 * n)px for each <div> inside .item.multiple. n is the index of the <div> inside .item.multiple, starting with 0.
The HTML structure has been changed
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.carousel {
display: flex;
width: 100vw;
overflow-x: auto;
color: white;
}
.carousel>.item {
flex: 1 0 33.33%;
//margin-right: 5px;
}
.carousel>.item:nth-child(odd) {
background: black;
}
.carousel>.item:nth-child(even) {
background: darkgrey;
}
.carousel>.item,
.carousel>.item.multiple>div {
height: 100px;
}
.carousel>.item.multiple {
position: relative;
overflow-y: auto;
}
.carousel>.item.multiple>div {
position: absolute;
width: 100%;
}
.carousel>.item.multiple>div:nth-child(2) {
top: 100px;
}
.carousel>.item.multiple>div:nth-child(3) {
top: 200px;
}
/* And so on ...
.carousel>.item.multiple>div:nth-child(...) {}
*/
<div class="carousel">
<div class="item">
<div>Item-1</div>
</div>
<div class="item multiple">
<div>Item-1.1</div>
<div>Item-1.2</div>
<div>Item-1.3</div>
</div>
<div class="item">
<div>Item-2</div>
</div>
<div class="item multiple">
<div>Item-2.1</div>
<div>Item-2.2</div>
<div>Item-2.3</div>
</div>
</div>
<div class="other">
Other div
</div>
Your desired result mean making the child overlap the parent, and i don't think that's possible. BUT you can "hack" this by wrapping the .carousel with another div (.demo it this general example), so the results will be something like this:
.demo {overflow: visible; height: 100px;}
.carousel {
color: #FFF;
white-space: nowrap;
overflow-x: auto;
overflow-y: hidden;
position: initial;
}
.carousel .c-item {
display: inline-block;
width: 35%;
background: #000;
height: 100px;
}
.carousel .abs {
background: #444;
display: inline-block;
vertical-align: top;
width: 35%;
max-height: 180px;
overflow-y: auto;
}
.carousel .abs .a-item {
height: 100px;
border: 1px solid #000;
}
.other {
background: yellow;
height: 200px;
}
<div class="demo">
<div class="carousel">
<div class="c-item">Item-1</div>
<div class="abs">
<div class="a-item">Abs Item-1.1</div>
<div class="a-item">Abs Item-1.2</div>
<div class="a-item">Abs Item-1.3</div>
</div>
<div class="c-item margin">Item-2</div>
<div class="abs">
<div class="a-item">Abs Item-2.1</div>
<div class="a-item">Abs Item-2.2</div>
<div class="a-item">Abs Item-2.3</div>
</div>
</div>
</div>
<div class="other">
Other div
</div>
As you can see from the snippet the scroll-x doesn't show - yet it exist. You can click one of the .carousel item and scroll them right and left.
Since it's not obvious that the .carousel is scrolling, you can add extra buttons to scroll it:
.demo {overflow: visible; height: 100px;z-index: 3;}
.carousel {
color: #FFF;
white-space: nowrap;
overflow-x: auto;
overflow-y: hidden;
position: initial;
}
.carousel .c-item {
display: inline-block;
width: 35%;
background: #000;
height: 100px;
}
.carousel .abs {
background: #444;
display: inline-block;
vertical-align: top;
width: 35%;
max-height: 180px;
overflow-y: auto;
}
.carousel .abs .a-item {
height: 100px;
border: 1px solid #000;
}
.other {
background: yellow;
height: 200px;
}
<div class="demo">
<button onclick="document.querySelectorAll('.carousel')[0].scrollLeft += 20;" style="position: fixed; top: 50%; right: 0;">L</button>
<button onclick="document.querySelectorAll('.carousel')[0].scrollLeft -= 20;" style="position: fixed; top: 50%; left: 0;">R</button>
<div class="carousel">
<div class="c-item">Item-1</div>
<div class="abs">
<div class="a-item">Abs Item-1.1</div>
<div class="a-item">Abs Item-1.2</div>
<div class="a-item">Abs Item-1.3</div>
</div>
<div class="c-item margin">Item-2</div>
<div class="abs">
<div class="a-item">Abs Item-2.1</div>
<div class="a-item">Abs Item-2.2</div>
<div class="a-item">Abs Item-2.3</div>
</div>
</div>
</div>
<div class="other">
Other div
</div>
Hope that helps!
You have to play with position check snippet.
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.carousel {
display: flex;
width: 100vw;
overflow-x: auto;
color: white;
}
.carousel>.item {
flex: 1 0 33.33%;
//margin-right: 5px;
}
.carousel>.item:nth-child(odd) {
background: black;
}
.carousel>.item:nth-child(even) {
background: darkgrey;
}
.carousel>.item,
.carousel>.item.multiple>div {
height: 100px;
}
.carousel>.item.multiple {
position: relative;
overflow-y: auto;
height: 200px;
}
.carousel>.item.multiple>div {
position: absolute;
width: 100%;
}
.carousel>.item.multiple>div:nth-child(2) {
top: 100px;
}
.carousel>.item.multiple>div:nth-child(3) {
top: 200px;
}
.other {
position: absolute;
z-index: -1;
top: 100px;
width: 100%;
background: green;
height: 117px;
}
/* And so on ...
.carousel>.item.multiple>div:nth-child(...) {}
*/
<div class="carousel">
<div class="item">
<div>Item-1</div>
</div>
<div class="item multiple">
<div>Item-1.1</div>
<div>Item-1.2</div>
<div>Item-1.3</div>
</div>
<div class="item">
<div>Item-2</div>
</div>
<div class="item multiple">
<div>Item-2.1</div>
<div>Item-2.2</div>
<div>Item-2.3</div>
</div>
</div>
<div class="other">
Other div
</div>
I need to give vertical height for the right element with full background. I tried by setting
height:100%;
max-height: 100%
but the element takes only content height
.full_container {
height: 350px;
border: 1px solid #ddd;
}
.pull-left {
float: left;
}
.width50 {
width: 50%;
}
.inline_height {
color: #fff;
padding: 10px;
background: #333;
}
.height100 {
padding: 10px;
height: 100%;
max-height: 100%;
background: #e8e8e8;
}
<div class="full_container">
<div class="clearfix">
<div class="pull-left width50">
<div class="inline_height">
Content height only
</div>
</div>
<div class="pull-left width50">
<div class="height100">
<div>I need to show this div element height to 100%</div>
</div>
</div>
</div>
</div>
Try giving the .clearfix class a display:flex and height:100%
.clearfix {
display: flex;
height: 100%;
}
Example below
.full_container {
height: 350px;
border: 1px solid #ddd;
}
.pull-left {
float: left;
}
.width50 {
width: 50%;
}
.inline_height {
color: #fff;
padding: 10px;
background: #333;
}
.height100 {
padding: 10px;
height: 100%;
max-height: 100%;
background: #e8e8e8;
}
.clearfix {
display: flex;
height: 100%;
}
<div class="full_container">
<div class="clearfix">
<div class="pull-left width50">
<div class="inline_height">
Content height only
</div>
</div>
<div class="pull-left width50">
<div class="height100">
<div>I need to show this div element height to 100%</div>
</div>
</div>
</div>
</div>
See this:
I have added display: flex for .full_container
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.full_container {
height: 350px;
border: 1px solid #ddd;
display: flex;
}
.pull-left {
float: left;
}
.width50 {
width: 50%;
}
.inline_height {
color: #fff;
padding: 10px;
background: #333;
}
.height100 {
padding: 10px;
height: 100%;
max-height: 100%;
background: #e8e8e8;
}
<div class="full_container">
<div class="pull-left width50">
<div class="inline_height">
Content height only
</div>
</div>
<div class="pull-left width50">
<div class="height100">
<div>I need to show this div element height to 100%</div>
</div>
</div>
</div>
I have a small section in the mobile version of my website with 50-50 image/text format which looks and functions as I want, but when I resize the browser a small gap appears at the bottom of the images.
I have tried everything but cannot figure out the issue (I am sure it is something minor I am staring right at). Closest I came was vertical-align: bottom; to the image but the gap just started appearing at the top instead.
.mobilecontainer1 {
display: block;
width: 100%;
float: left;
box-sizing: border-box;
}
.mobilebox {
float: left;
width: 50%;
overflow: hidden;
line-height: 0;
}
.mobilebox img {
width: 100%;
}
.mobiletextwrap {
padding-top: 19%;
}
#mobilebox1 {
background: black;
color: white;
}
#mobilebox2 {
background-color: white;
color: black;
}
#mobilebox3 {
background-color: black;
color: white;
}
.mobileboxwrap {
width: 100%;
float: left;
display: block;
}
#mobileboxwrap1 {
background: black;
color: white;
height: 100%;
overflow: hidden;
}
#mobileboxwrap2 {
background: white;
color: black;
height: 100%;
overflow: hidden;
}
#mobileboxwrap3 {
background: black;
color: white;
height: 100%;
overflow: hidden;
}
<div class="mobileboxwrap" id="mobileboxwrap1">
<div class="mobilebox">
<img src="img/mobilebackground_3.png">
</div>
<div class="mobilebox" id="mobilebox1">
<div class="mobiletextwrap">
<header>
<h2>Info</h2>
</header>
</div>
</div>
</div>
<div class="mobileboxwrap" id="mobileboxwrap2">
<div class="mobilebox" id="mobilebox2">
<div class="mobiletextwrap">
<header>
<h2>Drinks</h2>
</header>
</div>
</div>
<div class="mobilebox">
<img src="img/mobilebackground_2.png">
</div>
</div>
<div class="mobileboxwrap" id="mobileboxwrap3">
<div class="mobilebox">
<img src="img/mobilebackground_1.png">
</div>
<div class="mobilebox" id="mobilebox3">
<div class="mobiletextwrap">
<header>
<h2>Music</h2>
</header>
</div>
</div>
Just set the display: block for the img
.mobilebox img { width: 100%; display: block;}
I was playing with the opacity attribute and wrote the following code:
#outer {
width: 500px;
height: 500px;
background: pink;
margin: 50px;
position: relative;
}
.item {
width: 50px;
height: 50px;
margin-right: 10px;
background: black;
opacity: 0.5;
float: left;
}
.inner {
width: 500px;
height: 500px;
background: red;
display: none;
position: absolute;
left: 0;
top: 0;
}
.item:hover {
opacity: 1;
}
.item:hover .inner {
display: block;
}
<div id="outer">
<div class="item">
<div class="inner"></div>
</div>
<div class="item">
<div class="inner"></div>
</div>
<div class="item">
<div class="inner"></div>
</div>
<div class="item">
<div class="inner"></div>
</div>
</div>
I wanted the inner div to show up and cover its parent item div when its parent item div is hovered. Because all the other item divs are set to be transparent to show through the inner div and only the hovered item div's opacity is changed to 1, I expected only the hovered item to be covered. However, all the item divs before the hovered one are also hidden. What happened?
Your issue is with absolute positioning: it's relative to "containing block", which is not the parent, but the ascendent element with a non-static position (in your case, the #outer element).
Simply addd position: relative to .item, and it will become the containing box.
Note that this has nothing to do with opacity.
Working snippet:
#outer {
width: 500px;
height: 500px;
background: pink;
margin: 50px;
position: relative;
}
.item {
width: 50px;
height: 50px;
margin-right: 10px;
background: black;
opacity: 0.5;
float: left;
position: relative;
}
.inner {
width: 500px;
height: 500px;
background: red;
display: none;
position: absolute;
left: 0;
top: 0;
}
.item:hover {
opacity: 1;
}
.item:hover .inner {
display: block;
}
<div id="outer">
<div class="item">
<div class="inner"></div>
</div>
<div class="item">
<div class="inner"></div>
</div>
<div class="item">
<div class="inner"></div>
</div>
<div class="item">
<div class="inner"></div>
</div>
</div>
I've used some and inspired jquery tabs to make it easier
$(function(){
$('.item').hover(
function(e){
$('.item').removeClass('hover')
$(this).addClass('hover')
$('.content').removeClass('show')
var content = $(this).attr('data')
$(content).addClass('show')
},
function(e){
$('.item').removeClass('hover')
$('.content').removeClass('show')
}
)
})
#outer {
width: 500px;
height: 500px;
background: pink;
margin: 50px;
position: relative;
}
.btn{
position:absolute;
left:0;
top:0;
z-index:2
}
.btn .item {
width: 50px;
height: 50px;
margin-right: 10px;
background: black;
opacity: 0.5;
float: left;
position: relative;
-webkit-transition: all 0.2s; /* Safari */
transition: all 0.2s;
}
.content {
width: 500px;
height: 500px;
background: red;
position: absolute;
left:0;
top: 0;
z-index:1;
text-align:center;
opacity: 0;
-webkit-transition: all 0.2s; /* Safari */
transition: all 0.2s;
}
.item.hover {
opacity: 1;
}
.content.show{
opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="outer">
<div class="btn">
<div class="item" data="#content1"></div>
<div class="item" data="#content2"></div>
<div class="item" data="#content3"></div>
<div class="item" data="#content4"></div>
</div>
<div class="content" id='content1'>content 1
</div>
<div class="content" id='content2'>content 2
</div>
<div class="content" id='content3'>content 3
</div>
<div class="content" id='content4'>content 4
</div>
</div>
In the following mark up, I simply want to set the negative margin for the .text div so that it appears on the top of the .image div.
<div class="wrap">
<div class="image"><img src="imgage.jpg" /></div>
<div class="text">text with background</div>
</div>
.image {
overflow: hidden;
}
.text{
background: green;
padding: 20px;
margin-top: -60px;
color: #FFFFFF;
overflow: hidden;
}
I have set the background for the .text, but for some reason it does not appear on the image. Only the text appears. You can see the problem here: http://jsfiddle.net/ovkn4egc/
How I can fix it without using the absolute position.
Thanks.
You can simply add position:relative; to .text
DEMO
* {
margin: 0;
padding: 0;
}
.image {
overflow: hidden;
}
.image img {
display: block;
}
.text {
position:relative;
background: green;
padding: 20px;
margin-top: -60px;
color: #FFFFFF;
overflow: hidden;
}
<div class="wrap">
<div class="image">
<img src="https://farm8.staticflickr.com/7550/15615231269_e5a66cbe16_z.jpg" />
</div>
<div class="text">text with background</div>
</div>
Updated to :
<div class="wrap">
<div class="text">text with background</div>
<div class="image">
<img src="https://farm8.staticflickr.com/7550/15615231269_e5a66cbe16_z.jpg" />
</div>
</div>
*{
margin: 0;
padding: 0;
}
.image {
overflow: hidden;
}
.image img{
display: block;
}
.text{
background: green;
padding: 20px;
color: #FFFFFF;
overflow: hidden;
position:relative;
top: 0;
}
http://jsfiddle.net/ovkn4egc/4/
If you want the text to only span the width of the image only, there are a couple of changes more you need to make as below.
I believe this is the behaviour you are after:
DEMO
The HTML has to slightly change like below.
<div class="wrap">
<div class="image">
<img src="https://farm8.staticflickr.com/7550/15615231269_e5a66cbe16_z.jpg" />
<div class="text">text with background</div>
</div>
</div>
The CSS for the image div also has to change to such, where we set the position property to absolute:
.image {
overflow: hidden;
position: absolute;
}
Finally, the position property for the text, has to alter as such:
.text{
background: green;
padding: 20px;
margin-top: -60px;
color: #FFFFFF;
overflow: hidden;
position: relative;
}
Change you css to the below code
*{
margin: 0;
padding: 0;
}
.wrap{position:relative;}
.image {
overflow: hidden;
}
.image img{
display: block;
}
.text{
background: green;
padding: 20px;
top: 0px;
color: #FFFFFF;
overflow: hidden;
position: absolute;
width: 100%;
}
As well as your html to below one
<div class="wrap">
<div class="text">text with background</div>
<div class="image">
<img src="https://farm8.staticflickr.com/7550/15615231269_e5a66cbe16_z.jpg" />
</div>
</div>
I have written .wrap as relative because text div should be compare its position according to wrap div otherwise if you use only absolute for text div then it will be positioned according to body.