fitting content into a div without it dropping down - html

I have created a demo of my issue, I am trying to force text next to an image but when you shrink the browser window is knocks it down to underneath the image.
Jsfiddle
I need a better approach to this.
.avatar_ad_bg_desktop{
width: 100px;
height: 100px;
background-image: url('http://blogs.terrapinn.com/total-real-estate/files/2012/04/SAV_CMYK_LogocopyEng.jpg');
background-repeat: no-repeat;
background-size:cover;
float:left;
}
<div style="background: red; height: auto; overflow:auto;">
<div style="background: green;">title</div>
<div style="background: blue;">Web address</div>
<div style="float:left;">
<div class="avatar_ad_bg_desktop">
</div>
<div style="float: left; margin: 0 0px 0 10px; vertical-align:top; word-break: break-all;">Savills plc is a global real estate services provider listed on the London Stock Exchange</div>
</div>
</div>

Instead of setting float:left on the div with the text, set it with overflow:auto (or hidden)
FIDDLE
Now resize the browser window and see that the text fills up the remaining width

You can use table and table-cell format to get better result. Update your HTML like below.
<div style="background: red; height: auto; overflow:auto;">
<div style="background: green;">title</div>
<div style="background: blue;">Web address</div>
<div style="display: table;">
<div class="avatar_ad_bg_desktop">
</div>
<div style="display: table-cell; margin: 0 0px 0 10px; vertical-align:top; word-break: break-all;">Savills plc is a global real estate services provider listed on the London Stock Exchange</div>
</div>
</div>
</div>
DEMO

You can use the display:table / table-line / table-cell properties of CSS and remove the float:left declarations.
You set the container with display:table;
then you can have a display:table-cell; for each cells that must stay on the same line.
Here is an updated fiddle
http://jsfiddle.net/88anzh58/4/

Related

Alignment issues with html divs stacked on top of each other

I am having some difficulty with some html code (I have never done html programming of any significance) where I am trying to do something fairly simple. Actually I broke it down to the simplest form for now. So I have three blocks on top of each other. The first block at the very top has three sub boxes within it horizontally. I fixed the height of this block at 250px since my text fits into it. However my second block (Center) overlaps with the Top div. How do I specify that the center div start after the Top div? I want it to display a few pixels below the Top div.
<div id="Report" style="height: auto">
<div id="Top" style="width:inherit; height:250px">
<div id="First" class="TopMostLeft" >
<span style="font-family:Calibri; font-size:small">Info</span>
<table style="width:100%" > ... </table>
<div id="Second" class="TopMostCenter">
<span style="font-family:Calibri; font-size:small">Info2</span>
<div id="Third" class="TopMostRight">
<div id="About" class="TopRightDiv">
<table style="width:100%">
</div>
<div id="Center" style="border:solid; border-width:2px; border-color:lightgray; padding:4px; margin:10px">
<div id="Bottom" style="border-width:2px;border-width:2px; border-color:lightgray; padding:4px; margin:10px">
</div>
Here's what you need. Create a stylesheet so we can style it much better.
Working Fiddle: https://jsfiddle.net/v9jgj7n3/
I created your layout. This is how I understand what you need.
HTML
<div id="Report">
<div id="Top">
<div id="First" class="TopMostLeft" ></div>
<div id="Second" class="TopMostCenter"></div>
<div id="Third" class="TopMostRight"></div>
</div>
<div id="Center"></div>
<div id="Bottom"></div>
</div>
CSS
#Top {
width: 100%;
display: flex;
height: 250px;
}
#Top #First {
width: 10%;
background: red;
}
#Top #Second {
width: 40%;
background: blue;
}
#Top #Third {
width: 50%;
background: yellow;
}
#Center {
height: 200px;
width: 100%;
background: gray;
}
#Bottom {
height: 80px;
width: 100%;
background: black;
}
Edit the width and height to your desired value. This is how it will work
Also, I notice that you didn't close the child element of Top. You must always close DIV, so the HTML code will run well. It breaks your code.
Hope it helps. Cheers! Good Morning from Philippines.

CSS DIV Alignment with dynamic content

My question is about CSS and DIV tags. I have a dynamic page, and I would like one container DIV. There are two scenarios: in one case, this container DIV will just have one DIV in it, with a width of 50%, and should be centered. In the other case, there will be two DIVs in it, and they should be side by side, each taking up 50%.
I have tried float:center (using overflow: hidden on the container DIV), and that works for 1 DIV in it, but with two, they are stacked on top of each other. If I use float: left, then the 2 DIVS appear correct, but the single DIV is left aligned, not centered.
Any help on how to achieve this effectively would be greatly appreciated!
<div style="width:800; margin: 2px; background-color:blue">
<div style="width:50%; background-color:orange;">
Text
</div>
<div style="width:50%; background-color:red;">
Text
</div>
</div>
jsFiddle
For the two-div scenario:
<div style="width:800; margin: 2px; background-color:blue; display: table;">
<div style="background-color:orange; display: table-cell;">
Text
</div>
<div style="background-color:red; display: table-cell;">
Text
</div>
</div>
Now for the one-div scenario:
<div style="width:800; margin: 2px; background-color:blue; display: table;">
<div style="background-color:orange; display: table-cell;">
Text
</div>
</div>
In each case, the inner divs, whether there are 1 or 2, will take up a combined 100% of the outer div. Essentially, it acts like the <table> element without having the semantics of a <table>.
check this fiddle
HTML
<div class="wrapper">
<div class="divholder">
<div style="background-color:orange;">DIV 1</div>
<div style="background-color:red;">DIV 2</div>
</div>
</div>
CSS
.divholder div{
display:inline-block;
margin:auto;
width:49%;
}
.divholder {
text-align:center;
margin: 0 auto;
position:relative;
}
.wrapper{
width:100%;
background-color:blue;
}
This perfectly deals with your need..While there is only one div, the div gets centered and if two divs come then both will be equally divided and floated left.Please see the fiddle..
Similar to chharvey's answer, this can be achieved nicely with display: table;
In my example it is centered horizontally and will work with any number of columns as they will fit themselves to the full width of div.wrap. The columns are currently given a height of 100%, this can be adjusted.
Have a jsBin example!
HTML
<div class="wrap">
<div class="column">
</div>
<div class="column">
</div>
</div>
CSS
html,body {
height: 100%;
}
.wrap {
display: table;
width: 800px;
height: 100%;
margin: 0 auto;
}
.column {
display: table-cell;
background: #FF0;
}
.column:first-child {
background: #F00;
}

Fluid and fixed column

I have this code: http://jsfiddle.net/spadez/TPKLv/
<div style="background-color:white; width: 400px;">
<span class="status" id="active"></span>Title
6
</div>
<div style="width: 100%; background-color: white; float:right;">
Test test
</div>
I'm trying to make the right column fluid, taking up the remaining space, and the left column fixed width. Can anyone tell me what I am doing wrong with my code, and if this is the best way of doing it?
Simplest is to use display:table-cell; (IE8 and above supported )
this way, you can fix the width of one div and next div will take up the remaining space
Once added, you can even use inline native methods like vertical-align, and since its not floating, adjusting the position of divs is easy through margin and padding depending on you layout! :)
check this demo
It is most compatible and cleanest you can get for fixed width and dynamic width in a page
for calc, it is incompatible with IE9 still
if you have to use it on regular basis, create a span as below :
span.fake_width{
display:block;
width:20px;
}
then just add it to the existing layout Demo
I suggest this, with the condition always left div width is 400px. And i assume is cause you use inline-style
html
<div style="background-color:white; width: 400px;float:left;">
<span class="status" id="active"></span>Title
6
</div>
<div id="rightCol" style="background-color: white; float:right;">
Test test
</div>
css
body {background-color: gray;}
#rightCol{
width: calc(100% - 400px);
}
fiddle
The most native way of doing this is to manipulate the box model:
I've added a float to the first div, and removed it from the second one.
this way the first div is treated as an inline-block and the second one is a block, which tries hard to ignore other inline blocks.
i've updated your fiddle: http://jsfiddle.net/TPKLv/3/
<div style="background-color:white; width: 400px; float:left">
<span class="status" id="active"></span>Title
6
</div>
<div style="width: 100%; background-color: white;">
Test test
</div>
Try like this: Demo
CSS:
.container {
background-color:blue;
height: auto;
overflow: hidden;
}
.sidebar {
float:left;
background-color:grey;
width: 100px;
}
.content {
background-color:green;
float: none;
width: auto;
overflow: hidden;
}
HTML:
<div class="container">
<div class="sidebar">
<span class="status " id="active"></span>
Title
6
</div>
<div class="content">Test test</div>
</div>
UPDATED FIDDLE LINK.
As you need, I added padding and margin for the div's and its working fine.

Div next to div

I have two divs and i want div2 will be next to div1. I tried use float:left and display:inline but nothing help.
Jsfiddle
How to do this?
P.S Sorry for stupid question but Im learning
Use float: left and box-sizing: border-box:
#div1 , #div2{
float: left;
box-sizing: border-box;
}
JSFiddle: http://jsfiddle.net/95rmz/4/
Check this fiddle:
http://jsfiddle.net/95rmz/3/
<div class="row" style="width: 390px;">
<div id="div1" style="width: 190px; border: solid black 1px; float: left; margin-right: 5px;">
Street
<input class="form-rej-normal" type="text">
<div class="errorMessage" id="User_street_em_" style="">
Pole Ulica nie może być puste.
</div>
</div>
<div id="div2" style="width: 190px; border: solid black 1px; float: left;">
Numer domu
<input class="form-rej-normal" type="text">
<div class="errorMessage" id="User_house_number_em_" style="">
Pole Ulica nie może być puste.
</div>
</div>
</div>
decrease the width of divs. if the first div is of full width then how can 2nd div come next to it.
The problem is that you are not calculating the width correctly. You set a 380px on the main div and give a 190px to each contained div.
That would be fine if you did just that, but you also added a border around each div, so now each of them has an extra 2px (1px left and 1px right) so they are actually 192px each. so you can solve it in several ways:
Set the main div to at least 184px.
Set box-sizing: border-box;, that will cause the browser to put the borders inside the div and not add to it's width - see here for more data: http://css-tricks.com/box-sizing/
Fiddle: http://jsfiddle.net/95rmz/6/
You can then try to follow this approach
<html>
<body>
<div style="position:relative; width:auto; height:auto; float:left; border:solid red 1px;">
this is div 1
</div>
<div style="position:relative; width:auto; height:auto; float:left; border:solid red 1px;">
this is div 2
</div>
</body>
</html>
For testing purpose you can also add border:1px; to check if your div tags are occupying more space than the resolution of your screen

smart page resizing

Suppose I have an HTML page with three blocks of fixed width (their height can vary if that's important), like shown on picture:
I would like to make it behave as shown on next picture: when browser screen width is reduced so it can't fit all three blocks in one line, first block goes down.
Is it possible to achieve such behavior? Preferably with CSS only but any working solution would be great.
<div style="width: 100%;">
<div style="display: inline-block; background-color: red; width: 200px;">DIV2</div>
<div style="display: inline-block; background-color: yellow; width: 200px;">DIV3</div>
<div style="display: inline-block; float: left; background-color: lightBlue; width: 200px;">DIV1</div>
<br style="clear: left;">
</div>
This one works. You put block 1 as the last one and only make that one float left.
It's virtually impossible to let the first block drop down without any Javascript trickery. Making the right-most one drop with float: left is easy on the other hand.
Use divs with float:left and fixed width values
<div style="float:right; width:250px; position:relative; height:100px; border:solid 1px #000000">
3
</div>
<div style="float:right; width:250px; position:relative; height:100px; border:solid 1px #000000">
2
</div>
<div style="float:right; width:250px; position:relative; height:100px; border:solid 1px #000000">
1
</div>
like so...
I am aware that the 1st one will go right but this is the simplest i can do without getting into javascript..
Put all these three blocks inside a div and set it's width to 100%, when the screen will resize the blocks will be arranged automatically.
<div style="width: 100%;">
<div style="width: 50px; float left;">DIV1</div>
<div style="width: 50px; float left;">DIV2</div>
<div style="width: 50px; float left;">DIV3</div>
<br style="clear: left;" />
</div>