Positioning an absolute div along side a relative one - html

I'm trying to position an absolute object beside a relative object. The initial object causes the relative div to wrap to the next line.
Everything is working as it should be, with the exception of the logo forcing the title to move to a new "line". If I change the #logo div to be position:absolute I can fix the positioning problem, but then my logo hover ceases to function.
Edit: Here's a live demo: http://vaer.ca/warm-forest-8234/
Here is my HTML:
<div id="container">
<div id="header">
<h2>Collectif</h2>
</div>
<div id="nav">
<ul>
<li>About</li>
<li>Services</li>
<li>Work</li>
</ul>
</div>
And here is my CSS:
#container {
width: 980px;
margin: 0 auto;
position: relative;
}
#header {
height: 75px;
text-align: right;
position: relative;
}
#header h2 {
font-size: 2.5em;
font-weight: 400;
text-transform: uppercase;
letter-spacing: 0.1em;
padding-top: 15px;
}
#header a {
text-decoration: none;
color: #000000;
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition:.5s;
}
#header a:hover {
color: #7acfdd;
}
#logo {
position: relative;
display: block;
margin-top: 10px;
margin-left: 10px;
background: url('../img/logo.png') no-repeat;
width: 60px;
height: 60px;
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition:.5s;
}
#logo:hover {
position: relative;
background: url('../img/logo-hover.png') no-repeat;
}

You have set you #logo link to display:block forces the <a> element to take up that whole space. See it by using the developer tools. You can do the following to fix it all and along with that, make it more semantic.
#logo{
display:inline-block;
}
#header{
display:inline-block;
float:right
}
These changes will get you your desired results.

Try:
#logo {
float: left;
}

I would say :
#logo {
float: left;
position: relative;
}
#header {
height: 75px;
text-align: right;
position: relative;
clear:both;
}
And its just fine to use a float: left; and position: relative;
(I'm always using it when i need to float a div on top of an other because when you use a position: relative; you can apply an z-index)

Related

Page seems to be stuck in an specific height, I can't create more bottom space

I am trying to clone some website to improve my skills, but I have encounter a problem, the page seems to be stuck in an specific height and when I try to add more html it just disappear (it does not disappear, it's added at the top of the page behind the background image). I really want to know what is causing this and how to fix it without messing with the background image.
.center{
text-align: center;
}
*{
margin 0;
padding: 0;
}
a:link {
color: inherit;
}
a:visited {
color:inherit;
}
a:hover {
color: #ea7640;
}
a:active {
color:inherit;
}
a {
text-decoration: none;
}
#wrapper {
background-image: url("https://66.media.tumblr.com/f79df0dd538fc53292fe1aac7cd54daf/tumblr_oga789rskz1vxey6qo1_1280.png");
position: fixed;
top: 0;
left: 0;
min-width: 100%;
min-height: 70%;
background-repeat: no-repeat;
background-size: cover;
margin-top: 3em;
}
nav {
background-color: #312822;
padding: 3px;
margin-top: -8px;
margin-left: -8px;
margin-right:-8px;
font-size: 13px;
}
li {
list-style-type: none;
display: inline-block;
margin-right: 25px;
color: #bdb9b7;
}
#proyecto {
color: #ea7640;
}
.texto {
color: #ea7640;
font-weight: bold;
}
#logo {
margin-top: 4em;
}
#text{
margin-top: 4em;
font-weight: bold;
}
#marca {
margin-top: 10em;
font-style: italic;
}
#wrapper2{
position: fixed;
min-width: 100%;
min-height: 1000px;
background-color: #fff;
margin-top: 700px;
left: 0;
}
#wrapper2 ul {
margin-top: 20px;
}
Demo: http://codepen.io/njwda/pen/PbwaOV
Just erase position: fixed from your wrapper elements - that way the elements will simply appear below each other, as they are supposed to.
Your image has position: fixed, so the other content by default has property position: static and located under the image. If you want to see your new content, your should use one of the following properties for it:
position: absolute;
position: fixed;
position: relative;
For example try to add new <h1 style = 'position: relative;'>Test</h1> to your HTML.
Here is the working example: https://jsfiddle.net/o589ynts/
Good luck

How to inline elements with CSS

I'm trying to make menubar at the top of my website.
It should looke like this:
The red square is my button.
My problem is that my headline and my button are not in the same line. So I tried to use a table but then there are both aligned to the left.
After that I used float: right; for my button.
It is now aligned right but in the next line.
How can I fix it so my button and my headline are in the same line and aligned like my picture.
HTML:
<div id="topbar">
<h1>Fahrplan</h1>
<button type="button" id="settings"></button>
</div>
CSS:
h1 {
height: 44px;
margin: 0;
color:#FFFFFF;
text-align: center;
font-family: Helvetica, sans-serif;
font-size: 16px;
line-height: 44px;
}
#topbar button {
height: 20px;
width: 20px;
float: right;
}
For this kind of scenarios, you might consider using positions.
#topbar {
position: relative;
}
h1 {
margin: 0;
color:#FFFFFF;
text-align: center;
font-family: Helvetica, sans-serif;
font-size: 16px;
line-height: 44px;
background: #99f;
}
#topbar button {
height: 20px;
width: 20px;
position: absolute;
top: 50%;
right: 5px;
margin-top: -10px;
}
<div id="topbar">
<h1>Fahrplan</h1>
<button type="button" id="settings"></button>
</div>
Here I have given position to both #topbar and the button. The #topbar has a relative position and button has an absolute position:
#topbar {
position: relative;
}
button {
position: absolute;
top: 50%;
right: 5px;
margin-top: -10px;
}
And I have also adjusted the button to be vertically centred by using the negative margin of half the height. Hope this helps.
I would rather suggest you to use absolute along with translateY() to align your button vertically middle.
Demo (Note: Am using SCSS on jsFiddle so don't get confused with the syntax)
header {
height: 40px;
background: tomato;
position: relative;
h4 {
line-height: 40px;
text-align: center;
}
button {
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 10px;
}
}
Explanation:
I am using position: absolute; to move your button to the right. As far as vertical centering goes for your button, you can use top: 50% and transform to nudge your button exactly in the middle of your header vertically. It will always stay vertically centered without you declaring any static height.
For your interest, here's how to do it with inline-blocks.
div > * {
display:inline-block;
vertical-align:middle;
}
h1 {
width:100%;
text-align:center;
margin-right:-40px;
background-color:#4F81BD;
color:#FFF;
font-family: Helvetica, sans-serif;
font-size: 16px;
height: 44px;
}
button {
height: 30px;
width: 30px;
margin-right:-6px;
border: 3px solid #8C3836;
border-radius:5px;
background-color:#C0504D;
}
<div id="topbar">
<h1>Fahrplan</h1>
<button type="button" id="settings"></button>
</div>
Here is a quick demo of how to do this using http://tachyons.io
<div class="bg-light-gray dt w-100">
<div class="dtc v-mid w3"></div>
<div class="dtc v-mid tc pv3">
<h1 class="mv0 f5">Headline</h1>
</div>
<div class="dtc v-mid tr w3 pr2">
<button class="bg-black br2 h2 w2"></button>
</div>
</div>
CSS:
.br2 {
border-radius: .25rem;
}
.dt {
display: table;
}
.dtc {
display: table-cell;
}
.h2 {
height: 2rem;
}
.w2 {
width: 2rem;
}
.w3 {
width: 4rem;
}
.w-100 {
width: 100%;
}
.bg-black {
background-color: #111;
}
.bg-light-gray {
background-color: #eee;
}
.pr2 {
padding-right: .5rem;
}
.pv3 {
padding-top: 1rem;
padding-bottom: 1rem;
}
.mv0 {
margin-top: 0;
margin-bottom: 0rem;
}
.tr {
text-align: right;
}
.tc {
text-align: center;
}
.f5 {
font-size: 1rem;
}
.v-mid {
vertical-align: middle;
}
Demo:
https://jsfiddle.net/r21mdrzs/
The downside is that you include an empty div. The plus side is that even if you zoom in or out, change font-size or size of button, everything will always be aligned to the middle, not matter what. This is in my experience is less brittle than using magic number values for positioning.
Give your headline float property;
.classNameGiveToHeading {
float: left;
}
.buttonClassName {
float: right;
}
OR give "float:right" to both of them as you like.

hide webpage element partially

My webpage has static navigation menu at top, but is lowered a bit and background is visible above it. When text in page is scrolled down it goes beneath menu and then is again visible above the menu. How can I hide the part of text which is above menu. Also the background will be an image.
If I place the text in <div> element below menu and set overflow:auto, scroll bar is then moved to the side of div element, not the page body as it is intended to be.
EDIT: Here is jsfiddle link http://jsfiddle.net/EmpireGlitch/N9xg2/
HTML:
<div class="topline">
<div class="menu">
<ul class="interpage_navigation">
<li>Choice 1
</li>
<li id="active_tab">Choice 2
</li>
</div>
<div id="top_seperator"></div>
</div>
<div class="article">
<h1>Lorem ipsum</h1>
<p>Lorem ipsum.</p>
</div>
CSS:
.menu .interpage_navigation li {
background-color: rgba(25, 102, 25, 0.8);
}
.side, #top_seperator, #active_tab {
background-color: rgba(50, 205, 50, 0.8);
}
.topline {
position: fixed;
top: 60px;
width: 100%;
}
#top_seperator {
height: 40px;
width: 100%;
clear: both;
}
.menu {
margin:0px;
margin-left:100px;
position: absolute;
bottom: 40px;
}
.menu > ul {
overflow: auto;
list-style-type: none;
padding:0px;
padding-left: 15px;
margin:0px;
//border: solid blue 1px;
}
.menu li {
float:left;
margin:0px;
}
.menu .interpage_navigation li {
width:120px;
height: 40px;
margin-right: 15px;
font-family: verdana, arial;
text-align: center;
line-height: 40px;
text-decoration: none;
text-transform: uppercase;
}
.article {
background-color: rgba(200, 200, 200, 1);
margin-bottom: 20px;
padding:20px;
padding-top: 5px;
font-size: 18px;
position: absolute;
top: 0px;
margin-top: 125px;
left: 30%;
right:15%;
z-index: -1;
}
I have used the :before pseudo element to create an element to place over the top of the page - http://jsfiddle.net/N9xg2/1/
The problem with this is that it is dependant on having a solid colour.
You may want to re-think how you're positioning the article - using position:absolute is going to really limit you.
My solution was similar to Richard's, except I just gave .menu a background (and tweaked its padding).
.menu {
margin:0px;
padding-left:100px;
position: absolute;
bottom: 40px;
background-color: white;
width: 100%;
padding-top: 20px;
}
Demo here
But I know you want to use an image background. You can solve that problem by using:
background-attachment: fixed;
Demo here

Background overlay with responsive images

I have an image with a background overlay that needs to respond to screen devices.
The bg overly needs to remain full width but putting 100% width just creates an overflow on small devices and underflow on larger ones.
Appreciate the help.
Please see my js fiddle
.col-md-3{
width:30%;
}
img{
width:100%;
}
.brand-category{
position: relative;
}
.brand-text{
position: absolute;
background-color: rgba(130,130,130,0.5);
font-weight: 100;
font-size: 0.9em;
color: #fff;
text-align: center;
bottom: -10px;
padding: 5px;
}
Paddings cousing overflow
<div class="col-md-3 brand-category">
<p class="brand-text"><span>Category</span></p>
<img src="http://paulobriendesign.com/img/paul-obrien.jpg" class="img-responsive" alt="">
</div>
css
.col-md-3{
width:30%;
}
img{
width:100%;
}
.brand-category{
position: relative;
}
.brand-text{
position: absolute;
width: 100%;
background-color: rgba(130,130,130,0.5);
font-weight: 100;
font-size: 0.9em;
color: #fff;
text-align: center;
bottom: -10px;
}
.brand-text span {
display: block;
margin: 5px;
}
http://jsfiddle.net/XWfN4/
Simply add display:block; to your img CSS
img{
width:100%;
display:block;
}
Demo Without
Demo With

Adjusting spacing in Nav Bar with Centered Logo

Here I have a nav bar set up with a centered logo. The only problem is that I can't get it to look quite right with spacing. The logo is set to 'position: absolute' 'left: 50%' and 'margin-left: -125px' so that is always in the center. Now I just need to get the text balanced around it in a more symmetrical way, but I can't seem to figure out how to do so.
Here's the HTML
<link rel="stylesheet" href="stylesheet.css"/>
</head>
<body>
<div class="header">
<div class="nav">
<ul>
<li class="navright">HOME</li>
<li class="navright">MENU</li>
<li id="logo"><img src="Images/pantry logo.png" width="536" height="348"/></li>
<li class="navleft">CONTACT</li>
<li class="navleft">ABOUT</li>
</ul>
</div>
</div><!--end header-->
And the CSS.
.header {
width: 960px;
height: 200px;
margin: 0 auto;
text-align: center;
position: relative;
}
div ul li {
display: inline-block;
padding: 105px 70px 40px 0;
font-family: "Montserrat", "Helvetica", sans-serif;
font-size: 18px;
color: #4f4d4b;
text-decoration: none;
list-style: none;
}
div ul li a {
list-style: none;
text-decoration: none;
color: #4f4d4b;
}
.nav ul li a:visited {
text-decoration: none;
color: #4f4d4b;
}
#logo a img {
width: 250px;
height: auto;
position: absolute;
left: 50%;
margin-left: -125px;
display: inline-block;
}
#logo {
height: 60px;
padding-top: 40px;
width: 250px;
}
You can go to the site here.
Here's what I would do, and this is just the way I would normally go about things.
I would take the padding of the li, then add the 105px padding to the top of the header (or nav). Next, add some arbitrary margin-right to each li element (say 48px), while of course setting li:last-child to margin: 0; Next take the padding-top and the height off the li#logo and change it to this:
#logo { width: 250px; position: relative; }
Finally, just use a transform to center the logo if you are going to use absolute positioning. This essentially does the same thing as the margin-left, but it is a little more flexible. So the image css should look like this:
#logo a img {
width: 250px;
height: auto;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
I used this code and it worked perfectly for me. I can make you a fiddle or something also if you are having trouble still.