How do I align all my div's in one line? - html

I've looked at a bunch of websites, but none of them worked for me. I'm probably missing something really simple.
#container {
width:100%;
}
#one {
background-color:blue;
width:20%;
height:50%;
}
#two {
background-color:green;
width:20%;
height:50%;
}
#three {
background-color:yellow;
width:20%;
height:50%;
}
#four {
background-color:orange;
width:20%;
height:50%;
}
#five {
background-color:red;
width:20%;
height:50%;
}
This is what I want it to look like:
It doesn't display a lot, which I suspect is because of the height:50%...
Thanks in advance :)

You just need to add float left to each id in your container. This is a truncated version, no need to add the same css to each of your separate ids.
#container #one, #container #two, #container #three, #container #four, #container #five {
float:left;
}
or you can use display inline block
#container #one, #container #two, #container #three, #container #four, #container #five {
display:inline-block;
}
To center the divs if any space is left over you can add text align center to ensure the divs in your container are centered properly. This only works when using display block inline on your container.
#container {
text-align:center;
}

To put all the divs in the same line
use
display:inline-block;
if want to show divs in next line,
use
display:block;
default is set to block;
#container {
width:100%;
}
#one,#two,#three,#four,#five{
width:20%;
height:50%;
}
#one {
background-color:blue;
display:inline-block;
}
#two {
background-color:green;
display:inline-block;
}
#three {
background-color:yellow;
display:inline-block;
}
#four {
background-color:orange;
}
#five {
background-color:red;
}
<div id="container">
<div id="one">
One
</div>
<div id="two">
Two
</div>
<div id="three">
Three
</div>
<div id="four">
four
</div>
<div id="five">
five
</div>
</div>
Hope it helps

I've modified your code a bit but this should output what you are looking for.
You display the divs inline, and position them relatively with a slightly negative margin so that they take up 20% of the width each.
In a comment you mentioned you want to "make it exactly 50% tall", so you need to give the body 100% height, then set the divs to have 50% height:
html,
body {
height: 100%;
}
div {
display: inline-block;
width: 20%;
height: 50%;
position: relative;
margin: -2px;
}
#one {
background-color: lightblue;
}
#two {
background-color: green;
}
#three {
background-color: yellow;
}
#four {
background-color: orange;
}
#five {
background-color: red;
}
<div id="one">
</div>
<div id="two">
</div>
<div id="three">
</div>
<div id="four">
</div>
<div id="five">
</div>

im not sure if that's what you're asking but maybe you just need
div{float:right;}

Try this:
#container {
width: 100%;
height: 300px;
border: 1px solid #000;
}
#container div {
width: 20%;
height: 50%;
float: left;
}
#element-1 {
background-color: red;
}
#element-2 {
background-color: blue;
}
#element-3 {
background-color: pink;
}
#element-4 {
background-color: yellow;
}
#element-5 {
background-color: green;
}
<div id="container">
<div id="element-1"></div>
<div id="element-2"></div>
<div id="element-3"></div>
<div id="element-4"></div>
<div id="element-5"></div>
</div>
I have helped in some way

Related

Make element fit page scroll width with CSS

I have a header with width: 100% and I want to make it automatically adjustable not only to screen width, but to page full width that can be stretched beyond screen width by other elements. I can do it with JS, but I wonder if it is possible to use pure CSS to achieve this.
In the example below the A block is intended to be the same size as the B block, but it is much shorter on small screens (just scroll everything to the right and you can see).
html,body{
position:relative; width:100%;
}
#A{
position:relative; width:100%;
background:lightblue; color:white;
}
#B{
position:relative; width:5000px;
background:darkblue; color:white;
}
<html>
<head></head>
<body>
<div id="A">A</div>
<div id="B">B</div>
</body>
</html>
Any ideas?
You can try adding display: grid to the container, in this case body
body {
display: grid;
}
#A {
background: lightblue;
color: white;
}
#B {
position: relative;
width: 5000px;
background: darkblue;
color: white;
}
<html>
<head></head>
<body>
<div id="A">A</div>
<div id="B">B</div>
</body>
</html>
If you wrap the two divs (A & B) inside another you can achieve this.
html,body{
position:relative; width:100%;
}
#A{
position:relative; width:100%;
background:lightblue; color:white;
}
#B{
position:relative; width:5000px;
background:darkblue; color:white;
}
.header-wrap { display: inline-block; }
<html>
<head></head>
<body>
<div class="header-wrap">
<div id="A">A</div>
<div id="B">B</div>
</div>
</body>
</html>
html,
body {
position: relative;
width: 100%;
}
#B {
position: absolute;
width: 100%;
top: 100%;
left: 0;
background: lightblue;
color: white;
}
#A {
position: relative;
width: 5000px;
background: darkblue;
color: white;
min-width:100%;
}
<div id="A"><div id="B">B</div>A</div>
Try giving your A div a max-width of 5000px as well as width:100%
#A{
position:relative;
width:100%;
max-width:5000px;
background:lightblue;
color:white;
}

How do I float div's to the bottom of a page?

Like the question says, I can't float anything to the bottom... I tried float:absolute and it showed that. There's supposed to be 5 different boxes, but it only shows one of them. This is my code:
html,
body {
height: 100%;
}
#one,#two,#three,#four,#five {
margin:0;
padding:0;
float:left;
display:inline-block;
height:50%;
width:20%;
bottom:0;
right:0;
left:0;
position:absolute;
}
div {
margin:-2px;
padding:-2px;
}
#container {
display: inline-block;
width: 100%;
height: 100%;
position: relative;
}
#one {
background-color: blue;
}
#two {
background-color: green;
}
#three {
background-color: yellow;
}
#four {
background-color: orange;
}
#five {
background-color: red;
}
<div id="one">
</div>
<div id="two">
</div>
<div id="three">
</div>
<div id="four">
</div>
<div id="five">
Thanks in advance :)
Your code's not working because you've essentially set all 5 boxes to stack on top of each other by having absolute positioning, 0 margin and left set to 0 for all 5 boxes, so everything takes the same positioning on the bottom left corner of the screen. If you remove right:0 and add an individual left property for each box, you should be able to have all 5 boxes in a neat row at the bottom, like so:
html,
body {
height: 100%;
}
#one,#two,#three,#four,#five {
margin:0;
padding:0;
float:left;
display:inline-block;
height:50%;
width:20%;
bottom:0;
left:0;
position:absolute;
}
#container {
display: inline-block;
width: 100%;
height: 100%;
position: relative;
}
#one {
background-color: blue;
left:0;
}
#two {
background-color: green;
left:20%;
}
#three {
background-color: yellow;
left:40%;
}
#four {
background-color: orange;
left:60%;
}
#five {
background-color: red;
left:80%;
}
jsfiddle: https://jsfiddle.net/hq2hv1pw/
Also on a side note, I'd combine the selector with a class for the CSS style that has 5 IDs haha.
Hope this helps you out

How to stop my div from overlapping?

I am just a beginner in HTML/CSS
How to stop the floating div from overlapping.
jsfiddle-link
HTML
<body>
<div class="left">
</div>
<div class="right">
</div>
</body>
CSS
* {
margin: 0;
padding: 0;
}
body {
width: 100%;
}
.left {
float: left;
height: 500px;
width: 300px;
background: #fff;
position: absolute;
}
.right {
float: right;
height: 500px;
width: 300px;
background: #000;
}
Use widths in percentages and remove the absolute positions:
Here is the updated CSS:
*{
margin:0;
padding:0;
}
body{
width:100%;
}
.wrapper {
width: 100%;
}
.left{
float:left;
height:500px;
width:50%;
background:#fff;
}
.right{
float:right;
height:500px;
width:50%;
background:#000;
}
I have also wrapped left and right divs in a wrapper div
Check it here: https://jsfiddle.net/2Lk13045/2/
You set your width fixed instead of 100%.
https://jsfiddle.net/2Lk13045/1/]
Changed your
body{width:100%; }
to
body{width:600px; }

div stops to strech after a certain number of lines in a child div

<img id='imgT' src="...">
<div id="divL"></div>
<div id="divR">
<div id='chapter'>
aaaaaaaaa<br>
aaaaaaaaa<br>
aaaaaaaaa<br>
aaaaaaaaa<br>
.....
.....
</div>
</div>
css
html { height: 100%; }
body{
height: 100%;
max-width:1024px;
}
#imgT{
width:100%;
border:thin solid blue;
display:block;
}
#divL{
width:20%;
height: 100%;
background:#008080;
float:left;
}
#divR{
width:80%;
height: 100%;
background:blue;
float:left;
}
After a certain number of lines in div chapter - divL and divR stop to stretch to the height of 100% !?
You can see here
Use css tables.
FIDDLE
.wpr
{
display: table;
width: 100%;
}
#divL{
width:20%;
height: 100%;
background:#008080;
display:table-cell;
}
#divR{
width:80%;
height: 100%;
background:blue;
display:table-cell;
}

How to use the width that is left

I am having a left div width a fixed width of 200 px, then I want the content area to take the space that is left. How can I solve that? I have done this...but it doesn't work.
#sidebar {
float:left;
width:200px;
height:100%;
background-color: blue;
}
#mainContent {
float:left;
width: // USE WHATEVER SPACE IS LEFT;
height:100%;
background-color: red;
}
Have a look at this code:
http://jsfiddle.net/Ffx8R/
CSS:
#sidebar {
float:left;
width:200px;
height:100%;
}
#mainContent {
padding-left:200px;
height:100%;
border:1px blue solid;
}
#container
{
height:200px;
clear:both;
}
HTML:
<div id="container">
<div id="sidebar">here is sidebar info</div>
<div id="mainContent">main Content info</div>
</div>