HTML/CSS Css box messing up margin top - html

So I have this CSS box:
.navigation-div
{
text-align:right;
margin-top:14px;
box-shadow:0px 0px 10px rgba(0,0,0,0.47);
padding: 0;
color:#E3E3E3;
background-color: #333;
}
With an image and a piece of text inside of it
#mailtext
{
margin-top:-10px;
display:inline-block;
font-size:20px;
color:white;
font-style:italic;
}
#mailpicture
{
display:inline-block;
margin-top:16px;
}
This is the HTML I have for it:
<div class="navigation-div">
<nav class="navigation">
<h1 id="mailtext">Mail Us</h1>
<img id="mailpicture" src="images/gmail.png">
</nav>
</div>
Currently there is no styling for the class navigation. The Mail picture is in the correct position, but the text I want to go upwards. As you can see from the #mailtext styling I have margin-top:-10px; This does not move the text upwards.
How would I move this text upwards with or without using margin-top.
This question is like my previous question in a way, but now the text will not go to where I want it to (upwards). Using margin-left is bad, but when I did that I could move the margin top also. Since the navigation-div has a text align of right, this might be messing it up.
How would I keep the text in the same position with moving the margin top without using margin left. I would like to keep the text on the same line with the image, not above. The picture is in the right place, all i want to move is the text higher. I want the text to be parallel to the center of the image on the same line.
The previous question I have posted was about keeping all the elements on the same line, this one is about moving the margin-top.

To align the text a little higher you need to replace margin-top with position: relative and top:-10px, like in the code snippet and fiddle.
For a more efficient solution i recommend using the CSS property vertical-align. In this case if the image(size) is changed, it will still align with the text.
JSFiddle
.navigation-div {
text-align: right;
margin-top: 14px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.47);
padding: 0;
color: #E3E3E3;
background-color: #333;
}
#mailtext {
position: relative;
top: -10px;
display: inline-block;
font-size: 20px;
color: white;
font-style: italic;
}
#mailpicture {
display: inline-block;
margin-top: 16px;
}
<div class="navigation-div">
<nav class="navigation">
<h1 id="mailtext">Mail Us</h1>
<a href="mailto:info#email.com">
<img id="mailpicture" src="images/gmail.png">
</a>
</nav>
</div>

Edit:
.navigation {
margin-top:14px;
box-shadow:0px 0px 10px rgba(0, 0, 0, 0.47);
padding: 0;
color:#E3E3E3;
background-color: #333;
width: 100%;
height: 100px;
}
.container {
display: flex;
align-items: center;
justify-content: center;
float: right;
line-height: 0.5;
text-align: center;
margin-right: 20px;
}
#mailtext {
align-self: center;
font-size: 20px;
color: white;
font-style: italic;
margin-right: 15px;
}
#mailpicture {
align-self: center;
}
<nav class="navigation">
<div class="container">
<h1 id="mailtext">Mail Us</h1>
<a href="mailto:info#email.com">
<img id="mailpicture" src="images/gmail.png">
</a>
</div>
</nav>
JS Fiddle

There are issues with inline-block elements, especially when one is a block element and the other is an inline element. What I'd do is set parent font-size to 0 and come back to the H1 element to set the desired font-size. Then, I'd set vertical alignment to middle.
Mention: setting an id to the image element doesn't work, IMHO, without working with the link that contains it. They're both inline elements and one has to include the other, acting like a block, right?
Check the code, its a bit simplified, but you will definitely work it out.
.navigation-div {
background: #333;
color: #fff;
font-size: 0;
text-align: right;
}
#mailtext {
font-size: 20px;
margin-right: 5px;
}
.navigation a,
#mailtext {
display: inline-block;
vertical-align: middle;
}
#mailpicture {
display: block;
}
<div class="navigation-div">
<nav class="navigation">
<h1 id="mailtext">Mail Us</h1>
<img id="mailpicture" src="images/gmail.png">
</nav>
</div>

I don't know how big your logo will be, but here is an approach that is relatively clean.
For the two inline-block elements, #mailtext and #mailpicture, set vertical-align: middle.
For #mailtext, zero-out the default margins from the h1 tag.
For #mailpicture, adjust the left and right margins as need to get horizontal white space suitable with your design. and then set the top and bottom margin accordingly.
The vertical-align property will keep the two elements centered with respect to each other.
However, if your image is such that the visual center of the image is not at the mid-height of the image, you can add position: relative to #mailtext and adjust the top or bottom offset (use one or the other).
If your image height is actually less than the height of the text, apply the position-relative adjustment to the image instead of the text.
.navigation-div {
text-align: right;
margin-top: 14px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.47);
padding: 0;
color: #E3E3E3;
background-color: #333;
}
#mailtext {
display: inline-block;
vertical-align: middle;
margin: 0; /* zero out h1 margins */
font-size: 20px;
color: white;
font-style: italic;
}
#mailpicture {
display: inline-block;
margin: 10px; /*adjust as needed */
vertical-align: middle;
}
.ex2 #mailtext {
font-size: 2.5em;
margin: 10px 0;
}
.tweak #mailpicture {
position: relative;
bottom: 5px; /* use top if you want to move the image downward */
}
<div class="navigation-div">
<nav class="navigation">
<h1 id="mailtext">Large Logo - Mail Us</h1>
<a href="mailto:info#email.com">
<img id="mailpicture" src="http://placehold.it/100x50">
</a>
</nav>
</div>
<div class="navigation-div ex2">
<nav class="navigation">
<h1 id="mailtext">Small Logo - Mail Us</h1>
<a href="mailto:info#email.com">
<img id="mailpicture" src="http://placehold.it/100x10">
</a>
</nav>
</div>
<div class="navigation-div ex2 tweak">
<nav class="navigation">
<h1 id="mailtext">Position Tweak - Mail Us</h1>
<a href="mailto:info#email.com">
<img id="mailpicture" src="http://placehold.it/100x10">
</a>
</nav>
</div>

Related

HTML div element not allocates new line if sibling div element has <a href> alone

I am new to HTML and CSS.
In first div I want to display elements and in second div I want to display text over background image.
But in the result element is displaying over background image. I want to display element and in below line I want to display background image. How to achieve this?
Below is my code. I am using HTML and CSS.
.navbar {
width: 100%;
}
.nav-left {
float: right;
width: 25%;
position: absolute;
padding-top: 14px;
}
.feature {
border: 2px solid black;
padding: 25px;
background: url(https://static3.depositphotos.com/1005590/206/i/950/depositphotos_2068887-stock-photo-lightbulb.jpg);
background-repeat: no-repeat;
background-size: 100% 100%;
}
.navbar-Logo {
float: right;
color: #dd845a;
text-decoration: none;
}
<div class="navbar">
<div class="nav-left">
<a class="navbar-Logo" href="#">LOGO</a>
</div>
</div>
<div class="feature">
<h1> Sample Text</h1>
<p>Sample Text Sample Text Sample Text Sample Text Sample Text Sample Text.</p>
<p>Engage Now</p>
</div>
Seems you're not understanding the CSS you're using:
Remove the floats, replace the float:right with text-align: right in .nav-left
Remove position: absolute from .nav-left
Result below:
.navbar {
width: 100%;
}
.nav-left {
text-align: right;
width: 25%;
padding-top: 14px;
}
.feature {
border: 2px solid black;
padding: 25px;
background: url(https://static3.depositphotos.com/1005590/206/i/950/depositphotos_2068887-stock-photo-lightbulb.jpg);
background-repeat: no-repeat;
background-size: 100% 100%;
}
.navbar-Logo {
color: #dd845a;
text-decoration: none;
}
<div class="navbar">
<div class="nav-left">
<a class="navbar-Logo" href="#">LOGO</a>
</div>
</div>
<div class="feature">
<h1> Sample Text</h1>
<p>Sample Text Sample Text Sample Text Sample Text Sample Text Sample Text.</p>
<p>Engage Now</p>
</div>
Setting your .nav-bar element to position: absolute makes it leave the document flow, and so it will be rendered on top of the next <div>.
Using only float elements inside a block will make it 0-height with elements overflowing, and so rendered over following elements in document flow.
Change your .navbar css code to this:
.navbar
{
height: 120px;
}
I think what you are trying to do is that you like to position your link in the navbar to the right within the navbar and the feature div below it.
But the issue is the float property you are using here is taking the applied element out of the document flow, thus pushing the proceeding elements to take it's space.
Here you can use flexbox with justify content property set to flex end to achieve this. Never use float unless it's absolutely necessary.
<!DOCTYPE html>
<html>
<head>
<style>
.navbar
{
width: 100%;
border: 2px solid black;
padding: 35px;
}
.nav-left {
float: right;
width: 25%;
position: absolute;
padding-top: 10px;
}
.feature {
border: 2px solid black;
padding: 25px;
background: url(light_bulb.jpg);
background-repeat: no-repeat;
background-size: 100% 100%;
}
.navbar-Logo {
float:right;
color: #dd845a;
text-decoration: none;
}
</style>
</head>
<body>
<div class="navbar">
<div class="nav-left">
<a class="navbar-Logo" href="#">LOGO</a>
</div>
</div>
<div class="feature">
<h1> Sample Text</h1>
<p>Sample Text Sample Text Sample Text Sample Text Sample Text Sample Text.</p>
<p>Engage Now</p>
</div>
</body>
</html>

My CSS positioning is unstable. How can I stabilize it?

The elements of the web page I'm building move around when I want them to stay in place. If the line beginning "This is a Beta version" changes length, the 4 menu items (translucent gray rectangles) shift: If I shorten the bottom line, the menu items move to the right; if I lengthen it, the menu items move left.
How can I arrange things so that the width of one element won't change the horizontal position of other elements that are stacked vertically with it?
You can view the page at http://apdamien.info/nfair/GH/demo/mainmenu.html
Here is what I think is the relevant sections of the code:
CSS:
#mainmenu {
width: 350px;
padding-bottom: 14px;
margin-right: 60px;
}
#maindiv {
background: url(imgs/smalltown.jpg) no-repeat;
}
.menu-entry {
display: block;
cursor: default;
color: #fff;
width: 100%;
height: 39px;
padding-top: 13px;
font-size: 20px;
font-weight: bold;
font-family: Univers,sans-serif;
text-transform: uppercase;
margin-bottom: 19px;
margin-left: 8em;
border: 2px solid black;
text-align: center;
background: rgba(96,96,96,0.65);
text-decoration: none;
text-transform: uppercase;
}
.menu-entry:hover {
background: rgba(0,0,0,0.5);
}
And the relevant chunk of HTML:
<body>
<div id="maindiv">
<div id="titleauth">
<div id="title"><img alt="Demo Game" src="imgs/title.svg"/></div>
<div id="author"><a href="http://www.apdamien.info"
target="_blank"><img alt="A. P. Damien" src="imgs/author.svg"/></a>
</div>
</div>
<div id="lowerleft">
<div id="mainmenu">
<a class="menu-entry" href="game.html">New Game</a>
<a class="menu-entry" href="helpmain.html">How to Play</a>
<a class="menu-entry" href="restore.html">Restore Saved Game</a>
<a class="menu-entry" href="credits.html">Credits</a>
</div>
<div id="bottom-line">
<img alt="Beta version warning" src="imgs/beta.svg" />
</div>
</div>
</div>
</body>
Both #mainmenu and #bottom-line are positioned relative to the left-hand side of #lowerleft. If you want to have them positioned relative to the right-hand side, you'll need to float all children to the right. You'll also want to use clear: both, so that the children won't sit next to each other.
#lowerleft * {
float: right;
clear: both;
}
Using the above on your live site allows me to adjust the width of #bottom-line without affecting the postion of #mainmenu, so this should hopefully work for you :)
Note that this shifts the menu slightly to the right. If you want to move the main menu back to its original position, you can increase its margin-right value. It would have had no effect on the menu previously, though the addition of float: right also fixes that ;)

Weird margins on inline-block elements and unable to vertically align element inside them

I think I'm going insane over this now, no idea how to resolve it... please help guys.
I have three divs on a page that should all fit onto one line. They have to be square (with rounded corners) so I have to set a width and a height to keep the 1:1 aspect ratio. I have a heading inside them that should be vertically and horizontally centered. The wording of the heading may change and might run over 2 lines so a simple margin-top is not enough in this case.
First problem: there are weird margins at the top despite there not being anything else affecting that (well there must be but I can't see what). If I float the divs they line up but floating isn't the way to go is it... why is inline-block not working?
Second issue (which is likely related, so I'm posting it in one go) is that I'm unable to vertically center the title divs. Any ideas?
Here's a jsfiddle to illustrate: http://jsfiddle.net/fydC4/
The HTML:
<div id="container">
<div class="nav-left">
<p id="nav-left-title">In this section…</p>
<ul>
<li><a class="light" href="#">page title here</a></li>
<li><a class="light" href="#">page title here</a></li>
<li><a class="light" href="#">page title here</a></li>
</ul>
</div>
<div id="main">
<h1>Assignments</h1>
<p>Click on the titles of the assignments to find out more.</p>
<div class="box" id="good-designs">
<h2 class="box">3 good designs</h2>
</div>
<div class="box" id="temp">
<h2 class="box">title here</h2>
</div>
<div class="box" id="temp2">
<h2 class="box">title here</h2>
</div>
</div><!--end main-->
</div>
</div><!--end container-->
The CSS:
#container {
max-width: 960px;
margin: auto;
}
#main {
display: table-cell;
width: 73em;
padding: 1em 2em 2em;
background-color: white;
}
#nav-left-title {
padding-bottom: 0.5em;
margin: 0;
color: white;
}
.nav-left{
display: table-cell;
width: 14em;
background-color: #87a8b1;
padding: 1.1em;
font-size: 1.2em;
}
.nav-left li {
padding: 0.5em 0;
border-bottom: 1px solid white;
}
h2.box {
padding: 15px 0;
margin: 50% 15px;
margin: auto;
text-transform: uppercase;
text-align: center;
}
div.box {
padding: 15px;
height: 180px;
width: 180px;
border-radius: 50%;
margin-top: 25px;
margin-left: 1.5em;
display:inline-block;
/* float: left; */
}
#good-designs {
background-color: green;
}
#temp, #temp2 {
background-color: yellow;
}
Hi you may use two properties to align all your elements
vertical-align:middle;
display:inline-table on div.box and
display:table-cell on h2.box; (for the texts inside your divs)
Check this code http://jsfiddle.net/fydC4/16/
This worked for me, replace inline-block with float left.
you are also calling margins twice on some element which are not necessary
here you go
jsfiddle.net/fydC4/14

position of layers when resizing browser

when I'm resizing my browser-window the blue buttons go below the logo on the left, on the same line as the text "Welkom Bart" although they are two different layers. I want the text "Welkom Bart" to lower as well, so they are not on the same line. What do I need to add to my css?
html e.g.
<div id="mainmenu">
<div id="logo"><img ... /></div>
<div id="usermenu">Buttons</div>
</div>
<div id="maintitle">
<h2>Welkom Bart</h2>
<hr />
</div>
css
#mainmenu {
height: 100px;
position: relative;
}
#logo {
float: left;
width: 200px;
margin-right: 20px;
}
#usermenu {
float: right;
}
#maintitle {
margin-bottom: 20px;
}
#maintitle hr {
color: #56c2e1;
display: block;
height: 1px;
border: 0;
border-top: 1px solid #56c2e1;
margin: 10px 0;
}
Add clear:both to #maintitle =)
Add overflow:hidden to #mainmenu. This will cause its height to include all floated elements, such as your #usermenu element, allowing flow to continue underneath it.
Use this
<div id="maintitle" style="width:100%">
<h2>Welkom Bart</h2>
<hr />

How can I center an image with text that follows it using CSS?

I have the following HTML:
<p style="line-height: 20px; margin-bottom: 10px;"> </p>
<div class="icon sprite-book-open" style="margin-right: 5px"></div>
Topic: No Topic Title
<p></p>
and my CSS is like this:
.icon {
background-color: transparent;
background-image: url(/Images/fugue/sprite.png);
background-repeat: no-repeat;
display: inline-block;
height: 16px;
line-height: 16px;
width: 16px;
}
.sprite-book-open{ background-position: 0 -288px; }
Is there some way that I could do this without paragraphs and so the picture icon lines up with the text. Right now the icon
looks a few pixels too high. I guess I need some way to center this in
the line but I am not sure how to do this
<style>
.icon {
line-height: 1.6em;
padding: 0 0 0 40px;
background: url('http://cdn1.iconfinder.com/data/icons/fatcow/32x32_0120/book_open.png') no-repeat 5px 50%
}
</style>
<div class="icon sprite-book-open">
<p>
Topic: No Topic Title
</p>
</div>
<style>
.icon {
display:inline-block;
height:32px;
width:32px;
background: url('http://cdn1.iconfinder.com/data/icons/fatcow/32x32_0120/book_open.png') no-repeat center center }
span {
}
p, span, .icon {vertical-align:middle}
</style>
<p>
<div class="icon sprite-book-open"></div>
<span>Topic: No Topic Title</span>
</p>
You can do it like this.
<div class="container">
<div class="icon sprite-book-open" style="margin-right: 5px"></div>
<div class="text">Topic: No Topic Title</div>
</div>
css
.text{float:left;margin-top:5px;}
.icon{float:left;margin-top:5px}
You can now adjust margin-top to get them aligned. You can use margin-left to create space between icon and text.
I am not sure I understood what you are trying to do, but if you want to put an icon in front of a paragraph, the best way is to use padding left and background.
<style>
.paragraph-with-icon {
background:url(http://www.pangoo.it/img/pangoo.gif) no-repeat;
padding:0 0 0 120px;
line-height: 53px;
}
</style>
<p class="paragraph-with-icon">
Topic: No Topic Title
</p>
I used an icon of 100x53 px in the example (120px is 100px + 20 of spacing).
You can try it here: http://jsfiddle.net/apFvw/1/
If you want it aligned to top of image, change:
line-height: 53px;
with
height: 53px;
or
min-height: 53px;
(and there are solutions for browsers not supporting min-height)
You can use margins on the same paragraph to adjust spacing without adding divs and extra markup.
How about a vertical alignment of bottom for the icon:
vertical-align:bottom;
If you want to get rid of all the unnecessary markup, consider:
HTML:
<div class="title sprite-book-open" style="margin-right: 5px">
Topic: No Topic Title
</div>
CSS:
.title{
line-height: 16px;
}
.title:before {
background-color: transparent;
content: " ";
background-image: url(/Images/fugue/sprite.png);
background-repeat: no-repeat;
display: inline-block;
height: 16px;
width: 16px;
vertical-align:bottom;
}
.sprite-book-open:before{
background-position: 0 -288px;
}
Here is a demonstration: http://jsfiddle.net/V4EM6/8/