I want to have three columns, a 1000px middle column that is centered to the page and then a column on the left and right that takes up the remaining width.
I basically want something that looks like this:
Where the wrapper is 1000px and the two side spaces are half of the remaining total space.
You can easily centre an element with margin: 0px auto. This will leave a space on the left and right of the element. If the element is inside another which takes up the entire width, then a background can be placed and centred inside it.
An example might be:
<div id="container">
<div id="content"></div>
</div>
Then the CSS would look like:
#container {
width: 100%;
/* Background properties go here. */
}
#content {
margin: 0px auto;
width: 1000px;
}
It wouldn't be possible to put content either side of the #content div.
For a pure CSS approach, try something like http://jsfiddle.net/hKB9T/2/ (make sure to widen your browser window so that the "results" box is ~1200px wide or so)
it isn't complete (depending on your requirements, you may need to fiddle with the position of the .center element) but it should put you on the right track.
<div id="page">
<div class="center">center column</div>
<div class="leftcol">
<div class="inner">left column</div>
</div>
<div class="rightcol">
<div class="inner">right column</div>
</div>
</div>
and
.leftcol, .rightcol {
width: 50%;
float: left;
}
.leftcol .inner {
margin-right: 500px;
height: 200px;
}
.rightcol .inner {
margin-left: 500px;
height: 200px;
}
.center {
width: 1000px;
margin: 0 auto -200px auto;
background-color: #eee; /* just for illustration */
}
so lets say your "page width" is 1024px in width. I would do something like this --
html:
<div id="page_content">
<div id="element_left">
</div>
<div id="centered_element">
</div>
<div id="element_right">
</div>
</div>
css:
#page_content { width:1024px; margin:0px auto 0px auto;}
#element_left { width:12px; float:left;}
#element_right { width:12px; float:left;}
#centered_element { width:1000px; margin:0px auto 0px auto; float:left;}
Related
So I made a mockup of what I want to do so it's easier to understand. I have an outer div that takes up 33% of the page and an inner div that has a width of 50% (of the outer div). The outer div has a "text-align: center;" style so the space on each side of the inner div is 25% the width of the outer div. I want to offset the inner div from the top of the page so it is the same distance from the top as from the sides. Most of the responses to similar questions advise using jquery, but I would prefer to use a css-only solution if there is one. How can i do this?
I apologize if this is a basic question. I am inexperienced with frontend and I couldn't find any way of setting CSS attributes equal to the values of other attributes.
Any help would be appreciated!
The CSS:
.side{
background-color: #ECEFF1;
height: 100%;
width: 33%;
text-align: center;
}
.profpic{
width: 50%;
border-radius: 50%;
position: relative;
}
The Html:
<div class="side mdl-shadow--4dp" >
<image class="profpic mdl-shadow--4dp" src="/profpic.jpg"></image>
</div>
<div class="content">
</div>
This can be achieved simply by just putting these two CSS rules: padding: 25% and width: 50%. That will center the image equally from the left, top, and right. Here is the code I used:
HTML
<div id="side">
<image src="http://placehold.it/120x120&text=image1" id="box">
</div>
CSS
#side {
width: 33.33%;
height: 1000px;
background-color: #474747;
}
#box {
width: 50%;
padding: 25%;
}
Then a JSFIDDLE if you would like.
EDIT
To be a bit more question relevant, this is what the OP's CSS would be:
.side{
background-color: #ECEFF1;
height: 100%;
width: 33%;
}
.profpic{
width: 50%;
border-radius: 50%;
padding: 25%;
}
As of CSS3 You can use the units vw and vh to achieve this.
vh refers to the height of the viewport, vw to its width.
Your div is taking 33% of width, while the inner box 50% of that. Therefore, your margin on the left and right will be approx 25% of the outer divs width.
So, what you need as a top-margin is 33% * 25% = 8.25% of the vw unit:
<div id="outer">
<div id="inner">
lorem ipsum
</div>
</div>
#outer{
background-color:red;
width:33vw;
margin:0;
padding-top:1px;
height:100vh;
}
#inner{
background-color:blue;
width:50%;
margin: 0 auto;
margin-top:8.25vw; // this is 25% of 33% width
padding-top:1px;
}
http://jsfiddle.net/xaLc4zd2/
Resize the windows and see, how the inner div retains its relative position.
This should serve as a reference for your need. The key is to apply box-sizing to the 33% container, and apply 25% margin to the element inside the container.
<div class="box">
<div class="innerbox"></div>
</div>
<div class="box">
<div class="innerbox"></div>
</div>
<div class="box">
<div class="innerbox"></div>
</div>
.box{
width: 33%;
min-height:400px;
background-color: #777;
display: inline-block;
box-sizing: border-box;
}
.innerbox{
width:50%;
margin:25%;
min-height:200px;
background-color: #444;
}
See this codepen example
I want to place divs next to each other. I dont't know number of divs, since this is dynamically created and changed. I would like to have one parent div which will have scrollbar in case there are many child divs (and their width is greater than parent).
Here's the jsFiddle example. So, basically I would like to have all this three columns, next to each other and with scrollbar on the bottom of parent div.
HTML:
<div class="content">
<div class="column">Column</div>
<div class="column">Column</div>
<div class="column">Column</div>
</div>
CSS:
content {
width: 100px;
background- color: #355E95;
overflow: visible;
}
.column {
width: 50px;
display: inline-block;
}
Add white-space:nowrap to your parent div.
FIDDLE
You would need to use a content div for the scroll and then a wrapper for the columns, adjusting the width of the wrapper to fit all 3 of your columns (150px in your example).
The column structure is made by floating div's left, but it will float to the width of your parent container, in this case your parent container is only 100px so we need to add a wrapper for them to fit inside.
If you also want a vertical scroll you will need to set a height to your container.
Jsfiddle: http://jsfiddle.net/tYnH3/
css:
.content {
width: 100px;
background-color: #355E95;
overflow: auto;
}
.content-wrapper {
width: 150px;
}
.column {
width: 50px;
float: left;
}
html:
<div class="content">
<div class="content-wrapper">
<div class="column">
Column
</div>
<div class="column">
Column
</div>
<div class="column">
Column
</div>
<div class="column">
Column
</div>
<div class="column">
Column
</div>
<div class="column">
Column
</div>
</div>
</div>
Fiddle: http://jsfiddle.net/bdssw/
use float:left;
.column {
width: 50px;
display: inline-block;
float:left;
}
Try the following JS fiddle
http://jsfiddle.net/jT6SW/1/
#wrapper
{
float: left;
height: 150px;
width: 250px;
margin: auto;
border: 1px black solid;
overflow-y: hidden;
overflow-x: scroll;
}
use width:auto;
.content {
width: auto;
background-color: #355E95;
overflow:scrolling;
position:fixed;
}
.column {
width: 50px;
float:left;
}
http://jsfiddle.net/XqSJG/6/
I have a webpage containing a centered container with content and I want to display a logo next to it.
The layout is as following: div - container. Where the container is centered and the div lef of the container needs to fill out the width left on the screen.
html, body {
height: 100%;
width: 100%;
}
#container {
width: 800px;
margin-left: auto;
margin-right: auto;
min-height: 100%;
}
<div id="container">
</div>
<div id="lef">
</div>
A jsfiddle with this code is available on http://jsfiddle.net/7QJQn/
This is the option that comes closed
http://jsfiddle.net/7QJQn/4/
I think that the best solution for doing something like this is just using javascript / jQuery.
Depending on which browsers you wish to support, you could use calc().
Basically, you want 50% of the viewport width (50vw) minus half of width of #container (so you're measuring from the center of your #container and you use half of all the values) - I'm assuming that you're OK with absolute positioning #lef to the viewport to keep it to the right?
CSS (fiddle here):
#lef {
background-color:yellow;
width:calc(50vw - 100px);
height:20px;
position:absolute;
right:0;
top:0;
}
Add this to your css:
#lef{
float:left
}
And change the order of the divs in the html, like this:
<div id="lef"></div>
<div id="container"></div>
First of all, you should wrap your markup in a wrapper div so elements stay tight.
I made some changes, take a look:
<div id="wrapper">
<div id="lef">
</div>
<div id="container">
</div>
</div>
And the css:
html, body {
height: 100%;
width: 100%;
}
#wrapper{
width: 360px;
}
#container {
width: 200px;
margin-left: auto;
margin-right: auto;
height: 100px;
background-color:red;
}
#lef {
background-color:yellow;
width: 160px;;
height:100px;
float: left;
}
Example
If using flexbox is an option, you can do this with the flex-grow property:
With the following markup
<div class="main-row">
<div class="filler"></div>
<div class="row-content">Fixed width centered div</div>
<div class="filler"></div>
</div>
you need to set flex-grow: 1 on the filler divs. See this fiddle.
This question already has answers here:
2 column div layout: right column with fixed width, left fluid
(8 answers)
Closed 8 years ago.
Im searching for a way to have 2 divs as columns where div on right has a fixed width and div on left fill remaining space.
Does anyone happen to know if this can be done?
My attempt (renders block2 underneath block1):
<style>
.block1 {
width: auto;
height: 200px;
background-color: green;
}
.block2 {
float: right;
height: 200px;
width: 200px;
background-color: red;
}
</style>
<div class="block1">test1</div>
<div class="block2">test2</div>
You can do it like this:
HTML:
<div class="right">right</div>
<div class="left">left</div>
CSS:
.left{
background:red;
}
.right{
float:right;
width:200px;
background:green
}
Check this live example http://jsfiddle.net/QHTeS/2/
Float Both of the elements left:
<style>
.block1 {
width: auto;
height: 200px;
float: left;
background-color: green;
}
.block2 {
float: left;
height: 200px;
width: 200px;
background-color: red;
}
</style>
<div class="block1">test1</div>
<div class="block2">test2</div>
You should wrap them in a container as well to prevent messing up the rest of your layout. :)
http://jsfiddle.net/tcFjN/
That was wrong!
Use display: table; on parent and display: table-cell; on children:
<div id="wrapper">
<div class="block1">test1</div>
<div class="block2">test2</div>
</div>
#wrapper
{
display: table;
width: 100%;
}
.block1 {
width: auto;
height: 200px;
display: table-cell;
background-color: green;
}
.block2 {
display: table-cell;
height: 200px;
width: 200px;
background-color: red;
}
http://jsfiddle.net/tcFjN/1/
This is my solution without floats. The only caveat is that I need to use a wrapper. So, if the desired HTML is
parent (has a border, margin, padding,...)
left (fixed width)
right (variable width, fill the entire space)
I must rewrite it as
parent (has a border, margin, padding,...)
wrapper (has no styling)
left (fixed width)
right (variable eidthm, fill the entire space)
My HTML is
<div style="border:1px solid black; background:red; margin:10px; padding:10px;" >
<div style="">
<div style="display:table-cell; padding:10px; min-width:100px; max-width:100px;background:green;">Left</div>
<div style="display:table-cell; padding:10px; width:100%; background:yellow;">Main content</div>
</div>
</div>
The main points here are:
No use display:table because then we can not set the border
The use of min-width, max-width
The use of width:100%
Check this jsfiddle
Start out with a container <div> (#container) that holds both the left and right <div>s. Float one <div> to the right and give it a specific width (320px in my example). Then give the other <div> an absolute position starting at the absolute left (0px) and ending at the left edge of the <div> on the right (320px).
If you adjust the width of #container, the right <div> will remain fixed at 320px while the left <div> will expand to fill whatever the remaining area is.
Hi Guys I have this site:
http://www.ryansammut.com/orijen/
Basically so far I managed to make the top part strech as a background, now I need to make the other parts too. I'm not sure how to do it, so I'm asking for ideas how this would be done best, keeping the positioning all relative and the background image would adjust according to the needed content area.
PS. This is only needed for resolutions greater than 1280px, so zoom out if you need to see what's happening.
You can not stretch those elements because they are contained in a div named 'wrapper', which has a maximum width of 1280px.
add the following properties to : header, contentbackground, and footer:
margin-left:auto;
margin-right:auto;
this will make sure the elements are centered.
then remove the width property from #wrapper, and add the background to it so it reads as follows :
#wrapper {
position: relative;
margin: 0 auto;
top: 0px;
padding: 0px;
background-image: url(../images/contentBG.png);
}
However, now we won't see the horizontal stretch of the header anymore, so we need to move #header above #wrapper.
<div id="header">
...
</div>
<div id="wrapper">
...
</div>
Don't use tables, use DIVs only.
No need to include FlowPlayer script two times.
I dont see you use JQuery (no need to include that).
Replace Dreamweaver's rollover images with proper CSS:
.item {background: image.jpg}
.item:hover {background: image_rollover.jpg}
Get sprite images (you can read here: http://css-tricks.com/css-sprites/)
As the original question... you have to use two DIVs for each "row", like this:
#header_wrapper {
float: left;
width: 100%;
background: header_backgroud.jpg;
}
#menu_wrapper {
float: left;
width: 100%;
background: menu_backgroud.jpg;
}
#content_wrapper {
float: left;
width: 100%;
background: content_backgroud.jpg repeat center top;
}
.wrapper {
margin: 0 auto;
width: 1260px;
}
<div id="header_wrapper">
<div class="wrapper">
--- header content ---
</div>
</div>
<div id="menu_wrapper">
<div class="wrapper">
--- menu content ---
</div>
</div>
<div id="content_wrapper">
<div class="wrapper">
--- page content ---
</div>
</div>
You need to change the structure to something like this:
<div id="header">
<div>
<ul>Nav</ul>
</div>
</div>
<div id="mainContent">
<div>Content</div>
</div>
<div id="footer">
<div>Content</div>
</div>
Then the CSS could look something like this:
div#header { width: 100%; background: black; }
div#header div { width: 600px; margin: 0 auto; background: url(...); }
div#mainContent { width: 100%; background: url(...); }
div#mainContent div { width: 600px; margin: 0 auto; }
div#footer { width: 100%; background: black; }
div#footer div { width: 600px; margin: 0 auto; }
It is fast written, hope you can see the idea? I can't see why you would go with position absolute or relative. Use margin: 0 auto; to center divs instead :)