CSS: Fixed-sidebar in Vue.js - html

I am creating a layout where a sidebar will be fixed on the left and placed next to a container which will have its own grid. I use Vue.js so every template has its <div> where I need to put other <div>s with the sidebar or the main content. Seems like my layout doesn't want to accept my CSS rules when I try to place my sidebar or give it a color, etc. Please help!
Here is my layout:
<template>
<div>
<sidebar />
<main-content />
</div>
</template>
Where the sidebar is:
<template>
<div>
<div class="sidebar">
<div id="logo">
<img src="" />
</div>
<h1>Company</h1>
<ul>
<!--<li>About</li>-->
<!--<li>Popular</li>-->
<!--<li>News</li>-->
<!--<li>Pages</li>-->
<li>Users
<ul id="sublist">
<li>Import</li>
<li>Invitations</li>
</ul>
</li>
<!--<li>Organisations</li>-->
<li>Polls</li>
</ul>
</div>
</div>
</template>
And the main content is (there is a table component inside):
<template>
<div>
<div class="container-grid">
<div class="header">
<h2>Users</h2>
<button>Add</button>
<h3>Import users</h3>
</div>
<main-table />
</div>
</div>
</template>
Here is my CSS:
.sidebar {
height: 100%;
width: 300px;
max-width: 1024px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow-x: hidden;
box-sizing: border-box;
}
h1 {
display: inline;
height: 29px;
width: 156px;
}
ul,
#sublist {
list-style: none;
margin: 0;
padding: 0;
}
a {
height: 20px;
width: 200px;
text-decoration: none;
display: block;
}
// Main Content Style
.container-grid {
margin-left: 336px;
}

This is not the answer to your question but just a heads up.
You do not need to wrap your components with a div every time. You just need to ensure that you only ever have 1 root element.
This means if you have 2 elements like this you do need to wrap them:
<template>
<div>
<span></span>
<span></span>
</div>
</template>
But a single span element you can simple leave in your template like so:
<template>
<span></span>
</template>

It works as expected here in the snippet, except I had one puzzling thing: it would not style .container-grid for me. I finally figured out that the reason was the embedded comment (// Main content style), which is not valid css. I don't know whether that might be an issue in your development environment.
new Vue({
el: "#app",
components: {
sidebar: {
template: '#sidebar-template'
},
mainContent: {
template: '#main-template'
}
}
});
.sidebar {
background-color: #fee;
height: 100%;
width: 150px;
max-width: 1024px;
opacity: 0.5;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow-x: hidden;
box-sizing: border-box;
}
h1 {
display: inline;
height: 29px;
width: 156px;
}
ul,
#sublist {
list-style: none;
margin: 0;
padding: 0;
}
a {
height: 20px;
width: 200px;
text-decoration: none;
display: block;
}
.container-grid {
background-color: #eef;
margin-left: 180px;
}
<script src="//unpkg.com/vue#latest/dist/vue.js"></script>
<div id="app">
<sidebar></sidebar>
<main-content></main-content>
</div>
<template id="sidebar-template">
<div>
<div class="sidebar">
<div id="logo">
<img src="" />
</div>
<h1>Company</h1>
<ul>
<!--<li>About</li>-->
<!--<li>Popular</li>-->
<!--<li>News</li>-->
<!--<li>Pages</li>-->
<li>Users
<ul id="sublist">
<li>Import</li>
<li>Invitations</li>
</ul>
</li>
<!--<li>Organisations</li>-->
<li>Polls</li>
</ul>
</div>
</div>
</template>
<template id="main-template">
<div class="container-grid">
<div class="header">
<h2>Users</h2>
<button>Add</button>
<h3>Import users</h3>
</div>
</div>
</template>

Something like this:
HTML:
<section>
<p>sidebar</p>
</section>
<section>
<h2>Section1</h2>
<p>section1 whatever data you want to put in this section</p>
</section>
<section>
<h2>Section2</h2>
<p>section1 whatever data you want to put in this section</p>
</section>
<section>
<h2>Section3</h2>
<p>section1 whatever data you want to put in this section</p>
</section>
<section>
<h2>Section4</h2>
<p>section1 whatever data you want to put in this section</p>
</section>
CSS:
section:first-child {
width: 20%;
height: 100vh;
}
section {
width: 80%;
float: left;
}
But you'd need to change the height: 100vh; to something that will cover the whole page hight and not just the veiwport hight.
Doing this means all the other sections will be to the right of the first one (where you would put your sidebar)

Related

How to fit elements perfectly within another element?

Using the 98.css library: https://jdan.github.io/98.css/
I'm currently trying to fit the bottom element status-bar at the bottom of the box with its width set to stretch to the entire box's width. I feel like I'm doing it incorrectly and/or not doing it properly via percentages in .width-stretch.
The result is this:
Note that it is somehow overlapping the bottom-right edge of the box. When I try making the width: 99%, it gets pretty close to what I want but not quite, and it is left-aligned (and not centered). I'm trying to figure out a more elegant solution that fixes this issue; any suggestions? I'm also open to any CSS frameworks that can run alongside 98.css to make styling easier. Thanks!
...
<style>
html, body {
margin:0px;
background: #c0c0c0;
}
.center {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top:0px;
right:0px;
bottom:0px;
left:0px;
}
.relative-position {
position: relative;
width: auto;
}
.bottom {
position: absolute;
bottom: 0;
padding-bottom: 4px;
}
.width-stretch {
width: 100%;
}
</style>
</head>
<body>
<!-- MAIN -->
<div class="center">
<div class="window relative-position" style="width: 95%; height: 95vh;">
<div class="title-bar">
<div class="title-bar-text">A Window With A Status Bar</div>
</div>
<div class="window-body">
<p> There are just so many possibilities:</p>
<ul>
<li>A Task Manager</li>
<li>A Notepad</li>
<li>Or even a File Explorer!</li>
</ul>
</div>
<div class="status-bar bottom width-stretch">
<p class="status-bar-field">Press F1 for help</p>
<p class="status-bar-field">Slide 1</p>
<p class="status-bar-field">CPU Usage: 14%</p>
</div>
</div>
</div>
</body>
</html>
You can use flex-direction:column on .window, with flex:1 on the .window-body so that it fills all the available vertical space.
html,
body {
margin: 0px;
background: #c0c0c0;
}
.center {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
}
.window {
display: flex;
flex-direction: column;
}
.window-body {
flex: 1;
}
<link rel="stylesheet" href="https://unpkg.com/98.css">
<div class="center">
<div class="window " style="width: 95%; height: 95vh;">
<div class="title-bar">
<div class="title-bar-text">A Window With A Status Bar</div>
</div>
<div class="window-body">
<p> There are just so many possibilities:</p>
<ul>
<li>A Task Manager</li>
<li>A Notepad</li>
<li>Or even a File Explorer!</li>
</ul>
</div>
<div class="status-bar">
<p class="status-bar-field">Press F1 for help</p>
<p class="status-bar-field">Slide 1</p>
<p class="status-bar-field">CPU Usage: 14%</p>
</div>
</div>
</div>
Add these styles to the inside of the style tag.
.status-bar {
display: flex;
}
.status-bar-field {
flex: 1;
padding: 0 10px;
border: 1px solid black;
}

Two sidebars issue

I have problem with sidebars on my website. I have two sidebars both 250px, one is on the left side and the second on the right side and have between div with content. That left sidebar is ok but that second doesnt want to change place for the right side and is on the left on place that first.
.info {
float: right;
position: fixed;
z-index: 1000;
height: 100%;
width: 250px;
padding-right: 0;
padding-top: 50px;
box-sizing: border-box;
}
.sidebar-menu {
position: fixed;
z-index: 1000;
height: 100%;
width: 250px;
display: block;
padding-left: 0;
padding-top: 50px;
box-sizing: border-box;
}
.content-main {
height: 100%;
padding-top: 56px;
width: 100%;
}
<div class="fluid-container">
<ul class="sidebar-menu">
<div class="photo">
</div>
<div class="name">
<h2 class="getname">Tommy Hilfiger</h2>
</div>
<div class="active">
<p class="menu">Activity</p>
</div>
<div class="followers">
<p class="menu">Followers</p>
</div>
<div class="friends">
<p class="menu">Friends</p>
</div>
<div class="photos">
<p class="menu">Photos</p>
</div>
<div class="edit">
<p class="menu">Edit</p>
</div>
</ul>
<ul class="info">
<div class="aqt">
</div>
</ul>
<div class="content-main">
</div>
</div>
First, don't use <ul> with <div>, you should use <ul><li>...</li></ul> otherwise change <ul> to <div> (<ul> is unordered list, <li> is list item).
Secondly, the way you're building the html, you're not splitting <div> elements (division), you're actually building one block with two lists and a division.
if you want to have [SideBar][Content][secondBar] use this:
<div class="wrapper">
<div class="leftbar">
anything
</div>
<div class="content">
</div>
<div class="rightbar">
</div>
</div>
Use the following css as fundamental, style after:
div.wrapper{
display: grid;
grid-template-areas: "leftbar content rightbar";
grid-template-rows: 1fr;
}
div.leftbar{
grid-area: leftbar;
}
div.content{
grid-area: content;
}
div.rightbar{
grid-area: rightbar;
}

How to make an image inside a div overlap another div?

This is how it looks so far:
And this is what I need to achieve:
<section class="section sub-masthead--TC2"></section>
<section class="section qualifiers--Q1"></section>
The first <section class=sub-masthead--TC2> element contains the image in some nested divs. With the design above is very clear what I need. I already set position: relative to both sections and z-index: 1 to the upper section and z-index: 3 to the down one but it doesn't work.
This is the whole HTML for the section that contains the image:
<section class="section sub-masthead--TC2">
<div class="sub-masthead__wrapper">
<div class="sub-masthead__tiles">
<div class="sub-masthead-item">
<div class="sub-masthead-item__content">
<div class="sub-masthead-item__copy">
<p><!-- text --></p>
</div>
</div>
<div class="sub-masthead-item__image-container">
<!-- IMAGE HERE -->
<img class="sub-masthead-item__image" src="assets/images/bg_phone-unique.png" alt="" role="img">
</div>
</div>
</div>
</div>
</section>
Here is my try. I have created an example for you to follow along.
First add position: relative to the parent element of both sections or if there is none then the body.
Then add position absolute to image section and manipulate top values.
.sub-masthead--TC2 {
position: absolute;
top: 40px;
right: 0;
}
Result:
.wrapperDiv {
position: relative;
}
.sub-masthead--TC2 {
width: 100%;
height: 100px;
background-color: tomato;
}
.qualifiers--Q1{
width: 300px;
height: 300px;
position: absolute;
top: 40px;
right: 0;
border: 1px solid #000;
padding: 20px;
}
img{
width: 100%;
height: auto;
}
<div class ="wrapperDiv">
<section class="section sub-masthead--TC2"></section>
<section class="section qualifiers--Q1">
<img src="https://placeimg.com/640/480/any" />
</section>
</div>

CSS: Background color to extend to a certain element

I have the following code
<body>
<div style="height: 35%; background-color: black;"></div>
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>
One
</p>
<p>
Two
</p>
</div>
</div>
</body>
Ideally, I would like the top portion of the page to be a certain color (black in the example), and I want the header area (which contains the <h1> and <h3> elements) to be inside the black box. Then I would like the first paragraph of the content to also be included inside the black box. Very similar to this picture:
What is the best way to go about this?
The simplest way is to use an absolute positioned pseudo element on the header
Stack snippet
body {
background-color: #ddd;
margin: 0;
}
#header {
position: relative;
color: white;
background-color: black;
}
#header::before {
content: '';
position: absolute;
top: 100%;
left: 0;
height: 60px;
width: 100%;
background-color: inherit;
}
#header div {
width: 80%;
margin: 0 auto;
padding: 10px;
}
#content {
position: relative;
width: 80%;
margin: 0 auto;
background-color: white;
}
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>
One
</p>
<p>
Two
</p>
<p>
Thre
</p>
<p>
Fou
</p>
</div>
</div>
Three steps:
Apply a gradient background to the <body>.
Create two sectioning elements: <header> and <section>
Ensure all the relevant elements in <header> and at the top of <section> have an explicitly declared height in pixels which, combined, match the height of the first part of the gradient.
Make sure that the html and body have height: 100% or min-height: 100% otherwise height 35% is not going to be 35% of the viewport height.
You can make the black background with an absolute positioned element. I suggest to look into css position(relative, absolute, fixed, static).
Here's a demo and the code:
https://jsfiddle.net/n617L6rh/
<div id="bg"></div>
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>One</p>
<p>Two</p>
</div>
</div>
html,
body {
height: 100%;
}
#bg {
height: 35%;
background: black;
position: absolute;
top: 0;
left: 0;
right: 0;
}
#header {
height: 35%;
color: #fff;
position: relative;
}

Two Sections 100% Height of Parent Div

I have a specific layout that is causing me HUGE headaches. Here is an image:
My goal is to have the "Side panel" ALWAYS equal the height of the container. The "Enrollment Application" section is at 100% height already.
Current Markup
<body>
<div id="container" class="pure-g">
<div class="pure-u-md-1-4 pure-u-1 panel" id="left-panel">
<div class="panel-row">
<div class="panel p">
<div class="inner-panel">
<div class="panel-logo">
"Logo here text"
</div>
</div>
</div>
</div>
<div class="panel-row">
<div class="panel p">
<div class="inner-panel">
<nav class="panel">
</nav>
</div>
</div>
</div>
</div>
<div id="right-panel" class="pure-u-md-3-4 pure-u-1 panel p">
<div class="inner-panel">
<header class="pure-g">
<div class="pure-u-md-1-4 pure-u-1 header-logo">
LOGO Would go here, of course.
</div>
<div class="pure-u-md-3-4 pure-u-1 header-title">
<h1>Consumers Energy</h1>
<h1><strong>CARE 3.0 Program</strong></h1>
<h1>Enrollment Application</h1>
</div>
</header>
<div id="content">
"Enrollment application text..."
</div>
</div>
</div>
</div>
</body>
Current CSS
.panel {
box-sizing: border-box;
height: 100%;
display: table-cell;
}
.panel.p {
padding: 3px;
}
.panel .panel-row {
display: table-row;
}
.panel .inner-panel {
border-radius: 5px;
padding: 10px;
width: 100%;
box-sizing: border-box;
background-color: red;
}
Here is an alternative fiddle to play with: http://jsfiddle.net/3c3tqo3e/ but I really don't want to use a table...
Q How can we stack two divs and make their heights = 100% of parent? The "Logo here.." section will be an auto height.
NOTE I would really prefer an answer that is responsive-friendly. I am using PureCSS for the sections. (This means that absolute positioning is not preferred) Also, strongly prefer just css/html. Thanks!
I have created a demo for you, but it will work on all modern browsers only. and you might have to read flexbox and its demos in details to make your work more meaningful in terms of performance and maintenance.
Also read on calc() here
HTML:
<main>
<aside>
<div class="logo">Logo</div>
<div class="aside-content">Other Content</div>
</aside>
<section>Section</section>
</main>
CSS:
html, body{ height: 100%; }
main{
height: 100%; background: teal; padding: 2em; box-sizing: border-box;
display: flex; flex-direction: row;
}
aside{
height: inherit; margin: 0 1em 0 0; width: 200px;
}
aside .logo{
background: #fff; height: 140px;
}
aside .aside-content{
background: #fff; height: calc(100% - 150px); margin: 10px 0 0 0;
}
main section{
height: inherit; background: #fff; flex-grow: 2;
}
Demo Fiddle: http://jsfiddle.net/vpqqyo9L/1/
Edit:
Here's one for IE9: http://jsfiddle.net/vpqqyo9L/3/