expanding div width if other div is missing - html

I have two divs inside a containers floating to the left. In some of the containers the left div is missing, so I want to do it so that if the left div is missing then the right div should expand to the full width of container.
In my code below, I have specified the width of the right div and because of this if the left div is missing, it does not fill the whole container.
Here's the code:
HTML:
<div class="box">
<div class="left"><img src="http://i50.tinypic.com/2cxj31z.jpg" ></div>
<div class="right">... content...</div>
</div>
CSS:
.box{
width: 300px;
border: 1px solid red;
margin: 0 auto;
overflow: hidden;
}
.left{
width: 80px;
margin-right: 10px;
float: left;
}
.right{
float: left;
width: 210px;
}
JSFiddle:
http://jsfiddle.net/DMFz8/

You can use an adjacent selector, so that .right becomes floated only if there's a .left next to it.
Replace the .right selector and rule with this:
.left + .right{
float: right;
width: 210px;
}
Demo

I don't know what ypu want exactly but try this maybe helpful
<div class="box">
<div class="left"><img src="http://i50.tinypic.com/2cxj31z.jpg" ></div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eget ligula sapien, sed tempor felis. Vivamus eget bibendum augue. Sed sit amet nulla lectus. Etiam vitae lacus ipsum. Aliquam malesuada orci nec ipsum pretium fermentum. Fusce libero mauris, convallis ut aliquam et, scelerisque vel turpis.
</div>
jsFiddle

Related

Html <p> tag expand dynamically to parent that is next to another div

How could to put the right box next to the left box (inline-block) with precise sizes? I can put it up, but the size will not be the same in different browsers. Then how could I put the left box near to the right box without that whitespace? Thank you.
div {
border: 1px solid green;
}
.entry {
width: 560px;
margin: auto;
border: 1px solid red;
display: block;
position: relative;
}
.entry .img-cont,.body-cont {
vertical-align: top;
}
.entry .img-cont {
width: 50px;
display: inline-block;
}
.entry .body-cont {
width: 508px;
display: inline-block;
}
.entry .body-cont p {
display: table;
}
<div class="entry">
<div class="img-cont">
<img src="http://bit.ly/1RabLNk"/>
</div>
<div class="body-cont">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla libero, sollicitudin a erat semper, gravida pharetra augue. Phasellus convallis ultrices dolor vitae imperdiet. Curabitur mollis odio neque, in dictum nisi finibus nec. Vivamus pulvinar, turpis a volutpat semper, lacus diam convallis.</p>
</div>
</div>
see this
http://jsfiddle.net/leandroparrar/6omjqefj
Here is an example of what the problem looks like:
Your answer is pretty close to what you need.
If you zero-our the margins on the p element in .body-cont, that will get rid of the extra whitespace that appears on top of the paragraph.
If you try to use inline-block's, it is easy to get some extra whitespace between elements due to carriage-returns (newlines) in the HTML file.
If you use display: table-cell on .img-cont and .body-cont, then the
two elements will rest side-by-side and you can control the horizontal spacing using left/right padding as needed.
div {
border: 1px solid green;
}
.entry {
width: 560px;
margin: auto;
border: 1px solid red;
display: table;
}
.entry .img-cont, .entry .body-cont {
display: table-cell;
vertical-align: top;
}
.entry .img-cont img {
display: block;
}
.entry .body-cont p {
margin: 0;
}
<div class="entry">
<div class="img-cont">
<img src="http://bit.ly/1RabLNk" />
</div>
<div class="body-cont">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla libero, sollicitudin a erat semper, gravida pharetra augue. Phasellus convallis ultrices dolor vitae imperdiet. Curabitur mollis odio neque, in dictum nisi finibus nec. Vivamus pulvinar,
turpis a volutpat semper, lacus diam convallis.</p>
</div>
</div>

How do I put text and an image side by side?

I need to get my text and image to be next to each other on the same line, Without using tables. Float: left and right are not working.
<h1 id="profileHead">Connor Clarke - Lorem ipsum dolor sit amet.</h1>
<div id="profile">
<img id="profilePic" src="pictures/profilePicture.jpg">
<p id="profileDesc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in justo libero. In dapibus vulputate augue at auctor. Aliquam sagittis odio quis magna ornare, at molestie neque mattis. Proin non orci ac arcu cursus tempus et ac purus. Nam aliquet.</p>
</div>
#profilePic { width: 300px; height: 300px; float: right;}
#profileDesc {float: left;}
There are several different ways I'd think up of solving this.
The solution that involves changing the width of #profileDesc does not display gracefully if the layout is too narrow (it breaks if 50% is less than 300px), so I don't recommend that one.
The recommendation of divy3993 works, but it's not the solution I'd think of, and it wouldn't be best in all circumstances--it depends what you want to do, how you want this to fit into the rest of your page.
You can achieve a similar effect by removing the one line in your css:
#profileDesc {float: left;}
And you will find it displays as you want.
Try adding a new p element, and you'll see different behavior (in the solution with the floated img outside the p, the subsequent p will continue to wrap alongside the image, in divy3993's solution, there will be a gap).
Another solution, yielding yet different behavior, is to use a containing div with position:relative, allowing you to use absolute positioning for the image, then add padding to the right side of the div so the content in it doesn't spill over onto the image. This can be useful if you want text or other content to continue down in a column that never gets wider than the image, useful if you want to put other stuff under the image in a separate column. To do this:
<h1 id="profileHead">Connor Clarke - Lorem ipsum dolor sit amet.</h1>
<div id="profile">
<div id="column">
<img id="profilePic" src="pictures/profilePicture.jpg">
<p id="profileDesc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in justo libero. In dapibus vulputate augue at auctor. Aliquam sagittis odio quis magna ornare, at molestie neque mattis. Proin non orci ac arcu cursus tempus et ac purus. Nam aliquet.</p>
</div>
</div>
<style>
#profilePic { position: absolute; right: 0px; top: 0px; width: 300px; height: 300px; float: right;}
#column {position: relative; padding-right: 310px;}
</style>
You do not have to put your image inside the paragraph, you can do it without.
Here is a jsfiddle example
Your <p> is taking up 100% of the width, change your #profileDesc width to something like 50%. The <p> being 100% is forcing it to wrap.
#profileDesc {float: left; width: 50%;}
#profilePic { width: 300px; height: 300px; float: right;}
maybe use calc?
* {
padding: 0;
margin: 0;
}
#profile{
overflow: hidden;
}
#profilePic {
width: 300px;
height: 300px;
float: right;
margin-left: 20px;
}
#profileDesc {
width: calc(100% - 320px);
float: left;
}
<div class="b">
<img id="profilePic" src="pictures/profilePicture.jpg"/>
<p id="profileDesc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in justo libero. In dapibus vulputate augue at auctor. Aliquam sagittis odio quis magna ornare, at molestie neque mattis. Proin non orci ac arcu cursus tempus et ac purus. Nam aliquet.</p>
<div style="clear: both"></div>
</div>

HTML: <p> tag pushes down floating <div>

I'm working on a website design (relearning HTML and CSS, as I haven't used them for several years), and I've come across what appears to be a rather simple problem, and one I'm sure I've come across before, but can't remember how to fix.
In my design, I have the main content to the left of the page, and a sidebar to the right: jsfiddle. The sidebar is float: right;, and the content container is float: right;. The problem is, which ever element appears first pushes down the other element. I want them to sit side by side next to each other.
HTML:
...
<div id='page-container'>
<aside id='sidebar'>
<div class='sidebar-item'>
<p>Sidebar</p>
</div>
</aside>
<main id='body-container'>
<h1 id='main-title'>Welcome to WebsiteName!</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ut erat
volutpat, semper metus id, suscipit justo. Maecenas ut lacus sit amet lacus
elementum tempus. Suspendisse sit amet sem venenatis, mollis enim vel,
vehicula nisi. Phasellus sed condimentum ligula. Curabitur vehicula sem in
volutpat vulputate. Maecenas feugiat ipsum quis quam euismod lacinia. In
congue vel dui ac dignissim. Maecenas iaculis, odio fermentum tincidunt
aliquam, elit massa tristique nisl, quis fringilla nisl purus sed risus.
Cras malesuada posuere elit sed auctor. Phasellus hendrerit adipiscing
commodo.</p>
<img id='image' src='logo.svg' alt='PollardNET | Home' />
</main>
<div class='clearer'>
<!--Needed to ensure floats work correctly!-->
</div>
</div>
...
CSS:
...
#sidebar {
float: right;
margin: 100px;
margin-left: 50px;
margin-top: 50px;
width: 250px;
padding: 5px;
padding-left: 20px;
border-left-style: solid;
border-left-width: 2px;
border-left-color: #a4a4a4;
}
#body-container {
float: left;
position: relative;
margin: 100px;
margin-right: 427px;
margin-top: 50px;
}
...
.clearer {
clear: both;
}
...
If I remove any text (p tags) from the content section, the problem seems goes away. For some reason this does not work in jsfiddle, but shows in Chrome. Does anyone know how I can fix this?
Remove float: left; from your #body-container. Generally speaking you don't need to specify two floats for each side, only one float is enough. See updated JSFiddle.
You can use position: absolute and set a width for both division.
#sidebar {
...
position: absolute;
}
#body-container {
...
position: absolute;
}
Put it in span instead of p or use float on p
p {
float:left;
};
If you don't want to remove the float, you can also take the margin-right off body-container and give it a width.
Floating an element makes it display:block. By default, a block element in HTML will have a full line width. That's why the body-container pushes the sidebar down if it doesn't have a set width or if it doesn't have display: inline

Expanding div height in CSS

So on my page, I have a two column layout - 75% (left - blue) and 25% (right - red). I can't get the red column to fill. In Chrome's inspector I see that the container classed <div> has expanded vertically to the size of #left, but I just cannot coax #right to expand appropriately. Here's the jfiddle link for my css and html (followed by the code itself in case you don't want to click to tinker):
Just one quick note: In my actual development code I don't use blue and red (left and right div background colours) - I use white and navy, respectively.
http://jsfiddle.net/EG3zb/
<div class="container">
<div id="left">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam id nunc bibendum ligula tempus interdum a ac urna. Fusce sed nunc molestie, consequat orci eu, vehicula orci. Suspendisse nec leo sit amet tellus varius feugiat. Maecenas lacinia neque euismod, tincidunt nisi et, fermentum ipsum. Vivamus ut gravida velit, vitae ultrices ante. Nullam varius mattis tellus, vitae consectetur tortor porttitor eu. Donec congue eros mauris. Ut consequat aliquam mattis. Aliquam non neque eros.</p>
</div>
<div id="right">
Blah.
</div>
</div>
And the accompanying CSS:
.container {
width: 100%;
min-height: 100%;
overflow: auto;
}
#left, #right {
padding-top: 2%;
}
#left {
float: left;
width: 75%;
color: #fff;
background-color: blue;;
}
#right {
float: right;
width: 25%;
background-color: red;
color: #fff;
}
this is working
.container {
width: 100%;
min-height: 100%;
overflow: auto;
display:flex;
}
.container #left,.container #right{
-webkit-flex: auto;
-ms-flex:auto;
flex:auto;
}
here is working demo
http://jsfiddle.net/mauryaashish945/EG3zb/5/
Instead of default display:block you can use display:table-cell so there is no need for float:left\right :
#left, #right {
display:table-cell;
}
Example

Change div order with CSS

I've following HTML structure which can not be changed. It contains three vertical divs. I want to change the position of divs using CSS only so that the second div appears at top most and the first and third divs appear after this (only important thing is that second div should appear at top).
I am trying using the following CSS code, it displays the second div at top, but hides the first div. How can I display the other two divs after this? All three div's have different heights.
HTML:
<div class="wrap">
<div class="one">
This should be third.
</div>
<div class="two">
This should be first.
</div>
<div class="three">
This should be second.
</div>
</div>
CSS:
.wrap{
width: 100px;
overflow: hidden;
position: relative;
}
.one, .two, .three{
width: 100px;
margin-bottom: 10px;
background: yellow;
}
.two{
position: absolute;
top: 0;
background: green;
}
JSFiddle:
http://jsfiddle.net/UK6vK/
You use this style code for .one and .three
.one, .three{
width: 100px;
margin-bottom: 10px;
background: yellow;
float:right;
}
and set for .two this style
.two{
position: absolute;
top: 0;
background: green;
left:0;
width:100%;
}
None of these worked for me, but a couple tweaks to Saeed's seems to - basically duplicate the second div and use it for layout only, ie,
HTML
<div class="wrap">
<div style="position:relative;" class="two">
<strong>This should be first..</strong>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor diam eget lorem vehicula aliquam. Aliquam ac tellus eget justo facilisis malesuada. Curabitur mi sapien.</p>
</div>
<div class="one">
<strong>This should not be first..</strong>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
</div>
<div class="two">
<strong>This should be first..</strong>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor diam eget lorem vehicula aliquam. Aliquam ac tellus eget justo facilisis malesuada. Curabitur mi sapien.</p>
</div>
<div class="three">
<strong>This can be 2nd or third.</strong>
<p> Aliquam ac tellus eget justo facilisis malesuada. Curabitur mi sapien, bibendum sed eleifend non, pretium eget arcu. Vivamus et augue nec quam rutrum tempor in in urna. </p>
</div>
</div>
position:relative means other elements will be shifted down by this element (which anyway appears behind/in front of the DIV that's been shifted up).
CSS:
.one, .three{
width: 100%;
margin-bottom: 10px;
background: yellow;
float:right;
}
.two{
position: absolute;
top: 0;
background: green;
left:0;
width:100%;
margin-bottom:10px;
}
I realize you said the HTML can't be changed so pedantically-speaking this isn't an answer to your question, however in my case the reason I can't change the HTML structure is because I want a default mobile layout that re-orders when #media is screen and (min-width: 700px;). I can make the dummy div disappear (z-index or whatever) unless I need it for re-organizing the layout (without relying on JS).
http://jsfiddle.net/UK6vK/83/