How to align div to the bottom of another div in HTML? - html

How do I align div to the bottom of another div in HTML?
And why it doesn't work?
HTML:
<div id="big">
<div class="small">1</div>
<div class="small">2</div>
<div class="small">3</div>
</div>
CSS:
#big {
background-color: red;
margin: 10px;
}
.small {
width: 150px;
height: 150px;
background-color: blue;
float: left;
display: inline-block;
position: absolute;
bottom: 0px;
margin: 10px;
display: inline-block;
}

Your question is unclear but do you mean like this?..
#big {
display:table-cell;
position:relative;
vertical-align:bottom;
background-color: red; margin: 10px; width: 800px; height: 300px;
}
.small {
display: inline-block;
width: 150px; height: 150px; background-color: blue;
margin: 10px;
}
<div id="big">
<div class="small">1</div>
<div class="small">2</div>
<div class="small">3</div>
</div>

This will work:
http://jsfiddle.net/4f4ejwr0/5/
#big {
background-color: red;
margin: 10px;
position: relative;
height: 300px;
}
#bottom {
position: absolute;
bottom: 0px;
margin: 10px;
}
.small {
width: 150px;
height: 150px;
background-color: blue;
float: left;
margin-left: 10px;
}
<div id="big">
<div id="bottom">
<div class="small">1</div>
<div class="small">2</div>
<div class="small">3</div>
</div>
</div>

is this what youre looking for? http://jsfiddle.net/swm53ran/94/
should be changed to
position: relative;

Add the following to your CSS Class:
bottom:0 !important;
and remove the position portion.

Try this
#big {
background-color: red;
margin: 10px;
width: 150px; //new line
}
.small {
width: 150px;
height: 150px;
background-color: blue;
float: left;
position: relative; // new line
margin: 10px;
}
Live jsfiddle
Update: This is ok ? Jsfiddle

Related

How to align element to right side of div box? [duplicate]

This question already has answers here:
How do I right align div elements?
(17 answers)
Closed 3 years ago.
How to align element to right side of div box?
My div
<div id="foo">
<div id="tree">Some Text here</div>
</div>
My css
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
}
I need tree placed at top-right side of foo.
There are a few ways to accomplish this. One way is to add an automatic left margin to the tree:
margin-left: auto;
Another option would be to apply float: right; to the tree, which may or may not result in the content flow you need.
And finally, my recommendation honestly would be to just use flexbox.
Margin Example
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
margin-left: auto;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
Float Example
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
float: right;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
Flex Example
#foo {
display: flex;
justify-content: flex-end;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
display: flex;
width: 100px;
height: 30px;
background: #000000;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
Give float:right to #tree.
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
float: right;
width: 100px;
height: 30px;
background: #000000;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
It is possible with position:absolute
#foo {
display: block;
width: 500px;
height: 500px;
position: relative;
background: #5e5e5e;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
color: #fff;
position: absolute;
right: 0;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
One way would be to use a position: relative / absolute combination:
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
position:relative;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
position:absolute;
right:0;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
Update your css like this. #tree div will always be at the top right corner of #foo
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
position: relative;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
position: absolute;
top: 0;
right: 0;
}

how to stop this div's from overlapping?

So I have 3 divs side by side inside the div element and another div after them. However, this div is overlapping the others. How can I make "footer" come after "main"?
.main {
height: 500px;
width: 100%;
position: absolute;
}
.filter {
background: red;
height: 100%;
float: left;
width: 20%;
}
.post-bar {
background: blue;
height: 100%;
float: left;
width: 60%;
}
.advertisment {
background: green;
height: 100%;
float: left;
width: 20%;
}
.footer {
height: 250px;
width: 100%;
background: black;
position: relative;
}
<div class="main">
<div class="filter">
</div>
<div class="post-bar">
</div>
<div class="advertisment">
</div>
</div>
<div class="footer"></div>
Just get rid off position:absolute in your main class:
.main {
height: 500px;
width: 100%;
}
.filter {
background: red;
height: 100%;
float: left;
width: 20%;
}
.post-bar {
background: blue;
height: 100%;
float: left;
width: 60%;
}
.advertisment {
background: green;
height: 100%;
float: left;
width: 20%;
}
.footer {
height: 250px;
width: 100%;
background: black;
position: relative;
}
<div class="main">
<div class="filter">
</div>
<div class="post-bar">
</div>
<div class="advertisment">
</div>
</div>
<div class="footer"></div>
Just remove the
position: absolute;
display: block;
from
.main
I think you will find your desired result. Please , inform if there are any other issues. Thank you.
Remove positions from main and footer.
.main {
height: 500px;
width: 100%;
float:left;
}
.footer {
height: 250px;
width: 100%;
background: black;
float:left;
}

Make a div's width equal to the width of the image it contains

another newbie question here. Learning CSS. I am trying to do something that I thought would be very simple, but have not managed to find the way to do it, or a suitable answer to the question.
I have a simple project with a header, some content and a footer. The content has a div with a white border and an image inside it. I would like the div to be as wide as the image and no wider. I have provisionally set the width to 430px, but I would like to know the code to set the width to whatever the width of the image is.
Code
html
html,
body {
margin: 0px;
padding: 0px;
height: 100vh;
}
#header {
position: relative;
height: 10%;
width: 100%;
background-color: lightgray;
}
#footer {
position: relative;
height: 10%;
width: 100%;
background-color: lightgray;
}
#container {
height: 80%;
width: 100vw;
background-color: red;
}
#imagewrap {
position: relative;
border: 1px solid white;
width: 430px;
display: block;
margin: 0 auto;
top: 50%;
transform: translateY(-50%);
}
<div id="header"> </div>
<div id="container">
<div id="imagewrap">
<img src="Images/01Folder/Image.jpg" height="100%" id="front" />
</div>
</div>
<div id="footer"> </div>
Add display: inline-block; to your .imagewrap without setting it's width.
.imagewrap {
display: inline-block;
}
If you want a div with an image to be centered, add another div around them with:
.wrapper {
display: flex;
justify-content: center;
align-items: center;
}
But do you really need that div around an image? The border might be added to an image itself without additional div.
If you want a border on the image, add it there
html,
body {
margin: 0px;
padding: 0px;
height: 100vh;
}
#header {
position: relative;
height: 10%;
width: 100%;
background-color: lightgray;
}
#footer {
position: relative;
height: 10%;
width: 100%;
background-color: lightgray;
}
#container {
height: 80%;
width: 100vw;
background-color: red;
}
#imagewrap {
position: relative;
/*border: 1px solid white;
width: 430px;
display: inline-block;
line-height: 0;
margin: 0 auto;*/
top: 50%;
transform: translateY(-50%);
text-align: center; /*center image horizontally*/
}
#imagewrap img {
border: 1px solid white;
}
<div id="header"> </div>
<div id="container">
<div id="imagewrap">
<img src="https://unsplash.it/100/100" height="100%" id="front" />
</div>
</div>
<div id="footer"> </div>
Check out this fidde:
https://jsfiddle.net/56myv9g2/1/
#imagewrap img{
display:block;
}
#imagewrap{
position: relative;
border: 1px solid white;
display: inline-block;
margin: 0 auto;
top: 50%;
transform: translateY(-50%);
}
#container {
height: 80%;
width: 100vw;
text-align:center;
background-color: red;
}
Also, you could just give the border to the image tag all along without the div
If you set display: inline-block, then you need to add text-align: center to container
html,
body {
margin: 0px;
padding: 0px;
height: 100vh;
}
#header {
position: relative;
height: 10%;
width: 100%;
background-color: lightgray;
}
#footer {
position: relative;
height: 10%;
width: 100%;
background-color: lightgray;
}
#container {
text-align: center;
height: 80%;
width: 100vw;
background-color: red;
}
#imagewrap{
position: relative;
border: 1px solid white;
width: 430px;
display: inline-block;
top: 50%;
transform: translateY(-50%);
}
<div id="header"> </div>
<div id="container">
<div id="imagewrap">
<img src="Images/01Folder/Image.jpg" height="100%" id="front" />
</div>
</div>
<div id="footer"> </div>

CSS width:100% textarea for comment (responsive)

I need little help for my CSS.
I am trying to make a comment system but it has something went wrong.
This is my DEMO page from codepen.io
You can see there is a user avatar and textarea. The container max-width:650px; when you reduced width the browser the it is automatically changing.
anyone can help me in this regard?
HTML
<div class="container">
<div class="comment">
<div class="commenter">
<img src="https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11084950_1591056347778266_1536322251_a.jpg">
</div>
<div class="comment-text-area">
<textarea class="textinput" placeholder="Comment"></textarea>
</div>
</div>
</div>
CSS
body {
background-color: #dddddd;
}
.container {
position: relative;
max-width: 650px;
margin: 0px auto;
margin-top: 50px;
}
.comment {
background-color: blue;
float: left;
width: 100%;
height: auto;
}
.commenter {
float: left;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
float: left;
width:100%;
height: auto;
background-color: red;
}
.textinput {
float:left;
width: 100%;
min-height: 35px;
outline: none;
resize: none;
border: 1px solid #f0f0f0;
}
I want to make it like this:
You could try using calc(); to perform the calculation for you... baring in mind you would need to add the vendor prefixes to this.
body {
background-color: #dddddd;
}
.container {
position: relative;
max-width: 650px;
margin: 0px auto;
margin-top: 50px;
}
.comment {
background-color: blue;
float: left;
width: 100%;
height: auto;
}
.commenter {
float: left;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
float: right;
width: calc(100% - 45px);
height: auto;
background-color: red;
}
.textinput {
float:left;
width: 100%;
min-height: 35px;
outline: none;
resize: none;
border: 1px solid #f0f0f0;
}
<div class="container">
<div class="comment">
<div class="commenter">
<img src="https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11084950_1591056347778266_1536322251_a.jpg">
</div>
<div class="comment-text-area">
<textarea class="textinput" placeholder="Comment"></textarea>
</div>
</div>
</div>
as an option instead of float use display: table
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-color: #dddddd;
}
.container {
position: relative;
max-width: 650px;
margin: 0px auto;
margin-top: 50px;
}
.comment {
background-color: #00f;
height: auto;
display: table;
width: 100%;
padding: 5px;
}
.commenter,
.comment-text-area{
display: table-cell;
vertical-align: top;
}
.commenter{
width: 35px;
padding-right: 5px;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
width:100%;
height: 100%;
/*background-color: red;*/
}
.textinput {
width: 100%;
min-height: 35px;
outline: none;
resize: none;
border: 1px solid #f0f0f0;
}
<div class="container">
<div class="comment">
<div class="commenter">
<img src="https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11084950_1591056347778266_1536322251_a.jpg">
</div>
<div class="comment-text-area">
<textarea class="textinput" placeholder="Comment"></textarea>
</div>
</div>
</div>
For scenarios like this I combine float:left;and float:none; The avatar wrapper div gets the float:left; and the comment wrapper div gets the float:none;.
The trick here is to put padding-left on the float:none; div equal to the width of the float:left; div.
.comment {
background-color: blue;
float: left;
width: 100%;
height: auto;
}
.commenter {
float: left;
width:35px;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
float: none;
height: auto;
background-color: red;
padding-left:35px;
}
Here is a working demo
See this fiddle
I have changed your CSS a little bit. See the changes below. The problem with your CSS was that you used no width for .commenter. Thus it took default 100% width.
CSS
.commenter {
float: left;
width: 6%;
}
.comment-text-area {
float: left;
width: 94%;
height: auto;
background-color: red;
}
EDIT
use width for .commenter as width: 35px;..I chose 35px because it is the width of the avatar image.
only change .comment-text-area height:94%
body {
background-color: #dddddd;
}
.container {
position: relative;
max-width: 650px;
margin: 0px auto;
margin-top: 50px;
}
.comment {
background-color: blue;
float: left;
width: 100%;
height: auto;
}
.commenter {
float: left;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
float: left;
width: 94%;
height: auto;
background-color: red;
}
.textinput {
float:left;
width: 100%;
min-height: 35px;
outline: none;
resize: none;
border: 1px solid #f0f0f0;
}
<div class="container">
<div class="comment">
<div class="commenter">
<img src="https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11084950_1591056347778266_1536322251_a.jpg">
</div>
<div class="comment-text-area">
<textarea class="textinput" placeholder="Comment"></textarea>
</div>
</div>
</div>
you can give the class name form-control to the <textarea> like this:
<textarea class="form-control" rows="3" cols="90" ></textarea>
Reference: https://www.w3schools.com/bootstrap/bootstrap_forms_inputs.asp

div layout with float: left

How can I make this html structure
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
be displayed like this while div#1 and #2 have css float:left
( id names are integers only for demonstration purposes )
First of all, you will need to change the id's of your <div>'s to start with an alphabet rather than just one single digit since you won't be able to style your <div>'s using CSS then. Moreover, to achieve the sort of a layout which you're trying to create, you will need to wrap your two floated <div>'s inside a <div> and set the display property of that <div> to inline-block.
Here's a demo:
#one,
#two {
float: left;
}
#one {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#two {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#three {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#one {
background: pink;
}
#two {
background: brown;
}
#three {
background: gray;
}
div#row-left {
display: inline-block;
width: 200px;
overflow: hidden;
vertical-align: top;
}
div#row-right {
display: inline-block;
width: 200px;
vertical-align: top;
}
<div id="row-left">
<div id="one">One</div>
<div id="two">Two</div>
</div>
<div id="row-right">
<div id="three">Three</div>
</div>
Edit: If you want to align the three boxes to the right side of the page then you will need to wrap your HTML inside another <div> and set the text-align property of that <div> to right, like this:
#wrapper {
text-align: right;
}
#one,
#two {
float: left;
}
#one {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#two {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#three {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#one {
background: pink;
}
#two {
background: brown;
}
#three {
background: gray;
}
div#row-left {
display: inline-block;
width: 200px;
overflow: hidden;
vertical-align: top;
}
div#row-right {
display: inline-block;
width: 200px;
vertical-align: top;
}
<div id="wrapper">
<div id="row-left">
<div id="one">One</div>
<div id="two">Two</div>
</div>
<div id="row-right">
<div id="three">Three</div>
</div>
</div>
If you want to keep the given HTML structure, here's two different methods. One is working around the floats, the other is simply using absolute or relative positioning to force the third div into place.
HTML
<div id="d1">One</div>
<div id="d2">Two</div>
<div id="d3">Three</div>
CSS using inline-block (jsfiddle):
div {
width: 200px;
height: 200px;
margin: 10px;
}
#d1 {
float: left;
background-color: rgba(255,0,0,0.3);
}
#d2 {
float: left;
clear: left;
background-color: rgba(0,255,0,0.3);
}
#d3 {
background-color: rgba(0,0,255,0.3);
display: inline-block;
}
CSS using relative positioning (jsfiddle):
div {
width: 200px;
height: 200px;
margin: 10px;
}
#d1 {
float: left;
background-color: rgba(255,0,0,0.3);
}
#d2 {
float: left;
clear: left;
background-color: rgba(0,255,0,0.3);
}
#d3 {
background-color: rgba(0,0,255,0.3);
clear: both;
position: relative;
left: 220px;
top: -430px;
}
Fixed here - http://jsfiddle.net/3147og96/1/
html:
<div class="parent">
<div id="one">1</div>
<div id="two">2</div>
<div id="three">3</div>
</div>
css:
.parent {
height: auto;
width: 120px;
padding: 5px;
padding-left: 110px;
border: 1px solid red;
}
.parent div {
height: 50px;
width: 50px;
border: 1px solid red;
display: inline-block;
}
#one, #two {
float: left;
}