Make elements sticky to their parent container - html

I am attempting to create the following layout:
As you can see when I scroll, the alignment of the 1st and 3rd items has scrolled with the content. I need a way to get the first and third items to stay sticky to their respective sides. I also need this solution to be responsive as the container scales with screen size.
#container {
height: 300px;
width: 50%;
position: relative;
overflow: auto;
border: solid 1px black;
}
.item {
height: 500px;
width: 100px;
z-index: 5;
position: absolute;
background-color: red;
}
.item:nth-child(2) {
width: 800px;
z-index: 2;
background-color: green;
}
.item:nth-child(3) {
right: 0;
}
<div id="container">
<div class="item">&nbsp</div>
<div class="item">&nbsp</div>
<div class="item">&nbsp</div>
</div>
What I Am Actually Building
This is a generalized problem. The actual problem I am trying to solve is that of a custom-built HTML table. The left and right items (red sections) are going to be containers for static (sticky) columns. With that being said, I need the constraints of the problem to stay the same. Changing the width of the item:nth-child(2) to 100% will not work. Wrapping the contents inside of the item:nth-child(2) in another div and adding overflow: auto to that div will not work because then the scrollbar will not be shared amongst the entire container like it is in my example.

You can try like below:
#container {
height: 300px;
display:flex; /* added */
overflow: auto;
border: solid 1px black;
}
.item {
height: 500px;
width: 100px;
background-color: red;
flex-shrink:0; /* added */
}
.item:nth-child(2) {
width: 800px;
background-color: green;
}
.item:nth-child(1) {
left: 0;
top:0;
position: sticky;
}
.item:nth-child(3) {
right: 0;
top:0;
margin-left:auto; /* this is important*/
position: sticky;
}
<div id="container">
<div class="item">&nbsp</div>
<div class="item">&nbsp</div>
<div class="item">&nbsp</div>
</div>

.item:nth-child(2) {
width: 100%;
z-index: 2;
background-color: green;
}
You can make .item:nth-child(2) width is 100%

The main idea is use absolute wrapper to contain fixed child.
Also remember to add padding in long content.
body {
padding: 10px;
background: #eee;
display: flex;
justify-content: center;
}
.parent {
width: 100%;
height: 100px;
overflow-x: scroll;
position: relative;
border: 1px solid #000;
background: #0f0;
}
.fix {
position: fixed;
background: rgba(255, 0, 0, .3);
width: 100px;
height: 100px;
z-index: 2;
}
.fix-wrapper {
position: absolute;
top: 0;
}
.left {
left: 0;
}
.right {
right: 100px;
}
.content {
height: 100%;
width: 1000px;
padding: 0 100px;
}
<div class="parent">
<div class="fix-wrapper left">
<div class="fix">Left</div>
</div>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Pellentesque id nibh tortor id aliquet lectus proin. Viverra nam libero justo laoreet sit amet. Gravida cum sociis natoque penatibus et magnis dis parturient montes. At urna condimentum mattis pellentesque id nibh tortor id aliquet. Aenean pharetra magna ac placerat. Pellentesque habitant morbi tristique senectus et netus. Sed odio morbi quis commodo odio aenean. Ac turpis egestas maecenas pharetra convallis posuere morbi. Consectetur adipiscing elit ut aliquam purus sit amet luctus venenatis. Mollis aliquam ut porttitor leo a diam sollicitudin tempor. Amet mattis vulputate enim nulla aliquet porttitor lacus. Curabitur vitae nunc sed velit. Cursus eget nunc scelerisque viverra. Morbi tincidunt augue interdum velit euismod in pellentesque massa placerat. Etiam non quam lacus suspendisse faucibus.
</div>
<div class="fix-wrapper right">
<div class="fix">Right</div>
</div>
</div>

As I understand, you want the left and right sections to be fixed and the middle item to be responsive. If you consider a different approach to the problem. Here's my solution to it.
#container {
display: grid;
grid-template-columns: 200px 1fr 200px;
height: 80vh;
}
.item1 {
padding: 20px;
background-color: #f90;
}
.item2 {
flex: 1;
padding: 20px;
}
.item3 {
padding: 20px;
background-color: #936;
}
<div id="container">
<div class="item1">Left</div>
<div class="item2">Middle</div>
<div class="item3">Right</div>
</div>
You can add the position: sticky; as required to any item to make it sticky.

Related

Tons of whitespace on page for no reason

I'm writing a website for a restaurant (Not commercially) and need to know why there's so much whitespace to the right side of the page? Take a look
.top-container{
background-color: white;
background-image: url("images/charmlogoTrans.png");
padding: 170px;
text-align: center;
background-position: center;
background-repeat: no-repeat;
background-size:contain;
position: relative;
max-width: 100%;
}
.menuBar{
background-color: rgb(168, 123, 81);
padding: 30px 40px;
text-align: left;
z-index: 3;
}
.content{
padding:16px;
background-color:rgba(230,199,177,255);
margin-left: 2%;
margin-right: 50%;
margin-top: 2%;
border-radius:4px;
position: relative;
z-index: 1;
}
.imgContent{
position: absolute;
z-index: 2;
left: 50%;
width: 100%;
height:auto;
}
.exampleImg{
width: 32%;
height:auto;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.sticky + .content {
padding-top: 102px;
}
<body>
<div class="top-container"></div>
<div class="menuBar" id="menuBar"> The sticky bar</div>
<div class="mainContent">
<div class="imgContent">
<img class="exampleImg" src="Images/example.png" width="300" height="200">
</div>
<div class="content">
<h3>Vestibulum nulla turpis, hendrerit nec sodales vitae, congue at felis. Cras auctor ac quam sed
fermentum. Quisque libero est, aliquam ac lorem a, semper molestie mi. Cras suscipit eu erat eget
hendrerit.
</h3>
</div>
<img src="Images/example.png" width="300" height="200">
<img src="Images/example.png" width="300" height="200">
</div>
</body>
Please excuse me for such messy code, I'm still learning how to do some things. The measurements I'm still working on a bit, but any help is appreciated! If there's anything I've missed, or need to include, please let me know.
That's because imgContent is taking position: absolute, left: 50% so the element will overflow the page because you also set the width of imgContent to 100% there's 3 ways to fix this
set witdth to less than 100% something like 45% is enough
set Left to 0;
use flex box instead that's will be better in fact
Welcome to SO,
Try this!
It seems that the 50% left was adding the whitespace!
body {
margin: 0;
overflow-x: hidden;
width: 100vw
}
.top-container {
background-color: white;
background-image: url("images/charmlogoTrans.png");
padding: 170px;
text-align: center;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
position: relative;
max-width: 100vw;
}
.menuBar {
background-color: rgb(168, 123, 81);
padding: 30px 40px;
text-align: left;
z-index: 3;
}
.content {
padding: 16px;
background-color: rgba(230, 199, 177, 255);
margin-left: 2%;
margin-right: 50%;
margin-top: 2%;
border-radius: 4px;
position: relative;
z-index: 1;
}
.imgContent {
position: relative;
z-index: 2;
width: 100vw;
height: auto;
}
.exampleImg {
width: 32vw;
height: auto;
}
.sticky {
position: fixed;
top: 0;
width: 100vw;
}
.sticky+.content {
padding-top: 102px;
}
<body>
<div class="top-container"></div>
<div class="menuBar" id="menuBar"> The sticky bar</div>
<div class="mainContent">
<<div class="imgContent">
<img class="exampleImg" src="Images/example.png" width="300" height="200">
</div>
<div class="content">
<h3>Vestibulum nulla turpis, hendrerit nec sodales vitae, congue at felis. Cras auctor ac quam sed fermentum. Quisque libero est, aliquam ac lorem a, semper molestie mi. Cras suscipit eu erat eget hendrerit.
</h3>
</div>
<img src="Images/example.png" width="300" height="200">
<img src="Images/example.png" width="300" height="200">
</div>
</body>

Second floating child element aligns to next row of parent

I'm trying to resolve an issue I'm running into. I have a parent div with 3 floating children. I posted a previous question but that only partially resolved my problem. SEE HERE to reference.
So The problem is that the second child div's width is not known. The 1st and 2nd child div have fixed widths.
The 1st and 2nd child have fixed heights.
The 3rd Child div is set to display: block !important; clear: both; width: 100%; so that it goes to the next row and takes up the entire space of the parent div.
The issue is with the 2nd Child element. Since the width is not known and can change dynamically.
What I am trying to achieve is so that the 2nd child div expands horizontally but remains in line with the 1st child div.
In reference to my previous question. Using display: inline-table; does not allow the content to scroll if it overflows.
.container {
background: #ccc;
overflow: hidden;
float: left;
padding:5px;
}
.floating-box {
float: left;
width: 150px;
height: 75px;
margin: 5px;
border: 3px solid #73AD21;
}
.floating-box2 {
float: left;
width: auto;
height: 75px;
margin: 5px;
border: 3px solid #73AD21;
}
.floating-box3 {
float: left;
clear: both;
width: 100%;
height: 75px;
margin: 0;
background: magenta;
}
<div class="container">
<div class="floating-box">Floating box</div>
<div class="floating-box2">Floating box Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minus necessitatibus adipisci quisquam ducimus dolor fugit, officia perferendis harum temporibus laborum iure numquam, assumenda dignissimos neque, quod doloribus nihil autem dolores!</div>
<div class="floating-box3">Floating box</div>
</div>
JSFiddle
You can do it with the Flexbox, if I understand you correctly and this is what you want:
/* reset browser defaults */
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100%}
.flex-container {
display: flex; /* displays flex-items inline by default */
flex-wrap: wrap; /* enables wrapping */
}
.flex-container > div {border:1px solid}
.one {
flex: 0 0 200px; /* fixed width / adjust to your needs */
}
.two {
flex: 0 1 auto;
max-width: calc(100% - 200px); /* -200px of the .one fixed width */
word-wrap: break-word;
}
.three {
flex: 1 1 calc(100% - 200px); /* -200px of the .one fixed width */
}
.one, .two {height:100px} /* adjust to your needs / can also use just .one or just .two */
<div class="flex-container">
<div class="one">1</div>
<div class="two">2
..........fake content..........
</div>
<div class="three">3</div>
</div>
Thanks, #VXp. I referenced a CodePen and fiddled around with the flexbox. This is very close to the end result I'm trying to achieve.
body {
padding: 2em;
}
.wrapper {
display: flex;
flex-flow: row wrap;
font-weight: bold;
text-align: center;
}
.wrapper > * {
padding: 10px;
flex: 1 100%;
}
.aside-1 {
background: gold;
flex: 1 auto;
height: 155px;
max-width: 155px;
}
.main {
text-align: left;
background: deepskyblue;
flex: 3 0px;
order: 2;
height: 155px;
}
.footer {
background: lightgreen;
order: 3;
}
<div class="wrapper">
<aside class="aside aside-1">Aside 1</aside>
<article class="main">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</article>
<footer class="footer">Footer</footer>
</div>

CSS transparency issue with background text using transform

I have a colored semi-transparent div (.box) in front of some other divs (.textDisplay) containing text. One of these background divs (on the left) displays correctly, with the text faded due to the transparent div overlaid on top of it. The other one, though, doesn't fade at all. I want both divs displaying like the one on the left.
EDIT: I cannot modify the HTML's structure (it's generated by Elm in a nested manner). Is there a way to do this with CSS only?
EDIT 2: It was a stacking context problem due to the transform property. See my answer for further details.
.textDisplay {
height: 100%;
width: 100%;
text-align: center;
}
.box {
width: 200px;
padding: 20px;
background-color: #8CA8DA;
position: absolute;
top: 50%;
left: 50%;
margin-left: 20px;
transform: translateY(-50%);
opacity: 0.8;
}
.behind {
position: absolute;
transform: translate(-50%, -50%);
}
<div class="behind" style="left: 100px; top: 100px; width: 127px; height: 127px;">
<div class="content">
<div>
<div class="textDisplay">Test Text 0</div>
<div class="box" style="z-index: 100;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi molestie ornare ex et cursus. Donec nibh urna, bibendum nec molestie sed, condimentum ut lacus.
</div>
</div>
</div>
</div>
<div class="behind" style="left: 350px; top: 150px; width: 127px; height: 127px;">
<div class="content">
<div>
<div class="textDisplay">Test Text 1</div>
</div>
</div>
</div>
Like this?
.textDisplay {
height: 100%;
width: 100%;
text-align: center;
}
.box {
width: 200px;
padding: 20px;
background-color: #8CA8DA;
position: absolute;
top: 50%;
left: 50%;
margin-left: 20px;
transform: translateY(-50%);
opacity: 0.8;
}
.behind {
position: absolute;
transform: translate(-50%, -50%);
}
<div class="behind" style="left: 350px; top: 150px; width: 127px; height: 127px;">
<div class="content">
<div>
<div class="textDisplay">Test Text 1</div>
</div>
</div>
</div>
<div class="behind" style="left: 100px; top: 100px; width: 127px; height: 127px;">
<div class="content">
<div>
<div class="textDisplay">Test Text 0</div>
<div class="box" style="z-index: 100;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi molestie ornare ex et cursus. Donec nibh urna, bibendum nec molestie sed, condimentum ut lacus.
</div>
</div>
</div>
</div>
Put .box outside of .behind. I tweaked the css a bit to demo but you'll get the idea.
.textDisplay {
height: 100%;
width: 100%;
text-align: center;
}
.box {
width: 200px;
padding: 20px;
background-color: #8CA8DA;
position: absolute;
top: 50%;
left: 15%;
margin-left: 20px;
transform: translateY(-50%);
opacity: 0.8;
}
.behind {
position: absolute;
transform: translate(-50%, -50%);
}
<div class="box" style="z-index: 100;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi molestie ornare ex et cursus. Donec nibh urna, bibendum nec molestie sed, condimentum ut lacus.
</div>
<div class="behind" style="left: 100px; top: 100px; width: 127px; height: 127px;">
<div class="content">
<div>
<div class="textDisplay">Test Text 0</div>
</div>
</div>
</div>
<div class="behind" style="left: 350px; top: 150px; width: 127px; height: 127px;">
<div class="content">
<div>
<div class="textDisplay">Test Text 1</div>
</div>
</div>
</div>
The transform property creates a new stacking context. As the linked article explains, this stacking context is then atomically (as a single thing) incorporated within it's parent stacking context. The z-index defined within it cannot transcend the context. There are diagrams illustrating this much better in the article.
Instead of using transform: translate(-50%, -50%); for centering, we can use left: -50%; top: -50%;, as demonstrated by the modified snippet. There has to be one div with the desired width and height (.behind) who's top left corner is where we want the center of the target div to be. See the .content class for an example of how to achieve the centering of the target div.
Once the div is centered with an alternate method, everything is in the same stacking context. Now a simple z-index: 100 on the desired element (or one of it's children, like in the snippet) will raise it above the rest, achieving the desired transparency effect.
.textDisplay {
height: 100%;
width: 100%;
text-align: center;
}
.box {
width: 200px;
padding: 20px;
background-color: #8CA8DA;
position: absolute;
top: 50%;
left: 50%;
margin-left: 20px;
transform: translateY(-50%);
opacity: 0.8;
}
.content {
left: -50%;
top: -50%;
height: 100%;
width: 100%;
position: absolute; /* or relative */
}
.behind {
position: absolute;
/*transform: translate(-50%, -50%); REMOVED*/
}
<div class="behind" style="left: 100px; top: 100px; width: 127px; height: 127px;">
<div class="content">
<div>
<div class="textDisplay">Test Text 0</div>
<div class="box" style="z-index: 100;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi molestie ornare ex et cursus. Donec nibh urna, bibendum nec molestie sed, condimentum ut lacus.
</div>
</div>
</div>
</div>
<div class="behind" style="left: 350px; top: 150px; width: 127px; height: 127px;">
<div class="content">
<div>
<div class="textDisplay">Test Text 1</div>
</div>
</div>
</div>

Stick a Div element during Horizontal scroll

I am struggling with a layout problem and I hope to find some help to solve it.
I am designing a page with row/lane based content.
Each row has multiple div elements with some content.
So it has to be possible to scroll through the rows vertically as well as horizontally.
Now the problem is, that the first div element inside every row is kind of like the row header, which provides some description about the content of that row.
I want this div element to always stay visible during horizontal scrolling.
Position: fixed is not a option since it would prevent the "row-header" to scroll with its content during a vertical scroll.
What it should look like during a horizontal scroll
Here's the fiddle with my code: https://jsfiddle.net/rco56cbp/
html,
body {
font-family: 'Arial', sans-serif;
margin: 0;
}
* {
box-sizing: border-box;
}
header {
background: #F7F7F7;
box-shadow: 0px 2px 6px 0px rgba(0,0,0,0.50);
/**/
width: 100%;
height: 90px;
/**/
display: flex;
align-items: center;
justify-content: center;
position: fixed;
z-index: 55;
top: 0;
}
/* Left Sidebar */
.side-bar-bg {
width: 130px;
height: 100vh;
z-index: -1;
position: fixed;
background: #F7F7F7;
border: 1px solid #E0E0E0;
box-shadow: 0px 0px 6px 0px rgba(0,0,0,0.50);
}
/* Wrapper around main content */
.content-container {
margin-top: 110px;
height:100%;
position:absolute;
width:100%;
}
/* Content elements*/
.lane,
.lane-head,
.phase,
.lane-content {
display: flex;
}
.lane {
margin-bottom: 1.25em;
//flex-direction: row;
align-items: center;
}
.lane-head {
min-width: 130px;
background-color: #ffffff;
box-shadow:0px 0px 0px 1px #BFC0C2 inset;
/* Flex & Layout */
flex-direction: column;
flex-wrap: wrap;
justify-content: center;
align-items: center;
align-self: stretch;
/**/
margin-right: 1em;
}
.lane-label {
font-weight: 500;
font-size: 13px;
color: rgba(82,94,106,0.65);
line-height: 22px;
margin-left: 1em;
margin-right: 1em;
text-align: center;
}
.lane-content {
z-index: -3;
}
/* Grid System */
.col-1 { min-width: 200px; max-width: 200px; }
.col-2 { min-width: 420px; max-width: 420px; }
.col-3 { min-width: 640px; max-width: 640px; }
.col-1, .col-2, .col-3 { margin-right: 20px; }
.textbox {
padding: 0.7em 1em 1.5em 1em;
color: #FFFFFF;
min-width: 200px;
max-width: 200px;
background-color: #78BE20;
}
.phase {
background: #005691;
align-self: stretch;
align-items: center;
padding: 0.6em 1em;
}
/* Typo */
.phase {
font-weight: 500;
font-size: 16px;
color: #FFFFFF;
}
.textbox h3 {
font-weight: bold;
font-size: 16px;
margin: 0 0 0.3em 0;
}
.textbox p {
font-weight: normal;
font-size: 13px;
margin: 0 0;
}
<header>
<h1>Header</h1>
</header>
<div class="side-bar-bg"></div>
<!------ CONTENT ------>
<div class="content-container">
<div class="lane">
<div class="lane-head"><span class="lane-label">Small Boxes</span></div>
<div class="lane-content">
<div class="phase col-2">Lorem Ipsum</div>
<div class="phase col-3">Bacon Ipsum</div>
<div class="phase col-2">Egg Ipsum</div>
</div>
</div>
<div class="lane">
<div class="lane-head"><span class="lane-label">Bigger Boxes</span></div>
<div class="lane-content">
<div class="textbox col-1">
<h3>Aenean commodo</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</div>
<div class="textbox col-1">
<h3>Consequat</h3>
<p>Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
</div>
<div class="textbox col-1">
<h3>Consequat</h3>
<p>Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
</div>
<div class="textbox col-1">
<h3>Consequat</h3>
<p>Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
</div>
<div class="textbox col-1">
<h3>Consequat</h3>
<p>Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
</div>
<div class="textbox col-1">
<h3>Consequat</h3>
<p>Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
</div>
<div class="textbox col-1">
<h3>Consequat</h3>
<p>Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
</div>
</div>
</div>
</div>
The fixed left sidebar shall serve as a background to the "lane-headers".
The reason I want the lane-headers and the lane-content to be inside one div container is that later the whole canvas becomes dynamic and interactive. The content will be generated dynamically and the user will be able to interact with the canvas with drag-drop and so on.
I would appreciate your help very much, thanks in advance!
Hi you need to use javascript as well, once you scroll down or up add a class say posi_stat which will be position:static.and when you do horizontal scrolling just remove the class. In default case it will be position :fixed .
Something like this , you need to remove the class , and add on scrolldown too
$(function() {
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 500) {
//your code , what should be done//
}
});
});
Similarly to it for .scrollBottom(); .scrollRight(); .scrollLeft();
Okay I kind of found a solution to it thanks to this: Fixed position in only one direction in future releases of CSS?
Although the javascript solution did not work for me (I still can't figure out why), the CSS solution works pretty good.
By adding the following lines to the .lane-head class the problem was solved:
left: 0;
position: -webkit-sticky;
position: -moz-sticky;
position: -o-sticky;
position: -ms-sticky;
position: sticky;
Using the new "position: sticky" property you can perfectly stick any element basically anywhere you want.
Unfortunately Chrome still does not support this yet, but Safari and Firefox do.

Float elements from middle with width in %

I'm trying to create a layout where two elements float next to eachother originating from the midddle.
I've managed to do this but only when the two floating elements have a fixed width.
http://jsfiddle.net/q7uey80L/3/
Does anyone know how to do the same setup with width: 300px; for elements .right and.left replaced with a percentage?
If I change the 300px to say 20%, the structure fails and the elements originate from the left in stead of from the middle.
It's not clear to me what you are trying to do but I suspect float is not the way. This feels like flexbox is going to be the solution.
.container {
border: solid blue 1px;
display:flex;
justify-content:center;
}
.right, .left {
width: 25%;
border: green solid 1px;
}
h1 {
margin: 0;
padding: 0;
}
.wrapper {
border: solid red 1px;
text-align: center;
}
.container {
border: solid blue 1px;
display: flex;
justify-content: center;
}
.right,
.left {
width: 25%;
border: green solid 1px;
}
.right {
text-align: left;
}
.left {
text-align: right;
}
.clear {
clear: both;
}
<div class="wrapper">
<h1>Top titel</h1>
<div class="container">
<div class="left">
Quisque viverra ac augue porta auctor. Fusce sollicitudin tellus risus, sit amet commodo felis tristique in.
<br/>Integer tempor ultricies eleifend. Vivamus id pretium dolor, vitae sagittis massa.
<br/>Pellentesque pulvinar neque interdum dolor pulvinar tempus. Nullam congue tempus dignissim.
</div>
<div class="right">
Vivamus massa lacus, dignissim ac accumsan non, lacinia in libero. Nullam tempor, velit nec fringilla feugiat, arcu libero viverra nibh, ullamcorper ultricies ante felis non risus.
<br/>Vivamus feugiat augue nec tellus sodales interdum. Suspendisse ac libero malesuada
</div>
<div class="clear"></div>
</div>
</div>
You need to have another div between the container and left/right divs. You can then give width as percentage, with margin: 0 auto on the container.
.right, .left {
border: green solid 1px;
float: left;
width: 45%;
}
<div class="container" >
<div style="width: 60%;margin: 0 auto;height: 200px">
<div class="left">
Quisque
</div>
<div class="right">
Vivamus
</div>
</div>
</div
check this out:
http://jsfiddle.net/z6s1ddnx/1/
Is this what you meant: https://jsfiddle.net/anjalysaju123/5pypkfky
the css is:
h1 {
margin: 0;
padding: 0;
}
.wrapper {
border: solid red 1px;
width: 100%;
height: 100%;
text-align: center;
}
.container {
border: solid blue 1px;
display: inline-block;
}
.right, .left {
border: green solid 1px;
float: left;
width: 49%;
}
.right {
text-align: left;
}
.left {
text-align: right;
}
.clear {
clear: both;
}
I think it is because of .container display is set to inline-block and it has no width set. If you set percentage for .left and .right - percentage are related to containeng element. Set .container width to exact value and you can set percentage width for inner elements. http://jsfiddle.net/q7uey80L/5/
.container {
border: solid blue 1px;
display: inline-block;
width: 500px;
}
.right, .left {
border: green solid 1px;
float: left;
width: 30%;
}