Sudden overflow with float/clear - css

I'm trying to style a page without the common grid systems using float and clear. The page should use the full viewport for both width and height. The below setup works fine, unless I start resizing the viewport. Once I move the viewport, the navigation and content section suddenly start to overlap, with the content being rendered below the navigation.
Is there something wrong with my approach or should I not use vh and vw when users are likely to resize the viewport?
The setup is meant to be conceivably simple:
A header bar with a height of 40px.
A side navigation section with a width of 20vw
A content section with a width of 80 vw - some margins.
The navigation and content sections are meant to have a min height to fill the viewport.
Sizes like 79.99vw and 99.9vh were introduced to avoid any rounding issues. I have also tried 80vw and 100vh respectively.
My template looks like this:
<div class="dashboard-wrapper">
<header class="header-bar">
<h1 class="bar-title">Brand Logo</h1>
<button class="logout-button">Log out</button>
</header>
<section class="side-bar">
<nav role="navigation" class="sidebar">
<ul>
<li>Navigation One</li>
<li>Navigation Two</li>
<li>Navigation Three</li>
<li>Navigation Four</li>
<li>Navigation Five</li>
</ul>
</nav>
</section>
<section class="content">
<h2>Lorem Ipsum</h2>
<p>Sed ut perspiciatis ...</p>
</section>
</div>
My CSS looks like this:
.dashboard-wrapper {
margin-right: auto;
margin-left: auto;
}
.dashboard-wrapper header.header-bar {
float: top;
height: 40px;
}
.dashboard-wrapper header.header-bar h1 {
position: relative;
margin: 0 0 0 5px;
float: left;
}
.dashboard-wrapper header.header-bar button {
position: relative;
height: 40px;
width: 80px;
border: 0;
float: right;
}
.dashboard-wrapper section.side-bar {
float: left;
clear: left;
width: 20vw;
min-height: calc(99.9vh - 40px - 20px);
margin: 10px;
background: white;
}
.dashboard-wrapper section.content {
float: right;
clear: right;
position: relative;
width: calc(79.99vw - 30px);
min-height: calc(99.9vh - 40px - 20px);
margin: 10px 10px 10px 0;
background: white;
}
This was so far only tested in Chrome on Windows.

Removed invalid float:top,
added clearfix class : .cf to clear floats
added box-sizing:border-box just in case you might use padding
switched width using vw units for % units to get rounded values when using calc and to get consistent result across devices.
a few other small things.
* {
box-sizing: border-box
}
.cf:before,
.cf:after {
content: " ";
display: table;
}
.cf:after {
clear: both;
}
body {
margin: 0;
}
.dashboard-wrapper header.header-bar {
height: 40px;
/*demo*/
background: green
}
.dashboard-wrapper header.header-bar h1 {
margin: 0 0 0 5px;
float: left;
}
.dashboard-wrapper header.header-bar button {
height: 40px;
width: 80px;
border: 0;
float: right;
}
.dashboard-wrapper section.side-bar {
float: left;
width: 20%;
min-height: calc(100vh - 60px);
margin: 10px;
background: red;
}
.dashboard-wrapper section.content {
float: left;
width: calc(80% - 30px);
min-height: calc(100vh - 60px);
margin: 10px 10px 10px 0;
background: lightblue;
}
footer {
background:orange;
width:100%
}
<div class="dashboard-wrapper">
<header class="header-bar cf">
<h1 class="bar-title">Brand Logo</h1>
<button class="logout-button">Log out</button>
</header>
<div class="main-wrapper cf">
<section class="side-bar">
<nav role="navigation" class="sidebar">
<ul>
<li>Navigation One</li>
<li>Navigation Two</li>
<li>Navigation Three</li>
<li>Navigation Four</li>
<li>Navigation Five</li>
</ul>
</nav>
</section>
<section class="content">
<h2>Lorem Ipsum</h2>
<p>Sed ut perspiciatis ...</p>
</section>
</div>
<footer>
Lorem Ipsum ed ut perspiciatis ...
</footer>
</div>

Related

Cannot center vertically [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 11 months ago.
I want to center both horizontally and vertically the page.
<body>
<div class="container">
<main>
<section>
<nav>
<ul>
<li>
Create Book
</li>
<li>
Create Author
</li>
<li>
Create Publisher
</li>
</ul>
</nav>
</section>
</main>
</div>
</body>
While I achieved success with horizontal centering by setting all width to 100% and container's width to 50% and margin auto. However the same method doesn't seem to work vertically (height 50%).
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 100%;
width: 100%;
}
body {
height: 100%;
width: 100%;
color: #444;
font-family: sans-serif;
}
.container {
height: 50%;
width: 50%;
margin: auto;
}
Use flex properties
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 100%;
width: 100%;
}
body {
height: 100%;
width: 100%;
color: #444;
font-family: sans-serif;
}
.container {
height: 50%;
width: 50%;
margin: auto;
display: flex;
align-items: center;
justify-content: center;
}
<div class="container">
<main>
<section>
<nav>
<ul>
<li>
Create Book
</li>
<li>
Create Author
</li>
<li>
Create Publisher
</li>
</ul>
</nav>
</section>
</main>
</div>

div background stretch except the content

I'm trying to make the header and footer background to be 100% but all the content in the page to be 1200px and centre the page.
So far I put width:1200px in the html, body sure that centre the page but it fixed width the background which I don't want to happen.
Also I've been trying to fix the navigation as well to be positioned on the right and align to the logo.
html,body{
background: #fff;
width: 1200px;
}
.container{
min-height: 100%;
margin: 0 auto;
}
header{
background: #363636;
color: #fff;
padding: 30px;
margin: 0 auto;
}
.logo{
float: left;
clear: both;
display: block;
}
nav{
float: right;
}
<div class="container">
<header clas="main_header">
<div class="logo">
<h2>LOGO.COM</h2>
</div>
<div class="clearfix"></div>
<nav class="main_nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Testimonials</li>
<li>Contact Us</li>
<li>Get a Quote</li>
</ul>
</nav>
</header>
.....
</div>
Don't restrict the width of the HTML or body...restrict the width of the container.
html,body{
background: #fff;
}
.container{
width: 1200px;
margin: 0 auto;
}
If an element needs to be wider than the container just close one .container add your wider element and open a new .container.
Codepen Demo

Footer Overlaps Div before Dropping Below

I managed to make my footer to stick to the bottom of the window, then avoid my content div... but now, it's overlapping for about 15px before dropping below.
I want the footer to drop, but to never overlap the content. I think it may have something to do with my margins, but my tweaking has not yet solved this.
Any suggestions?
Adding margin-bottom:15px doesn't seem to help, because it's only shown once the footer overlaps enough to push the footer down. Then the margin is shown, but not before that small amount of overlap.
The #push div is supposed to make this all possible, from what I understood on Twitter's bootstrap example.
CSS:
* {
margin: 0;
padding: 0;
}
html {
height: 100%;
}
body {
margin: 0;
height: 100%;
}
#wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -50px;
min-width: 900px;
}
.main_nav {
list-style-type: none;
margin: 0;
width: 160px;
float: left;
padding-left: 40px;
overflow: hidden;
}
#bio_content {
width: 700px;
min-height: 445px;
margin-bottom: 15px;
float: left;
}
#bio_text {
padding: 10px;
}
#push {
height: 50px;
}
#footer {
height: 50px;
width: 100%;
float: left;
}
HTML:
<body>
<div id="wrapper">
<div id="header">
<h1></h1>
</div>
<ul class="main_nav">
<li>Home</li>
<li>About</li>
<li>Music</li>
<li>Services</li>
<li>Gallery</li>
<li>Contact</li>
</ul>
<div id="bio_content">
<div id="bio_text"></div>
</div>
<div id="push"></div>
</div>
<div id="footer">
<div id="footer_content"></div>
</div>
</body>
</html>
I thin it's a float issue. A fiddle would help diagnose this better. I would suggest doing this
<body>
<div id="wrapper">
<div id="header">
<h1></h1>
</div>
<ul class="main_nav">
<li>Home</li>
<li>About</li>
<li>Music</li>
<li>Services</li>
<li>Gallery</li>
<li>Contact</li>
</ul>
<div id="bio_content">
<div id="bio_text"></div>
</div>
<div style="clear:both"></div> //add this to clear the bio_content div
<div id="push"></div>
</div>
<div id="footer">
<div id="footer_content"></div>
</div>
</body>
</html>
You need to clear the styles in your footer. Add a clear:both to your footer css.
#footer {
clear: both;
height: 50px;
width: 100%;
}

CSS Page Centering

I'm not sure where my code is messed up, but I'm trying to get my code to center. Here is what I have so far:
The problem that I am running into is that the page won't actually center. When it loads, the left margin is only a 10px. I'm sure there is a conflict somewhere, or I just borked something, and I've been searching for hours now, and have found nothing. Here is the following code:
body {
background-color: #12121D;
}
#contain {
display: block;
width: 1000px;
margin: 0 auto;
}
.version{
text-align:right;
font-family: Calibri, sans-serif, serif;
text-decoration:none;
color:#00F;
}
#header {
width: 1000px;
height: 230px;
}
#logo {
width: 600px;
margin: 50px 0px 0px 10px;
padding: 0px;
float: left;
position: absolute;
}
#banner {
text-align: right;
float: right;
z-index: -1;
}
#menu {
height: 80px;
margin: 0 auto;
}
#main {
height: 500px;
width: 850px;
margin-left: 75px;
/*position: absolute;*/
z-index: 4;
/*float: left*/;
}
#content {
width: 850px;
height: 500px;
float: left;
margin-left: 0px;
}
And the html follows as:
<body>
<div id="contain">
<div id="header">
<div id="logo"> <img src="images/logo.png" alt="logo" />
<br />
<a class="version" href="JavaScript:changeSheets(1)">View Mobile<a/>
</div>
<div id="banner"><img src="images/banner.png" alt="banner"/></div>
</div>
<div id="menu">
<div class="pages">
<ul>
<li>Front Page</li>
<li>Reviews</li>
<li>Releases</li>
<li>About Me</li>
<li>Contact Info</li>
</ul>
</div>
</div>
<div id="main">
<div id="content">
<div class="why" id="whyBackground"> <img src="images/reviewsites.png" alt="reviewsites"/>
<h1>Why am I doing this?</h1>
<br />
Insert text here
<h1>What is my goal?</h1>
Insert text here
</div>
</div>
</div>
</div>
</body>
Hi there is Your code:
http://jsfiddle.net/w9Zgc/2/
On my screen larger 1920px page is centered. I think that you have screen resolution close to 1024px width so than left margin left equal 10px is quite reasonable. Another advice is to reset padding and margin of body
body, html {
padding:0;
margin: 0;
}
One thing that will help you center a div is to specify the width, and then set the right & left margins to auto:
#main{
width: 850px;
margin-left: auto;
margin-right: auto;
}
Judging from your link at http://jsbin.com/itiwez/2/edit
I removed #charset "utf-8" in your CSS and it instantly worked (at least in your jsbin demo). Is your CSS file actually in UTF-8 format?
FYI: Why specify #charset "UTF-8"; in your CSS file?

How do I center two divs within a larger div?

I am trying to center two divs (#about and #testimonial-snippets) within the larger black div. How can I do this?
JS Fiddle: http://jsfiddle.net/DgtqM/
HTML
<footer>
<div id="footer-section">
<section id="about">
<img class="profile-photo" src="http://dummyimage.com/42x42/000/fff" alt="profile" height="44" width="44">
<p>Lorem ipsum dolor sit amet. Find him on Twitter and Instagram. <a id="slide-toggle" href="#">Contact</a> | Archive</p>
</section>
<section id="testimonial-snippets">
<img class="profile-photo" src="http://dummyimage.com/42x42/000/fff" alt="profile" height="44" width="44">
<div class="snippet">
<p>This is a testimonial.</p>
<a class="read-testimonial" href="/testimonials">read more</a>
</div>
</section>
</div>
</footer>
CSS
footer {
background: #222;
clear: both;
color: #f4f3f1;
float: left;
padding: 50px 0;
width: 100%;
}
#footer-section {
margin: 0 auto;
overflow: hidden;
position: relative;
width: 940px;
}
footer section {
float: left;
width: 300px;
}
#about {
margin-right: 20px;
}
footer a {
border-bottom: 1px dotted #f4f3f1;
color: #f4f3f1;
}
.profile-photo {
border: 1px solid #f4f3f1;
float: left;
margin: 4px 10px 10px 0;
}
p {
margin: 0 0 1em;
}
I allowed myself to reduce that problem to a minimum of markup. Everything else is not relevant to the question and only makes it harder to understand.
<footer>
<section id="about">About</section>
<section id="testimonial-snippets">Testimonial</section>
</footer>
One solution would be to make these sections inline-block elements and have them centered in the footer:
footer {
background: #222;
padding: 50px 0;
width: 100%;
text-align: center;
}
footer section {
width: 300px;
height: 50px;
display: inline-block;
text-align: left;
}
/* Just coloring the different divs */
#about { background: red; }
#testimonial-snippets { background: green; }
http://jsfiddle.net/DgtqM/6/
Wrap the elements within a new div. Then give the new div a fixed width and use margin: 0px auto for styling.
HTML
<div id="footer-section">
<div class="wrap">
<section id="about">
<!--Content -->
</section>
<section id="testimonial-snippets">
<!--Content-->
</section>
</div>
</div>
CSS
.wrap{
width: 620px;
margin: 0px auto;
overflow: auto;
}
Example http://jsfiddle.net/DgtqM/5/

Resources