Three DIVs, two are closely, one is left-shifted - html

Have a structure:
<div id="div1">
<div id="div2"></div>
</div>
<div id="div3"></div>
How can I make div3 floating left to div2, and then make a left shift on div2?

Try This:
#div1 {
float: left;
width: 200px;
height: 30px;
border: 5px solid #ccc;
margin: 5px;
padding: 2px;
text-align: center;
}
#div2 {
float: right;
width: 80px;
text-align: center;
border: 5px solid #000;
}
#div3 {
float: left;
width: 200px;
height: 30px;
border: 5px solid red;
margin: 5px;
padding: 2px;
text-align: center;
}
<div id="div1">
div 1
<div id="div2">div 2</div>
</div>
<div id="div3">div 3</div>
Here is jsfiddle : https://jsfiddle.net/nmsptskp/

Use display:inline-block on all 3 div & float:right on div2
#div1 {
border: 1px solid #f00;
height: 20px;
width: 100px;
display:inline-block;
}
#div2 {
float: right;
border: 1px solid #0f0;
height: 20px;
width: 50px;
display:inline-block;
}
#div3 {
border: 1px solid #00f;
height: 20px;
width: 50px;
display:inline-block;
}
<div id="div1">
<div id="div2"></div>
</div>
<div id="div3"></div>

My answer is using flexbox css
.container{
display: flex;
}
#div1{
flex-grow: 1;
border:5px solid #ccc;
text-align:center;
display: flex;
justify-content: space-between;
flex-direction: col;
}
#div2{
width: 50%;
text-align:center;
border:5px solid #000;
self-
}
#div3{
flex-grow: 1;
border:5px solid red;
text-align:center;
}
<div class="container">
<div id="div1">
div 1
<div id="div2"> div 2</div>
</div>
<div id="div3"> div 3</div>
</div>

Use float left and right
<style>
#div1{width:300px;height:50px;background:green;margin:10px;float:left;}
#div2{width:100px;height:50px;background:red;float:right;opacity:0.7}
#div3{width:100px;height:50px;background:yellow;margin:10px;float:left;}
</style>
<div id="div1">
<div id="div2"></div>
</div>
<div id="div3"></div>

Related

Creating a layout using HTML and CSS

The below diagram was given to me in an interview questions and the interviewer told me that I am missing clear:both in my code.
I tried something like this. But couldn't get the desired results
.name3 {
border: 1px solid black;
height: 50px;
width: 90px;
}
.name {
border: 1px solid black;
height: 10px;
width: 90px;
}
.name1 {
border: 1px solid black;
height: 40px;
width: 30px;
}
#name2 {
border: 1px solid black;
height: 20px;
width: 30px;
float: left;
}
<body>
<div class="name3">
<div class="name"></div>
<div class="name1"></div>
<div id="name2"></div>
<div id="name2"></div>
<div id="name2"></div>
<div id="name2"></div>
</div>
</body>
Try this
.name3 {
border: 1px solid black;
height: 55px;
width: 100px;
float: left;
}
.name {
border: 1px solid black;
height: 10px;
width: 99px;
float: left;
}
.name1 {
border: 1px solid black;
height: 42px;
width: 34px;
float: left;
}
#name2 {
border: 1px solid black;
height: 20px;
width: 30px;
float: left;
}
<body>
<div class="name3">
<div class="name"></div>
<div class="name1"></div>
<div id="name2"></div>
<div id="name2"></div>
<div id="name2"></div>
<div id="name2"></div>
</div>
</body>
I tried to reproduce this with Flexbox.
Here you can learn more: https://www.w3schools.com/css/css3_flexbox.asp
.top,.side,.square {
padding: 5px;
box-sizing: border-box;
border: 1px solid black;
}
.container {
display: flex;
flex-direction: column;
width: 200px;
}
.container .main {
display: flex;
flex-direction: row;
max-width: 200px;
}
.container .main .content {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.container .main .content .square {
width: 50%;
}
<div class="container">
<div class="top">.top</div>
<div class="main">
<div class="side">.side</div>
<div class="content">
<div class="square">.square</div>
<div class="square">.square</div>
<div class="square">.square</div>
<div class="square">.square</div>
</div>
</div>
</div>
Hi SIMIN i have read your question and here is my solution to that. Copy past the code below in your editor. Also note that i was using opera browser for running this code, so if you are using different browser there may be a little difference in output. Good luck
<html>
<head>
<style>
.name3{
border: 1px solid black;
height: 53px;
width: 93px;
}
.name{
border: 0.5px solid black;
height: 10px;
width: 92px;
float: left;
}
.name1{
border: 0.5px solid black;
height: 41px;
width: 30px;
float: left;
clear: left;
}
#name2one{
height: 20px;
width: 30px;
border: 0.5px solid black;
float: left;
clear: none;
}
</style>
</head>
<body>
<div class="name3">
<div class="name"></div>
<div class="name1"></div>
<div id="name2one"></div>
<div id="name2one"></div>
<div id="name2one"></div>
<div id="name2one"></div>
</div>
</body>
</html>
How about using percentage in width
<div class="wrapper">
<div class="header"></div>
<div class="sidebar"></div>
<div class="article"></div>
<div class="article"></div>
<div class="article"></div>
<div class="article"></div>
</div>
.wrapper, .header, .sidebar, .article{
border: 1px solid black;
float: left;
}
.wrapper {
height: 100px;
width: 100px;
}
.header{
width: 100%;
height: 20px;
}
.sidebar {
height: 80px;
width: 33.33%;
}
.article {
height: 40px;
width: 33.33%;
}

How to create this layout using "display:inline-block or block or inline" properties in style sheet?

I'm stuck with wrong output.
Code:
<!DOCTYPE html>
<html>
<head>
<style>
.largebox { display: block; margin: 10px; border: 3px solid #73AD21; }
.box1 { display:inline-block; width:20%; height:200px; border:2px solid red; }
.box2 { display:inline-block; width:78%; height:100px; border:2px solid red; }
.col1 { display:inline-block; border:2px solid red; }
</style>
</head>
<body>
<div class="largebox"> <div class="box1">
<div class="leftbox"></div>
</div>
<div class="box2">
<div class="col1">float</div>
</div>
</body>
</html>
You can create this layout with Flexbox.
.largebox, .bottom, .box1 {
display: flex;
flex: 1;
}
.box2 {
flex: 3;
}
.box {
border: 1px solid black;
margin: 10px;
padding: 25px;
flex: 1;
}
<div class="largebox">
<div class="box1">
<div class="box">Div</div>
</div>
<div class="box2">
<div class="box">Div</div>
<div class="bottom">
<div class="box">Div</div>
<div class="box">Div</div>
<div class="box">Div</div>
</div>
</div>
</div>
This is how you can create same layout with inline-block, note that height on container is fixed.
* {
box-sizing: border-box;
}
.largebox {
height: 300px;
}
.bottom, .box1, .box2 {
display: inline-block;
vertical-align: top;
}
.box1 {
width: calc(30% - 10px);
height: 100%;
}
.box2 {
width: calc(70% - 10px);
height: 100%;
margin-left: 5px;
}
.box {
border: 1px solid black;
margin: 10px;
padding: 25px;
display: inline-block;
width: 100%;
height: 100%;
}
.box2 > .box {
height: 50%;
margin-bottom: 0;
width: calc(100% - 10px);
}
.bottom {
width: 100%;
height: 50%;
padding-bottom: 10px;
}
.bottom > .box {
width: calc(33.334% - 10px);
margin-right: 0;
}
<div class="largebox">
<div class="box1">
<div class="box">Div</div>
</div>
<div class="box2">
<div class="box">Div</div>
<div class="bottom">
<div class="box">Div</div><div class="box">Div</div><div class="box">Div</div>
</div>
</div>
</div>
I'd consider experiment with CSS property flex: https://developer.mozilla.org/en/docs/Web/CSS/flex
or use some grid templating system, e.g. Bootstrap https://getbootstrap.com/examples/grid/

Aligning Boxes next to eachother

I have 3 simple boxes that I would like to align horizontally next to each other. But currently they are just stacking on-top of each other vertically. I am using Twitter Boostrap as well for the 'row' class.
HTML:
.img-box-kai {
width: 300px;
height: 450px;
border: 3px solid red;
}
.img-box-lucas {
width: 300px;
height: 450px;
border: 3px solid red;
}
.img-box-bryant {
width: 300px;
height: 450px;
border: 3px solid red;
}
<div class="container">
<div class="row">
<div class="img-box-kai">Rectangle Test</div>
<div class="img-box-lucas">Rectangle Test</div>
<div class="img-box-bryant">Rectangle Test</div>
</div>
</div>
This is what they look like currently: picture
If you intend on using Bootstrap, you'll want to remove the set widths and use the grid system to stack them horizontally using Bootstrap:
.img-box-kai {
height: 450px;
border: 3px solid red;
}
.img-box-lucas {
height: 450px;
border: 3px solid red;
}
.img-box-bryant {
height: 450px;
border: 3px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-4 img-box-kai">Rectangle Test</div>
<div class="col-xs-4 img-box-lucas">Rectangle Test</div>
<div class="col-xs-4 img-box-bryant">Rectangle Test</div>
</div>
</div>
Use display: flex on .row, like:
.row {
display: flex;
}
Have a look at the snippet below:
.img-box-kai {
width: 300px;
height: 450px;
border: 3px solid red;
}
.img-box-lucas {
width: 300px;
height: 450px;
border: 3px solid red;
}
.img-box-bryant {
width: 300px;
height: 450px;
border: 3px solid red;
}
.row {
display: flex;
justify-content: center; /* Aligning Horizontally */
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="img-box-kai">Rectangle Test</div>
<div class="img-box-lucas">Rectangle Test</div>
<div class="img-box-bryant">Rectangle Test</div>
</div>
</div>
Hope this helps!
you need to add float:left to each of them.
like
.img-box-kai {
width: 300px;
height: 450px;
float:left;
border: 3px solid red;
}
.img-box-lucas {
width: 300px;
height: 450px;
float:left;
border: 3px solid red;
}
.img-box-bryant {
width: 300px;
height: 450px;
float:left;
border: 3px solid red;
}
You can add this code and will work.
.row{
display: flex;
flex-direction: row
}
.img-box-kai {
width: 300px;
height: 450px;
border: 3px solid red;
}
.img-box-lucas {
width: 300px;
height: 450px;
border: 3px solid red;
}
.img-box-bryant {
width: 300px;
height: 450px;
border: 3px solid red;
}
.row{
display: flex;
flex-direction: row
}
<div class="container">
<div class="row">
<div class="img-box-kai">Rectangle Test</div>
<div class="img-box-lucas">Rectangle Test</div>
<div class="img-box-bryant">Rectangle Test</div>
</div>
</div>
You need to use Bootstrap class to each of them. like
HTML:
<div class="container">
<div class="row">
<div class="img-box-kai col-md-4 pull-left">Rectangle Test</div>
<div class="img-box-lucas col-md-4 pull-left">Rectangle Test</div>
<div class="img-box-bryant col-md-4 pull-left">Rectangle Test</div>
</div>
</div>
CSS:
.img-box-kai {
min-height: 450px;
border: 3px solid red;
}
.img-box-lucas {
min-height: 450px;
border: 3px solid red;
}
.img-box-bryant {
min-height: 450px;
border: 3px solid red;
}

Making a footer that has small div boxes inside of it responsive to the size of the browser window

Within a footer there are 4 small boxes (created with divs that have a red border around them) and they all need to be made responsive to the width of the browser window as it is re-sized. They need to be centered and have an equal percentage space in between each other no matter what the window size is.
Like this: http://s7.postimg.org/tvmmw91jf/theboxes.png
Fiddle: http://jsfiddle.net/NightSpark/1L5027qr/
#footer {
width: 100%;
clear: both;
text-align: center;
background-color: black;
opacity: 0.7;
height: 200px;
}
#fbox1 {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
position: inline-block;
float: left;
}
#fbox2 {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
position: inline-block;
float: left;
}
#fbox3 {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
position: inline-block;
float: left;
}
#fbox4 {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
position: inline-block;
float: left;
}
<body>
<div id="footer">
<div id="fbox1">
</div>
<div id="fbox2">
</div>
<div id="fbox3">
</div>
<div id="fbox4">
</div>
<div>
</body>
Update: I put in a clearer illustration above than the one I had at first.
The easiest thing you could do to center the elements is using CSS Flexbox.
Here's the HTML :
<div id="footer">
<div id="fbox1">
</div>
<div id="fbox2">
</div>
<div id="fbox3">
</div>
<div id="fbox4">
</div>
</div>
Here's the CSS :
#footer {
display: flex;
flex-direction: row;
justify-content: space-between;
clear: both;
background-color: black;
opacity: 0.7;
height: 200px;
}
#fbox1 {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
position: inline-block;
}
#fbox2 {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
position: inline-block;
}
#fbox3 {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
position: inline-block;
}
#fbox4 {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
position: inline-block;
}
Here's a Fiddle : http://jsfiddle.net/1L5027qr/1/
You can create a 25% width around each div.
<div id="footer">
<div style="width:25%;display:inline-block;text-align:center;">
<div id="fbox1">
</div>
</div><div style="width:25%;display:inline-block;text-align:center;">
<div id="fbox2">
</div>
</div><div style="width:25%;display:inline-block;text-align:center;">
<div id="fbox3">
</div>
</div><div style="width:25%;display:inline-block;text-align:center;">
<div id="fbox4">
</div>
</div>
</div>
If you are able to modify the mark-up a little:
<div id="footer">
<div id="fbox1" class="outer">
<div class="inner">...</div>
</div>
<div id="fbox2" class="outer">
<div class="inner">...</div>
</div>
<div id="fbox3" class="outer">
<div class="inner">...</div>
</div>
<div id="fbox4" class="outer">
<div class="inner">...</div>
</div>
<div>
CSS:
#footer {
width: 100%;
clear:both;
}
#footer .outer {
width: calc(100% / 4 - 4px);
text-align: center;
display: inline-block;
margin: 0px;
border: 0px;
}
#footer .inner {
border: 5px outset #ea2f2f;
width: 100px;
height: 100px;
display: inline-block;
}
Fiddle: http://jsfiddle.net/simbunch/wcvb88yg/

trying to make 3 divs centered inside a div

Hi I'm trying to center 3 divs inside of a div.
.outer {
border: 1px solid blue;
width: 80%;
}
.inner {
border: 1px solid red;
float: left;
width: 20px;
height: 20px;
margin-left: 20px;
}
.clear {
clear: both;
}
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner"></div>
<div class="clear"></div>
</div>
Can someone help on this.
Thanks.
If you want to achieve this just:
HTML:
<div class="outer">
<div style="display: inline-block;">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner"></div>
</div>
</div>
CSS:
.outer{
border: 1px solid blue;
width: 80%;
text-align: center;
}
.inner:first-of-type{
margin-left: 0px;
}
.inner{
border: 1px solid red;
width: 20px;
height: 20px;
margin-left: 20px;
float: left;
}