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>
Related
I want to implement a div which contains 2 divs:
A row with two sides, left and right.
A div below the first one
For that I am doing as follows:
<div id="main">
<div id="box1">
<div id="left" />
<div id="right" />
</div>
<div id="box2" />
</div>
And this is the css
.main {
margin-top: 75px;
}
#box1 {
display: flex;
flex-direction: row;
width: 100%;
height: 450px;
transition: ease all 0.5s;
}
#box1 #left {
width: 50%;
height: 100%;
background-color: lime;
}
#box1 #right {
width: 50%;
height: 100%;
background-color: red;
}
#box2 {
width: 100%;
height: 600px;
background-color: gold;
}
But for some reasons, I am getting the second div "box2" in the same row of "box1", next to it.
Why is that?
this seems to work (check full page view):
.left {
background: red;
width: 50%;
float: left;
}
.right {
background: green;
position: relative;
float: left;
width: 50%
}
.bottom {
background: blue;
position: relative;
width: 100%;
top: 9vh;
float: none;
}
<div id = "main">
<div id = "box2" class = "bottom" > bottom </div>
<div id = "box1">
<div class = "left"> left </div>
<div class = "right"> right </div>
</div>
</div>
You just missed the closing </div> tags
here is when it fixed, working perfectly fine
.main {
margin-top: 75px;
}
#box1 {
display: flex;
flex-direction: row;
width: 100%;
height: 450px;
transition: ease all 0.5s;
}
#box1 #left {
width: 50%;
height: 100%;
background-color: lime;
}
#box1 #right {
width: 50%;
height: 100%;
background-color: red;
}
#box2 {
width: 100%;
height: 600px;
background-color: gold;
}
<div id="main">
<div id="box1">
<div id="left">Left Contents Here</div>
<div id="right">Right Contents Here</div>
</div>
<div id="box2">Belowe Contents Here</div>
</div>
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 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>
I need to have overflow-x: scroll like on first part of my image, and overflow visible to div bigger than its parent like on part 2 of my image.
So my problem is when I add overflow:visible to parent div, all overflows inside it are visible. I want to make overflow:visible, but keep overflow-x:scroll on parent div. Is it even possible?
HTML:
<div id="wrapper">
<div class="content">
<div class="child">
<button onclick="action(this)" class="triggerBtn">Click</button>
</div>
</div>
<div class="content">
<div class="child">
<button onclick="action(this)" class="triggerBtn">Click</button>
</div>
</div>
<div class="content">
<div class="child">
<button onclick="action(this)" class="triggerBtn">Click</button>
</div>
</div>
<div class="content">
<div class="child">
<button onclick="action(this)" class="triggerBtn">Click</button>
</div>
</div>
<div class="content">
<div class="child">
<button onclick="action(this)" class="triggerBtn">Click</button>
</div>
</div>
</div>
CSS:
#wrapper {
height: 200px;
width: 400px;
white-space:nowrap;
overflow-x: auto;
overflow-y: hidden;
margin:auto;
background-color: rgb(240,240,240);
position: relative;
z-index: 1;
}
.content {
width: 200px;
height: 100%;
overflow-x: hidden;
display: inline-block;
position: relative;
z-index: 100;
background-color: rgb(200,200,200);
}
.child {
width: 200px;
height: 50%;
position: absolute;
left: 0;
top: 0;
/* transform: translateX(50%); */
background-color: #bada55;
border-radius: 5px;
z-index: 200;
transition: all .15s ease-in-out;
text-align: center;
}
div.child.active {
width: 400px;
left: -50%;
background-color: #111
}
Bunch of JS:
window.action = function(el) {
var parent = el.parentNode;
if(parent.classList.contains('active')) {
parent.classList.remove("active");
} else {
parent.className += " active";
}
};
a quick fix is to use !important on your child selectors, like so:
.parent {
overflow: visible;
}
.child {
overflow: hidden !important;
}
!important simply tells the system to use the style instead of inheriting the parent's styling. You can also use the immediate child selector >.
I have searched for similar questions however unfortunatley the left:50% soultion does not work here.
I have a container (.leftLanding) with a relative postion, inside this I have a div with an absolute position (.imageCenter) which I would like to center horizontally. Adding left: 50% doesn't actually center it however as the container has a with of 85% I also tried 42.5% but this didn't work either.
I've removed all unnecessary code.
HTML:
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG1.png">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG3.png">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG2.png">
</div>
</div>
CSS:
.leftLanding {
display: flex;
position: relative;
width: 85%;
margin-left: 3%;
transition: all 0.5s ease;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
}
If you add this rule, where flex: 1 tells the flex items (in this case the first div and the last img) to take all the available space (and since they are 2 they share it 50/50)
.leftLanding div:first-child,
.leftLanding img{
flex: 1;
}
And the use left: 50%, transform: translate(-50%) like this it will work
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translate(-50%);
border: 1px solid gray;
}
Added borders on the two so it clearly shows
.leftLanding {
display: flex;
position: relative;
width: 85%;
margin-left: 3%;
transition: all 0.5s ease;
border: 1px solid gray;
}
.leftLanding div:first-child,
.leftLanding img{
flex: 1;
}
.leftLanding div:first-child {
background: lightblue;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translate(-50%);
border: 1px solid gray;
}
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
</div>
To center an absolutely positioned element horizontally, use a combination of left: 50%; transform: translateX(-50%); and it will center it relative to it's closest non-static positioned ancestor.
Though I'm not sure what you're trying to do with this layout, so not sure if that's really what you're looking for, but added some borders/background colors to show the children are centered horizontally.
I have a container (.leftLanding) with a relative postion, inside this I have a div with an absolute position (.imageCenter) which I would like to center horizontally.
This will center .imageCenter in .leftLanding.
.leftLanding {
display: flex;
position: relative;
width: 85%;
transition: all 0.5s ease;
background: #aaa;
border: 1px solid blue;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translateX(-50%);
background: #eee;
border: 1px solid red;
}
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG1.png">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG3.png">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG2.png">
</div>
</div>
Try to set the .imageCenter to width:100% and style display:block margin:auto to the img tag
.imageCenter {
position: absolute;
width: 100%;
height: 30%;
align-self: center;
z-index: 100;
}
img{
display: block;
margin: auto;
}