Two div classes side-by-side - html

Even though I used float, my two div classes do not want to align side-by-side. How to do it?
Basically the entire width is 520px and each box is 250px in width with a margin between the boxes of 20px.
<div id="car-box">
<div class="well-car">
<div class="add_box">
<h1 class="add_heading">car model</h1>
</div>
</div>
<div class="car-brand">
<a class="button" href="www.placehold.it">car brand</a>
</div>
</div>
And CSS:
.car-box {
width:520px;
height:500px;
border:5px dashed blue;
margin-right:10px;
float:left
}
.well-car {
width:250px;
height:250px;
border:10px solid red;
}
.car-brand {
width: 250px;
height:250px;
border:10px dashed blue;
font-size: 20px;
float:left
}
Here Fiddle...Fiddle

Your border width gets added to the content widths. 250+2*10 + 250+2*10 == 540.
(You can read here https://developer.mozilla.org/en/docs/Web/CSS/box-sizing how do browsers calculate block elements' sizes)
For your custom styles it's usually best to set box-sizing: border-box(http://www.paulirish.com/2012/box-sizing-border-box-ftw/)
Edit: and yes, also float:left on the .well-car class, as others pointed out.

You need to float .well-car as well:
http://jsfiddle.net/b3kd9mwf/26/

You just need to add float: left; to your div with the class "well-car".
.well-car {
width:250px;
height:250px;
border:10px solid red;
float: left;
}

You are not floating your elements correctly. Class wellcar should be floated to the left and class car-brand should be floated to the right. The following code should work.
#car-box {
width:520px;
height:500px;
border:5px dashed blue;
margin-right:10px;
}
.well-car {
width:250px;
height:250px;
border:10px solid red;
float: left;
}
.car-brand {
width: 250px;
height:250px;
border:10px dashed blue;
font-size: 20px;
float:right;
}

Related

Fix floating box in Html

So I want my boxes to stay the same position even when I shrink my window.
But for now when I shrink them, they will resize and push the floating box to the bottom space and not making a line together.
.bodybox1
{
border:1px solid black;
width:45%;
background:white;
margin:1% auto 2% auto;
text-align:center;
font-size:2em;
padding:3%;
overflow:hidden;
}
.boxcoverblack
{
text-align:center;
width:120px;
padding:7%;
border:1px solid black;
margin-top:2%;
}
.signuparea
{
width:30%;
float:right;
border:1px solid black;
text-align:center;
padding:3%;
overflow:hidden
}
.signuptext
{
text-align:center;
width:180px;
padding:7%;
word-spacing:15px;
border:1px black solid;
line-height:35px;
}
.freeshipping
{
float:left; /*image*/
}
<div class="bodybox1">
<img class="freeshipping" src="https://via.placeholder.com/500x300" width="500" height="300" >
<div class="signuparea">
<div class="signuptext">Sign up to get freeshipping coupon code! ( per phone number )
</div>
<div class="boxcoverblack">Sign up</div>
</div>
</div>
help me please....
http://jsfiddle.net/abxf6c9h/
.bodybox1 {
display:flex;
justify-content:center;
border: 1px solid black;
width: 100%;
background: white;
margin: 1% auto 2% auto;
text-align: center;
font-size: 2em;
padding: 3%;
overflow: hidden;
}
Try this. I guess you want this box displayed as flexed. Tell me if that gave you a hint . I just added:
display:flex;
justify-content:center;
See my fiddle:
http://jsfiddle.net/abxf6c9h/1/
You need to use flexbox and a bit of arrangements :
use
display:inline-block
instead of
float:left
use vertical-align:top to remove the auto centering align from the wraping div
and finaly
display:flex;
flex-wrap:nowrap;
to the parent of the 2 involved divs
Here's the result
fiddle

Padding change the layout two div side by side

I want to make div side by side , I can achieve this but when I add som margin or padding they can disturb the lay out, I just want that two div display side by side with padding and margin property.
#center{
width:100%;
border:1px solid gray;
overflow:hidden;
display:inline-block;
}
#leftdiv{
height:200px;
width:50%;
background-color:gray;
float:left;
margin:2px;
}
#rightdiv{
height:200px;
width:50%;
background-color:yellow;
float:left;
margin:2px;
}
<div id="center">
<div id="leftdiv"></div>
<div id="rightdiv"></div>
</div>
To use padding on the <div>s you can set the box-sizing property to border-box so the padding is included in the width of the <div>. But the margin is more difficult to include in the width because it is on the outside of the box. So you have to calculate the margin on the width (see example on #leftdiv):
#center{
width:100%;
border:1px solid gray;
overflow:hidden;
display:inline-block;
}
#leftdiv{
height:200px;
width:calc(50% - 20px); /** 20px = sum of margin left and right */
background-color:gray;
float:left;
padding:10px;
margin-right:20px;
box-sizing:border-box;
}
#rightdiv{
height:200px;
width:50%;
background-color:yellow;
float:left;
padding:10px;
box-sizing:border-box;
}
<div id="center">
<div id="leftdiv"></div>
<div id="rightdiv"></div>
</div>
border-box: The width and height properties include the content, the padding and border, but not the margin.
content-box: This is the initial and default value as specified by the CSS standard. The width and height properties are measured including only the content, but not the padding, border or margin.
source: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing#Values
You can see the box model on the Chrome Developer Tools:
There you can see the margin surrounding the border. The width and height is calculated until the border and doesn't include the margin.
just add
#rightdiv,#leftdiv{
box-sizing:border-box;
}
You are going to have to change their display type from block
and css is:
#center{
width:100%;
border:1px solid gray;
overflow:hidden;
display:inline-block;
}
#leftdiv{
height:200px;
width:50%;
background-color:gray;
float:left;
margin:2px;
display: inline-block;
}
#rightdiv{
height:200px;
width:50%;
background-color:yellow;
float:left;
display: inline-block;
margin:2px;
}
This should allow them to respond and align side by side.
margin will apply (space) to the outside of the boxmodel.
padding will apply (space) to the inside of the boxmodel - use in conjuction with box-sizing: border-box; to negate additional padding affecting the inherit height and width of the element.
Where alignment is concerned, in this case, you have a few options to explore:
#center {
width: 100%;
border: 1px solid gray;
overflow: hidden;
}
.inline-div {
height: 200px;
width: 48%;
display: inline-block;
margin: 2px;
}
.float-left {
float: left;
}
.float-right {
float: right;
}
.flex-wrapper {
display: flex;
justify-content: space-between;
}
.flex-wrapper .inline-div {
flex: 1;
}
#leftdiv {
background-color: gray;
}
#rightdiv {
background-color: yellow;
}
<h1>Inline</h1>
<div id="center">
<div id="leftdiv" class="inline-div"></div>
<div id="rightdiv" class="inline-div"></div>
</div>
<h1>Float</h1>
<div id="center">
<div id="leftdiv" class="inline-div float-left"></div>
<div id="rightdiv" class="inline-div float-right"></div>
</div>
<h1>Flex</h1>
<div id="center" class="flex-wrapper">
<div id="leftdiv" class="inline-div"></div>
<div id="rightdiv" class="inline-div"></div>
</div>
Let's first examine #center's css. You are set width to 100% and 1px for border(1px on the left and 1px on the right) which mean that actual width will be 100% + 2px, which might be not exactly what you want. To solve this you can use either box-sizing:border-box; or width:calc(100% - 2px). Also you might not need "overflow:hidden" and "display:inline-block"
Box-sizing is really useful property. You can read more here: https://www.w3schools.com/cssref/css3_pr_box-sizing.asp
#center { #center {
width:100%; width:calc(100% - 2px);
box-sizing:border-box; or border:1px solid gray;
border:1px solid gray; }
}
Then in order to have 2 children side by side you can use either flex layout or float layout as you did, but again you have assume that "width:50%" is actually without the margin so real width will be 50% + 4px (2px left + 2px right) margin. In order to solve this you can use again calc();
#leftdiv { #rightdiv {
height:200px; height:200px;
width:calc(50% - 4px); width:calc(50% - 4px);
background-color:gray; background-color:gray;
float:left; float:right;
margin:2px; margin:2px;
}
Also have in mind that because the children elements are floated, the parent element will have a height of 0. In order to make parent element to wrap its children you must either set some height of #center element (in your case 204px, 200px for children and 4px for its margin) or to use the following css which does the trick. The css will add empty block element right after both children(because it has propeerty "clear") and because it is block element, the parent will extend.
#center:after {
content:"";
display:block;
clear:both;
}
First of all you have to divide this within 100% width with margin as i have done!
#center{
width:100%;
border:1px solid gray;
overflow:hidden;
display:inline-block;
}
#leftdiv{
height:200px;
width:48.5%;
background-color:gray;
float:left;
margin:1%;
margin-right:0px;
}
#rightdiv{
height:200px;
width:48.5%;
background-color:yellow;
float:left;
margin:1%;
}
<div id="center">
<div id="leftdiv"></div>
<div id="rightdiv"></div>
</div>

How do I make a inline-block div occupy the rest of the space

I have a link and an inline div next to it (to the right). I want the div to occupy the rest of the space to the right. Is there a way to do that?
what<div style="display:inline-block;width:200px;border:1px solid red">hello</div>
If you can wrap a span around the div, like:
what<span><div>hello</div></span>
jsFiddle example
You can apply this CSS to get what you're after:
a {
background: #ccc;
float: left
}
span {
display: block;
overflow: hidden;
padding: 0 4px 0 6px
}
div {
width: 100%;
display:inline-block;
border:1px solid red
}
You could wrap everything within a div and give it table and table cell to children:
http://jsfiddle.net/T4Qcd/
.inner{
border:1px solid red;
display:table-cell;
width:100%;
}
a{
display:table-cell;
}
.wrapper{
display:table;
}
You can do so by applying width to both anchor tag as well as the div.
a{
width:10%;
}
div{
width:90%;
}
Your html would be.
what
<div style="display:inline-block;border:1px solid red">hello</div>

why my div is slightly moved to the right

So I'm just a begginer to this HTML and CSS stuff, and I tried to make my own webpage. The thing is, it looks like this:
While I would like to get the second div(#diary) centered, but I can't do it without screwing up the whole webpage. Which will be the correct code?
This is what I have:
HTML:
<div id="progress">
Blablabla
</div>
<div id="diary">
blablabla
</div>
CSS:
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin:auto;
width:30em;
display:inline-block;
}
#progress {
font-size:16px;
width:auto;
float:left;
display:inline-block;
margin-left:25px;
}
Thanks in advance ^^
You have mixed display: inline-block and float:left which makes no sense. Elements that float become display: block; by default. http://www.w3.org/TR/CSS2/visuren.html#float-position
There are two ways to solve your problem.
Way1 Go Inline-block all the way:
http://jsfiddle.net/fDx2U/
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin:auto;
width:30em;
display:inline-block;
vertical-align: top;
}
#progress {
font-size:16px;
width:auto;
vertical-align: top;
display:inline-block;
margin-left:25px;
}
How to the rid of the margin between the items: How to remove the space between inline-block elements?
Vital for this solution is the vertical-align:top; (your initial problem)
Way2 Go floating all the way:
http://jsfiddle.net/fDx2U/1/
div {
border: 7px solid #142538;
background-color: #c7d0e1;
}
#diary {
margin-left: 100px;
}
#progress {
font-size:16px;
width:auto;
float:left;
margin-left:25px;
width: 100px;
}
Vital for this solution is that the width of .diary equals the margin-left of #progress
Try this
#diary {
margin:0 auto;
width:30em;
display:block;
}

text wrapping below image

I have a small image and i have to show some text beside that image. for that i have used the below html and css.
<div class="main">
<img alt="image"/>
<h2>this is heading text. This is heading text. This is heading text</h2>
</div>
.main{
border:1px solid black;
height:200px;
width:400px;
padding:20px;
}
h2{
display:inline;
}
it is showing like this
The second line is wrapping below the image. I have to get the second line just below the first line not below the image.
I tried using float also. but not working. please help.
I created a fiddle so you can edit it easily: http://jsfiddle.net/codingsolver/MtqHh/1/
You could simply float the image, and push the h2 across with a left margin.
http://jsfiddle.net/MtqHh/8/
img { float: left; }
h2{ margin: 0 0 0 50px; }
hope it will help you
.main{
border:1px solid black;
height:200px;
width:400px;
padding:20px;
}
h2{
display: flex;
}
img{
float:left;
margin-right:10px;
}
Working demo http://jsfiddle.net/MtqHh/13/
.main{
border:1px solid black;
height:200px;
width:400px;
padding:20px;
display: inline-flex;
}
.main img{
float: left;
width: 50px;
height: 50px;
}
h2{
float:left;
margin-left: 50px;
text-align: left;
display: inline-block;
padding;0px;
margin:0px;
}
use this code usefull for you. and see this link http://jsfiddle.net/bipin_kumar/MtqHh/10/
A good way of achieving this is shown on an updated fiddle:
http://jsfiddle.net/y5ewr/1/
The advantage is highlighted in the use of overflow:hidden on the <h2>. This means that if the <img> is not in place the heading will flow full width and no margins are needed on the heading element.