Can't get divs to align properly with css - html

My setup is this:
.cprp_data {
width: 100%;
overflow: hidden;
}
.cprp_title {
width: 80%;
float: left;
}
.gelijkeniscore {
float: right;
}
.cprp_percentage {
float: right;
}
.cprp_excerpt {
width: 80%;
float: left;
}
.cprp-custom-container {
float: right;
}
<div class="cprp_data">
<div class="cprp_title">TITLE</div>
<div class="gelijkeniscore">SOME CONTENT</div>
<div class="cprp_percentage">SOME CONTENT</div>
<div class="cprp_excerpt">SOME CONTENT</div>
<div class="cprp-custom-container">SOME CONTENT IN SEVERAL DIVS
</div>
</div>
cprp_data should function as the wrapper, and this is how i want the other divs inside it to to align:
<cprp_title> <gelijkeniscore>
<cprp_excerpt> <cprp_percentage>
<cprp-custom-container>
To be clear, divs cprp_excerpt and cprp_title should align left on top of each other using a width of 80%, and the other 3 divs are supposed to align right also on top of each other using the remaining 20%, looking kinda like a sidebar.
I can't get it to work. Been trying for hours using several CSS setups. The above CSS is as close as i have gotten so far. But somehow the cprp_excerpt div keeps taking up 100% of the width pushing the cprp-custom-container way down. Any help would be much appreciated!

shift the cprp_percentage div after the cprp_excerpt div

As inferred from the problem statement:
all the internal divs are now stacked upon each other in the desired hierarchy
left container occupies 80% screen width
right container occupies 20% screen width
The container wrapper remains intact, i have just introduced two sub-wrappers to distinguish contents of left and right side of screen
Hope this code helps!
.cprp_data {
width: 100%;
overflow: hidden;
/* display flex to control the scenario */
display: flex;
}
/*.cprp_title {
width: 80%;
float: left;
}
.gelijkeniscore {
float: right;
}
.cprp_percentage {
float: right;
}
.cprp_excerpt {
width: 80%;
float: left;
}
.cprp-custom-container {
float: right;
}
*/
/* as the left section should be 80% in width */
.left{
width: 80%;
border: 1px solid #000000;
}
/* as the right section should be 20% */
.right{
width: 20%;
border: 1px solid #000000;
}
<div class="cprp_data">
<div class="left">
<div class="cprp_title">TITLE</div>
<div class="cprp_excerpt">SOME CONTENT</div>
</div>
<div class="right">
<div class="gelijkeniscore">SOME CONTENT</div>
<div class="cprp_percentage">SOME CONTENT</div>
<div class="cprp-custom-container">SOME CONTENT IN SEVERAL DIVS
</div>
</div>
</div>

Related

Why do "negative margin and float applied elements" overlap?

First of all, please look at this code.
I learned that this was a common way to realize liquid layout.
But I can not understand some of this code.
.container {
overflow: hidden;
}
main {
float: left;
width: 100%;
margin-right: -340px;
background: red;
}
.main-inner {
margin-right: 340px;
background: blue;
}
.sidebar {
float: right;
width: 340px;
background: green;
}
<div class="container">
<main>
<div class="main-inner">
<p class="main-title">Main</p>
</div>
</main>
<aside class="sidebar">
<div class="sidebar-inner">
sidebar
</div>
</aside>
</div>
Question 1
I understand that the negative margin has the effect of moving an element in the specified direction. However, when you run this code, the main element does not seem to be moving at all. Why is this?
Question 2
Since we set the width of the main element to 100%, I understand that the aside element hits the main element and that the main element and aside element can not be side by side.
So, I think that we prepare a horizontal width that can apply the aside element by applying negative margin, but the background color of the main element is applied in the same way as when the horizontal width is 100%. Why is the background color of the main element not (100% - aside width)? How is this series of rendering done?
Question 3
Which document on W3.org describes these actions? I tried looking, but I could not find any detailed information on them.
thank you.
Let's start by adding the properties one by one and see what is happening.
Intially we have this code with no margin applied and only float elements:
.container {
overflow: hidden;
background:yellow;
}
main {
float: left;
width: 100%;
background: red;
}
.main-inner {
background: blue;
}
.sidebar {
float: right;
width: 340px;
background: green;
}
<div class="container">
<main>
<div class="main-inner">
<p class="main-title">Main</p>
</div>
</main>
<aside class="sidebar">
<div class="sidebar-inner">
sidebar
</div>
</aside>
</div>
It's clear that you made the red element to be width:100% floating on the left and the green one to float on the right with a fixed width. You may also notice that p element is having a default margin that's why the blue is not totally covering the red.
Now if you add negative margin-right you will not move the element or decrease the width but you will pull the content from the right in order to overlap the element. Here is a basic illustration:
.box {
width: 200px;
height: 200px;
background: red;
float: left;
}
<div class="box" style="margin-right:-100px;height:220px">
</div>
<div class="box" style="background:blue;">
</div>
As you can see the blue box is overlapping the red one by exactly 100px because we applied -100px to the margin-right of the red box. Same logic will happen in your case, you applied a negative margin equal to the size of the sidebar so you created the need space to move the sidebar at the same level of the main element.
.container {
overflow: hidden;
background:yellow;
}
main {
float: left;
width: 100%;
background: red;
margin-right:-340px;
}
.main-inner {
background: blue;
}
.sidebar {
float: right;
width: 340px;
background: green;
}
<div class="container">
<main>
<div class="main-inner">
<p class="main-title">Main</p>
</div>
</main>
<aside class="sidebar">
<div class="sidebar-inner">
sidebar
</div>
</aside>
</div>
So the main element is still 100% width BUT the sidebar is overlapping it due to negative margin.
Now the last step is to add the margin inside the main and in this case it will reduce the width of the inner element to make the total (width + margin) always equal to the width of parent element (containing block)
.container {
overflow: hidden;
background:yellow;
}
main {
float: left;
width: 100%;
background: red;
margin-right:-340px;
}
.main-inner {
background: blue;
margin-right:340px;
}
.sidebar {
float: right;
width: 340px;
background: green;
}
<div class="container">
<main>
<div class="main-inner">
<p class="main-title">Main</p>
</div>
</main>
<aside class="sidebar">
<div class="sidebar-inner">
sidebar
</div>
</aside>
</div>
Here is another illustration of margin with block element non floated:
.container {
border: 2px solid;
max-width: 50vw;
margin: auto;
}
.first {
height: 100px;
background: red;
margin: 0 -50px;
}
.second {
height: 100px;
background: blue;
margin: 0 50px;
}
<div class="container">
<div class="first">
</div>
<div class="second">
</div>
</div>
In this case the width is increasing/decrasing due to margin because the logic is always: width + margin = width of containing block.
With elements like float and inline block the logic is the same but we won't have width changes because the width is defined either by the content or explicitly.
.container {
border: 2px solid;
display:inline-block;
}
.first {
float:left;
height: 100px;
background: red;
margin-right:-50px;
}
.second {
display:inline-block;
width:200px;
height: 120px;
background: blue;
margin-top:20px;
margin-right:-100px;
}
<div class="container">
<div class="first">
some text here
</div>
<div class="second">
</div>
</div>
Here the float element has a width defined by the content, the inline-block has a width equal to 200px. The negative margin is creating the overlap and the size of the parent element (the containing block) is equal to width + margins.
For the references:
8 Box model
9 Visual formatting model
10 Visual formatting model details
The above explanation is very simplifed. Refer to the specification links for a full and details explanation.
The odd placement from <main> comes from a browser css-rule
p {
display: block;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
}
You can reset it using a css reset like normalize.css.
However, I recommend using display: flex. Some wonderful resources.
.container {
display: flex;
}
main {
width: 75%;
}
aside {
width: 25%;
}

Floating divs left/right without separating them on large window

The following example shows floating div on the left and right with two different container widths.
For large container (or large window), the divs floating left stick to the left border, and the div floating right stick to the right.
Is there a way to force the divs to stick together (horizontally centered in the container) when the container is large? Note that fixing the width of the container or that of any div is not an option.
Ideally, I would like to avoid javascript.
.container {
display: inline-block;
border: 1px solid black;
}
.inner1 {
float: left;
background-color: #aff;
}
.inner2 {
float: right;
background-color: #ffa;
}
.inner3 {
float: left;
background-color: #faf;
}
First example with wide container<br />
<div class="container" style="width:400px">
<div class="inner1"><---------- First</div>
<div class="inner2">Second
<br />--------
<br />------</div>
<div class="inner3">Third ----------</div>
</div>
<br />
Second example with small container<br />
<div class="container" style="width:100px">
<div class="inner1"><---------- First</div>
<div class="inner2">Second
<br />--------
<br />------</div>
<div class="inner3">Third ----------</div>
</div>
EDIT :
The reason why I'm using floats is to prevent the default ordering of divs. When non-floating, they are ordered either left-to-right or top-to-bottom, or both. This ordering prevents, sometime, useful space to be filled. For example, imagine div1 wide but not tall, and div2 thin and tall. If the screen is large enough, div2 will be to the right of div1. There will be a lot of space left below div1 and to the left of div2. I want to put div3 in that space. But because of the ordering, div3 will go to the right or below div2.
To resolve this problem, I used floating divs, but it created the other problem I stated initially: floating left/right makes divs separate on large screens.
If the problem can be solved without floats, it would be best.
i cant comment yet, since im pretty new.
so i will just talk
if i understand the question right, You
might be looking for the
vertical-align: middle;
I hope thats what you where looking for.
Do you mean something like this? Just remove float from .inner3.
.container {
display: inline-block;
border: 1px solid black;
}
.inner1 {
float: left;
background-color: #aff;
}
.inner2 {
float: right;
background-color: #ffa;
}
.inner3 {
background-color: #faf;
}
First example with wide container<br />
<div class="container" style="width:400px">
<div class="inner1"><---------- First</div>
<div class="inner2">Second
<br />--------
<br />------</div>
<div class="inner3">Third ----------</div>
</div>
<br />
Second example with small container<br />
<div class="container" style="width:100px">
<div class="inner1"><---------- First</div>
<div class="inner2">Second
<br />--------
<br />------</div>
<div class="inner3">Third ----------</div>
</div>
You can use CSS3 #media query. Just float the divs on screen with 400px width and above.
.container {
border: 1px solid black;
}
.inner1 {
float: left;
background-color: #aff;
}
.inner2 {
float: right;
background-color: #ffa;
}
.inner3 {
float: left;
background-color: #faf;
}
#media (min-width: 400px) {
.container {
margin: 0 auto;
}
.inner1 {
float: left;
}
.inner2 {
float: left;
}
.inner3 {
float: left;
}
}
The container will centered on the screen width >= 400px. And the inner divs will float to the left. By default, the inner1, inner2, and inner3 will be floated to the left, right, and left respectively.
Example on JSFiddle
I think, in this case remove display: inline-block; from .container and add overflow: hidden; here and into .inner3 :
.container {
border: 1px solid black;
overflow: hidden;
}
.inner1 {
float: left;
background-color: #aff;
}
.inner2 {
float: right;
background-color: #ffa;
}
.inner3 {
overflow: hidden;
background-color: #faf;
float: left;
}
EDIT jsfiddle:
jsfiddle-link

Make an inner div take up 100% of an outer responsive div (Floating Issues)

This is sort of hard to explain so I made a fiddle:
https://jsfiddle.net/8wujkpqb/
I have a right floating div with a max-width set. I need the div inside of that to take up 100% of the max-width so the content can be left-aligned to the content in the div below.
<div class="container">
<div class="mainleft">
<div class="outer-red">
<div class="first">
I need this<br/> pushed to the left<br/> to align with the<br/> lower text but still<br/> be in a "max-width"<br/> container floating right.
</div>
<div class="clear"></div>
</div>
<div class="outer-gray">
<div class="second">
this is fine because there is enough content to take up the max-width. this is fine because there is enough content to take up the max-width. this is fine because there is enough content to take up the max-width
</div>
<div class="clear"></div>
</div>
</div>
<div class="mainright">
<div class="right-content">
content
</div>
</div>
<div class="clear"></div>
CSS
.container {
width: 100%;
}
.mainleft {
width: 50%;
float: left;
}
.mainright {
width: 50%;
float: left;
}
.outer-red {
width:100%;
background: red;
padding: 40px;
box-sizing:border-box;
}
.outer-gray {
width:100%;
background: gray;
padding: 40px;
box-sizing:border-box;
}
.first {
float: right;
max-width:250px;
clear:both;
}
.second {
float: right;
max-width:250px;
}
.right-content {
width:100%;
background: blue;
padding: 40px;
box-sizing:border-box;
}
.clear {
clear: both;
}
https://jsfiddle.net/8wujkpqb/
Any help is greatly appreciated!
Just add another div into your "first" container that possesses the max-width and let the parent be the size that is necessary to align the inner container left. Like so
.first {
float: right;
max-width: 100%;
clear:both;
width: 200px
}
.inner {
max-width: 200px
}
https://jsfiddle.net/8wujkpqb/1/

CSS: Having 3 div on the same line with the middle one taking the remaining space

I'm building a toolbar, I'd like the yellow part in the following example to take the whole space left (in white):
http://jsfiddle.net/MWjGH/1/
<div class="left"> Some content </div>
<span class="middle"> This should fill the space left </span>
<div class="right"> Some other content </div>
with css:
.left {
float: left;
background-color: #ddd;
display: inline-block;
}
.middle {
background-color: yellow;
display: inline-block;
}
.right {
float: right;
background-color: #ddd;
display: inline-block;
}
Edit: the content of left and right is dynamic, it can change, so I don't want to set width on them
I don't know if that suits you because of a slight HTML change:
<div class="left"> Some content </div>
<div class="right"> Some other content </div>
<span class="middle"> This should fill the space </span>
But I believe it is what you want,
CSS:
.left {
float: left;
background-color: #ddd;
}
.middle {
background-color: yellow;
display: block;
overflow:hidden;
}
.right {
float: right;
background-color: #ddd;
}
DEMO :http://jsfiddle.net/pavloschris/MWjGH/12/
Put the middle div after the floated divs:
<div class="left"> Some content </div>
<div class="right"> Some other content </div>
<div class="middle"> This should fill the space left </div>
Then, don't change any of the display properties so they stay on block (the default for div)
.left {
float: left;
background-color: #ddd;
}
.middle {
background-color: yellow;
}
.right {
float: right;
background-color: #ddd;
}
Fiddle: http://jsfiddle.net/hLmj7/
If you do't have a fixed width for the two side columns, you can always display:table-cell.
.left {
background-color: #ddd;
display: table-cell;
}
.middle {
background-color: yellow;
display: table-cell;
width:100%;
}
.right {
background-color: #ddd;
display: table-cell;
}
JSFiddle example.
With this you're then able to add min-width to the outer columns without having to keep changing the width of the middle element.
JSFiddle example with min-width applied.
I would wrap your divs in a wrapper, and assign the background-color to the wrapper div
Then, you don't need to specify width at all.
jsfiddle
Html:
<div class="toolbar">
<div class="left"> Some content </div>
<div class="middle"> This should fill the space left </div>
<div class="right"> Some other content </div>
</div>
CSS:
.toolbar {
background-color: yellow;
}
.left {
float: left;
background-color: #ddd;
display: inline-block;
}
.middle {
display: inline-block;
}
.right {
float: right;
background-color: #ddd;
display: inline-block;
}
Consider adding clearfix to the wrapper div as the divs inside are floating :)
try this:
don't right align your last div
make all your containers float:left
and give percentage width to each of your containers, so that their total sum should be 100%;
working fiddle
if you don't want to enforce a static width do this:
give each of your containers a width:auto, but be notified, that,
if the total sum of the width of each of the containers turns out to be more than that of parent( body in your case) contaienr, then line-break would occur,
a div will slide down to the next row.
see this fiddle

Three column fluid width, which works with or without the sidebars

I have sidebars which can be turned on and off on specific pages, but I am trying to design a CSS layout that fills up the available space regardless of what sidebars are enabled. I have this so far:
CSS:
#container{
width: 100%;
}
#sidebar-left, #sidebar-right, #content{
height: 50px;
}
#sidebar-right{
background: gray;
width: 50px;
float: right;
}
#sidebar-left{
background: yellow;
width: 50px;
float: left;
}
#content{
background: orange;
}
​
HTML:
<div id="container">
<div id="content">content content content content content content content
<div id="sidebar-left">sidebar</div>
<div id="sidebar-right">sidebar</div>
</div>
</div>
JSFiddle link:
LINK
The problem is the right column overlaps the main content column. However, I still want to keep the middle column at 100% so it fills up all the space when one of the sidebars is disabled.
Add a container that holds your main content, and apply overflow: hidden to it in order to apply a new block formatting context to it. You'll need to rearrange the elements slightly. See below:
HTML
<div id="container">
<div id="content">
<div id="sidebar-left">sidebar</div>
<div id="sidebar-right">sidebar</div>
<div id="col-main">content content content content content content content contente conte tnoe o toa nao no ton oanota ona</div>
</div>
</div>
​CSS
#container {
width: 100%;
}
#sidebar-left, #sidebar-right, #content {
height: 50px;
}
#sidebar-right {
background: gray;
width: 50px;
float: right;
}
#sidebar-left {
background: yellow;
width: 50px;
float: left;
}
#content {
background: orange;
height: auto;
}
#col-main {
overflow:hidden;
}
​
Here's a jsfiddle demonstrating it with columns: http://jsfiddle.net/aBbtN/8/
and without: http://jsfiddle.net/aBbtN/10/