Change div order with CSS - html

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/

Related

Centring text in CSS within a "V" shape

I have this picture.
Is this possible in CSS?
Now I know you can use text-align: center and I could even make every line a span and incrementally increase the padding but is there a property I don't know about like centre from centre, or something similar?
This feels really obvious but I just can't think of a reasonable approach.
Update
The question was because in the picture the text tends to get shorter towards the bottom, much like the letter V, so t starts off wide and gets progressively narrower.
I had never seen a similar thing done so was very curious.
shape-outside can do it:
.wrapper {
display:flex; /* this is needed for the percentage height */
}
.box {
font-size: 25px;
text-align:justify;
}
.box > div,
.box::before{
height:150%; /* should be at least 100% but we consider bigger to avoid overflow */
width:100px; /* adjust this */
}
.box > div {
float:right;
shape-outside:linear-gradient(to bottom right,#0000 50%,#000 0);
}
.box::before {
content:"";
float:left;
shape-outside:linear-gradient(to bottom left,#0000 50%,#000 0);
}
/* the below is not need, it's only to illustrate the shape
uncomment it to see what is happening
.box > div {
background:linear-gradient(to bottom right,#0000 50%,red 0);
}
.box::before {
background:linear-gradient(to bottom left,#0000 50%,green 0);
}
*/
<div class="wrapper">
<div class="box">
<div></div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pretium lacus ipsum, at placerat nunc sollicitudin scelerisque. Donec vulputate ex odio, et facilisis nisi tristique et. Maecenas tortor nisi, euismod et tellus sit amet, tincidunt congue
eros. Nam et viverra magna, id ultricies augue.
</div>
</div>
White-space: break-spaces.
With appropriate width you will get this:
#text {
font-family:sans-serif;
padding:10px;
text-align: center;
width: 600px;
white-space: break-spaces;
background-color:gray;
color: silver;
}
<div id="text">“Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sodales ultrices nibh
at bibendum. Vivamus in volutpat diam. Vivamus ut mi non leo vehicula mattis.
Maecenas nec dolor id tellus consequat laoreet nec sit amet lacus.”
Jane from Cool Business Ltd</div>
Just apply text-align:center on the parent div.
Like this:
<div style='text-align:center'>
Your<br>
Multiline<br>
Text<br>
</div>

Position image left of Bootstrap container

I'm trying to build a simple web page and I need an image to be positioned on the left of the container. Ideally I'd like to make sure the image resize nicely when the browser window is resized but still keeps its position independently from the text. What I'm trying to achieve looks something like this:
The text part is inside the Bootstrap container CSS class. Not sure how to achieve the look I need - tried a few things with image being inside a wrapper with absolute position on the same level as the container div, but then I can't seem to center the image inside the wrapper (vertically).
This is what I have so far:
.header {
background-color: #333333;
}
.logo-wrapper {
position: absolute;
}
.logo {
position: relative;
/* padding-top: 50%; */
height: 100%;
}
<div class="header">
<div class="logo-wrapper">
<img src="assets/logo.png" class="logo">
</div>
<div class="container">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<h3>Sed tristique augue turpis, ullamcorper convallis augue fermentum vel. Cras a tempus nulla. Vestibulum congue sollicitudin vulputate</h3>
<h2> Integer vitae tortor id elit pharetra dignissim</h2>
</div>
</div>
</div>
.logo-wrapper {
width: 20%;
display: table-cell;
vertical-align: middle;
}
.logo-wrapper img{
max-width:100%
}
.container{
width:80%;
display: table-cell;
}
<div class="header">
<div class="logo-wrapper">
<img src="https://pixabay.com/static/uploads/photo/2015/03/04/22/35/head-659652_640.png" class="logo">
</div>
<div class="container">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<h3>Sed tristique augue turpis, ullamcorper convallis augue fermentum vel. Cras a tempus nulla. Vestibulum congue sollicitudin vulputate</h3>
<h2> Integer vitae tortor id elit pharetra dignissim</h2>
</div>
</div>
</div>
.header {
background-color: #333333;
line-height:1.5em;
display:inline;
}
.container{
float:right;
width:70%;
padding-left:10%;
}
.logo-wrapper {
float:left;
width:20%;
}
.logo-wrapper img{
max-width:100%
}
}
.logo {
position: relative;
/* padding-top: 50%; */
height: 100%;
}
<div class="header">
<div class="logo-wrapper">
<img src="https://pixabay.com/static/uploads/photo/2015/03/04/22/35/head-659652_640.png" class="logo">
</div>
<div class="container">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<h3>Sed tristique augue turpis, ullamcorper convallis augue fermentum vel. Cras a tempus nulla. Vestibulum congue sollicitudin vulputate</h3>
<h2> Integer vitae tortor id elit pharetra dignissim</h2>
</div>
</div>
</div>
I think the answer of #Mitul is correct. But there's a slight modification in the solution of Mitul. If you want no text below the image, you should modify it like.

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>

CSS equal height columns with :before - unexpected background behaviour/stacking order

I'm trying to implement something based upon:
http://webdesign.tutsplus.com/tutorials/quick-tip-solving-the-equal-height-column-conundrum--cms-20403
In a nutshell, here is my test case:
http://jsfiddle.net/7CGCW/
Why are not both .panel's displayed with a green background?
All is well when my column sits atop a background-color'ed BODY (the second .panel), but when it sits atop a background-color'ed block (the first .panel, on top of .content), then the background of this DIV appears to sit atop my column, even when it is seemingly given an z-index that is lower in the stacking order.
The HTML:
<div class="content">
<div class="positioned">
<div class="panel">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam vel rutrum libero. Proin in metus id odio rutrum posuere at ac dui. Etiam sollicitudin in dui sed imperdiet. Suspendisse eu erat nec ipsum facilisis iaculis.
</div>
</div>
</div>
<div class="positioned">
<div class="panel">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam vel rutrum libero. Proin in metus id odio rutrum posuere at ac dui. Etiam sollicitudin in dui sed imperdiet. Suspendisse eu erat nec ipsum facilisis iaculis.
</div>
</div>
The CSS:
body {
background-color: red;
color: blue;
}
.content {
background-color: red;
z-index: -2;
}
.positioned {
position: relative;
}
.panel:before {
content: '';
position: absolute;
top: 0;
bottom: 0;
z-index: -1;
background-color: green;
width: 100%;
}
your content div needs to be positioned for z-index to work - read this about stacking context
at the moment it will have a z-index of 0 so it will be above your :before content
try
.content {
background-color: red;
position:relative;
z-index: -2;
}
Example
ps, that tutorial is rubbish - if he is going to use pseudo selectors to make a background be equal height then he may as well make use of the display table property for proper equal height columns
Much easier to do with less markup and styles and don't have to think about all that z-indexing and positioning

expanding div width if other div is missing

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