I am trying to create a div that has a button on the left, right and then a header in between. Something that looks like this:
But I'm getting something like this:
You can see that there is a margin being added and I can't figure out why. I have inspected everything I can, and I can't figure out what I'm doing wrong. (I understand that the h2 is at 50%. I did this to try and figure out why the right button was being pushed down.)
Here is my HTML:
<div class="day_buttons">
<button id="previous_day"></button>
<h2 id="zip_h2">What day and time would you like your stuff picked up?</h2>
<button id="next_day"></button>
</div><!--end nav_buttons-->
Here is the CSS to go with it:
#next_day
{
float: right;
background: transparent url(./images/icons/forward_button.gif) no-repeat top left;
width:4em;
height:4em;
z-index:5;
}
#previous_day
{
position:relative;
top:0;
left:0;
float: left;
background: transparent url(./images/icons/backward_button.gif) no-repeat top left;
width:4em;
height:4em;
z-index:5;
}
.day_buttons
{
height:3em;
width:100%;
display:inline-block;
}
#zip_h2
{
width: 50%;
margin:0px !important;
overflow:hidden;
}
Here is a Fiddle for those who need it: http://jsfiddle.net/tK72Z/
H2 elements are block-level so it will always try to fill 100% of the width on the "line" unless you tell it not to.
Just add float:left to your h2 styling and you should be good.
I think you're looking for something like this.
#zip_h2
{
width: 50%;
margin: 0 auto;
text-align: center;
}
OR
#zip_h2
{
width: auto;
text-align: center;
}
try it like this:
HTML:
<div class="day_buttons">
<button id="previous_day"></button>
<h2 id="zip_h2">What day and time would you like your stuff picked up?</h2>
<button id="next_day"></button>
</div>
CSS:
.day_buttons
{
position: relative;
}
.day_buttons h2
{
text-align: center;
}
.day_buttons button
{
position: absolute;
top: 10px;
}
.day_buttons #previous_day
{
left: 0;
}
.day_buttons #next_day
{
right: 0;
}
Just a small Change
top: 0;
right: 0;
Working Fiddle
Changes in the CSS :
For the right button
#next_day
{
position: absolute;
float: right;
top: 10px;
right: 10px;
background: transparent url(right.jpg) no-repeat top left;
width:4em;
height:4em;
z-index:5;
}
For the heading
#zip_h2
{
margin:0px !important;
padding-left: 20px;
padding-right: 70px;
text-align: center;
overflow:hidden;
}
Related
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.
I'm sorry I tried everything, tried for thirty minutes couldn't get anything. Ok so this is my code. For some reason "div2" is not working and not moving down but ONLY able to move left and right. I want it to be able to move down and up from top or bottom of screen, but its only moving left and right and is located on the same level as the navbar
body {
background-color: LightGoldenRodYellow;
font-family:"Arial Black", Gadget, sans-serif;
}
#rightcolumn {
float: left;
margin-left: 100px;
}
#wrapper {
background-color: #33CC00;
color: #000066;
}
#midcolumn {
float: left;
width: 100px;
Margin: 15px;
}
#leftcolumn {
float: left;
width: 100px;
margin: 10px;
}
#footer {
clear: both;
position: relative;
z-index: 10;
height: 3em;
margin-top: -3em;
}
.navbar {
text-decoration: none;
margin: 15px;
display: block;
}
.div2{
position:absolute;
float: left;
left: 200px;
margin-top:100px:
}
.title{
color: LightCoral;
position:absolute;
left: 50%;
top: 10%;
}
.titlesub{
color: Green;
position:absolute;
font-size: 12pt;
left: 50%;
top; 30%;
}
.content{
color: LightCoral;
}
ok next file
<!doctype htm>
<html>
<head>
<link href="midterm_helper.css" rel="stylesheet" type="text/css" />
<title>Vacation Destination </title>
</head>
<body>
<h1 class="title">
Vacation Island
<p class="titlesub">
~vacation awaits!
</p>
</h1>
<p class="div2">
test
</p>
<div>
<a class="navbar" href="hotel_form.html" target="_blank">Reservations</a>
<a class="navbar" href="hotel_room.html" target="_blank">Rooms</a>
</div>
</body>
</html>
If any of you actually bothered to read his code, you'd see that he has a colon: after his top style instead of a semi-colon;
This will do what you want it to do. The float: left; isn't necessary though :)
.div2 {
position:absolute;
float: left;
left: 200px;
margin-top:100px;
}
To position the div2 from bottom try this giving bottom value to the div
.div2{
bottom:0; // change the value as you need
}
why you giving position and float at same time in class. If you used position:absolute then you may give the values to top bottom and left and right.
Also your HTML code is really messed up. remove unwanted space and first make it proper HTML.
For example you are giving position and floating the element at same time. you should use only one at one time.
.div2{
position:absolute; /*either you should remove it*/
float: left; /*or you should remove this line*/
left: 200px;
top:100px;
left:0;
}
You can set top to move your div2 up and down like you have set by left
.div2{
top: 20px; /*set the value depending upon your requirement */
}
I am facing a problem in positioning a text at the top of the image. The image is not in background.It's just with image tag.
The thing is I can't change the html code. Is it possible to achieve what I want but without changing the html code.
<div class="home_box">
<img src="http://netdna.seospecialist.co.uk/wp-content/uploads/2012/12/christmas-three.png" class="holding">
<h4>hot off the server</h4>
</div>
Jsfiddle : http://jsfiddle.net/EkzdE/11/
I have updated the fiddle. Now when you resize the window the image is moving but the text is staying there.Is there any way to make it responsive
Try this:
FIDDLE
CSS:
.home_box {
position:relative;
text-align:center;
}
img.holding {
position:relative;
margin-top:40px;
}
.home_box h4 {
color: #000;
font-family:'arial';
font-size: 15px;
left: 140px;
line-height: 33px;
position: absolute;
text-align: center;
text-transform: uppercase;
width: 200px;
height:40px;
left:50%;
top:0;
margin-left:-100px;
}
Write:
.home_box h4 {
width: 200px;
top:0;margin:0;
}
Updated fiddle here.
All you need to do is set the h4 element's top property to zero so it sits at the top of the div, right over the image.
.home_box h4 {
...
left: 0;
top: 0;
...
}
Here is an updated fiddle.
You should make the positioning on both the image and the text relative.
This ensures that they both move according to the div as the window size changes.
Then, in order to put the text on top of the image, use a negative top margin.
.home_box {
position:relative;
text-align:center;
}
img.holding {
position:relative;
}
.home_box h4 {
color: #000;
font-family:'arial';
font-size: 15px;
margin-top: -200px;
margin-left: auto;
margin-right: auto;
line-height: 33px;
position: relative;
text-align: center;
text-transform: uppercase;
width: 200px;
}
Updated Fiddle
The question doesn't describe this pretty well.
So I got three small images that are suppose to change on hover and work as a link, but it ''detects'' the hover only in a small part of the image. If I drag my mouse to the bottom of the image link, it's not even clickable, so the link only works in the top part of the image.
See for yourself:
http://jsfiddle.net/M3LC9/ (JSFiddle doesn't like pictures..)
<div class="kielet">
<nav>
<!--Englanti-->
<img class="icon" src="iconit/en.gif" title="in english" onmouseover="this.src='iconit/en_hover.gif'" onmouseout= "this.src='iconit/en.gif'">
<!--Ruotsi-->
<img class="icon" src="iconit/swe.gif" title="på svenska" onmouseover="this.src='iconit/swe_hover.gif'" onmouseout="this.src='iconit/swe.gif'">
<!--Venäjä-->
<img class="icon" src="iconit/ru.gif" title="По русски" onmouseover="this.src='iconit/ru_hover.gif'" onmouseout="this.src='iconit/ru.gif'">
</div>
.kielet {
top:0px;
width:100%;
background: black;
right: 0px;
margin-bottom:0px;
padding:0px;
}
.kielet nav {
right: 0px;
margin-right: 0px;
text-align: right;
}
.icon {
width: 50px;
height: 100%;
right: 0px;
margin: 20px;
margin-top:0px;
margin-bottom:0px;
display:inline;
padding: 0px;
}
You currently have your images set to display as inline. This will make them adhere to any line-height defaults a browser may have set on your a element, keeping your a element at a smaller height. This can be visualised in Chrome's Element Inspector:
To change this, simply set the display on your a elements to inline-block:
a {
display: inline-block;
}
JSFiddle demo.
Note that you may want to be a bit more specific with your a selector by specifying .kielet nav a, for instance, or giving your a elements their own class identifier.
Try changing the display property to display:inline-block
.icon {
width: 50px;
height: 100%;
right: 0px;
margin: 20px;
margin-top:0px;
margin-bottom:0px;
display:inline-block; <----
padding: 0px;
}
JSFiddle
Usually you don't implement your hover-state with javascript and <img />
You can easily do this with CSS.
HTML
<div class="kielet">
<nav>
<!--Englanti-->
<!--Ruotsi-->
<!--Venäjä-->
</nav>
</div>
CSS
.kielet {
background: black;
padding: 5px;
text-align: center;
}
a.icon {
display: inline-block;
width: 16px;
heiht: 16px;
line-height: 16px;
}
a.icon_ru { background: url(http://placehold.it/16x16/ffc) center no-repeat; }
a.icon_ru:hover { background: url(http://placehold.it/16x16/ff0) center no-repeat; }
a.icon_en { background: url(http://placehold.it/16x16/cff) center no-repeat; }
a.icon_en:hover { background: url(http://placehold.it/16x16/0ff) center no-repeat; }
a.icon_swe { background: url(http://placehold.it/16x16/fcf) center no-repeat; }
a.icon_swe:hover { background: url(http://placehold.it/16x16/f0f) center no-repeat; }
jsFiddle
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)