I am trying to create the following structure using embedded divs inside a header:
The height and width of the red part is known and fix
The height of the green rectangle is known and fix
The overall width of the frame can vary, but is never smaller than the red part
I have tried with the following html code:
<header id="header">
<div id="yellowAndGreen">
<div id="yellow"></div>
<div id="green"></div>
</div>
<div id="red"></div>
</header>
and the following CSS:
#header {
width: 400px;
}
#yellowAndGreen {
}
#yellow {
background-color: yelow;
}
#green {
background-color: green;
height: 40px;
}
#red {
height: 150px;
width: 150px;
background-color: red;
}
but it does not work. I have created a JsFiddle. Can anyone modify it to create what I am looking for?
Use float:right; to make the red box float to the right. You'll need to adjust the html:
<header id="header">
<div id="alldivs">
<div id="red"></div>
<div id="yellow"></div>
<div id="green"></div>
</div>
</header>
and the css:
#header {
width: 400px;
}
#yellow {
background-color: yellow;
height:110px;
}
#green {
background-color: green;
height: 40px;
}
#red {
height: 150px;
width: 150px;
background-color: red;
float:right;
}
Please note you had misspelled "yellow", and that you need to set a height for the yellow <div>.
Finally here is the adjusted fiddle
Hope this helps
This uses flexbox, which has some support drop off on older browsers. I've excluded vendor-specific attirbutes for simplicity.
#header {
width: 400px;
display: flex;
}
#yellowAndGreen {
flex: 1 1 auto;
display: flex;
flex-direction: column;
}
#yellow {
background-color: yellow;
flex-grow: 1;
}
#green {
background-color: green;
height: 40px;
}
#red {
height: 150px;
width: 150px;
background-color: red;
}
No floats or flexbox necessary!
Rearranged your HTML a bit. Set #header background-color to red and min-width so it will never be too small. Use calc() for #yellow's width so that the background red of it's parent will always be 150px wide.
#header {
width: 400px;
background-color: red;
width: 100%;
min-width: 150px;
}
#yellow {
height: 150px;
width: calc(100% - 150px);
background-color: yellow;
}
#green {
background-color: green;
height: 40px;
}
<header id="header">
<div id="yellow"></div>
<div id="green"></div>
</header>
Related
I need to make a flag with HTML and CSS.
I'm running into a few problems:
Each colour of the flag needs to be 33,3% of height.
The flag needs to stay the same shape when you change the size of the browser(it needs to fill the entire page).
The flag has a minimum width of 500px and a minimum height of 300px.
body {
margin: 0;
padding: 0;
}
#red {
background-color: red;
height: 33, 3%;
width: 500px;
}
#white {
background-color: white;
height: 33, 3%;
width: 500px;
}
#blue {
background-color: blue;
height: 33, 3%;
width: 500px;
}
<div id="red"></div>
<div id="white"></div>
<div id="blue"></div>
Simply use background on the html element:
html {
min-height:300px;
min-width:500px;
height:100%;
background:
linear-gradient(red,red) top,
linear-gradient(blue,blue) bottom;
background-size:100% 33.3%;
background-repeat:no-repeat;
}
You could try the following code, please note:
Wrap your markup in a div with id flag and set its size
Measurements should be written as 33.3%, you had few syntax errors
min-width and min-height to set the minimum size of the element
body {
margin: 0;
padding: 0;
}
#red {
background-color: red;
height: 33.3%;
width: 500px;
}
#white {
background-color: white;
height: 33.3%;
width: 500px;
}
#blue {
background-color: blue;
height: 33.3%;
width: 500px;
}
#flag {
min-width: 500px;
width: 100%;
min-height: 300px;
height: 300px;
}
<div id="flag">
<div id="red"></div>
<div id="white"></div>
<div id="blue"></div>
</div>
The best and efficient way is to place all three div in a parent div and handle with that.
Here is the code snippet:
CSS FILE:
#red
{
background-color: red;
height: 33.3vh;
}
#white
{
background-color: white;
height: 33vh;
}
#blue
{
background-color: blue;
height: 33.3vh;
}
#parent {
min-height: 300px;
min-width: 500px;
}
HTML FILE
<div id="parent">
<div id="red"></div>
<div id="white"></div>
<div id="blue"></div>
</div>
And here is the working code link at Stackblitz:
https://stackblitz.com/edit/angular-y1jfby
I have a current issue in my current project, where i have an area in which i want to center some text. This text can be different from each use of the area.
This part i have fully understood, but i want to place another piece of text, exactly in the center of the remaining space between the end of the first text and the end of the area.
How would i structure my css and html to make this possible?
The image below should help display what it is, that i want to do:
html,
body {
height: 100%;
text-align: center;
}
#left {
width: 200px;
display: inline-block;
background: #f00;
height: 200px;
justify-content: center;
}
#right {
display: inline-block;
background: #0f0;
height: 200px;
}
<div id="left">
CONTENT
</div>
<div id="right">
Other content
</div>
Edit:
Sorry about not including code
An attempt i took: http://jsfiddle.net/5jRaY/298/
I get the red block to fit as wanted, other than the div should wrap the container. My issue is that i can't get the green box to fill the remaining space of the page.
You can try a different layout. This is what I will use:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#wrapper {
display: table;
width: 100%;
text-align: center;
}
#one,
#two,
#three {
display: table-cell;
width: 33.333%;
}
#one {
width: 200px;
height: 200px;
background: white; /*Change color to see it*/
}
#two {
background: red;
height: 200px;
}
#three {
height: 200px;
background: green;
}
<div id="wrapper">
<div id="one"></div>
<div id="two">CONTENT</div>
<div id="three">Other content</div>
</div>
Let me know if it works for you!
Hope this helps:
#container {
height: 200px;
text-align: center;
position: relative;
}
#left {
width: 200px;
margin: auto;
height: 100%;
background: #f00;
}
#right {
top: 0;
right: 0;
bottom: 0;
background: #0f0;
position: absolute;
width: calc(50% - 100px); /* 100px is 50% of #left */
}
<div id="container">
<div id="left">
CONTENT
</div>
<div id="right">
Other content
</div>
</div>
Consider the following HTML structure,
<div class='floated' id='div1'></div>
<div class='floated' id='div2'></div>
<div class='floated' id='div3'></div>
with the following CSS:
.floated {
width: 50%;
float: left;
}
#div1 {
height: 300px;
background-color:red;
}
#div2 {
height: 30px;
background-color: green;
}
#div3 {
height: 30px;
background-color: yellow;
}
This way, #div1 will take up a 300px tall part of the left side of the page, while #div2 and #div3 will get floated to the right side of the page. How could I set up my CSS, so #div1 and #div2 takes up a single row(of height 300px, the maximum height of the two), and #div3 will be placed right below #div1?
I am not controling the height of these divs, this is dynamic, it is possible that sometimes the first one will be only 20 pixels, and the second one will be 1000 pixels, and the other way around is also a possibility
Here's a fiddle: https://jsfiddle.net/1u55fukj/
You can use Flexbox on parent element (body in this case) and use flex-wrap: wrap. This will always make both div's in same row equal height or equal to height of taller one DEMO
body {
display: flex;
flex-wrap: wrap;
}
.floated {
flex: 0 0 50%;
}
#div1 {
height: 300px;
background-color: red;
}
#div2 {
background-color: green;
}
#div3 {
height: 30px;
background-color: yellow;
}
<div class='floated' id='div1'></div>
<div class='floated' id='div2'></div>
<div class='floated' id='div3'></div>
If there will be only 2 divs in row, then you can try to give clear:left to odd child.
.floated {
width: 50%;
float: left;
}
#div1 {
height: 300px;
background-color: red;
}
#div2 {
height: 30px;
background-color: green;
}
#div3 {
height: 30px;
background-color: yellow;
}
div.floated:nth-child(odd) {
clear: left
}
<div class='floated' id='div1'>
</div>
<div class='floated' id='div2'>
</div>
<div class='floated' id='div3'>
</div>
flexbox is your best option i think.
you could use a div container and then use display flex
.container{
height: 500px;
width: 100%;
display: flex;
flex-flow: row wrap;
justify-content: center;
}
.floated {
width: 50%;
}
#div1 {
height: 30%;
background-color:red;
}
#div2 {
height: 60%;
background-color: green;
}
#div3 {
height: 40%;
background-color: yellow;
}
<div class="container">
<div class="floated" id="div1"></div>
<div class="floated" id="div2"></div>
<div class="floated" id="div3"></div>
</div>
you can also center the 3rd div and a lot more :D. Flexbox have a good crossbrowsing support using -moz-, -webkit- etc,
I am trying to make multiple divs, specifically five and center them all. I have used the display:inline-block to get them to be side by side but then when I use margin: 0 auto, the display:inline-block seems to get negated and then it's a vertical strip going down the page.
Below is my code:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
I tried looking at the other relevant posts on SO but they don't do it with as many divs or they use static positioning which I don't want to use.
Can someone point me in the right direction?
This happens cause the width of the container is 50px. One quick solution is to set width of container to 100%:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 100%;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
You can align to center using text-align center to container:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 100%;
text-align: center;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
To achieve both and vertical and horizontal align you can use position: absolute to the container top: 50% left: 50% and margin-top: -150px; /* Half the height */ margin-left: -135px; /* Half the width */:
div {
width: 50px;
height: 300px;
border-radius: 0;
display:inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 270px;
position: absolute;
top: 50%;
left:50%;
margin-top: -150px; /* Half the height */
margin-left: -135px; /* Half the width */
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
You can set text-align: center on .container. Updated you code:
.container {
width: 100%;
text-align: center;
}
.container > div{
width: 50px;
height: 300px;
border-radius: 0;
display:inline-block;
}
http://jsfiddle.net/jermund/wzdLrs0m/
Description:
I am trying to learn to align elements such as divs and headers.
Here's what I have so far > http://jsfiddle.net/QxV6p/
Below are the issues:
The "Main section - in red" is not aligned with the blue header on the right hand side.
I have set the width of the body and the header to the same value of 1000px. And I have set the left div (black) to have a width of 20% and the main div to have a width of 79% (both inside the body) leaving a margin of 10px between the two divs.
I believe I have positioned the div correctly using the "position: relative" feature.
Please suggest what is wrong with the code? Also is there a better way of making the divs (in this case the left/black div and the main/red div) align as if they were inline?
I've tried "display: inline" but for some reason it makes the divs disappear. Any help is appreciated.
Code:
<!DOCTYPE html>
<head>
<style>
header {
max-width: 1000px;
height: 100px;
background: blue;
}
body {
max-width: 1000px;
}
.left {
width: 20%;
height: 2000px;
background: black;
margin-top: 10px;
}
.main {
width: 79%;
height: 2000px;
margin-top: 10px;
background: red;
position: relative;
top: -2010px;
left: 210px;
}
</style>
</head>
<html>
<header>
</header>
<body>
<div class="left"></div>
<div class="main"></div>
</body>
</html>
firstly you need a valid html code
<html>
<body>
<div class="wrapper">
<header>
</header>
<div class="left"></div>
<div class="main"></div>
</div>
</body>
</html>
CSS
header {
max-width: 1000px;
height: 100px;
background: blue;
}
.wrapper {
max-width: 960px;
margin:0 auto;
}
.left, .main {
display:inline-block;
margin-top:10px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.left {
width: 20%;
height: 2000px;
background: black;
}
.main {
width: 79%;
height: 2000px;
background: red;
margin-left:4px;
}
DEMO
My recommendations:
Avoid positioning using pixels.
Avoid floats for layout.
KISS: if you want your main content to be 80%, set it to 80%. Manually manouvering it into position will take more time and scales poorly.
Demo (I changed some of the sizes for easier viewing in the fiddle)
HTML
<header></header>
<body>
<div class="left"></div><div class="main"></div>
</body>
CSS
header {
height: 100px;
background: blue;
}
.left {
display: inline-block;
width: 20%;
height: 100px;
background: black;
}
.main {
display: inline-block;
width: 80%;
height: 100px;
background: red;
}
use float:left on each element
see it here
<!DOCTYPE html>
<head>
<style>
header {
max-width: 1000px;
margin: 0 auto;
height: 100px;
background: blue;
}
body {
margin:0;
padding:0;
}
.left {
width: 20%;
height: 2000px;
background: black;
margin-top: 10px;
}
.main {
width: 79%;
height: 2000px;
margin-top: 10px;
background: red;
position: relative;
top: -2010px;
left: 210px;
}
.wrapper{
margin:0 auto;
}
.container{
margin: 0 auto;
width: 1200px;
}
</style>
</head>
<html>
<body>
<header>
</header>
<div class="wrapper">
<div class="container">
<div class="left"></div>
<div class="main"></div>
</div>
</div>
</body>
</html>
You can use float:left instead of using "top and left position"
Here is the updated Code:
HTML
<body>
<header></header>
<div class="left"></div>
<div class="main"></div>
</body>
CSS
header {
max-width: 1000px;
height: 100px;
background: blue;
}
body {
max-width: 1000px;
}
.left {
width: 20%;
height: 2000px;
background: black;
margin-top: 10px;
float:left;
}
.main {
width: 78%;
height: 2000px;
margin-top: 10px;
background: red;
float:left;
margin-left:2%;
}
And Working Demo for the same
Hope this helps!!!