The following implementation i came up with for a sidemenu works fine
https://jsbin.com/getotijela/edit?html,css,output
HTML:
<link rel="stylesheet" type="text/css" href="main.css">
<div class="container">
<div class="menu">
a
</div>
<div class="content">
<span id="clickable">
basdasdasdasdas
</span>
</div>
<div>
<script>
document.getElementById("clickable").addEventListener('click',function(){
var el = document.getElementsByClassName("menu")[0]
el.className = "menuClosed"
})
</script>
CSS:
.test{
background-color:red;
}
.container{
display:flex;
flex-direction: row;
position:relative;
height:100%;
}
.menu{
width:200px;
/* left:20px; */
background-color:red;
}
.menuClosed{
width:0px;
background-color:red;
transition: 0.3s all;
}
.content{
background-color:yellow;
flex:1;
}
but what it does is collapse the sidemenu to a width of 0 ( as i've instructed it to). What i was aiming for was: having the sidemenu move to the left as many units as it's width, something that in the past i would achieve by modifying the leftcss property.
In other words, i dont want the menu to collapse to a width of 0, just have it move out of sight
How could i achieve something similar to that in flexbox if at all?
#PossessWithin Why would you use a translateX for that ?
You should use the flexbox-shrink and flexbox-basis property's they are made especially for these cases.
I made a fiddle check it out here JSFiddle
<aside class="sidebar is-visible">
<p class="sidebar-content">
Put all your content inside of here since this keeps the width of your sidebar <br>(so a div with the width you want the sidebar to be).
</p>
</aside>
<main class="content">
<button class="toggle-sidebar">Toggle sidebar</button>
<p>
scale this content
</p>
</main>
CSS
*{
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
margin: 0;
}
body {
display: flex;
justify-content: flex-end;
}
.content {
display: flex;
min-height: 0;
flex-direction: column;
flex-grow: 1;
flex-shrink: 0;
flex-basis: 100%;
}
.sidebar {
flex-grow: 0;
flex-shrink: 0;
flex-basis: auto;
height: 100%;
}
.sidebar.is-visible ~ .content {
flex-basis: 0;
}
/* Animation */
.content {
transition: flex-basis .1s cubic-bezier(.465, .183, .153, .946);
will-change: flex-basis;
}
.sidebar.is-visible ~ .content {
transition: flex-basis .3s cubic-bezier(.465, .183, .153, .946);
}
.sidebar-content {
display: block;
width: 300px;
background-color: #eee;
height: 100%;
margin: 0;
padding: 20px;
border-right: 1px solid #ccc;
}
.content {
padding: 20px;
}
Please vote this up since it's the way it's supposed to be done.
Please try this:
HTML:
<link rel="stylesheet" type="text/css" href="main.css">
<div class="container">
<div class="menu">
a
</div>
<div class="content">
<span id="clickable">
basdasdasdasdas
</span>
</div>
<div>
CSS
.test{
background-color:red;
}
.container{
display:flex;
flex-direction: row;
position:relative;
height:100%;
}
.menu{
width:200px;
transition: 0.3s all ease-in-out;
background-color:red;
}
.menuClosed{
background-color:red;
transition: 0.3s all ease-in-out;
transform: translateX(-200px);
margin-right: -200px;
}
.content{
background-color:yellow;
flex:1;
}
I have changed the "menuClosed" class by moving it to the left with a transform: translateX(-200px). To make the screen move back to the right, I have added a negative margin right of the same size.
This may not be the best practice - I'm open minded to hear other solutions!
Also, as a plus, I have changed your Javascript code to toggle the class, making it work to open and close on click, instead of opening only.
You can see the new code here, on Codepen. I hope it helps you!
Related
I would like two create two buttons that overlay a div using HTML like the following:
*Both the same DIV with two buttons overlapping each side. So one div with two buttons overlapping.
I would like the buttons to be transparent and overlay the div but I am not sure how.
I have created my Div:
<div class="container">
<div id="slides">
<img src="img/example-slide-1.jpg" alt="Photo by: Missy S Link: http://www.flickr.com/photos/listenmissy/5087404401/">
</div>
</div>
The div I would like to overlay is called "container" and the two buttons are:
<i class="icon-chevron-left icon-large"></i>
<i class="icon-chevron-right icon-large"></i>
Is there any way in CSS or HTML to do this?
You have to place your buttons absolutely on top of your image. To do so, first make .container take a position: relative; and then put your buttons as siblings of your .slides div and place them absolutely.
.container {
position: relative;
}
.slidesjs-navigation {
position: absolute;
top: 0;
display: block;
width: 50%;
height: 100%;
background: rgba(0,0,0,0); /* Added in case you want to transition this */
}
.slidesjs-navigation:hover {
background: rgba(0,0,0,0.25); /* Makes the hovered button visible */
}
.slidesjs-previous {
left: 0;
}
.slidesjs-next {
right: 0; /* left: 50%; works too */
}
.slides img {
display: block; /* Avoids the space usually seen under inline images */
width: 100%; /* Ensures the image takes up the whole width */
}
<div class="container">
<div id="slides" class="slides">
<img src="https://c1.staticflickr.com/5/4147/5087404401_d24513119a_b.jpg" alt="Photo by: Missy S Link: http://www.flickr.com/photos/listenmissy/5087404401/"><!-- original `src`: "img/example-slide-1.jpg" -->
</div>
<i class="icon-chevron-left icon-large"></i>
<i class="icon-chevron-right icon-large"></i>
</div>
Here is a simple way to do it. Put both buttons inside a div with a height:100%, width:50% and float:left;. This way each button takes up the full height of the div but only half of its width. The float:left; will then put them side by side in the div, hopefully achieving what you want!
.box {
border:1px solid black;
height:200px;
width:400px;
background-color:#005680;
}
.button1 {
width:50%;
height:100%;
float:left;
background-color: rgba(0,0,0,0);
border:0px solid black;
}
.button2 {
width:50%;
height:100%;
float:left;
background-color: rgba(0,0,0,0);
border:0px solid black;
}
.button1:hover {
background-color: rgba(10,10,10,0.1);
}
.button2:hover {
background-color: rgba(10,10,10,0.1);
}
<div class="box">
<button class="button1"></button>
<button class="button2"></button>
</div>
This can be your code.
.d {
position:relative;
}
.b1 {
float:left;
height:100px;
width:75px;
}
.b2 {
position:absolute;
left:75px;
height:100px;
width:75px;
}
<div class="d">
<button class="b1"></button>
<button class="b2"></button>
</div>
So basically you would like to create something similar to a toggle button or on/off switch? You could try something like:
HTML:
<div id="toggle">
<a id="left-side" href="">Left</a>
<a id="right-side" href="">Right</a>
</div>
CSS:
<script type="text/css">
DIV#toggle {
width:100px;
height:50px;
margin:0px;
padding:0px;
}
DIV#toggle>A {
display:block;
width:50%;
height:100%;
padding:0px;
text-size:10pt;
text-align:center;
}
DIV#toggle>A#right-side {
margin:0px auto 0px 0px;
background-color:#ff0000;
}
DIV#toggle>A#left-side {
margin:0px 0px 0px auto;
background-color:#00ff00;
}
</script>
Since you mentioned that the buttons are in the div, you can simply position them using position: absolute. By adding position: relative to the container, you can position them within that container rather than within the document as a whole.
/* -------------------------------------------------- --
The part that you actually need
-- -------------------------------------------------- */
/* Allow elements to be positioned relative to the container */
.container {
position: relative;
}
/* Let the buttons both cover the (left) half of the div */
.container .slidesjs-navigation {
position: absolute;
top: 0;
left: 0;
width: 50%; /* Of .container, its positioning parent */
height: 100%; /* Of .container */
}
/* Make an exception for the second button to move it to the right half */
.container .slidesjs-next {
left: 50%;
}
/* -------------------------------------------------- --
The part that's just for the demonstration.
-- -------------------------------------------------- */
/* Make the content large to show that the buttons scale */
#slides {
padding: 50px;
}
/* Make the div red, as in the question */
.container {
background-color: red;
}
/* Have white, semi-transparent buttons with a border, so you see where they are */
.container .slidesjs-navigation {
background-color: white;
border: 1px dashed black;
box-sizing: border-box;
opacity: 0.5;
}
/* Make the buttons opaque on hover to show that they respond */
.container .slidesjs-navigation:hover {
opacity: 1;
}
<div class="container">
<i class="icon-chevron-left icon-large"></i>
<i class="icon-chevron-right icon-large"></i>
<div id="slides">
<img src="img/example-slide-1.jpg" alt="Photo by: Missy S Link: http://www.flickr.com/photos/listenmissy/5087404401/">
</div>
</div>
Hope this is what you were looking for. Happy to explain or help in a better solution if needed.
.container {
width: 100vw;
height: 50vh;
background-image: url('https://c1.staticflickr.com/5/4147/5087404401_d24513119a_n.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
margin: 0;
padding: 0;
}
.container a{
width: 49.5%;
height: 50vh;
margin: 0;
padding: 0;
display: inline-block;
}
.container a:hover{
width: 49.5%;
height: 50vh;
background: rgba(255,255,255,0.4);
margin: 0;
padding: 0;
display: inline-block;
}
<div class="container">
<i class="icon-chevron-left icon-large"></i>
<i class="icon-chevron-right icon-large"></i>
</div>
When I view my web on full size, it will show like the left image.
When I narrow the window, it will display like the right image
#charset "utf-8";
/* CSS Document */
*{
margin: 0;
padding: 0;
max-height: 1200px;
}
body {
max-height: 1200px;
overflow-y: hidden;
color: rgba(88,88,88,0.92);
}
#bgimg{
width: 40%;
margin: 0;
padding: 0;
max-width: 864px;
}
.bgimg{
background: url(sideimage_4.png) no-repeat center ;
background-size: cover;
height: 1100px;
margin: 0;
}
.goleft{
float: left;
}
#sideright{
width: 60%;
float: right;
background-color:white;
height: 1100px;
overflow: scroll;
overflow-x: hidden;
margin-left: auto;
}
#intro{
width: 100%;
margin: 0;
padding: 0;
background-color: white;
}
#introinner{
text-align: center;
padding: 10% 10px;
}
#introinner h1{
font-family: 'Sofia';
font-size: 500%;
font-weight: 800;
}
#introinner p{
margin: 8px;
font-family: 'Sofia';
}
#introinner a i{
font-size: 80%;
color: rgba(88,88,88,0.92);
}
#banner{
width: 100%;
margin: 0;
padding: 0;
}
#banner img{
width: 100%;
}
#cf {
position:relative;
width:100%;
margin:0 auto;
}
#cf img {
width: 100%;
position:absolute;
left:0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#cf img.top:hover {
opacity:0;
}
#selfintro{
margin-top: 500px;
clear: top;
}
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="style/homecss.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Sofia" rel="stylesheet">
</head>
<body>
<nav id="bgimg" class="goleft">
<div class="bgimg"></div>
</nav>
<section id="sideright" class="goleft">
<section id="intro">
<div id="introinner">
<h1>Troy Wu</h1>
<p>The King in the South</p>
<i class="fa fa-download">DownLoad My Pic</i>
</div>
</section>
<section id="banner">
<img src="http://via.placeholder.com/350x150">
</section>
<div id="cf">
<img class="bottom" src="http://via.placeholder.com/350x150">
<img class="top" src="http://via.placeholder.com/450x150">
</div>
<div id="selfintro"><img src="http://via.placeholder.com/350x150"></div>
<p>asdfasdf asd</p>
</section>
</body>
</html>
I post my code for these two sections. I am pretty sure that the issue happens on the div (cf), because of the position:relative and absolute. I tried to put a margin-top on the css, but it doesn't work. I've been trying to solve this for two days, and still have not clue to this. can you help me solve this issue? how to avoid the space between these two sections?
The problem is usually caused by the elements being inline-block elements instead of block elements, and as inline-block elements respect the whitespace in the markup, you get these gaps between elements.
You can reduce the font-size to zero, and as the space is dependent on the font size, that can solve the problem, or you can remove all the whitespace in your markup (which makes for messy HTML), or you can make the offending elements block items instead of inline-block items if that does't break your layout.
As I don't see any text on your layouts, I would set the font size to zero and see if that solves it, and then set the font size specifically on spans or divs containing text if you need to add labels or titles etc.
I need the content box to reach to the footer even when the content box is empty. I want to achieve this using only CSS.
padding-bottom is not an option.
I don't want to use a background image, such as background-image: url center repeat-y;
How can I achieve this?
.wrap {
height: 100%;
}
.l-col {
padding-top: 0;
height: 100%;
}
.footer {
position: relative;
height: 60px;
clear: both;
float: left;
width: 100%;
z-index: 3;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="wrap">
<div class="container">
<div class="col-xs-12 l-col">
<div class="col-xs-12" style="padding:0px">
<table>Content</table>
</div>
</div>
</div>
</div>
Current Layout:
Desired Layout:
Using CSS's calc() function, you can use calculate the min-height of your content div.
body {
margin: 0
}
.header,
.footer {
width: 100%;
height: 60px;
background-color: grey;
}
.content {
width: 100%;
min-height: calc(100vh - 120px);
background-color: #FFF8DC;
}
<div class="wrap">
<div class="container">
<div class="header">
Header
</div>
<div class="content">
Content
</div>
<div class="footer">
Footer
</div>
</div>
</div>
If your preferred method is jQuery, the following code will work, even on page resize.
function setContentHeight() {
var headerHeight = $(".header").height();
var footerHeight = $(".footer").height();
var winHeight = $(window).height();
$(".content").css("min-height", winHeight-(headerHeight+footerHeight));
}
setContentHeight();
$(window).resize(setContentHeight);
Here you go just calculate your min height so if content gets more it will expand if you don't want that just use regular height.
body{margin:0;}
.head, .foot {
width: 100%;
background: gray;
height: 50px;
float: left;
display: inline-block;
}
.content {
width: 80%;
min-height: calc(100vh - 100px);
background: lightgray;
float: left;
display: inline-block;
}
<div class="head">Head</div>
<div class="content">hello</div>
<div class="foot">foot</div>
flex + html5, looks like a school case ...
html {
display:flex;
height:100%;
}
body {
flex:1;
display:flex;
flex-flow:column;
color:white;
}
header , footer{
text-align:center;
background:#4F81BD;
line-height:4em;
}
main {
flex:1;
display:flex;
justify-content:center;
padding-left:10%;/* cause aside is set to 10% width */
}
section {
border:30px #4F81BD solid;
padding:1em;
margin:2px;
box-shadow:0 0 0 2px , inset 0 0 0 2px ;
width:60%;/* whatever */
color: #385D8A
}
aside {
background: #4F81BD ;
box-shadow:0 0 0 2px #385D8A;
margin:auto 0;
width:10%;
min-height:30vh;/* demo purpose, use content instead */
display:flex;/* optionnal to center on XY axis */
align-items:center;
justify-content:center;
}
<header>
header (any height)
</header>
<main><!-- fill gap in between -->
<section>section, run snippet in full page mode and resize window</section>
<aside>aside</aside>
</main>
<footer>
footer (any height)
</footer>
pen to play with: http://codepen.io/gc-nomade/pen/ORyXZA
Problem
I am trying to animate a div to 100% of it's child's width. I'm animating the max width on hover, but it is pushing the div to it's right away very abruptly instead of animating it smoothly to the right. Can anybody see why it isn't animating correctly? I would prefer not to use javascript if possible.
My Attempt
I have copied the fiddle below:
http://jsfiddle.net/tVHYg/1662/
into the following source
.contents {
white-space:nowrap;
display:inline-block;
}
.inner {
background:#c3c;
width: 100%;
max-width:50px;
overflow:hidden;
transition: all .3s ease-in-out;
padding: 5px 0 5px 0;
}
.contents:hover .inner {
max-width:100%;
}
<div class="contents">
<div class="inner">A bit of text never hurt anyone</div>
</div>
<div class="contents">
<div class="inner">A bit of text never hurt anyone</div>
</div>
The percent is the issue in regards to your code and probably because you have width: 100%; while at the same time you have max-width: 50px; for the inner class .
Using pixels fixes that. any pixels over the size of the boxes will simply animate it faster e.g max-width: 1000px; will just speed up the animation without enlarging the box boundaries
.contents {
white-space:nowrap;
display:inline-block;
}
.inner {
background:#c3c;
width: 100%;
max-width:50px;
overflow:hidden;
transition: all .5s ease-in-out;
padding: 5px 0 5px 0;
}
.contents:hover .inner {
max-width:1250px;
}
<div class="contents">
<div class="inner">A bit of text never hurt anyone</div>
</div>
<div class="contents">
<div class="inner">A bit of text never hurt anyone</div>
</div>
You can do this with display: inline-flex
.container {
display: inline-flex;
}
.contents {
display: inline-flex;
transition: all .3s ease-in;
overflow: hidden;
width: 50px;
margin: 2px;
}
.inner {
background:#c3c;
white-space:nowrap;
padding: 10px 0;
}
.contents:hover {
transition-delay: 0.2s;
width: 100%;
}
<div class="container">
<div class="contents">
<div class="inner">A bit of text never hurt anyone</div>
</div>
<div class="contents">
<div class="inner">A bit of text never hurt anyone</div>
</div>
</div>
I am trying to build simple horizontal image slider with overflow:hidden and floating divs. Hovewer, I am not able to float them horizontally - they always appear in vertical order. I tried many examples from the web, hovewer I still don't know where I am wrong.
HTML:
<div id="slidingWindow">
<div class="slidingSection clearfix">Something something</div>
<div class="slidingSection clearfix">Again something</div>
</div>
CSS:
#slidingWindow {
overflow:hidden;
width: 470px;
height: 500px;
background-color: red;
}
.slidingSection {
margin: 5px;
background-color: green;
width: 470px;
height: 400px;
float: left;
}
.clearfix:after {
content: ".";
visibility: hidden;
display: block;
height: 0;
clear: both;
}
This JSFiddle contains simplest example of my problem:
http://jsfiddle.net/v4udd47t/
If your support is IE10+ and are not concerned with Opera Mini, then you can use display: flex. That way you don't need any extra markup or even floats and clearfix.
Along with using flex you will also have to set a min-width on the slides that is equal to the container minus any margins and padding. In regards to margins and padding, the container will also have to accommodate any that are applied to the slides (I noticed you have a 5px margin on them).
HTML:
<div id="slidingWindow">
<div class="slidingSection clearfix">Something something</div>
<div class="slidingSection clearfix">Again something</div>
</div>
CSS:
#slidingWindow {
overflow:hidden;
width: 480px; /* width of slide + left and right margins */
height: 500px;
background-color: red;
display: -ms-flex;
display: -webkit-flex;
display: flex;
}
.slidingSection {
margin: 5px;
background-color: green;
width: 470px;
min-width: 470px; /* required */
height: 400px;
}
Below is a fork of your fiddle with the size reduced and having an animation on hover to show it is working properly:
#slidingWindow {
overflow:hidden;
width: 260px;
height: 300px;
background-color: red;
display: -ms-flex;
display: -webkit-flex;
display: flex;
}
.slidingSection {
margin: 5px;
background-color: green;
width: 250px;
min-width: 250px;
height: 200px;
-webkit-transition: -webkit-transform 750ms;
transition: transform 750ms;
}
#slidingWindow:hover > .slidingSection {
-webkit-transform: -webkit-translate3d(-260px, 0, 0);
transform: translate3d(-260px, 0, 0);
-webkit-transition: -webkit-transform 750ms;
transition: transform 750ms;
}
<div id="slidingWindow">
<div class="slidingSection">Something something</div>
<div class="slidingSection">Again something</div>
</div>
That's because you need to Wrap the individual sliders in another div that has a width = number of slides*width. I updated your example here, and made the overflow: scroll so you can see the difference http://jsfiddle.net/v4udd47t/1/
<div id="slidingWindow">
<div class="slider-container">
<div class="slidingSection clearfix">Something something</div>
<div class="slidingSection clearfix">Again something</div>
</div>
</div>
.slider-container {
width: 1000px;
}
to set the width dynamically you can do this
var number_of_slides = $(".slidingSection").length;
$(".slider-container").css('width', number_of_slides * 470 + "px");