I have a navbar with a container in it. This container will hold many divs later on. It should act like a tree view. I want the navbar to fill the whole left side from top to bottom. But when the content grows bigger, it should stop growing, a scrollbar should appear.
Using height: 100% does not work because currently my navbar is empty so the bar is a small one.
Here I attached two pictures, showing what I need. I want the bar "navContent" filling untill it reaches the bottom bar.
Here you can see a working fiddle with a full overview, I want the yellow bar to grow till it reaches the bottom.
* {
margin: 0;
}
.bar {
display: flex;
align-items: center;
justify-content: center;
margin: auto;
}
#navBar {
height: 100%;
}
#btnBar {
height: 40px;
}
#navContent {
background-color: yellow;
}
#wrapper {
margin: 0;
}
#navBar {
float: left;
width: 30%;
overflow: hidden;
}
#mainContainer {
float: left;
width: 70%;
overflow: hidden;
}
#header {
height: 50px;
background-color: red;
}
#headerContent {
height: 100%;
width: 80%;
}
#headerTitle {
margin: auto;
}
.headerBtn {
margin: 0px 10px 0px 10px;
}
#footer {
position: fixed;
bottom: 0;
height: 50px;
width: 100%;
background-color: red;
}
#footerContent {
height: 100%;
}
.footerBtn {
margin: 0px 20px 0px 20px;
}
#mainContainer {
height: 100%;
background-color: inherit;
}
<div id="header">
<div id="headerContent" class="bar">
<p id="headerTitle">Title</p>
<button class="btn headerBtn">Profile</button>
<button class="btn headerBtn">Logout</button>
</div>
</div>
<div id="wrapper">
<div id="navBar">
<div id="btnBar" class="bar">
<button class="btn navBtn">New Folder</button>
<button class="btn navBtn">New File</button>
<button class="btn navBtn">Delete</button>
</div>
<div id="navContent">
navContent
</div>
</div>
<div id="mainContainer">
Content
</div>
</div>
<div id="footer">
<div id="footerContent" class="bar">
<button class="btn footerBtn">Help</button>
<button class="btn footerBtn">Conditions</button>
<button class="btn footerBtn">Terms</button>
<button class="btn footerBtn">Imprint</button>
</div>
</div>
I think you may be looking for:
overflow-y: scroll;
Here, you can review what i did here: https://codepen.io/anon/pen/JJRrjG. I used the overflow property: https://developer.mozilla.org/en/docs/Web/CSS/overflow?v=example and percentages for the heights. The code is below:
https://codepen.io/anon/pen/JJRrjG
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
body {
color: #fff;
font-family: sans-serif;
}
.top {
height: 10%;
background: #111;
}
.navbar {
height: 10%;
background: #444;
width: 50%;
box-sizing: border-box;
padding: 0.5%;
text-align: center;
}
.navbar span {
margin: 10px;
}
.navbar-content {
height: 80%;
width: 50%;
background: #d9d9d9;
overflow-y: auto;
}
.everything {
height: 100%;
}
.filler-space {
height: 10000px;
background: blue;
width: 70%;
margin: 10px auto;
color: #fff;
}
<div class="everything">
<div class="top"></div>
<div class="navbar">
<span>New folder</span>
<span>New file</span>
<span>Delete</span>
</div>
<div class="navbar-content">
<div class="filler-space">I take up a lot of space</div>
</div>
<div>
You can just set either a fixed height or a max-height. In both cases a scrollbar will automatically appear if the elements grows beyond the defined height.
If you want the scrollbar always visible (even if the content isn't that high) you can add overflow-y: scroll
Try it using the property overflow: auto on your class too.
This property is used when the content overflows an element's box.
It's very useful when your content is too big to fit in a specified area.
Related
I would like to have a layout as follows:
Whereby I have a parent container, and centred inside of that is a breadcrumb. However, I also would like a logo inside of the container which floats to the left of the breadcrumb, but respects the boundaries of the breadcrumb.
I have been playing around with flexbox and can only get it to work with absolutely positioning the logo, which means the breadcrumb does not respect the boundaries of the logo.
I have put together a JSFiddle playground here: https://jsfiddle.net/joyqwpc1/25/.
The difficult thing is, the logo can be a variable width, so setting a margin is not viable for this.
body {
margin: 0;
}
#container {
background: red;
display: flex;
align-items: center;
justify-content: center;
height: 50px;
padding: 0 50px;
}
#logo {
height: 50px;
width: 50px;
background-color: blue;
position: absolute;
left: 0;
}
#breadcrumb {
width: 200px;
height: 20px;
background-color: green;
}
<div id="container">
<div id="logo"></div>
<div id="breadcrumb"></div>
</div>
I've created 2 separate containers for the logo and breadcrumbs and set them a width. Then, I aligned elements inside these containers.
https://jsfiddle.net/dmitriifrlv/vbhxrj1u/39/
<div id="container">
<div class="logoContainer">
<div id="logo">
</div>
</div>
<div class="breadcrumbContainer">
<div id="breadcrumb">
</div>
</div>
</div>
body {
margin: 0;
}
#container {
background: red;
display: flex;
align-items: center;
justify-content: flex-start;
height: 50px;
}
.logoContainer{
width: 10%;
height: 100%;
background: yellow;
}
#logo {
height: 50px;
width: 100%;
background-color: blue;
}
.breadcrumbContainer{
width:90%;
display: flex;
justify-content: center;
}
#breadcrumb {
width: 200px;
max-width: calc(100% - 2rem);
height: 20px;
background-color: green;
}
For clean solution a little bit of JavaScript is needed:
Make sure to click "Run with JS" button: https://jsbin.com/ziziqidole/edit?html,output
<html>
<head>
<script>
function handleResize() {
var crumb = document.getElementById("breadcrumb");
var logoWidth = document.getElementById("logo").offsetWidth;
var calcWidth = (window.innerWidth - crumb.offsetWidth) / 2 - logoWidth;
if (calcWidth < 10) {
calcWidth = 10;
}
crumb.style.marginLeft = calcWidth;
}
</script>
<style media="all">
body {
margin: 0;
}
#container {
background: red;
display: flex;
align-items: center;
height: 50px;
padding-right: 50px;
}
#logo {
height: 50px;
width: 150px;
background-color: blue;
flex-shrink: 0;
}
#breadcrumb {
width: 200px;
height: 20px;
background-color: green;
}
#center {
width: 200px;
height: 20px;
margin: 0 auto;
background-color: khaki;
text-align: center;
}
</style></head
>
<body onresize="handleResize()" onload="handleResize()">
<div id="container">
<div id="logo"></div>
<div id="breadcrumb"></div>
</div>
<div id="center">Page Center</div>
</body>
</html>
Solution without JavaScript. But some hardcoding needed e.g. logo width and crumb width.
https://jsbin.com/juxijowova/edit?html,output
<html>
<head>
<style media="all">
body {
margin: 0;
}
#container {
background: red;
display: flex;
align-items: center;
height: 50px;
padding-right: 50px;
}
#logo {
height: 50px;
width: 150px;
background-color: blue;
flex-shrink: 0;
}
#breadcrumb {
width: 200px;
height: 20px;
background-color: green;
position: absolute;
left: max(260px, 50%); /* logo width + breadcrumb width/2 + margin*/
transform: translate(-50%, 0);
/*margin: 0 auto;
margin-left: 10px;/*use this if want to center on remaining area instead of screen center*/
}
#center {
width: 200px;
height: 20px;
margin: 0 auto;
background-color: khaki;
text-align: center;
}
</style></head
>
<body>
<div id="container">
<div id="logo"></div>
<div id="breadcrumb"></div>
</div>
<div id="center">Page Center</div>
</body>
</html>
This is usually how I lay something like this out: 3 containers, the side 2 will flex to fill space equally because they have the same exact basis (auto basis would break this because the left "Logo" content would be included in the basis for the left container). The middle is sized to the content and stays centered unless it becomes too wide and will start to take up space on the right and become uncentered.
.f-row {
display: flex;
}
.left-box {
flex: 1 1 0.0000001px;
padding: 1em;
border: 1px solid blue;
}
.middle-box {
padding: 1em;
border: 1px solid red;
justify-self: center
}
.right-box {
padding: 1em;
border: 1px solid green;
flex: 1 0 0.0000001px;
}
<div class="f-row">
<div class="left-box">Logo</div>
<div class="middle-box">Breadcrumb</div>
<div class="right-box"></div>
</div>
<br><br>
<div class="f-row">
<div class="left-box">Logo</div>
<div class="middle-box">Breadcrumb > Longer > Space</div>
<div class="right-box"></div>
</div>
<br><br>
<div class="f-row">
<div class="left-box">Logo</div>
<div class="middle-box">Breadcrumb > Longer > Space > Too Much Now It's Taking Up Space From the Right, Uncentered Now</div>
<div class="right-box"></div>
</div>
I am new to html and css. My question is how do I place image and two buttons to be sure that they will be displayed together? Look at the image to understand what i mean. Thanks!!!!
Height alignment
This is my solution. Try opening the snippet in full screen, and then resize the browser.
When the width of the screen is greater than 768px(u can change this value) the width of the container is 500px.
And when the width is less, then the container takes full width of the screen.
This is handled by
.container {
width: 500px;
margin: 0 auto;
}
#media(max-width:768px) {
.container {
width: 100%;
}
}
As for the buttons, their combined width will be always equal to that of the image.
This is handled by
.btn-container {
font-size: 0;/*used for removing whitespace from inline elements*/
}
.btn-container button {
width: 50%;
font-size: initial;
padding: 15px;
}
* {
box-sizing: border-box;
}
.container {
width: 500px;
margin: 0 auto;
}
#media(max-width:768px) { /* can be any number less than this depending on ur choice */
.container {
width: 100%;
}
}
img {
display: block;
width: 100%;
height: 200px;
}
.btn-container {
font-size: 0;
}
.btn-container button {
width: 50%;
font-size: initial;
padding: 15px;
}
/*Space between */
.btn-holder {
width: 50%;
display: inline-block;
}
.btn-holder button {
width: 100%;
}
.b1 {
padding-right: 5px;
}
.b2 {
padding-left: 5px;
}
<div class="container">
<img src="http:placehold.it/250x250">
<div class="btn-container">
<button>button 1 </button>
<button>button 1 </button>
</div>
</div>
<br/>
<br/>
<br/>
<h3>If u want space between buttons</h3>
<div class="container">
<img src="http:placehold.it/250x250">
<div class="btn-container">
<div class="btn-holder b1">
<button>button 1 </button>
</div>
<div class="btn-holder b2">
<button>button 1 </button>
</div>
</div>
</div>
for the image centering you can just use:
<div id="container">
<img style="margin-left:auto;margin-right:auto;"></img>
</div>
Then for the buttons you can use:
<div id="wrapper">
<button id="button1">button left</button>
<button id="button2">button right</button>
</div>
and then this css:
#button1 {
display: inline-block;
width:120px;
height:120px;
}
#button2 {
display: inline-block;
width:160px;
height:160px;
}
Which you then combine into this:
#button1 {
display: inline-block;
width:120px;
height:120px;
}
#button2 {
display: inline-block;
width:160px;
height:160px;
}
<div id="container">
<img src="https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F8%2F8f%2FExample_image.svg%2F1024px-Example_image.svg.png&f=1" style="margin-left:auto;margin-right:auto;"></img>
<br>
<button id="button1">button left</button>
<button id="button2">button right</button>
</div>
in case you're confused as to what the <br> tag does it is just a line break in order to make sure that the buttons don't get placed next to the image.
I have created a jsFiddle that shows the content.
.container {
height: 100%;
width: 100%;
margin: 0;
}
.body {
position: relative;
height: 100%;
width: 100%;
margin: 0 auto;
background-color: #F7F4F2;
text-align: center;
}
.form {
background-color: #fff;
padding: 50px 20px;
color: #333;
}
.footer {
margin-top: 26px;
font-size: 20px;
line-height: 26px;
}
.content {
padding: 5% 5%;
}
<div class="container">
<div class="body">
<div class="content">
<div class="form">
<h1>Content</h1>
</div>
<div class="footer">
This is a link
</div>
</div>
</div>
</div>
What I am failing to do is to have the container element to fill the available viewport and have the inner white div also stretch to the bottom with the padding respected.
I would also like to move the link to the bottom of the viewport.
Is this possible without JavaScript?
How about something like this using flexbox?
Put display: flex; on .content
Then use flex: 1; on the main child div and then height: 100vh;.
Now you don't need all that width and height 100% styles.
Also used box-sizing: border-box so the padding doesn't mess with the size of the containers.
* {
box-sizing: border-box;
}
.container {
margin: 0;
}
.body {
position: relative;
margin: 0 auto;
background-color: #F7F4F2;
text-align: center;
}
.content {
display: flex;
flex-direction: column;
height: 100vh;
padding: 5% 5%;
}
.form {
flex: 1;
background-color: #fff;
padding: 50px 20px;
color: #333;
}
.footer {
margin-top: 26px;
font-size: 20px;
line-height: 26px;
}
<div class="container">
<div class="body">
<div class="content">
<div class="form">
<h1>Content</h1>
</div>
<div class="footer">
This is a link
</div>
</div>
</div>
</div>
Use this CSS:
html, body {
margin: 0;
padding: 0;
height: 100%;
}
You need height: 100%; on every parent, all the way up to html & body. Without it, the height won't be dynamic aren't going to work.
JSfiddle
I have a design using some bootstrap styling which has a white column on the right. The height should be 100%, but it isn't rendering at 100%. It renders at 100% of the initial screen height, but when you scroll down it's no longer white.
I've looked at several other CSS solutions on this site. I've tried making all parent elements 100% height. I've tried making it a flexbox column. I've tried putting "position: relative;" in the body. Nothing has worked yet. I'd prefer not to use JS to achieve this.
Simplified version of my HTML:
<body>
<div class="container">
<div class="main">
<h1>This is the main content area</h1>
</div>
<div class="right pull-right">
<div class="content">
</div>
<div class="content">
</div>
<div class="content">
</div>
<div class="content">
</div>
</div>
</div>
</body>
The CSS:
body,html {
height: 100%;
background-color: #aaa;
}
body {
position: relative;
}
.main {
display: inline-block;
}
.container {
height: 100%;
}
.right {
display: inline-flex;
flex-direction: column;
background-color: #fff;
width: 350px;
height: 100%;
min-height: 100%;
border: 1px solid #E1E6E9;
margin-right: 100px;
position: absolute;
right: 100px;
}
.content {
height: 300px;
min-height: 300px;
margin: 10px 10px;
background-color: #ccc;
border: 1px solid #000;
}
Change your .right class to have height: auto;
It will size itself to fit with its content.
.right {
display: inline-flex;
flex-direction: column;
background-color: #fff;
width: 350px;
height: auto;
min-height: 100%;
border: 1px solid #E1E6E9;
margin-right: 100px;
position: absolute;
right: 100px;
}
http://codepen.io/smlariviere/pen/WrWgxQ
I'm trying to create three buttons on either side of my main logo. These buttons should appear vertically center however when applying different display properties and/or margins and padding of 24px~ nothing changes. The attached JSFiddle shows the issue I have.
http://jsfiddle.net/LRaJy/
.header {
width: 100%;
background-color: #64767f;
background-position: bottom;
padding: 10px 0;
}
.header .logo {
background-image: url('../img/header-logo.png');
display:inline-block;
width: 415px;
height: 100px;
background: #fff;
}
.header-container {
width: 733px;
height: 100px;
margin: 0 auto;
}
.hover-buttons {
height: 44px;
display: inline-block;
padding-top: -5px;
}
.hover-buttons .button {
width: 44px;
height: 44px;
display: inline-block;
margin-right: 5px;
background: #fff;
}
with the HTML
<div class="header">
<div class="header-container">
<div class="hover-buttons">
<div class="button backups"></div>
<div class="button currency"></div>
<div class="button contact"></div>
</div>
<div class="logo">
</div>
<div class="hover-buttons">
<div class="button satisfaction"></div>
<div class="button security"></div>
<div class="button care"></div>
</div>
</div>
</div>
Is this what you're looking for?
jsFiddle Demo
.button, .logo {
vertical-align: middle;
}