I created a tumblr theme, where everything is centered and 660px wide.
However, I also post large imagery that is 940px wide, and have been centering that by giving it a negative margin of -140px (940-660/2), but this is not ideal because I then have to post all images as this dimension, or they are just aligned way left.
Scroll to the bottom of my site to see the images that are not aligned properly: http://seans.ws
The css:
section {display: block; clear: both; margin: 0 auto;width: 660px;}
article img {clear: both; max-width: 940px; margin-left: -140px;}
Thanks for any help!
You can choose between these two solutions:
Markup:
<div id="content">
<div class="a"><div class="b">
<img src="http://placekitten.com/100/100">
</div></div>
<div class="a"><div class="b">
<img src="http://placekitten.com/2000/100">
</div></div>
Common css:
#content {
width: 300px;
margin: 0 auto;
border: 1px solid blue;
}
.a {
/* extend image area */
margin-left :-9999px;
margin-right:-9999px;
/* but without scrollbars */
position: relative;
left: -9999px;
}
.a .b {
/* undo scrollbar-removing positioning */
position: relative;
left: 9999px;
}
The display: table way:
http://jsfiddle.net/ZhEku/3/
.a .b {
display: table; /* shrink-wrap to content (= the image) */
width: 300px; /* content width, acts as min-width when display:table */
margin: 0 auto; /* center inside the (2*9999+300)px area */
}
The display: inline-block way:
http://jsfiddle.net/ZhEku/4/
.a {
/* center content (= the image wrapped into .b) */
text-align: center;
}
.a .b {
display: inline-block; /* shrink-wrap to content (= the image) */
min-width: 300px; /* content width */
text-align: left; /* if image is smaller than the content */
}
Enjoy :)
Here's the infinite scroll js: http://static.tumblr.com/q0etgkr/ytzm5f1ke/infinitescrolling.js
Here is my margin-left script for images larger than the default width of containers:
<!--Dynamicaly center big images-->
<script>
$(window).load(function() {
$(function() {
$('img').css('marginLeft', function(index, value){
if($(this).width() > 660) {
return -($(this).width() - 660)/2;
}
return value;
});
});
});
</script>
The only thing I need to figure out is how to do this same function on images that dynamically load because I have infinite scroll (like the bottom images are not loaded until you go down the page.
Related
I want to center the text (namely h1) both vertically and horizontally using vh and vw units only.
My HTML body looks like this,
<section class="centered_text">
<h1>Centered text.</h1>
</section>
My simple css (scss) looks like this,
* {
margin: 0;
padding: 0;
}
.centered_text {
margin: 50vh 50vw;
}
The problem is that the text goes more to the right side of a screen. I figured out that when I set the h1 width to the fixed one and subtract half of that width from the margin with the calc function, it seems like it's working, but I don't really know why. The code is this,
* {
margin: 0;
padding: 0;
}
.centered_text {
margin: 50vh calc(50vw - 75px);
h1 {
width: 150px;
}
}
My two questions are,
Why does it work?
How to do it otherwise, in a more convenient way (EDIT: but still using the vh and vw units)?
Thanks in advance.
It works because the position in css is the top-left position of the box where your text is in. 50vw is exactly half the view-width, so the text starts in the middle (and is not centered), removing half the width of the text fixes that.
For centering horizontally I usually use:
css:
.centered_text {
margin-left:auto;
margin-right:auto;
h1 {
width: 150px;
}
}
50vw/50vh is exactly half of the view area. The child element placed at that coordinate starts at it's top left corner, so if you subtract half of it's width, it gets centered.
If a parent container has display: flex;, you can center it's child element with simple margin: auto:
section
{
border: 1px solid red;
text-align: center; /* center wrapped text */
display: flex;
}
h1
{
margin: auto; /* center element */
}
.centered_text
{
width: 100vw;
height: 100vh;
}
html,body
{
height: 100vh; /* set height so we can use % values */
}
.containerBox
{
width: 60%; /* 60% of body width (100vw) */
height: 60%;
}
.container
{
width: 80%; /* 80% of parent width (60% of 100vw) */
height: 80%;
}
<div class="containerBox">
<section class="container">
<h1>Centered text in 80% of 60% box</h1>
</section>
</div>
<section class="centered_text">
<h1>Centered text in 100vh box</h1>
</section>
I am trying to make a basic responsive structure for a website with CSS. So far I have managed to make three column divs, a menu, a sidebar and one for content.
What I would like to achieve now is to have the menu and the sidebar to be 100% of the viewport height and fixed so that the content div is "scrollable" but the menu and the sidebar stays on top no matter how much content there is in the col content column. Naturally, I do not want this to happen in the media query though.
How can I achieve this most efficiently with CSS. Do I have to restructure the divs in HTML or is there any way to achieve this with CSS?
/* SECTIONS */
.section {
clear: both;
}
/* COLUMN SETUP */
.col {
display: block;
float: left;
}
/* GRID OF THREE */
.menu {
width: 33%;
background-color: #98D2ED
}
.sidebar {
width: 33%;
background-color: #D3ADAD
}
.content {
width: 33%;
background-color: #C9E4D1
}
/* GO FULL WIDTH BELOW 480 PIXELS */
#media only screen and (max-width: 480px) {
.menu {
width: 100%;
}
.sidebar {
width: 100%;
}
.content {
width: 100%;
}
}
<div class="section">
<div class="col menu">
<p>
Menu
</p>
I want this cloumn to be fixed and full height of the viewport when the screen size is above 480px.
</div>
<div class="col sidebar">
<p>
Sidebar
</p>
I want this cloumn to be fixed and full height of the viewport when the screen size is above 480px.
</div>
<div class="col content">
Content
</div>
</div>
What I am trying to achieve:
You can use flexbox, either for known/unknown width and height elements, The key is to set the content area to overflow:auto, and switch the flex-direction to column in media queries.
jsFiddle
html, body {
height: 100%;
}
body {
margin: 0;
display: flex;
}
.content {
flex: 1;
overflow: auto;
}
.menu { background: grey; }
.sidebar { background: silver; }
#media (max-width: 480px) {
body {
flex-direction: column;
}
}
<div class="menu">Menu</div>
<div class="sidebar">Sidebar</div>
<div class="content">
<!-- scroll test -->
<div style="height:1000px;">Content</div>
</div>
Or, the traditional way to set the menu and sidebar to position:fixed.
jsFiddle
html, body {
height: 100%;
margin: 0;
}
body {
margin-left: 200px;
}
.menu, .sidebar {
position: fixed;
top: 0;
height: 100%;
overflow: auto;
}
.menu {
left: 0;
width: 100px;
background: grey;
}
.sidebar {
left: 100px;
width: 100px;
background: silver;
}
.content {
overflow: auto;
}
#media (max-width: 480px) {
body {
margin: 100px 0 0;
overflow: hidden;
}
.menu, .sidebar {
left: 0;
width: 100%;
height: 50px;
}
.sidebar {
top: 50px;
}
.content {
height: calc(100% - 100px);
}
}
<div class="menu">Menu</div>
<div class="sidebar">Sidebar</div>
<div class="content">
<!-- scroll test -->
<div style="height:1000px;">Content</div>
</div>
As I understand, you want your .menu and .sidebar to be stuck to the screen in one place and have the content be scrollable. And add some more code to other things as well, I know that sounds vague, but it would be a waste of time to write everything down, as I have edited you I finished copy, and I have notes that explain all my changes (and the reasons for doing so) in the code below.
I removed the floats and their classes, as I believe those are not necessary, and that the floats do more harm than good. As well as moved the .content to be in the middle column (between .menu and .sidebar). However, if you need to, feel free to change any or al of these things back.
Here's the updated code: (and here's a JSFiddle: JSFiddle)
I know that .menu has a weird space above it (when running the snippet and the JSFiddle), but I have it live on my website here, and it behaves perfectly fine, and uses the same code.
* {
margin: 0px; /* Added to remove margin from everything */
padding: 0px; /* Added to remove margin from everything */
}
.section, .menu, .sidebar, .content {
display:inline-block !important; /* Added so they will line up next to each other */
}
.section {width:100%;} /* Pretty self explanatory, added to set ".section" to a width of 100% */
/* GRID OF THREE */
.menu {
width: 33%; /* Was already here */
background-color: #98D2ED; /* Was already here */
height:100vh; /* Makes it be 100% of the Viewport Height, or 100% of the browser window height */
position: fixed; /* Makes it stay "fixed" to one place on the screen */
}
.sidebar {
width: 33%; /* Was already here */
background-color: #D3ADAD; /* Was already here */
position:absolute; top:0px; left: 67%; /* To make the element in the right place, add the width of "menu" and "content" */
height:100vh; /* Makes it be 100% of the Viewport Height, or 100% of the browser window height */
position: fixed; /* Makes it stay "fixed" to one place on the screen */
}
.content {
width: 34%; /* Was already here, but changed it to 34 to make the website fill the page */
background-color: #C9E4D1; /* Was already here */
position:absolute; top:0px; left:33%; /* To make the element in the right place, make this the width of "menu" */
}
/* The CSS below this was already here */
/* GO FULL WIDTH BELOW 480 PIXELS */
#media only screen and (max-width: 480px) {
.menu { width: 100%; }
.sidebar { width: 100%; }
.content { width: 100%; }
}
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<div class="section">
<div class="menu">
Menu
</div>
<div class="content">
Content
</div>
<div class="sidebar">
Sidebar
</div>
</div>
Really Hope that helped!
I've been having trouble finding this one.
I have a div that is centered in the body margin: 0 auto;.
It contains multiple divs. I want it to expand to the width of it's widest child width: auto;
The problem is I want to have one of the child div's aligned on the right, however this expands my parent to 100%. How would I accomplish this without a fixed width for the parent?
You could do what you are after by setting the wrapper div to inline-block, and setting text-align: center on its parent (instead of using margin: 0 auto;). Here is an example: http://jsfiddle.net/joshnh/6Ake5/
Here is the HTML:
<div class="wrapper">
<div class="foo"></div>
<div></div>
<div></div>
</div>
And the CSS:
body {
text-align: center;
}
div {
border: 1px solid red; /* To see what is going on */
}
.wrapper {
display: inline-block;
text-align: left; /* Resetting the text alignment */
vertical-align: top; /* Making sure inline-block element align to the top */
/* Inline-block fix for IE7 and below */
zoom: 1;
*display: inline;
}
.wrapper div {
float: left;
height: 200px; /* To see what is going on */
margin: 10px; /* To see what is going on */
width: 200px; /* To see what is going on */
}
.foo {
border-color: blue; /* To see what is going on */
float: right;
}
I'm designing a web page at: http://trackstudent.pt/index.php
I ran into a bug that I can't fix, if you notice after inspecting the link, the <p></p> is getting pushed to the right, by a measure and I can't figure out why. All the divs have margin:0, padding:0 and there's enough space so that that element should fit that space.
Amazingly in the 2nd row of content that thing doesn't happen.
I have css from 1kbgrid (960px grid with 12 columns):
// Grid Cell (column)
.column {
margin: 5px 10px;
overflow: hidden;
float: left;
display: inline;
}
// Grid row
.row {
width:960px;
margin: 0px auto;
overflow: hidden;
}
// Nested rows
.row .row {
margin: 0px -10px;
width: auto;
display: inline-block;
}
In HTML I have:
<body> -> width: 100%
<div id="container"> -> width: 100%
<div id="mainWrapper"> -> width: 960px
<section class="row"> -> width: 960px
EDIT: With clear:both; in #wrapperMain it works! But I still don't get why this is happening, because the header has height:40px and the logo too, so there shouldn't be any area from the logo expanding pass the header. Anyone can offer insight?
Thank you in advance.
It's from the floating logo. Simply add the following CSS:
#wrapperHeader {
overflow:hidden; /* Forces header to contain elements */
}
Or:
#wrapperMain {
clear:both; /* Pushes wrapper down until it's past bottom of header elements */
}
I have a container div with a floating left-hand navigation pane and a content pane to the right:
<div id="header"></div>
<div id="container">
<div id="leftnav"></div>
<div id="content"></div>
<div class="clearfix"></div>
</div>
<div id="footer"></div>
CSS:
body
{
text-align: center; /* IE center div fix */
}
#container
{
width: 800px; /* site width */
background-color: red; /* so I can see it */
text-align: left; /* undo text-align: center; */
margin: 0 auto; /* standards-compliant centering */
}
#leftnav
{
float: left;
width: 200px;
}
#content
{
height: 100%;
width: 600px;
margin-left: 200px;
background-color: green; /* so I can see it */
}
.clearfix { clear: both; }
The #container div stretches to the full height of the floating #leftnav div, but the contained #content div does not stretch to 100% of the height. I've read elsewhere that this is due to the parent #container not having a specified height (defaults to auto) and therefore the 100% is not based on that container; however, I can't specify the height because the left navigation pane height isn't constant.
How can I get the #content div to be 100% of the height of the #container div when the #container div's height is defined by the floating #leftnav?
This is similar to the 3 column liquid "holy grail" CSS layout that has been plaguing people for years (though has been solved in the past couple years, though many of the solutions required browser hacks or Javascript to function).
I'd highly suggest you not reinvent the wheel here as it is difficult to get CSS to perform exactly as you're describing. Here is a good resource for this layout and many other similar liquid layouts:
http://matthewjamestaylor.com/blog/perfect-2-column-left-menu.htm
The easy way would be to use JS to set the height of #content to the height of #leftnav. You can use faux columns on #container and make a slice/gif of the green background and repeat it vertically on #container along with the red however you have it but I'm not sure if it fits your needs.
try this CSS
body
{
text-align: center; /* IE center div fix */
}
#container
{
width: 800px; /* site width */
background-color: red; /* so I can see it */
text-align: left; /* undo text-align: center; */
margin: 0 auto; /* standards-compliant centering */
}
#leftnav
{
float: left;
width: 200px;
}
#content
{
height: 100%;
width: 600px;
background-color: green; /* so I can see it */
float:right;
}
.clearfix { clear: both; }
I would also suggest using a line break with a clear both rather than a div.