Positioning of HTML elements with css - html

HTML Code
<header>
<h1>Event Heading</h1>
<div class="meta">09 JUL 2014</div>
<div class="textblock">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</div>
</header>
Issue
I have this HTML structure which I can not edit/rearrange. I would like to position the h1, div.meta and div.textblock is as in the picture below.
I can't work it out with floats the way I want to because of the sequence of the HTML.
Illustration

This can be achieved with absolute positioning:
header {
position: relative;
min-height: 100px; }
div.meta {
position: absolute;
width: 100px; height:100px;
top:0; left:0;
border: 1px solid red; }
header h1 {
margin-left: 120px;
border-bottom: 2px solid red; }
header div.textblock { margin-left: 120px; }
See fiddle: http://jsfiddle.net/utsKx/
You can change the div.meta widths and h1/textblock margin-left to percentages if you want a responsive layout.
EDIT
Added min-height to header to ensure div.meta never falls outside the parent header block. (Thanks for MarcAudet for pointing this out)

See this example:
Codepen Example
I think this is what you're looking for!

You can use this demo
Code Pen Demo
HTML
<header>
<div class="meta L">09 JUL 2014</div>
<h1 class="R">Event Heading</h1>
<div class="textblock R">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</div>
</header>
CSS
header
{
width:650px;
display:inline-block;
}
.meta
{
width:150px;
height:150px;
border:1px solid red;
margin:5px;
font-size:22px;
text-align:center;
}
h1,.textblock
{
width:400px;
text-align:left;
border:1px solid red;
}
h1
{
color:#B1003B;
margin-top:5px;
}
.textblock
{
margin-top:-22px;
}
.L
{
float:left;
}
.R
{
float:right;
}
.C1
{
color:#000000;
font-weight:bold;
font-size:36px;
}
.C2,.C3
{
color:#777777;
}
JQuery
var str = $(".meta").html();
s = str.split(' ');
$(".meta").html("<span class='C1'>"+s[0]+"</span></br><span class='C2'>"+s[1]+"</span></br><span class='C3'>"+s[2]+"</span>");

Related

fixed-width and auto-width divs in one line

I have chat window where I want to put photo and message next to photo. Conversation window must be responsive and message div auto-adjustable to the screen. But I can't find any way to do this, because once message has few lines of text, it drops to the next line.
If I use table, I can't make fixed-width photo TD. If I use DIVS, I can't do auto-width message DIV :)
Here is JSFiddle with an example:
https://jsfiddle.net/s95tdcLw/3/
HTML:
<div class="receiver">
<div class="receiverPhoto"></div>
<div class="receiverMessage">
Lorem ipsum dolor sit
</div>
</div>
<div class="receiver">
<div class="receiverPhoto"></div>
<div class="receiverMessage">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin et mauris eget est maximus condimentum nec a turpis.
</div>
</div>
<div class="receiver">
<div class="receiverPhoto"></div>
<div class="receiverMessage">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin et mauris eget est maximus condimentum nec a turpis. Nulla nulla est, feugiat vitae posuere et, efficitur ac justo. Suspendisse pulvinar, urna quis vehicula malesuada, lorem lacus luctus odio,
eu mattis nisi turpis vel lectus.
</div>
</div>
CSS:
.receiver {
clear: both;
padding-top: 1rem;
}
.receiverPhoto {
float: left;
width: 40px;
height: 40px;
background: blue;
border-radius: 20px;
}
.receiverMessage {
float: left;
width: auto;
background: rgb(230, 230, 230);
border-radius: 10px;
margin-left: 0.5rem;
padding: 10px;
}
Leave the float settings, use these instead:
.receiver {
position: relative;
}
.receiverPhoto {
position: absolute;
left: 0;
}
.receiverMessage {
margin-left: 45px;
}
jsFiddle
Your .receiverMessage element should not float and it should reserve left-margin space for the .receiverPhoto element.
.receiverMessage {
/* should not float */
width: auto;
background: rgb(230, 230, 230);
border-radius: 10px;
margin-left: 50px; /* reserve space of .receiverPhoto width */
padding: 10px;
}
See the forked Fiddle: https://jsfiddle.net/092b077c/
In response to your comment how to make it work for the opposite...
I'd use the classes on the wrapping div elements to determine the message type. In my example I introduce a new class .sender. Now I create four selectors that determine whether the photo element floats left or right and whether the message element has left or right padding:
New CSS:
.sender .receiverPhoto {
float: right;
}
.sender .receiverMessage {
margin-right: 50px;
}
.receiver .receiverPhoto {
float: left;
}
.receiver .receiverMessage {
margin-left: 50px;
}
HTML:
<div class="sender">
<div class="receiverPhoto"></div>
<div class="receiverMessage">...</div>
</div>
Now the .receiverPhoto and .receiverMessage styles do not need to declare margin or float.
See the updated Fiddle: https://jsfiddle.net/092b077c/1/

nth child overriding first child

I have 3 styles I need in a repeating design without changing the classes on the HTML side. I've been trying to accomplish this with nth-child selectors. The first class needs to have a different background image than the even and odd classes. My odd class keeps keeps overriding my first child class. I tried changing it to nth-child(2n+3), but no luck either.
How can I get my first child div to keep its background image?
/* Even Featured */
.home-feature-container:nth-child(even)
{
background-color:#393939;
padding-bottom: 20px;
}
.home-feature-container:nth-child(even) .home-featured-left
{
width:50%;
display:inline-block;
background-image: url('/images/featured-top-gray.png');
background-repeat:no-repeat;
background-position:top;
background-size: 100%;
vertical-align: top;
}
.home-feature-container:nth-child(even) .home-featured-right
{
width:50%;
display:inline-block;
background-image: url('/images/featured-top-spacer-gray.png');
background-repeat:no-repeat;
background-position:top;
background-size: 100%;
vertical-align: top;
}
/* Odd Featured */
.home-feature-container:nth-child(2n+3)
{
background-color:#252424;
padding-bottom: 20px;
}
.home-feature-container:nth-child(2n+3) .home-featured-left
{
width:50%;
display:inline-block;
background-image: url('/images/featured-top-black.png');
background-repeat:no-repeat;
background-position:top;
background-size: 100%;
vertical-align: top;
}
.home-feature-container:nth-child(2n+3) .home-featured-right
{
width:50%;
display:inline-block;
background-image: url('/images/featured-top-spacer-black.png');
background-repeat:no-repeat;
background-position:top;
background-size: 100%;
vertical-align: top;
}
/* First Featured */
.home-feature-container:first-child
{
background-color:#252424;
padding-bottom: 20px;
}
.home-feature-container:first-child .home-featured-left
{
width:50%;
display:inline-block;
background-image: url('/images/featured-top-first.png');
background-repeat:no-repeat;
background-position:top;
background-size: 100%;
vertical-align: top;
}
.home-feature-container:first-child .home-featured-right
{
width:50%;
display:inline-block;
background-image: url('/images/featured-top-spacer-first.png');
background-repeat:no-repeat;
background-position:top;
background-size: 100%;
vertical-align: top;
}
And the HTML
<div class="div_wrapper home-feature-container">
<div class="home-featured-left">
<div class="home-featured-left-content">
<h3 class="title">Feature</h3>
<h3>Sed tincidunt purus</h3>
<div class="home-featured-copy">Eu lectus varius auctor. Integer et elit bibendum, fermentum velit a, aliquam est. Donec varius arcu rutrum lorem ultrices, et tristique leo pretium. Nam porttitor lacinia nunc, sit amet maximus justo placerat ac. Curabitur ut tellus sed nisi faucibus.
</div>
</div>
</div><div class="home-featured-right">
<div class="home-featured-right-content"><img src="/images/featured-image-home.jpg" class="home-featured-image" /></div>
</div>
</div>
If I am understanding the question properly, the selectors are not selecting anything in your html, you need to select the children of the home-feature-container div.
I used the wildcard (*) for simplicity, see this fiddle
Your HTML:
<div class="div_wrapper home-feature-container">
<div class="home-featured-left">
<div class="home-featured-left-content">
<h3 class="title">Feature</h3>
<h3>Sed tincidunt purus</h3>
<div class="home-featured-copy">Eu lectus varius auctor. Integer et elit bibendum, fermentum velit a, aliquam est. Donec varius arcu rutrum lorem ultrices, et tristique leo pretium. Nam porttitor lacinia nunc, sit amet maximus justo placerat ac. Curabitur ut tellus sed nisi faucibus.
</div>
</div>
</div><div class="home-featured-right">
<div class="home-featured-right-content"><img src="/images/featured-image-home.jpg" class="home-featured-image" /></div>
</div>
</div>
Modified CSS:
/* Even Featured */
.home-feature-container *:nth-child(even)
{
background-color:#393939;
padding-bottom: 20px;
}
.home-feature-container *:nth-child(even) .home-featured-left
{
width:50%;
display:inline-block;
background-image: url('/images/featured-top-gray.png');
background-repeat:no-repeat;
background-position:top;
background-size: 100%;
vertical-align: top;
}
.home-feature-container *:nth-child(even) .home-featured-right
{
width:50%;
display:inline-block;
background-image: url('/images/featured-top-spacer-gray.png');
background-repeat:no-repeat;
background-position:top;
background-size: 100%;
vertical-align: top;
}
/* Odd Featured */
.home-feature-container *:nth-child(2n+3)
{
background-color:#252424;
padding-bottom: 20px;
}

Positioning entire wrapper (div) inside of a main wrapper ( div )

I currently have a site I'm working on ( copying another site as Practice )
This is the site I am trying to re-create
http://www.north2.net/
.
I am almost done, however I cannot position the two side sections(left and right of main image) correctly.
Can anyone help me out?
I have 3 "sections" left, middle, right, all are in a wrapper
I've tried
margin-top,
removing inline-block on the wrappers
...
MY GOAL :
Is to be able to raise the two side bars to my liking, but I don't see how to raise them in any way.
north2.net to see what I mean.
JSFIDDLE
http://jsfiddle.net/abXk4/
Not Important ::
Also, when I position anything, my background image moves and there is a white gap on the bottom of the page, my screen is 1920 x 1080, so any adjustment makes a white space,
I've been fixing this with
padding-bottom: X%;
Is this just something I have to do? Or is it because I coded incorrectly.
HTML
<title> ENTER TITLE </title>
</head>
<body>
<div id='page'>
<!--All of Left Side Bar Contents -->
<div class="swrap">
<div id="logo">
<img src="img/logo_green.png">
</div>
<div id="about">
<aside class="tlb"><p>About Us</p></aside>
<p>Welcome. We are Author, nulla mauris odio, vehicula in, condimentum sit amet, tempus id, metus. Donec at nisi sit amet felis blandit posuere. Aliquam erat volutpat.</p>
</div>
<div id="services">
<aside class="tlb"><p>Services</p></aside>
<ul>
<li>Web Site Dev and Applications </li>
<div class='hr'></div>
<li>CMS</li>
<div class='hr'></div>
<li>Digital Branding and Industry</li>
<div class='hr'></div>
<li>UI Design</li>
<div class='hr'></div>
<li>Social Media</li>
<div class='hr'></div>
<li>User Experience</li>
<div class='hr'></div>
<li>Creative Ingenuity</li>
</ul> </div>
</div>
<!-- Center Content ( main header, main image ) -->
<div class="mwrap">
<!-- Main Nav Above Slider -->
<nav class='mnav'>
<ul>
<li class="m1"><a href='#'>home</a></li>
<li class="m2"><a href='#'>Author</a></li>
<li class="m3"><a href='#'>work</a></li>
<li class="m4"><a href='#'>clients</a></li>
<li class="m5"><a href='#'>contact</a></li>
</ul>
</nav>
<div id="fimg">
<img src="img/fumic_naslovna.jpg">
</div>
<div id="featart">
<article>
<h1>Lorem Ipsum</h1>
<p> Nulla mauris odio, vehicula in, condimentum sit amet, tempus id, metus. Donec at nisi sit amet felis blandit posuere. Aliquam erat volutpat. Cras lobortis orci in quam porttitor cursus. Aenean dignissim. Curabitur facilisis sem at nisi laoreet placerat. Duis sed ipsum ac nibh mattis feugiat. Proin sed purus. Vivamus lectus ipsum, rhoncus sed, scelerisque sit amet, ultrices in, dolor. Aliquam vel magna non nunc ornare bibendum. Sed libero. Maecenas at est. Vivamus ornare, felis et luctus dapibus, lacus leo convallis diam, eget dapibus augue arcu eget arcu.</p>
</article>
</div>
</div>
<div id="rwrap">
<div class="rfc">
<aside class="tlb">Featured Clients</aside>
<p> Nulla mauris odio, vehicula in, condimentum sit amet, tempus id, metus. Donec at nisi sit amet felis blandit posuere. Aliquam erat volutpat. Cras lobortis orci in quam porttitor cursus.</p>
<div class='hr'></div>
<p> Nulla mauris odio, vehicula in, condimentum sit amet, tempus id, metus. Donec at nisi sit amet felis blandit posuere. Aliquam erat volutpat.</p>
</div>
</div>
</div>
</body>
</html>
CSS
body {
background-image: url(img/brown.jpg);
background-repeat:no-repeat;
-webkit-background-size:cover;
-moz-background-size:cover;
-o-background-size:cover;
background-size:cover;
background-position:center;
padding-bottom:12%;
color: #fff;
font-weight: bold;
font-size: large;
text-align: left;
}
* {
border-radius: 1px;
}
#page {
margin: 30px 25%;
width: auto;
/* width should be 50% ... 25% on each side, 50% in middle, centered!*/
border: 2px solid black;
}
/*Left Content Begins ------------------ */
.swrap {
width: 23%;
display:inline-block;
/*1% margin on each side */
margin-top: 100px;
}
#logo {
background-color: rgba(0,0,0,.7);
}
#about {
margin: 3px 0;
background-color: rgba(89, 194, 141, 1);
padding: 5%;
}
#about aside {
margin-left: -6% !important;
}
#services {
background-color:rgba(66, 161, 75, .96);
padding: 2%;
margin: 3px 0;
}
.tlb {
background-color: rgba(0,0,0,.6);
width: 75%;
margin: -10px 0 0 -2% !important;
padding-left: 4%;
}
/*Middle Content Begins ------------------ */
.mwrap {
width: 48%;
margin: 0 auto;
/*1% margin on each side for .mwrap*/
display:inline-block;
}
.mnav ul {
list-style:none;
}
.mnav ul li {
display: inline;
font-size: large;
font-weight:bold;
padding: 2px 2%;
border-radius: 1px;
}
.mnav ul li a {
text-decoration: none;
color: #fff;
}
.m1 {background-color:rgba(46, 206, 87, 1); }
.m2 {background-color: rgba(39, 197, 80, 1); }
.m3 {background-color: rgba(70, 182, 99, 1); }
.m4 {background-color: rgba(64, 164, 90, 1);}
.m5 {background-color: rgba(63, 140, 83, 1); }
.mnav ul li:active {
background-color:none !important;
}
.mnav li:hover {
background-color: rgba(0,0,0,.3);
}
#fimg {
width: 100%;
}
#fimg img {
width: 100%;
}
#featart {
margin-top: -10px;
background-color: rgba(64, 164, 90, .9);
padding: 1% 1%;
}
/*Right Content Begins ------------------ */
#rwrap {
width: 23%;
display:inline-block;
/*1% margin on each side */
}
.rfc {
background-color:rgba(66, 161, 75, .96);
padding: 2%;
}
.rfc .tlb {
margin-top: 9px !important;
margin-left: -2.3% !important;
border-top-right-radius: 2px;
border-bottom-right-radius: 2px;
}
The easy way is to use position relative position: relative; bottom: [how ever many pixels]
A better (and later much more flexible) way is to change you HTML structure a little bit.
If I were building this site I'd break it into two wrapping divs with three column divs under each of them like here:
<div class="header">
<div class="left-column">
<img id="logo" src="img/logo.png" />
</div>
<div class="middle-column">
<ul class="nav"></ul>
</div>
<div class="right-column">
Put content here if you want it
</div>
</div>
<div class="content">
<div class="left-column">
Content in left column
</div>
<div class="middle-column">
Content in middle
</div>
<div class="right-column">
Content on right
</div>
</div>
Now, use CSS to float those columns just like you did before. The difference with this is you can define a height for the header and the logo and navigation will be much easier to align as they are separate from the other columns.
If you want to get more technical check out CSS Flexbox, it would work well here.
http://philipwalton.github.io/solved-by-flexbox/demos/holy-grail/
set
a position: relative;
bottom: X px;

Problems aligning an element at the bottom of a parent element

I'm trying to align a <div> with a <h2> inside it at the bottom of a parent div. The best way to show you is through code so here's the JSFiddle example: http://jsfiddle.net/3GGa7/
As you can see, the project-title div (and the <h2> inside it) is aligned to the top of the project-header div. I would like it to sink to the bottom of that div, to look like this:
However if I apply a margin-top to project-title it pushes everything down rather than just that div, and if I apply a padding the black background will cover the image.
What's the most elegant way to accomplish this?
Since the .project-title must be contained within the .project-header, give the .project-header a position:relative; and the .project-title a position:absolute;
.project-header {
height: 100px;
position:relative;;
}
.project-title {
background: black;
opacity: 0.75;
position:absolute;
bottom:0px;
left:0px;
right:0px;
}
Check it out http://jsfiddle.net/gXyEU/
This way, whether you use a bigger image, or change its position or margin, you'll never have to worry about the title, it will always be positioned where it should be.
If your picture size is steady. You can try the css below:
.project {
width: 335px;
float: left;
border: 1px solid #ccc;
border-radius: 6px;
}
.project-header {
height: 100px;
}
.project-title {
background: black;
opacity: 0.75;
float:left;
width:100%;
margin-top:25%;
}
.project-title h2 {
color: #fff;
margin-bottom:0px;
float:left;
}
just close your project-header div before start of project-title div like as
<div class="project">
<div class="project-header" style="background-image:url('http://placekitten.com/200/300');" ></div>
<div class="project-title">
<h2>Project title</h2>
</div>
<div class="project-description">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ornare felis id enim dignissim dapibus. Maecenas dui mi, ullamcorper eget semper non, varius quis orci. Suspendisse lobortis nibh sed nisi luctus dictum. Sed vel arcu eros. Etiam id varius neque. Cras ac sapien in est fringilla tempor vitae et est.</p>
</div>
</div>
FIDDLE is here
If you don't mind setting the width of .project-header
.project-header {
width: 335px;
height: 100px;
display: table-cell;
vertical-align: bottom;
}
Modified JSFiddle

Div with paragraphs with paragraph numbers aligned and outside the div (see sketch)

I need to do this in CSS
The red box is a <div> with several paragraphs <p>
I want to have the paragraph numbers to the right of the red box, and the paragraph numbers are aligned to the top of the respective <p>
Can I do this layout only with CSS?
I have tried so far to do this with javascript, recording the position of each paragraph element then positioning the numbers in the same y coordinate.
Thanks
You could do
<p style="position: relative;">
<div style="width: 30px; position: absolute; top: 0; right: -30px">#1</div>
Lorum ipsum...
</p>
You would probably want to use classes too, inline styles for example only.
Also, a valid argument is to use an ordered list. This is easily done by wrapping those p elements in li elements, which in turn will be wrapped by an ol element. Be sure to use ol { list-style: none; }, otherwise you will get 2 sets of numbers!
As for adding the numbers, you could use server side script and a DOM parser or use JavaScript
var p = document.getElementById('content').getElementsByTagName('p');
for (var i = 0; i < p.length; i++) {
p[i].getElementsByTagName('div')[0].innerHtml = '#' + (i + 1);
}
Of course, you can also use jQuery
$('#content p').each(function(i) {
$(this).find('div:first').html('#' + (i + 1));
});
This should semantically be an <ol>.
In any case something like this might work:
ol
{
border-top: 1px solid red;
border-bottom: 1px solid red;
border-left: 1px solid red;
}
p { border-right: 1px solid red; padding: 10px 0; }
span.number { vertical-align: top; float: right; }
.clear { clear: both; }
<ol>
<li>
<p>
content
</p>
<div class="number">
#1
</div>
<div class="clear"><div>
</li>
</ol>
Here's what I'd do:
<div>
<p>
content 1
<span>#1</span>
</p>
<p>
content 2
<span>#2</span>
</p>
<p>
content 3
<span>#3</span>
</p>
</div>
and the css looks like:
div {
padding:10px;
border:1px solid red;
width:500px;
}
p {
position:relative;
}
p span {
font-size:30px;
position:absolute;
top:0;
right:-60px;
}
and now just play around with positioning.
This answer builds on Graphain's answer (he's right on that OL should be used, since it's semantically correct). It uses jQuery to add the numbering.
jQuery
$(document).ready(function(){
$("ol li").each(function(i){
$(this).prepend('<span class="number">#'+(i+1)+'</span> '); // Append the number (using prepend, but the CSS will put the number after the box
});
});
CSS
ol {
list-style: none;
border: 1px solid red;
overflow: auto;
width: 500px;
}
li {
margin: 0.75em 0.75em 0.75em -28px;
}
.number {
position:absolute;
left: 560px;
}
HTML
<ol>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt nisl a purus mollis tempor. Donec dapibus blandit purus at semper. Aliquam sit amet dolor at sapien gravida pharetra rutrum at libero.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt nisl a purus mollis tempor. Donec dapibus blandit purus at semper. Aliquam sit amet dolor at sapien gravida pharetra rutrum at libero.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt nisl a purus mollis tempor. Donec dapibus blandit purus at semper. Aliquam sit amet dolor at sapien gravida pharetra rutrum at libero.</li>
</ol>