I've got the following setup http://jsfiddle.net/47x60k4w/529/.
HTML
<div class="header">
header
</div>
<div class="inner_block">
<div class="column">
<img src="xxx" />
</div>
<div class="column">
<img src="xxx" />
</div>
<div class="column">
<img src="xxx" />
</div>
</div>
<div class="footer">
footer
</div>
The inner_block should overlap the header class and the footer should be placed right behind the inner_block.
In my solution I just don't get the footer behind the inner_block without doing not responsible stuff like calling a margin-top with x.xem on it. I just found some links with z-index stuff which didn't worked for me because the inner_block lost his passed height and width from the nested block.
The result should look like this beautiful mockup.
Do you have any ideas?
Thanks in advance.
So I made the following changes to your code:
Remove the position: absolute for the inner-block.
As you are floating the contents of the inner-block you have clear the floats so that the parent container will not lose height.
.inner_block:after {
content: '';
display: block;
clear: both;
}
Whenever using floats, remember to clear it.
Added position: relative to the inner_block to position it over the header and footer.
Added display: block to the img so that you can remove the small space below it characteristic on inline elements (the default display).
Also tinkered a bit with the margins and widths to achieve the layout.
.header {
position: relative;
background-color: black;
width: 100%;
height: 50px;
}
.footer {
clear: both;
background-color: red;
width: 100%;
height: 50px;
}
.inner_block {
position: relative;
/*width: 100%;*/
border: solid 1px black;
padding: 5px;
margin-left: 2.5%;
margin-top: -2.5%;
margin-right: 2.5%;
margin-bottom: 2.5%;
background-color: white;
}
.inner_block:after {
content: '';
display: block;
clear: both;
}
.column {
max-width: 30%;
float: left;
margin-right: 2.5%;
}
.column:first-child{
margin-left: 2.5%;
}
.column:last-child{
margin-left: 0;
}
.column img {
max-width: 100%;
height: auto;
display: block;
}
<div class="header">
</div>
<div class="inner_block">
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg" />
</div>
</div>
<div class="footer">
test
</div>
Hope this gives you a head-start. Check it out and let me know your feedback on this. Thanks!
Alternate Solution:
So here is a solution using a flexbox which is easier to set up:
First remove the floating container and the clearfix.
Now Wrap the inner_block with another div
.inner_block_wrapper {
margin: -2.5% 2.5% 2.5% 2.5%;
background-color: white;
position: relative;
}
.inner_block {
border: solid 1px black;
background-color: white;
padding: 5px;
display: flex;
justify-content: center;
}
.column {
margin: 5px;
}
Using display: flex allows the images to take the available space along the row and justify-content: center aligns it along the center. Check this out!
.header {
position: relative;
background-color: black;
width: 100%;
height: 50px;
}
.footer {
clear: both;
background-color: red;
width: 100%;
height: 50px;
}
.inner_block_wrapper {
margin: -2.5% 2.5% 2.5% 2.5%;
background-color: white;
position: relative;
}
.inner_block {
border: solid 1px black;
background-color: white;
padding: 5px;
display: flex;
justify-content: center;
}
.column {
margin: 5px;
}
.column img {
max-width: 100%;
height: auto;
display: block;
}
<div class="header">
</div>
<div class="inner_block_wrapper">
<div class=" inner_block ">
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg " />
</div>
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg " />
</div>
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg " />
</div>
</div>
</div>
<div class="footer ">
test
</div>
You can even try something as below, your codes were fine just set your .footer margin-top equal to the height of .header and .inner_block using css calc() function.
.header{
position:relative;
background-color:black;
width:100%;
height:50px;
}
.footer{
background-color:red;
width:100%;
height:50px;
margin-top:calc(100% - 82%);
}
.inner_block{
position: absolute;
width:90%;
border:solid 1px black;
padding: 5px;
background-color:white;
margin:-2.5% calc(100% - 97%);
}
.column {
width:30%;
float:left;
margin:0 1.6%;
}
.column img {
max-width:100%;
height:auto;
}
<div class="header">
</div>
<div class="inner_block">
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg" />
</div>
</div>
<div class="footer">
test
</div>
is this what you were looking for ?
.header{
position:relative;
background-color:black;
width:100%;
height:50px;
}
.footer{
clear:both;
background-color:red;
width:100%;
height:50px;
}
.inner_block{
position: absolute;
width:100%;
border:solid 1px black;
padding: 5px;
margin-left: 2.5%;
margin-top:-2.5%;
background-color:white;
}
http://jsfiddle.net/8y4e8L08/
.header {
height: 200px;
width:800px;
background-color:#000;
margin:20px;
}
.header {
margin-bottom: -25px;
}
.inner_block {
width: 35%;
height: 150px;
margin: auto 200px;
background-color:#FFF;
border:1px solid #000;
margin-top: -45px;
}
.column{
max-width:20%;
float:left;
border: 2px soid #999;
margin:25px;
}
.column img{
max-width:100%;
height:auto;
}
.footer {
height: 100px;
margin-top: -25px;
margin:20px;
background-color:#F00;
width:800px;
}
.content {
position: relative;
z-index: 1;
}
<div class="header"></div>
<div class="inner_block">
<div class="column">
<img src="download.jpg"/>
</div>
<div class="column">
<img src="download.jpg"/>
</div>
<div class="column">
<img src="download.jpg"/>
</div>
</div>
<div class="footer">
</div>
Well just using the z-index won't always work. You also need to specify the 'position' property as well so as to define the z-index wrt some position of the frame.
Z-index is a property which defines the 'depth' or 'height' of an element. If your <header> has z-index of '100' and; <div> element defined inside the header, usually it would be shown above it but once you define the z-index:50; since 50<100, <div> element would be hidden behind it.
Example of z-index
1) http://www.w3schools.com/cssref/tryit.asp?filename=trycss_zindex
2) https://css-tricks.com/almanac/properties/z/z-index/
Hope it helps.
Related
I have a vertical list of element and I want to add a divider that has bulb on top with an icon and is clickable.
For example like in the code bellow but where the divider has a circle on top with an icon( in the margin on top).
.container {
display:flex;
}
.left, .right {
width: 150px;
height: 150px;
background-color:red;
margin-top: 24px;
}
.divider {
background-color: grey;
width: 4px;
height:150px;
margin-top: 24px;
}
<div class="container">
<div class="left">left</div>
<div class="divider"></div>
<div class="right">right</div>
</div>
Do you mean something like this?
.container {
display:flex;
}
.left, .right {
width: 150px;
height: 150px;
background-color:red;
margin-top: 24px;
}
.divider {
display: block;
margin: 20px 0;
text-align: center;
min-height: 20px;
position: relative;
}
<div class="container">
<div class="left">left</div>
<div class="divider"> <img src="https://icons-for-free.com/download-icon-lightbulb+outline+24px-131985192644521589_16.png"></div>
<div class="right">right</div>
</div>
.left {
width: 50%;
display: inline-block;
float: left;
}
/* Second user block */
.right {
border-color: lightskyblue;
background-color: darkcyan;
width: 50%;
display: inline-block;
float: right;
}
These are the css for the wrappers, not sure why it does not work still.
.boxed {
border: 2px solid black;
padding-left: 20px;
padding-right: 20px;
padding-bottom: 20px;
background-color: darkgoldenrod;
max-width:800px;
margin:1rem auto;
overflow:hidden;
}
/* Chat blocks */
.block {
border: 2px solid darkgreen;
background-color: lightgreen;
border-radius: 30px;
padding: 10px;
margin: 10px 0;
border-radius:20px;
margin-bottom:10px;
width:60%;
}
I am making a mock UI and I can't seem to get the blue chat bubbles to the right side, I tried using right: 0px; and float: right; but both don't seem to work. Are there any other css tags I can use position it correctly?
Here are the div tags:
<div class="boxed card my-3">
<h2 align="center">Chat Log:</h2>
<div class="block left">
<img src="User1.jpg" alt="FortniteGamer">
<p>Hey whats up?. Do you want to get a game going?</p>
<span class="time-right">11:00</span>
</div>
<div class="block right">
<img src="User2.jpg" alt="CODGamer" class="right">
<p>Hey! Yeah defintely that sounds fun!</p>
<span class="time-left">11:01</span>
</div>
<div class="block left">
<img src="User1.jpg" alt="FortniteGamer">
<p>Sweet! Whats your username on fornite?</p>
<span class="time-right">11:02</span>
</div>
<div class="block right">
<img src="User2.jpg" alt="CODGamer" class="right">
<p>My username is: CODSav</p>
<span class="time-left">11:02</span>
</div>
</div>
It seems that the wrapper (.card) is a flex container, in which case floats wont have any effect.
Instead, set the opposite margin to auto:
.right {
border-color: lightskyblue;
background-color: darkcyan;
width: 50%;
margin-left:auto;
}
Put position:absolute and try:
.right {
position:absolute;
border-color: lightskyblue;
background-color: darkcyan;
width: 50%;
display: inline-block;
float: right;
}
The div inside another div won't go to the right but just stays to the left. The code below is some what look like chat box the first div with green background has a position of 0px left and it works but the second div has 0px right and it still stick to the left please help me with this it bothers me for 2 day without right solution
<html>
<head>
</head>
<body>
<div style="width:100% height:100%; position: relative; top:0px; left:0px; background-color:white;">
<div style="width:200px; height:100px; position: relative; top:10px; left:0px; background-color:green; font-size:20px;"><p>1</p></div>
<div style="width:200px; height:100px; position: relative; top:10px; right:0px; background-color:red; color: white; font-size:20px;"><p>2</p></div>
</div>
</body>
</html>
position: relative means the div is positioned "relative to itself". So right: 0px just means "move the div 0px to the right of where it would normally be"... not to the right edge of the container.
You could use position: relative on the container, and apply position: absolute to the children instead, but assigning top values will be cumbersome.
Info about position
An alternative might be adding display: flex to the wrapper. Then you can use margin-left: auto to push a div to the right.
.wrapper {
background: lightgrey;
display: flex;
flex-direction: column;
}
div > div {
width: 200px;
height: 100px;
margin-bottom: 10px;
}
.left {
background: green;
}
.right {
background: red;
margin-left: auto;
}
<div class="wrapper">
<div class="left">
<p>1</p>
</div>
<div class="right">
<p>2</p>
</div>
</div>
.parent{
width:100%;
position: relative;
clear: both;
}
.incoming {
float: left;
max-width: 80%;
background-color: #ccc;
padding: 4px;
overflow: auto;
}
.reply {
float: right;
max-width: 80%;
background-color: powderblue;
padding: 4px;
}
<div class="parent">
<div class="incoming"><p>Incoming chat that takes up a maximum of 80%
of screen width.</p></div>
<div class="reply"><p>Reply, that also does the same, but from the right of the screen.</p></div>
</div>
Edited to reflect updated requirement
Using relative element with top, left, bottom and right properties is useless. you have to change it to absolute value.
<div style="width:100% height:100%; position: relative; background-color:white;">
<div style="width:200px; height:100px; position: absolute; top:10px; left:0px; background-color:green; font-size:20px;"><p>1</p></div>
<div style="width:200px; height:100px; position: absolute; top:10px; right:0px; background-color:red; color: white; font-size:20px;"><p>2</p></div>
</div>
UPDATE
here is another way to position elements
<div style="width:100%; height:100px; background-color:white;">
<div style="width:200px; height:100px; float:left; background-color:green; font-size:20px;"><p>1</p></div>
<div style="width:200px; height:100px; float:right; background-color:red; color: white; font-size:20px;"><p>2</p></div>
</div>
UPDATE#2
here is markup for your chat
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.chat {
width: 100%;
background: lightblue;
padding: 10px;
overflow: hidden;
}
.message {
clear: both;
font-family: sans-serif;
color: white;
}
.to, .from {
width: 40%;
padding: 10px;
overflow: hidden;
}
.to {
background: pink;
float: left;
}
.from {
background: lightgreen;
float: right;
}
<div class="chat">
<div class="message">
<div class="to">hi</div>
</div>
<div class="message">
<div class="to">how are u?</div>
</div>
<div class="message">
<div class="from">fine, thnks</div>
</div>
<div class="message">
<div class="to">can u help me?</div>
</div>
<div class="message">
<div class="from">sure, no problem</div>
</div>
</div>
Use float: right; instead of right:0px;.
Here is the code.
<html>
<head>
</head>
<body>
<div style="width:100% height:100%; position: relative; top:0px; left:0px; background-color:white;">
<div style="width:200px; height:100px; position: relative; top:10px; left:0px; background-color:green; font-size:20px;"><p>1</p></div>
<div style="width:200px; height:100px; position: relative; top:10px; float:right; background-color:red; color: white; font-size:20px;"><p>2</p></div>
</div>
</body>
</html>
I've re-structured this question as my previous one was too broad. Hopefully, this is refined enough?
I need to reproduce the same as in the image... Ive spent a day trying to produce it but just cant get it to work.
The red box is a div which can be of varying height or width. The checkbox needs to be centered vertically. Both green divs will be parent containers for other inline elements. The first green box will have a set width and the second will take up the remaining space.
If I have asked this incorrectly then please let me know...how best to ask it...?
Here is my markup so far
#profiles-container {
width: 100%;
height: 100%;
background-color: #dedede;
padding: 20px;
}
.profile-container {
float: left;
width: 50%;
vertical-align: top;
font-size: 0;
box-sizing: border-box;
position: relative;
}
.profile-checkbox {
position: absolute;
width: 40px;
left: 0;
text-align: center;
line-height: 100px;
}
.profile-container-inner {
height: 100px;
background-color: #fff;
border-left: solid 1px #bbb;
border-right: solid 1px #bbb;
border-radius: 5px;
font-size: 13px;
margin-left: 40px;
}
.container1 {
float: left;
width: 200px;
background-color: #ccc;
height: 100%;
}
.container2 {
float: left;
background-color: #ccc;
height: 100%;
}
.profile-bar-color {
background-color: #00bfff;
width: 10px;
float: left;
height: 100%;
}
<ul id="profiles-container">
<li class="profile-container">
<div class="profile-checkbox"><input type="checkbox"/></div>
<div class="profile-container-inner">
<div class="profile-bar-color"> </div>
<div class="container1">
<h3>Annie Jane</h3>
</div>
<div class="container2">Some content</div>
</div>
</li>
<li class="profile-container">
<div class="profile-checkbox"><input type="checkbox"/></div>
<div class="profile-container-inner">
<div class="profile-bar-color"></div>
<div class="container1">
<h3>Joe Bloggs</h3>
</div>
<div class="container2">Some content</div>
</div>
</li>
</ul>
.module {
height: 100px;
width: 100%;
}
.checkbox {
float: left;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.content {
position: relative;
height: 100%;
margin-left: 25px;
}
.fixed-width {
float:left;
height: 100%;
width:180px;
}
.dynamic-width {
height: 100%;
width: 100%;
}
<div class="module" style="background-color: green">
<input class="checkbox" type="checkbox">
<div class="content" style="background-color: orange">
<div class="fixed-width" style="background-color: yellow">
<p>Test</p>
</div>
<div class="dynamic-width" style="background-color: blue">
<p>Test</p>
</div>
</div>
</div>
Use html tables.
CodePen
<table id="container">
<tr>
<td class="left">
<input type="checkbox">
</td>
<td class="center">Center</td>
<td class="right">Right</td>
</tr>
</table>
#container{
width:100%;
height:200px;
background:red;
padding:10px;
}
.left{
background:blue;
width:50px;
vertical-align: middle;
padding:10px;
}
.center{
background:green;
}
.right{
background:green;
width:100%;
}
Here is a code which matches what you need and also centers the text vertically :
.container {
height: 200px;
}
.right {
width:auto;
height:100%;
background:red;
overflow:hidden;
}
.left {
height:100%;
width:100px;
background:blue;
float:left;
}
.left2 {
height:100%;
width:300px;
background:green;
float:left;
}
.vert-center {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);
}
.center {
text-align: center;
}
<div class="container">
<div class="left">
<div class="vert-center center">
<input type="checkbox" name="name" />
</div>
</div>
<div class="left2">
<div class="vert-center">
Here some text
</div>
</div>
<div class="right">
<div class="vert-center">
Here some more text
</div>
</div>
</div>
Code adapted from the well explained answer of Xanthir, by adding another div and vertical aligns :
Expand a div to take the remaining width
Flexbox can do the basic layout.
.container {
height: 100px; /* or any height */
display: flex;
border: 1px solid red;
padding: 1em;
margin: 1em;
}
.container input {
align-self: center; /* vertically centered */
margin-right: 1em;
}
.left,
.right {
border: 1px solid green;
}
.left {
width: 150px; /* fixed width */
background: pink;
}
.right {
flex: 1; /* remaining width */
background: #c0ffee;
}
<div class="container">
<input type="checkbox" />
<div class="left"></div>
<div class="right"></div>
</div>
I have a div with a height en width of 33.33%. I want text in the middle of the div.
HTML
<div class="blogs" id="content">
<div id="blog1">tests</div>
<div id="blog2"></div>
<div id="blog3"></div>
</div>
CSS
#blog1 {
width: 33.33%;
padding-bottom: 33.33%;
background: red;
float: left;
}
How can i make this?
I suggest this:
html
<div class="blogs" id="content">
<div id="blog1">text in the middle
<span>blog 1</span>
</div>
<div id="blog2"><span>blog 2</span></div>
<div id="blog3"><span>blog 3</span></div>
</div>
css
#blog1{
width: 33.33%;
/*padding-bottom: 33.33%;*/
background: red;
text-align: center;
display:table-cell;
vertical-align:middle;
position: relative;
}
.blogs > div > span{
position: absolute;
bottom: 0px;
width: 100%;
left: 0px;
}
#blog2{
width: 33.33%;
padding-bottom: 33.33%;
background: green;
text-align: center;
display:table-cell;
position: relative;
}
#blog3{
width: 33.33%;
padding-bottom: 33.33%;
background: blue;
text-align: center;
display:table-cell;
position: relative;
}
#content{
display:table;
}
fiddle
And another example with static width e.g. 500px fiddle
Have a look at this fiddle.
Just set height and line-height equal and add vertical-align:middle;
Your code will look like this:
#blog1{
width: 33.33%;
height:300px;
background: red;
float: left;
text-align:center;
vertical-align:middle;
line-height:300px; /* has to bee the same value as the height of the div */
}
<div class="blogs" id="content">
<div id="blog1">tests</div>
<div id="blog2"></div>
<div id="blog3"></div>
<!-- You need to add this after the last <div> -->
<div style="clear:right;"></div>
</div>
#blog1, #blog2, #blog3 {
float:left;
padding: 3% 0;
background: red;
width: 100px;
height:100%;
text-align:center;
}
JS Fiddle