HTML Positioning using CSS - html

I'm having trouble with HTML positioning. I messed around for a while and this was the farthest I got. Here is the HTML and CSS
.group:after {
content: "";
display: block;
clear: both;
}
article {
width: 400px;
border: 1px solid black;
}
#tip,
#title {
padding: 0 0 0 0;
margin: 0 0 0 0;
}
#tip {
background: yellow;
}
#title {
background: orange;
}
#box_1 {
float: left;
width: 60px;
height: 60px;
background: pink;
}
#box_2 {
float: right;
width: 60px;
height: 120px;
background: blue;
}
#box_3 {
float: left;
clear: left;
width: 60px;
height: 60px;
background: green;
}
<article class="group">
<h2 id="tip">tip</h2>
<h2 id="title">title</h2>
<div id="box_1"></div>
<div id="box_3"></div>
<div id="box_2"></div>
</article>
I want my #box_3 to be right below #box_1 and I got it to work by giving #box_3 clear: left;. But I also want my #box_2 to be rendered right below #title on the right.
Something to note is that the #box_3 comes before #box_2 in my HTML code and the height of #box_1 is not fixed to just 60px. Here is my jsfiddle.
http://jsfiddle.net/92um5o2c/2/

if you suffle your html code little bit so its not a problem. You should place #box1 first then #box2 and then #box3
Here is the example
.group:after {
content: "";
display: block;
clear: both;
}
article {
width: 400px;
border: 1px solid black;
}
#tip, #title{
padding: 0 0 0 0;
margin: 0 0 0 0;
}
#tip {
background: yellow;
}
#title {
background: orange;
}
#box_1 {
float: left;
width: 60px;
height: 60px;
background: pink;
}
#box_2 {
float: right;
width: 60px;
height: 120px;
background: blue;
}
#box_3 {
float: left;
clear: left;
width: 60px;
height: 60px;
background: green;
}
<article class="group">
<h2 id="tip">tip</h2>
<h2 id="title">title</h2>
<div id="box_1"></div>
<div id="box_2"></div>
<div id="box_3"></div>
</article>

Assuming the height of the boxes is fixed and will not grow, you can simply give #box_2 a negative margin which equals the height of box_1:
#box_2 {
float: right;
width: 60px;
height: 120px;
background: blue;
margin-top:-60px;
}
http://jsfiddle.net/92um5o2c/4/

You mean like this right? Just change the order in the HTML like below.
<div id="box_1"></div>
<div id="box_2"></div>
<div id="box_3"></div>
#box_1 {
float: left;
width: 60px;
height: 60px;
background: pink;
}
#box_2 {
float: right;
width: 60px;
height: 120px;
background: blue;
}
#box_3 {
clear: left;
width: 60px;
height: 60px;
background: green;
}
http://jsfiddle.net/xn0pL6mn/

You can add "position: absolute;" and "right: 0;" to #box_2 and then "position: relative;" to article.
#box_2 {
position:absolute;
right: 0;
}
article {
position: relative;
}
https://jsfiddle.net/92um5o2c/9/

Related

Creating half a border between DIV elements within a DIV

I am trying to create half borders between DIV elements contained within a DIV element with the help of CSS using ::after. Unfortunately , this only ever renders the border on the outside of the encompassing DIV element. I would appreciate the help.
Here is my code:
HTML:
<div class="menu">
<div class="subDiv1">Foo</div>
<div class="subDiv2">Bar</div>
<div class="subDiv3">Baz</div>
</div>
CSS:
.menu {
position: relative;
display: inline-block;
float: left;
padding: 0 10px;
margin-left:auto;
margin-right:auto;
width: 75%;
height: 150px;
position: relative;
margin-top: 2%;
border-width: 1px;
border-style: thin solid;
border-color: #008040;
overflow: hidden;
box-shadow: 0 0 10px 1px #7e8083;
}
.subDiv1 {
width: 33%;
height: 150px;
background-color: #fff;
float: left;
color: #7e8083;
}
.subDiv1::after {
content:"";
background: black;
position: absolute;
bottom: 25%;
right: 0;
height: 50%;
width: 1px;
}
.subDiv2 {
width: 33%;
height: 150px;
background-color: #fff;
float: left;
color: #7e8083;
}
.subDiv2::after {
content:"";
background: black;
position: absolute;
bottom: 25%;
right: 0;
height: 50%;
width: 1px;
}
.subDiv3 {
width: 33%;
height: 150px;
background-color: #fff;
float: left;
color: #7e8083;
}
https://jsfiddle.net/2yGQD/1727/
Add position:relative to your subdivs
.subDiv1 {
position:relative;
width: 20%;
height: 150px;
background-color: #fff;
float: left;
color: #7e8083;
}

How to position div div above another divs

I'm trying to set div's position like this:
but i can't set image (green box) in position.
orange box is on top
blue and lightgreen div are buttons
red frame is static distant under orange box
green box is link with image inside, covering partly blue and lightgreen buttons.
every links must stay clickable every time.
I can't centering green image and set it above orange div partly.
Example code here
<div class="header-container">
<div class="nav-container">
<div class="logo">
Click!
</div>
<div class="nav">
Click!
</div>
</div>
<div class="header-image">
<div class="image">
Click!
</div>
</div>
<div class="menu-container">
Click!
</div>
.nav-container{
width: 100%;
height: 50px;
background: orange;
}
.logo{
width: 25%;
height: 40px;
margin: 5px;
background-color: lightblue;
float: left;
}
.nav{
width: 25%;
height: 40px;
margin: 5px;
background-color: lightgreen;
float: right;
}
.header-image{
width: 100%;
border: 1px solid green;
position: relative;
z-index: 2;
text-align: center;
}
.image{
height: 100px;
width: 60%;
background: green;
opacity: 0.6;
}
.header-image a{
padding: 40px 0;
}
.menu-container{
width: 100%;
border: 1px red solid;
height: 40px;
margin-top: 50px;
}
I've uploaded your jsfiddle here.
Addded the following css:
.header-image {
position: absolute;
top: 40px;
left: 20%;
}
also added extra margin-top for the .menu-container
.menu-container {
margin-top: 80px; //instead of 50px
}
I've positioned it absolute because this way it will go wherever you want it based on the body relative positioning.
adding this to image should work:
margin:0 auto;
position:relatve;
z-index:66;
margin-top:-10px
http://jsfiddle.net/o3oyuzb9/2/
try this
only changed the css
body,html{margin: 10px;}
.header-container{
width: 100%;
}
a{
text-decoration:none;
color:#000;
padding: 10px 0px;
display: block;
text-align: center;
}
.nav-container{
width: 100%;
height: 50px;
background: orange;
}
.logo{
width: 25%;
height: 40px;
margin: 5px;
background-color: lightblue;
float: left;
}
.nav{
width: 25%;
height: 40px;
margin: 5px;
background-color: lightgreen;
float: right;
}
.header-image{
width: 100%;
border: 1px solid green;
position: relative;
z-index: 2;
text-align: center;
}
.image{
height: 100px;
width: 60%;
margin: 0 auto;
margin-top: -20px;
background: green;
opacity: 0.6;
}
.header-image a{
padding: 40px 0;
}
.menu-container{
width: 100%;
border: 1px red solid;
height: 40px;
margin-top: 50px;
}
just add this to your image class:
margin: 0 auto;
margin-top: -20px;

A specific layout using float

Ii's been a while since I programmed a website and it seems that I have forgot some basic concepts about FLOAT!
Can anyone help making this (on picture) layout using 4 divs and FLOAT?
I have tried this:
<header>
<div id="fill_left"></div>
<div id="top"></div>
<div id="fill_right"></div>
<div id="bottom"></div>
</header>
And CSS:
header {
width: 100%;
height: 9em;
background-color: lightblue;
}
#fill_left{
width: 5%;
height: 100%;
background-color: lightcoral;
float: left;
}
#top{
width:80%;
height: 50%;
background-color: lightgray;
float: left;
}
#bottom{
width:80%;
height: 50%;
background-color: lightseagreen;
float: left;
}
#fill_right{
width: 5%;
height: 100%;
background-color: lightcoral;
float: left;
}
And of course they're all inside a wrapper{ width: 100%, height: 138px}
Thanks
You could do it by using an extra div to warp div-2 and div-3 by set float: left;
JSFiddle - DEMO or Full Screen View
HTML:
<div id="div-p">
<div id="div-1"></div>
<div id="div-c">
<div id="div-2"></div>
<div id="div-3"></div>
</div>
<div id="div-4"></div>
</div>
CSS:
body {
margin:0;
}
#div-p {
width: 100%;
height: 138px;
}
#div-c {
width: 90%;
height: 138px;
float: left;
}
#div-1 {
width: 5%;
float: left;
height: 100%;
background: red;
}
#div-2 {
width: 100%;
height: 50%;
background: green;
}
#div-3 {
width: 100%;
height: 50%;
background: blue;
}
#div-4 {
width: 5%;
float: left;
height: 100%;
background: black;
}
Solution 2:
You could also do it by using display: inline-block; and set font-size:0px; to #div-p for remove the white-space and then set font-size: 16px; (i.e 16px = 1em) to child elements.
JSFiddle - DEMO or Full Screen View
body {
margin:0;
}
#div-p {
width: 100%;
height: 138px;
font-size: 0px;
}
#div-c {
width: 90%;
height: 138px;
display: inline-block;
font-size: 16px;
}
#div-1 {
width: 5%;
display: inline-block;
height: 100%;
background: red;
font-size: 16px;
}
#div-2 {
width: 100%;
height: 50%;
background: green;
}
#div-3 {
width: 100%;
height: 50%;
background: blue;
}
#div-4 {
width: 5%;
display: inline-block;
height: 100%;
background: black;
font-size: 16px;
}
That simple Notice the order: http://jsbin.com/liqulo/1/edit
<div id="fill_left"></div>
<div id="fill_right"></div>
<div id="top"></div>
<div id="bottom"></div>
CSS:
#fill_left,
#fill_right{
width: 5%;
height: 100%;
background-color: lightcoral;
}
#fill_left{ float: left; }
#fill_right{ float: right; } /* and notice Float Right */
But if I were you I would do it even simpler: http://jsbin.com/liqulo/2/edit
<header>
<div class="cont" id="top"></div>
<div class="cont" id="bottom"></div>
</header>
header {
width: 100%;
height: 9em;
background-color: lightcoral;
}
.cont{
width:90%;
height: 50%;
margin:0 auto;
}

Why is there spacing below this image

For some reason, I can't figure out why there is like an 8px spacing below this image within a div. check it out:
If you look at the bottom of the image, there's about an 8px space, how can i get rid of it?
.side {
margin-top: 40px;
float: right;
}
.sidehead {
width: 260px;
height: 27px;
}
.sidecont {
background-color: #394d49;
width: 258px;
height: auto;
border: 1px solid #6b958b;
margin-top: -1px;
margin-bottom: 20px;
vertical-align: middle;
}
<!----HTML BELOW---->
<div class="sidecont">
<center><img src="images/rankings.jpg"></center>
</div>
Here's the entire code, if anyone needs it:
body {
background-image: url('images/bg.png');
background-color: #5a6a7a;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
.wrap {
width: 960px;
margin: auto;
}
.head {
width: 960px;
height: 60px;
margin-top: 33px;
}
.head img {
float: left;
}
.head .home img { background: url('images/home.jpg'); width: 116px; height: 60px; }
.head .register img { background: url('images/register.jpg'); width: 116px; height: 60px; }
.head .forum img { background: url('images/forum.jpg'); width: 116px; height: 60px; }
.head .banner img { background: url('images/banner.jpg'); width: 263px; height: 60px; }
.head .chat img { background: url('images/chat.jpg'); width: 116px; height: 60px; }
.head .downloads img { background: url('images/downloads.jpg'); width: 116px; height: 60px; }
.head .donate img { background: url('images/donate.jpg'); width: 117px; height: 60px; }
.head .home img:hover { background: url('images/homehover.jpg'); width: 116px; height: 60px; }
.head .register img:hover { background: url('images/registerhover.jpg'); width: 116px; height: 60px; }
.head .forum img:hover { background: url('images/forumhover.jpg'); width: 116px; height: 60px; }
.head .chat img:hover { background: url('images/chathover.jpg'); width: 116px; height: 60px; }
.head .downloads img:hover { background: url('images/downloadshover.jpg'); width: 116px; height: 60px; }
.head .donate img:hover { background: url('images/donatehover.jpg'); width: 117px; height: 60px; }
.logo {
background-image: url('images/logo.png');
width:250px;
height: 110px;
display:block;
position:absolute;
left:0;
right:0;
bottom:0;
margin: -85px auto;
z-index: 1;
}
.main {
margin-top: 40px;
float: left;
clear: both;
}
* {
margin: 0;
padding: 0;
}
.mainhead {
background-image: url('images/content.jpg');
height: 27px;
width: 680px;
}
.maincont {
background-color: #394d49;
width: 668px;
border: 1px solid #6b958b;
margin-top: -1px;
height: auto;
word-wrap: break-word;
padding: 5px;
}
.side {
margin-top: 40px;
float: right;
}
.sidehead {
width: 260px;
height: 27px;
}
.sidecont {
background-color: #394d49;
width: 258px;
height: auto;
border: 1px solid #6b958b;
margin-top: -1px;
margin-bottom: 20px;
vertical-align: middle;
}
.footer {
background-image: url('images/footer.jpg');
width: 960px;
height: 19px;
margin-top: 20px;
font-size: 11.6px;
font-family: 'MS Serif';
text-align: center;
color: #dfd591;
text-shadow: 0 1px 2px black;
line-height: 19px;
}
__
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="wrap">
<div style="position:relative;">
<div class="logo"></div>
</div>
<div class="head">
<div class="home"><img></div>
<div class="register"><img></div>
<div class="forum"><img></div>
<div class="banner"><img></div>
<div class="chat"><img></div>
<div class="downloads"><img></div>
<div class="donate"><img></div>
</div>
<div class="main">
<div class="mainhead"></div>
<div class="maincont">
</div>
</div>
<div class="side">
<div class="sidehead"><img src="images/servinfo.jpg"></div>
<div class="sidecont">asdf</div>
<div class="sidecont">
<center><img src="images/rankings.jpg"></center>
</div>
<div class="sidehead"><img src="images/partners.jpg"></div>
<div class="sidecont">
<center><img src="images/epvp.jpg"></center>
</div>
</div>
<div style="clear:both;"></div>
<div class="footer"></div>
</div>
</body>
An image is an inline element, which means that it is placed on the baseline of the text. There is space below the baseline in a text block, for characters with descenders like p and g. It's that space that creates the spacing below the image.
You can make the image a block element to avoid that space, and don't use the deprecated center tag, use margins to center the image:
HTML:
<div class="sidecont">
<img src="images/rankings.jpg">
</div>
Add to the CSS:
.sidecont img { display: block; margin: 0 auto; }
no tag center <!doctype html> html5
<div class="sidecont">
<img src="images/rankings.jpg">
</div>
.sidecont {
margin: 0 auto;
background-color: #394d49;
width: 258px;
height: auto;
border: 1px solid #6b958b;
}

Slideshow flickering image on :hover

I'm creating some sort of slideshow. When you hover over an image the image will expand and overlap the other images. But when this happens I still want those images behind the expanded image to work when hovering over their area.
Right now it almost works perfectly, only the expanded image is flickering when you hover over it. I don't want that. How can I solve this?
DEMO
HTML:
<div id="expand">
<div class="expand_1">
<figure></figure>
</div>
<div class="expand_2">
<figure></figure>
</div>
<div class="expand_3">
<figure></figure>
</div>
<div class="expand_4">
<figure></figure>
</div>
</div>
CSS:
#expand {
height: 200px;
}
.expand_1 {
width: 100px;
height: 200px;
margin: 0;
padding: 0;
float: left;
}
.expand_1 figure {
width: 100px;
height: 200px;
margin: 0;
padding: 0;
float: left;
background: grey;
}
.expand_1 figure:hover {
width: 400px;
height: 200px;
float: left;
position: absolute;
z-index: 1;
pointer-events: none;
background: grey;
}
.expand_2 {
width: 100px;
height: 200px;
margin: 0;
padding: 0;
float: left;
}
.expand_2 figure {
width: 100px;
height: 200px;
margin: 0;
padding: 0;
float: left;
background: black;
}
.expand_2 figure:hover {
margin-left: -100px;
width: 400px;
height: 200px;
float: left;
position: absolute;
z-index: 1;
pointer-events: none;
background: black;
}
.expand_3 {
width: 100px;
height: 200px;
margin: 0;
padding: 0;
float: left;
}
.expand_3 figure {
width: 100px;
height: 200px;
margin: 0;
padding: 0;
float: left;
background: green;
}
.expand_3 figure:hover {
margin-left: -200px;
width: 400px;
height: 200px;
float: left;
position: absolute;
z-index: 1;
pointer-events: none;
background: green;
}
.expand_4 {
width: 100px;
height: 200px;
margin: 0;
padding: 0;
float: left;
}
.expand_4 figure {
width: 100px;
height: 200px;
margin: 0;
padding: 0;
float: left;
background-color: blue;
}
.expand_4 figure:hover {
margin-left: -300px;
width: 400px;
height: 200px;
float: left;
position: absolute;
z-index: 1;
pointer-events: none;
background-color: blue;
}
Working DEMO
Don't use pointer-events: none; It is causing the flickering effect. It is also not supported by old IE browsers.
Hover on div instead of figure:
.expand_1:hover figure