Here is a simple box with two columns:
HTML:
<div id="wrapper">
<div class="left">
<div class="image-title">Image title
</div>
<img src="http://placehold.it/100x100">
</div><!-- left -->
<div class="right">
<div class="title">
<h2>Title here</h2>
</div><!-- title -->
<div class="content">
<p>Content here.</p>
<p>Content here.</p>
</div><!-- content -->
</div><!-- right -->
</div><!-- wrapper -->
CSS:
#wrapper {
overflow: auto;
width: 100%;
border: 1px solid red;
padding: 1px;
}
.left {
display: inline-block;
float: left;
width: 30%;
border: 1px solid grey;
text-align: center;
}
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
}
Demo
I expected that the right div should be placed on the right side, but it's placed under the left div element. How can I make it done?
Thanks for your help.
It's really easy to do.
Just use float: right;
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
float: right;
}
You can use box-sizing: border-box; to align it perfectly. No need to use align:right; or adjusted width.
#wrapper {
overflow: auto;
width: 100%;
border: 1px solid red;
padding: 1px;
}
.left {
display: inline-block;
float: left;
width: 30%;
border: 1px solid grey;
text-align: center;
box-sizing: border-box;
}
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
box-sizing: border-box;
}
<div id="wrapper">
<div class="left">
<div class="image-title">Image title
</div>
<img src="http://placehold.it/100x100">
</div><!-- left -->
<div class="right">
<div class="title">
<h2>Title here</h2>
</div><!-- title -->
<div class="content">
<p>Content here.</p>
<p>Content here.</p>
</div><!-- content -->
</div><!-- right -->
</div><!-- wrapper -->
Not sure what you want. But if you want the .right div to be on the right side then you need to apply
float:right
See the demo here.
Also you have a border of 1px outside the box. It needs to be inside you could use box-shadow.
-webkit-box-shadow:inset 0px 0px 0px 1px gray;
-moz-box-shadow:inset 0px 0px 0px 1px gray;
box-shadow:inset 0px 0px 0px 1px gray;
See the demo here.
Update: You have no height on the divs, so the height is being established by your text. Simply apply a height.
.left {
height:130px;
.right {
height:130px;
See the example here.
The first issue here is that you're adding 3px as borders. These aren't counted into the % when you measure the width, which makes the inner divs too wide to fit on one line. if you add the following line to the right div you will see what I mean:
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
float: right;
}
Now we've fixed the second issue; the div will now float to the right as intended, but you can see the boxes are cutting each other's edges. Nowadays there's a nice trick to fix that, and that is box-sizing: border-box; which will automatically match the borders as your div's edge, not outside of it as the standard does.
#wrapper {
overflow: auto;
width: 100%;
border: 1px solid red;
padding: 1px;
}
.left {
display: inline-block;
float: left;
width: 30%;
border: 1px solid grey;
text-align: center;
box-sizing: border-box;
}
.right {
display: inline-block;
width: 70%;
border: 1px solid grey;
float: right;
box-sizing: border-box;
}
<div id="wrapper">
<div class="left">
<div class="image-title">Image title
</div>
<img src="http://placehold.it/100x100">
</div><!-- left -->
<div class="right">
<div class="title">
<h2>Title here</h2>
</div><!-- title -->
<div class="content">
<p>Content here.</p>
<p>Content here.</p>
</div><!-- content -->
</div><!-- right -->
</div><!-- wrapper -->
A minimized css file for this could look like this.
#wrapper {
overflow: auto;
width: 100%;
border: 1px solid red;
padding: 1px;
}
.left, .right {
height:130px;
display: inline-block;
/* choose your border method
border: 1px solid grey;
Or
-webkit-box-shadow:inset 0px 0px 0px 1px gray;
-moz-box-shadow:inset 0px 0px 0px 1px gray;
box-shadow:inset 0px 0px 0px 1px gray;
*/
}
.left {
float: left;
width: 30%;
text-align: center;
}
.right {
float: right;
width: 70%;
}
To the right div to be on the right, you need to add float:right to the css:
.right {
display: inline-block;
width: 70%;
float:right;
border: 1px solid grey;
}
DEMO
Related
I am writing a website in HTML and CSS. For the footer I would like to have 2 boxes for "contact" and "address" aligned, with the contact-box having 25% width and the addressbox having the rest. Somehow my divs won't line up correctly, the address-box won't vertically align with the top.
My attmept:
.footer {
margin-top: 5px;
width: 100%;
border: 1px solid black;
}
.anschrift {
width: 25%;
padding: 5px;
display: inline-block;
border-right: 1px solid black;
border-bottom: 1px solid black;
vertical-align: top;
}
.kontakt {
width: 70%;
margin-left: 28%;
display: inline-block;
padding: 4px;
vertical-align: top;
}
<div class="footer">
<div class="anschrift">
<h2>
Anschrift:
</h2>
<h3>
Lorem ipsum...
</h3>
</div>
<div class="kontakt">
<h2>
Kontakt:
</h2>
<h3>
Lorem ipsum...
</h3>
</div>
</div>
.footer {
margin-top: 5px;
width: 100%;
border: 1px solid black;
}
.anschrift {
width: 25%;
padding: 5px;
display: inline-block;
border-right: 1px solid black;
border-bottom: 1px solid black;
vertical-align: top;
}
.kontakt {
width: 70%;
display: inline-block;
padding: 4px;
vertical-align: top;
float:right;
}
<div class="footer">
<div class="anschrift">
<h2>
Anschrift:
</h2>
<h3>
Lorem ipsum...
</h3>
</div>
<div class="kontakt">
<h2>
Kontakt:
</h2>
<h3>
Lorem ipsum...
</h3>
</div>
</div>
Remove the margin left and float the div to right in .kontakt i.e change the code of .kontakt to
.kontakt {
width: 70%;
display: inline-block;
padding: 4px;
vertical-align: top;
float:right;
}
It's the margin-left of the contact block, which makes the block bigger than you want it to be. Just remove the margin (or make it smaller).
.footer {
margin-top: 5px;
width: 100%;
border: 1px solid black;
}
.anschrift {
width: 25%;
padding: 5px;
display: inline-block;
border-right: 1px solid black;
border-bottom: 1px solid black;
vertical-align: top;
}
.kontakt {
width: 70%;
//margin-left: 28%;
display: inline-block;
padding: 4px;
vertical-align: top;
}
<div class="footer">
<div class="anschrift">
<h2>
Anschrift:
</h2>
<h3>
Lorem ipsum...
</h3>
</div>
<div class="kontakt">
<h2>
Kontakt:
</h2>
<h3>
Lorem ipsum...
</h3>
</div>
</div>
.kontakt:margin-left: 28%; forces a line break as 25%+28%+70% is more than 100%
Actually, If you use the margin in your blocks then you have to reduce same amount width from others blocks/ html div.
So i prefer to use padding instead of margin to make the spacing between the two of multiple column and you have to use box-sizing while using the padding.
box-sizing:border-box
I'm facing a css problem realted to inline-div.
When the text(or sentece) is long, the inline div pushes down as on the image below:
But, when I add a line break, It works perfectly.
How can I make it work without having to use <br>? The main content to be posted is dynamic and it also needs to be responsive.
Thanks
Please Note: This is a simplified version of my actual code. In the
actual code the width of the main container is 100%
HTML
<div id="container">
<div id="firstDiv">FIRST</div>
<div id="secondDiv">SECOND</div>
<div id="thirdDiv">THIRD
<br>some more content<br> some more content
</div>
CSS
body{
width: 350px;
margin: 0px auto;
}
#container {
border: 15px solid orange;
}
#firstDiv{
border: 10px solid brown;
display:inline-block;
width: 70px;
overflow:hidden;
vertical-align:top;
}
#secondDiv{
border: 10px solid skyblue;
float:left;
width: 70px;
}
#thirdDiv{
display:inline-block;
border: 5px solid yellowgreen;
vertical-align:top;
}
use : white-space: nowrap; for the div containing the long sentences.
You can use flexbox. Just add
#container {
display: flex;
}
body {
width: 350px;
margin: 0px auto;
}
#container {
display: flex;
border: 15px solid orange;
}
#firstDiv {
border: 10px solid brown;
display: inline-block;
width: 70px;
overflow: hidden;
vertical-align: top;
}
#secondDiv {
border: 10px solid skyblue;
float: left;
width: 70px;
}
#thirdDiv {
display: inline-block;
border: 5px solid yellowgreen;
vertical-align: top;
}
<div id="container">
<div id="firstDiv">FIRST</div>
<div id="secondDiv">SECOND</div>
<div id="thirdDiv">THIRD
<br>some more content<br> some more content
</div>
Please see this Fiddle:
https://jsfiddle.net/vdb9y2m0/
HTML:
<div class="header">
<div class="container">
Testcontent in centered container
</div>
</div>
<div class="body">
<div class="sidebar">
Content (text) should start at header container
</div>
<div class="content">
Rest of the content, should end at header container
</div>
</div>
CSS:
* {
box-sizing: border-box;
}
.header {
width: 100%;
height: 200px;
background: orange;
}
.container {
width: 600px;
margin: 0px auto;
border-left: 1px solid red;
height: 200px;
border-right: 1px solid red;
}
.sidebar {
width: 30%;
background: #FFF;
float: left;
padding: 20px 20px;
}
.content {
width: 70%;
background: darkblue;
color: #FFF;
padding: 20px 20px;
display: inline-block;
}
I would like to start the content of the left div in the body at the start of the container in the header - and end the div of the main content (darkblue) at the end of the container.
Like so:
If this possible, without using some ugly Javascript hack? Thanks in advance!
How to I align text to the right side of the image icon like the one in the example picture.
I would usually use "float:left" but it's for a mobile responsive design so I prefer not using things like "float:left" because it ruins the red divs height with responsive designs.
I have to align the title, subsitle and a little square image.
It is easy to use float: left and NOT break the height of red border div.
You only have to add display: table-cell to the .app_top block. Here's the solution:
.app_top {
display: table-cell;
}
.app_top img {
float: left;
}
See the working example. Here's the code.
You could use display: inline-block instead.
#main-container {
border: 5px solid #3F3F3F;
width: 270px;
}
.container {
width: 250px;
height: 200px;
border: 5px solid #7F0008;
margin: 5px;
}
.box {
display: inline-block;
vertical-align: top;
width: 85px;
height: 85px;
background: #446C74;
margin: 5px;
}
.content {
display: inline-block;
vertical-align: top;
}
.title, .sub-title {
margin: 0;
padding: 3px 10px 3px 0;
}
.title {
font-size: 17px;
font-weight: bold;
}
.sub-title {
font-weight: bold;
color: #3F3F3F;
}
.img {
background: url(http://placehold.it/100/25);
width: 100px;
height: 25px;
border: 5px solid #EBEAAE;
}
<div id="main-container">
<div class="container">
<div class="box">
</div>
<div class="content">
<p class="title">Title</p>
<p class="sub-title">Sub-Title</p>
<div class="img"></div>
</div>
</div>
<div class="container">
<div class="box">
</div>
<div class="content">
<p class="title">Title</p>
<p class="sub-title">Sub-Title</p>
<div class="img"></div>
</div>
</div>
</div>
Maybe another option is to put the attribute in a parent div instead of the element
html:
<div id="wrapper">
<div class="twoColumn">
<img src="https://pbs.twimg.com/profile_images/444650714287972352/OXTvMFPl.png" />
</div>
<div class="twoColumn">
<p> this is a testingalot test</p>
<button> mybutton </button>
</div>
</div
css:
#wrapper{
border: 1px solid black;
}
.twoColumn{
width: 49%;
float:left;
border: 1px solid red;
}
button{
width: 50px;
height: 40px;
background: red;
}
img{
max-width: 100%;
}
http://jsfiddle.net/Equero/df2wvcet/
I hope it's help
Most simple solution would be to change your markup structure by wrapping your spans in a div just the way you did with the image, then add .app_top div{display:inline-block} .app_top div span{display:block}
.top{
width: 95%;
position: fixed;
background-color: #f7f7f7;
padding-top: 10px;
padding-bottom: 10px;
padding-left: 3%;
padding-right: 3%;
border-bottom: 1px solid #b2b2b2;
}
.search{
width: 100%;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
border: none;
background-color: #e3e3e6;
}
.search::-moz-focus-inner {
border: 0;
padding: 0;
}
.items{
background-color: #ffffff;
width: 100%;
padding-top: 50px;
}
.app{
margin-left: 25px;
margin-right: 25px;
}
.app_top div{display:inline-block}
.app_top div span{display:block}
<div class="main">
<div class="top" >
<input type="text" class="search" />
</div>
<!-- Items -->
<div class="items" style="border: 1px solid black; padding-top: ">
<div class="app" style="border: 1px solid red;" >
<div class="app_top">
<div>
<img src="_img1.jpg" width="95"/>
</div>
<div>
<span class="app_txt" style="border: 1px solid aqua;"> text text - House text house! Las...</span>
<span class="ltd" style="border: 1px solid pink;"> textic-texttive ltd</span>
<span class="" style="border: 1px solid blue;"> </span>
</div>
</div>
<div class="app_bottom">
</div>
</div>
</div>
.content contains all text in right side. If you use overflow:hidden the height of div will stay normal.
img { float:left; }
.content { overflow:hidden; }
I have the following html markup:
.container {
border: 1px solid green;
}
.right {
border: 1px solid #000;
float: right;
width: 40px;
}
.left {
border: 1px solid red;
overflow: hidden;
}
<div class="container">
<div class="right">Right</div>
<div class="left-container">
<div class="left">
Left fluid
<br/>
multiple rows
</div>
</div>
</div>
As you can see right block looks ugly. How could I make right element fluid height 100%?
Add the rule height:100% the right div, and remove float:right. I changed it to position:absolute, so that you didn't need the container's height.
.container {
border: 1px solid green;
position: relative;
width: 100%;
}
.right {
border: 1px solid #000;
width: 40px;
height: 100%;
position: absolute;
right: 0;
}
.left {
display: block;
border: 1px solid red;
overflow: hidden;
margin-right:40px;
}
<br><br><div class="container">
<div class="right">Right</div>
<div class="left-container">
<div class="left">
Left fluid
multiple rows a really long sentence a really long sentence a really long sentence a really long sentence a really long sentence a really long sentence.
</div>
</div>
</div>
If your application will run in a modern browser, then using flexbox is a good way to go: http://jsfiddle.net/2hn9zgog/.
HTML:
<div class="container">
<div class="right">Right</div>
<div class="left">
Left fluid
<br/>multiple rows
</div>
</div>
CSS:
.container {
display: flex;
width: 100%;
outline: 1px dotted gray;
}
.right {
order: 2;
flex: 0 0 auto;
border: 1px solid green;
}
.left {
flex: 1 0 auto;
border: 1px solid red;
}
add clear: both; after floated element.
<div class="right"></div>
<div style="clear: both"></div>
Add
html, body{
height: 100%;
min-height: 100%;
}
.your-container{
height: 100%;
}