Desired layout:
So I have this layout which should be centered and should not exceed 1440px, it is split in 2 parts, let's call them 'sider' and 'content'. sider is max-width: 560px and container is max-width: 880px, together 1440px.
Makes sense right? Content can be infinitely long AND scrollbar ABSOLUTELY has to be on the side of the screen.
So the million dollar question is how do I center these guys?
Here is a replicated, simplified code of my layout: https://codepen.io/andrisladuzans/pen/yLOajrX
HTML:
<div class="content-body">
<div class="sider-container">
<div class="sider-content">
sider
</div>
</div>
<div class="content-container">
<div id="populate" class="content">
</div>
</div>
</div>
CSS:
body{
padding:0px;
margin:0px;
}
.content-body{
background-color: skyblue;
height: 90vh;
width:100%;
overflow: hidden;
display:flex;
}
.sider-container{
background-color: pink;
width:40%;
height: 100%;
overflow:scroll;
display:flex;
justify-content:flex-end;
}
.content-container{
background-color: #ddd;
width:60%;
height:100%;
overflow:scroll;
}
.sider-content{
background-color:magenta;
height: 100%;
width: 100%;
max-width: 560px;
}
.content{
background-color: lime;
width: 100%;
max-width: 880px;
}
JS helper:
const content = document.getElementById("populate")
const populate = () => {
for(let i = 0; i<50; i++){
const textElement = document.createElement("div");
textElement.className = "textElement";
textElement.innerText = 'some text';
content.appendChild(textElement)
}
}
populate();
The problem with my approach is that, on larger screens, you can clearly tell that one side is growing larger than the other and is not centered.
edit:
just realised, stackoverflow, has exact same layout i'm looking for.
solution:
a bit hacky, but here's what i finally managed to come up with (not responsive) https://codepen.io/andrisladuzans/pen/MWyjxLY
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
body {
padding: 0px;
margin: 0px;
height: 100%;
}
.content-body {
background-color: skyblue;
width: 100%;
height: 100vh;
overflow: hidden;
}
.container {
background-color: pink;
display: block;
width: 100%;
height: 100%;
overflow: scroll;
}
.inner-wrapper {
max-width: 1000px;
margin: 0 auto;
min-height: 100%;
border: 1px solid green;
display: flex;
position: relative;
}
.sider-container {
position: fixed;
z-index: 1;
background-color: magenta;
height: 100vh;
overflow: scroll;
width: 300px;
}
.sider-content {
background-color: magenta;
}
.content-container {
background-color: #ddd;
border: 1px solid midnightblue;
min-height: 100%;
width: 100%;
display: flex;
}
.content-spacer {
width: 300px;
height: 100%;
flex-shrink: 0;
}
.content {
background-color: lime;
flex: 1;
margin-left: auto;
}
</style>
</head>
<body>
<div class="content-body">
<div class="container">
<div class="inner-wrapper">
<div class="sider-container">
<div id="spacer" class="sider-content"></div>
</div>
<div class="content-container">
<div class="content-spacer"></div>
<div id="populate" class="content"></div>
</div>
</div>
</div>
</div>
<script>
const content = document.getElementById("populate");
const spacer = document.getElementById("spacer");
const populate = (element, text) => {
for (let i = 0; i < 150; i++) {
const textElement = document.createElement("div");
textElement.className = "textElement";
textElement.innerText = text;
element.appendChild(textElement);
}
};
populate(spacer, "spacer");
populate(content, "content");
</script>
</body>
</html>
The problem with my approach is that, on larger screens, you can clearly tell that one side is growing larger than the other and is not centered.
Not in my experience. At least from what I can tell from your codepen, everything works like you'd expect. Here is an image that shows that when I expand the window way past 4K size the container is still perfectly centered. Perhaps there is something on your browser or local machine causing a strange issue. Have you tried viewing your code on a different browser/computer?
Related
hello i want to make my site responsive.I have divide body in 4 divs. Every 2 divs has the 100% of the screen and the other two has margin-top : 50%;. Now i want everytime max-width = 800px, I want every div to has the fullscreen and the user scroll down to see the other divs. My site is https://frontjim.github.io if you want to see it. thanks is advance and sorry for my bad english.
i have used this but it didnt work
#media screen and (max-width: 800px) {
body,
html {
margin: 0;
padding: 0;
height: 100%;
max-height: 100vh;
display: flex;
flex-direction: row;
flex: 1 1 auto;
min-height: 0;
}
}
.back {
display: flex;
flex-direction: column;
}
.navbar {
width: 100vw;
height: 100vh;
}
.mater {
width: 100vw;
height: 100vh;
}
.cb {
width: 100vw;
height: 100vh;
}
.scroll {
width: 100vw;
height: 100vh;
}
You can have a general container that has child elements. This container has width: 100vw and height: 100vh, and display: flex. You don't need to add flex-direction: row since it's the default.
The children will have width: 100%.
Snippet:
* {
margin: 0;
padding: 0;
}
.flex {
width: 100vw;
height: 100vh;
display: flex;
}
.div1 {
background-color: red;
width: 100%;
}
.div2 {
background-color: blue;
width: 100%;
}
.div3 {
background-color: green;
width: 100%;
}
.div4 {
background-color: grey;
width: 100%;
}
<div class="flex">
<div class="div1"></div>
<div class="div2"></div>
</div>
<div class="flex">
<div class="div3"></div>
<div class="div4"></div>
</div>
Use CSS Grid, it can get the job done easily
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Responsive Grid</title>
</head>
<body>
<div class="main">
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
<div class="div4"></div>
</div>
</body>
</html>
CSS:
* {
margin: 0;
padding: 0;
}
.main {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 100vh 100vh;
}
.div1 {
background-color: lightgreen;
}
.div2 {
background-color: lightseagreen;
}
.div3 {
background-color: coral;
}
.div4 {
background-color: lightblue;
}
#media screen and (max-width: 800px) {
.main {
grid-template-columns: 1fr;
grid-template-rows: 100vh 100vh 100vh 100vh;
}
}
Live view: https://codepen.io/zgheibali/pen/ZEWWVvr
I hope that my answer was helpful :)
change the design.use like this:
in html::
<div class='flexyy'>
<div class='div1'>....</div>
<div class='div2'>....</div>
<div class='div3'>....</div>
<div class='div4'>....</div>
</div>
in css::
div1{
width:100%;
}
div2{
width:100%;
}
div3{
width:100%;
}
div4{
width:100%;
}
#media(min-width:800px){
.div1,.div2,.div3,.div4{
width:auto;
}
.flexyy{
display:flex;
flex-direction:row;
flex: 0.5 1 auto;
flex-wrap:wrap;
}
}
I have a rectangle area on my page that needs to be filled with a square in the center. This rectangle can be both horizontal and vertical. This means that the plethora of existing questions based on making a square from just the width of a containing box don't work. [1] [2]
This square can also change dynamically and this approach doesn't resize the square. I'd also like to not use JavaScript or JQuery if possible. This is the only thing that I'd use JQuery for.
Below you can see what the code should do at the beginning, but when you resized the box it doesn't resize the square.
/* based on https://stackoverflow.com/a/5445536*/
$('.body').resizable();
var containingBlock = $('.box-rect');
var cmin = Math.min(containingBlock.width(), containingBlock.height());
$('#box-square').css({
'height': cmin+'px',
'width': cmin+'px'
});
.body{
width: 200px;
height: 100px;
}
.box-rect {
width: 100%;
height: 100%;
min-width: 50px;
min-height: 50px;
display: flex;
justify-content: center;
align-items: center;
/* example bounding box */
border: 1px solid gray;
}
.box-square {
width: 50px; /* min(box-rect.height, box-rect.width) */
height: 50px; /* box-rect min */
}
/*
The following is using a mix between the following two answers:
https://stackoverflow.com/a/6615994
https://stackoverflow.com/a/20117454
*/
.box-reset {
position: relative;
height: 100%;
width: 100%;
}
.box-reset:before {
content: "";
display: block;
padding-top: 100%;
}
.box {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
/* example gray box */
height: 100%;
background-color: gray;
}
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class='body'>
<div class="box-rect">
<div class="box-square">
<div class="box-reset">
<div class="box">
Not working at all
</div>
</div>
</div>
</div>
</div>
<br />
<div class='body'>
<div class="box-rect">
<div class="box-square" id="box-square">
<div class="box-reset">
<div class="box">
Fills to the bounds of the rectangle on load.
</div>
</div>
</div>
</div>
</div>
To be completely clear:
The first example above doesn't resize the gray square at all.
The second example resizes the gray square to the bound of the rectangle. (What I want)
However, it doesn't resize the gray square to the bound of the rectangle when resizing the containing block.
I want to resize the square to the bound of the rectangle. Like in the second example, when I resize the containing block.
I would prefer to do this all in pure CSS.
You may look at the jQuery example. It's better to understand how it worked.
I made a similar example on jsfiddle.
* {
margin: 0;
padding: 0;
box-sizing:border-box;
}
#resizable {
display:flex;
justify-content:center;
align-items:center;
width: 150px;
height: 150px;
min-width:100px;
min-height:100px;
}
#resizable h3 {
display:flex;
justify-content:center;
align-items:center;
height: 100px;
width: 100px;
text-align:center;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Resizable - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#resizable" ).resizable();
} );
</script>
</head>
<body>
<div id="resizable" class="ui-widget-content center-center">
<h3 class="ui-widget-header">Resizable</h3>
</div>
</body>
</html>
<!-- Reference link https://jqueryui.com/resizable/ -->
Removed the static height and width that was getting applied from js and added
width: 50%; and height: 50%; to .box-square
$('.box-rect').resizable();
.body{
width: 400px;
height: 200px;
}
.box-rect {
width: 100%;
height: 100%;
min-width: 100px;
min-height: 100px;
display: flex;
justify-content: center;
align-items: center;
/* example bounding box */
border: 1px solid gray;
}
.box-square {
width: 100px;
height: 100px;
/* width: 100px; min(box-rect.height, box-rect.width) */
/* height: 100px; box-rect min */
}
/*
The following is using a mix between the following two answers:
https://stackoverflow.com/a/6615994
https://stackoverflow.com/a/20117454
*/
.box-reset {
position: relative;
height: 100%;
width: 100%;
}
.box {
height: 100%;
background-color: gray;
}
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class='body'>
<div class="box-rect">
<div class="box-square">
<div class="box-reset">
<div class="box">
</div>
</div>
</div>
</div>
</div>
Why the container is the outside of body?
I want to content have 100% width of all body, not body and header.
<!DOCTYPE html>
<html>
<head>
<title>dsfsd</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="styl.css">
</head>
<body>
<div id="doc">
<header class="topbar"></header>
<div class="container"></div>
</div>
</body>
</html>
This is my css:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
}
#doc {
width: 100%;
height: 100%;
background: #f4f4f4;
}
.topbar {
width: 100%;
height: 48px;
background: #e0e0e0;
}
.container {
margin: 0 auto;
width: 960px;
height: 100%;
background: red;
}
Please, help mi with this problem. :)
If you want your container width to be the width of the body change .container to width:100%; instead of giving it a px width. If that is what you are asking.
I'd like to create a layout that acts like a titlebar from iphone:
I tried to put together the following example, but I'm not sure how to get the middle column to expand in width so it uses all left over space. I can do this in javascript at runtime, but wondering if there's a css solution. Here it is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style type="text/css">
html, body {
margin: 0px;
padding: 0px;
}
#parent {
background-color: #eee;
width: 100%;
}
#colLeft {
background-color: #ff8b8b;
height: 48px;
display: inline;
}
#colMiddle {
background-color: #c9ffc3;
height: 48px;
display: inline;
text-align: center;
}
#colRight {
background-color: #c3d0ff;
height: 48px;
display: inline;
}
</style>
</head>
<body>
<div id="parent" style="width:100%">
<div id="colLeft">left</div>
<div id="colMiddle">title</div>
<div id="colRight">right</div>
</div>
</body>
</html>
Thank you
A simpler way to approach this is to use an HTML structure like this:
<div id="parent" style="width:100%">
<div id="colLeft">left</div>
title
<div id="colRight">right</div>
<div>
Float the left and right divs to the appropriate sides and set the text align on the parent to center. Any styles from the middle div for text, etc can be applied to the parent.
I'm a bit late in the answer, but see if this is more like what you need, without the need to sacrifice the middle <div>:
You'll have to float the 3 columns and make the inner column have a 100% width. Then, setting the inner column's margin (based on left and right columns' widths), you achieve the result.
Have a look at this fiddle: http://jsfiddle.net/fabio_silva/d7SFJ/
The HTML/CSS:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style type="text/css">
html, body {
margin: 0px;
padding: 0px;
}
#parent {
background-color: #eee;
width: 100%;
}
#colLeft {
background-color: #ff8b8b;
height: 48px;
width: 100px;
float: left;
}
#colMiddle {
height: 48px;
text-align: center;
float: left;
width: 100%;
margin-left: -100px; /* negative colLeft width */
margin-right: -150px; /* negative colRight width */
}
#colMiddleInner
{
margin-left: 100px;
margin-right: 150px;
height: 48px;
background: #c9ffc3;
}
#colRight {
background-color: #c3d0ff;
height: 48px;
width: 150px;
float: left;
}
</style>
</head>
<body>
<div id="parent" style="width:100%">
<div id="colLeft">left</div>
<div id="colMiddle">
<div id="colMiddleInner">
title
</div>
</div>
<div id="colRight">right</div>
</div>
</body>
</html>
You need to define the widths...
#colLeft {
background-color: #ff8b8b;
height: 48px;
width: 50px
display: inline;
}
#colMiddle {
background-color: #c9ffc3;
height: 48px;
display: inline;
width: auto;
text-align: center;
}
#colRight {
background-color: #c3d0ff;
height: 48px;
width: 50px;
display: inline;
}
Note: default value for width is auto.
Here's a simple puzzle that's been frustrating me for a while today:
Consider this page markup:
<head>
<style type="text/css">
#wrapper { overflow: hidden; }
#content { width: 750px; height: 100px; background: orange; }
</style>
</head>
<body>
<div id="wrapper">
<div id="content">Foo bar</div>
</div>
</body>
How can I get div#content centered in the page regardless of viewport width?
I've tried a variety of tricks (including text-align: center; display: inline-block;) and absolute positioning, but with all of them the div#content is left-aligned when the browser window is brought under 750px in width.
I've seen a few high-profile websites do this in the past. For example on Apple.com when they advertised the new retina iPad: the iPad pictured was a very wide image that extended past the main page area (note it was not a CSS background image of the <body> element), but it didn't cause scrolling when the browser window only fit the main page content. Unfortunately I can't seem to find any existing sites that do this so I can't find a reference.
Thanks.
Is this it? Take a look -> http://jsfiddle.net/joplomacedo/CkvuG/
HTML
<div id="page">
<div id="main">
<div id="extended-out"><img src="http://myfreeipad.us.com/wp-content/uploads/2011/10/ipad.png" /></div>
</div>
</div>
CSS
#page {
overflow: hidden;
min-width: 200px; /*same as #mains width*/
}
#main{
position: relative;
height: 500px;
width: 200px;
margin: 0 auto;
background-color: lightgreen;
}
#extended-out {
height: 200px;
margin: 0 -100px;
background: indianred;
border: 1px solid red;
}
#extended-out img {
width: 100%; height: 100%;
}
http://jsfiddle.net/CNNcV/
<head>
<style type="text/css">
#wrapper { overflow: hidden; }
#content { width: 750px; height: 100px; background: orange;
margin:0px auto;
width:100%;}
</style>
</head>
<body>
<div id="wrapper">
<div id="content">Foo bar</div>
</div>
</body>
Is that what you're looking for?
Add margin: auto to this,
#content { width: 750px; height: 100px; background: orange; margin: auto}