how to right-align a nested div in all major browsers? - html

I just have a top nav of a website. I want to float a div to the right for User Login controls etc.
I looked on the web and tried but it does not work in Chrome / Firefox.
What is the EXACT way to do this correctly in ALL browsers using Divs and CSS? WHat I have works only in IE 9 .. Chrome and Firefox both drop the loginControls Div down to the next line.
<div id="topNav">
<div id="logoBar">
<img src="images/header_logo.gif" width="132" height="50" border="0" />
<div id="loginControls">User: John Doe Logout</div>
</div>
<div id="navTabs">
</div>
</div>
#logoBar {
width: 1300px;
height: 50px;
background-image: url(../images/line.gif);
background-repeat: repeat-x;
padding: 0px 22px;
position: relative;
}
#navTabs {
background-color: #333b52;
height: 30px;
}
#loginControls {
color: #FFF;
position: absolute;
right: 0;
width: 250px;
}
#loginControls a:link, #loginControls a:visited, #loginControls a:active {
color: #FFF;
text-decoration: underline;
}

Just add
float : right;
In #loginControls' CSS

The person who answered just add float is correct as long as you remove the absolute positioning.
#logoBar {
width: 1300px;
height: 50px;
background-image: url(../images/line.gif);
background-repeat: repeat-x;
padding: 0px 22px;
position: relative;
}
#navTabs {
background-color: #333b52;
height: 30px;
}
#loginControls {
float:right
}
#loginControls a:link, #loginControls a:visited, #loginControls a:active {
color: #000;
text-decoration: underline;
}
<div id="topNav">
<div id="logoBar">
<img src="images/header_logo.gif" width="132" height="50" border="0" />
<div id="loginControls">User: John Doe Logout</div>
</div>
<div id="navTabs">
</div>
</div>

The nature of a div is to always use 100% of the width of the parent Container.
you may have to try span instead of div for the Login Controls.
OR use float:
#LoginControls {
float:right;
width: 250px;
}
I can't test it yet but I hope Im right. ;)
Edit: Ok someone was faster then my but did you try span?

Related

Styling divs to align correctly

I think I don't perfectly understand inline, block, float, etc. and was hoping for some help. I'm trying to achieve this setup:
The idea is to display the most common languages our clients use. I want to have a block that has the top 5 most common, with the most common getting a spotlight section. Then just list the rest of the languages underneath.
However, my code just pushes everything to the left, and the lets the rest of the page (like the footer) next to it. I want the top language and the next 4 to be next to each other, in the center of the screen, without affecting the page below it.
body {
font-family: Arial;
transition: all ease .5s;
text-align: center;
color: rgb(58,58,58);
}
#footer ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 100%;
background-color: #666;
}
#footer li {
float: left;
}
#footer li p {
display: block;
color: #555;
text-align: center;
padding: 7px 16px;
}
br {
display: block;
}
#footer li a {
display: block;
color: #555;
text-align: center;
text-decoration: none;
}
#footer li a:hover {
display: block;
color: #555;
text-align: center;
text-decoration: underline;
}
#language-stats li {
display: inline-block;
padding: 13px;
margin: 6px;
border-radius: 5px;
color: rgb(58,58,58);
background-color: rgb(210,210,210);
font-weight: bold;
}
#top-5 {
width: 600px;
}
#top-1 {
float: left;
width: 250px;
height: 300px;
background-color: #fff;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
text-align: center;
}
#next-4 {
float: left;
width: 350px;
height: 300px;
background-color: #f5f5f5;
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Test</title>
</head>
<div id="languages">
<div id="top-5">
<h3>Top Languages</h3>
<div id="top-1">
<svg height="70" width="60">
<circle cx="30" cy="40" r="30" fill="#CECECE" />
<text x="18" y="53" style="font-size: 40;
stroke: #fff;
fill: #fff">1</text>
</svg>
</div>
<div id="next-4">
<ul>
<li>2: Example</li>
<li>3: Example</li>
<li>4: Example</li>
<li>5: Example</li>
</ul>
</div>
</div>
<div id="language-stats">
<h3>Language Stats</h3>
<ul id="language-stats-list">
<li>Languages Represented: 37</li>
<li>Coolest Language: Example</li>
</ul>
</div>
</div>
<br />
<hr />
<div id="footer">
<ul>
<li><p>Download CSV</p></li>
<li><p>Link to dictionary</p></li>
</ul>
</div>
</body>
</html>
(You have to see the snippet in full page to witness the issue.)
What am I missing to have the cleaner layout I desire?
In your example, if you'd like the language-stats div to appear under your floated top-1 and next-4 elements, you'll just need to add the style clear: left to language-stats. This will effectively line-break it below the other two elements.
Additionally, you'll want to add margin: 0px auto to the styles of the top-5 div- this will equally space the left and right margins of your 600px-wide element and center it on the page.

CSS: FIxing background size when doing Ctrl and -

I'm in the process of building a webpage, and I have a background image that spans the entire screen. When pressing ctrl and - (or ctrl and scrolling down), this scales down the image so it becomes smaller (I'm sure everyone's aware of what it does).
My question is, is there a css property that allows me to keep the image scaled (i.e. the size never changes, and it still spans the whole webpage) regardless of zooming in and out of the window?
I've googled for my answer, checked w3school, etc, but everything seems to be linking to actually physically scrolling up and down the webpage and leaving the image static, which is not what I want.
This is what my webpage looks like normally: https://gyazo.com/bf2a6cbcd8bda136c371278c2ca7c538
and when I zoom out: https://gyazo.com/0a79d2142073fa0dec0eba60e8f9c5e4
I'd like the background to continue spanning the entire page, I don't care about the navigation bar/footer size decreasing.
http://www.dragonstoneproductions.com/ This website demonstrates what I'm trying to achieve (try zooming out).
Any help/pointers to some resources is much appreciated
HTML:
<head>
<meta http-equiv="refresh" content="4">
<link href="main1.css" rel="stylesheet">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lobster" />
</head>
<header>
<nav>
<ul>
<li class="active"><strong>Home</strong></li>
<li><strong>About Me</strong></li>
<li><strong>Portfolio</strong></li>
<li><strong>Contact</strong></li>
<li><strong>Links</strong></li>
</ul>
</nav>
</header>
<body>
</body>
<footer>
<div class="col">
Copyright © 2016<br>
<span style="font-size:0.6em;">HTML5 | CSS3 | JavaScript</span></span></div>
<div class="col">
<a href="http://www.w3schools.com">
<img src="images/linkedin.png" alt="My LinkedIn" height="41" width="50" border="0">
</a>
</div>
<div class="col">
<a href="http://www.w3schools.com">
<img src="images/gmail.png" alt="Email Me" height="41" width="50" border="0">
</a>
</div>
<div class="col">
<a href="http://www.w3schools.com">
<img src="images/bitbucket.png" alt="My BitBucket" height="41" width="50" border="0">
</a>
</div>
</footer>
CSS:
body {
margin: 0;
}
header {
background: url(images/bg-image.jpg) center no-repeat fixed;
width: 100%;
height: 100%; /* viewport units are good for sizing big containers */
background-color: red;
background-size: cover;
}
.tint img {
opacity: 0.8;
background-size: cover;
}
nav {
position: fixed;
top: 0;
left: 0;
right:0;
background: rgba(139,23,28, 0.5);
padding-top: 5px;
padding-bottom: 5px;
}
nav h1 {
display: inline;
font-family: lobster;
color: #fff;
padding-left: 30px;
padding-right: 60px;
}
nav ul {
display: inline;
text-align : right;
}
nav ul li {
font-family: arial;
display: inline-block;
list-style-type: none;
padding: 5px 20px;
text-align: center;
}
nav ul li a {
color: #ffffff;
text-decoration: none;
letter-spacing: 0px;
margin: 0;
font-size: 15px;
text-align: center;
}
footer {
background: #8B171C;
color: white;
width: 100%;
display: inline-block;
}
#copyright {
text-align: left;
padding-right: 150px;
display: inline;
}
.sociallink {
height: 30px;
width: 30px;
}
.col {
float: left;
margin: 1%;
}
The answer is in the page you linked: background-size: cover;.
Working Example on how to apply it here: http://codepen.io/wilman/pen/vGKqjm
body {
background: url(background.jpg) center no-repeat fixed;
background-size: cover; /* forces bg to span the entire screen */
background-color: #111;
color: #FFF;
}
Also try to avoid using html img elements for backgrounds as they are not very convenient for that purpose. See When to use IMG vs. CSS background-image? for more info.

HTML header trouble. Why is there spacing?

Before i created a search bar or the menu-bar-search items the header looked great! Great spacing between the logo and links, they were centered on screen...etc. Now there is a GIANT gap to the left and right of the search bar and everything is leaning on the right side of the page. I've played with the menu-bar-overlay size and the size of all three elements within it, I've tried every suggestion I've found googgling for the last three days. Nothing works :/ stack overflow, you are my final hope. Please help.
My style.css
#main-container {
width: 100%;
min-height: 800px;
}
#menu-bar {
max-width: 100%;
height: 50px;
background-image: url('../images/menu.jpg');
background-repeat: repeat-x;
}
#menu-bar-overlay {
width: 1450px;
height: 50px;
margin:0 auto;
}
#menu-bar-left {
width: 250px;
height: 50px;
float: left;
padding-right: 8px;
background-image: url('../images/blog_small.png');
background-repeat: no-repeat;
}
#menu-bar-right {
float: left;
width: 670px;
height: 50px;
}
#menu-bar-right li {
color: white;
line-height: 15px;
font-size: 14pt;
font-family: "Arial Black";
font-weight: normal;
list-style: none;
display: inline;
border-right-style: groove;
border-right-color: rgba(47,173,255,0.5);
padding-right: 8px;
}
#menu-bar-right li a:link,
#menu-bar-right li a:active,
#menu-bar-right li a:visited {
color: white;
text-decoration: none;
}
#menu-bar-right li a:hover {
color: white;
background-color: rgba(211,211,211,1);
text-decoration: none;
}
#menu-bar-search {
float: left;
width: 300px;
height: 50px;
}
My header.jsp
<div id="main-container">
<div id="menu-bar">
<div id="menu-bar-overlay">
<div id="menu-bar-left"></div>
<div id="menu-bar-right">
<ul>
<li>Home</li>
<li>Admin</li>
<li>Add Article</li>
<li>Update Article</li>
<li>Delete Article</li>
</ul>
</div>
<div id ="menu-bar-search">
<form method = "post" action = "searchResults" name = "search">
<input type = "text" maxlength = "20" id = "search-field" required>
<input type = "submit" id = "search-button" value = "Search" style = "float:left; width:100px; margin-left:5px;">
</form>
</div>
</div>
</div>
<div id="top-picture">
<div id="picture-overlay-spacer"></div>
<div id="picture-overlay"></div>
</div>
<div id="header-spacer"></div></div>
Your #menu-bar-right has a width of 670px, which is much wider than its contents. Try setting that to auto, and then it'll stop pushing your search bar over to the left.
Your #menu-bar-search also has a width of 300px, which is why it might be appearing to have extra space to right of it.
Last, #menu-bar-overlay has a width of 1450px, which might be wider than you want it in many scenarios. Since it's so wide, the other content won't be able to fill it up.
Example: http://jsfiddle.net/dThv6/
Tried changing the size of bar-search?
width: 300px; to width: 530px; or a little less

Menu not in position

I am having a very weird html problem. My main menu is not in its place.
<html>
<head>
<link rel="stylesheet" href="style5.css" type="text/css">
</head>
<body>
<div id="outer">
<ul>
<li class="current">Home</li>
<li>content</li>
<li>search</li>
<li>more</li>
</ul>
<div id="clear"></div>
</div>
<div id="wrapper">
<div id="pic">
<img src="logo.png">
<div id="content">
<p> Secure Search </p>
</div>
</div>
<div id="forms">
<form>
<input type="text" name="submit" size="78" style="font-size:20pt;"/>
</form>
</div>
</div>
</body>
</html>
and here's the css
body {
margin: 0;
padding: 0;
background-color: white;
}
h1,h2,h3 {
margin: 0;
padding: 0;
}
p,ul,ol,li {
margin: 0;
padding: 0;
}
#outer {
background-color: rgb(67,68,71);
}
#outer ul {
list-style: none;
margin-left: 5px;
border-left: 1px solid;
}
#outer li {
float: left;
}
.current {
background-color: rgb(56,63,137);
}
#outer a {
width: 90px;
display: block;
text-transform: uppercase;
text-decoration: none;
text-align: center;
font-weight: bold;
color: white;
border-right: 1px solid;
padding: 5px;
}
#outer a:hover {
color: black;
background-color: white;
}
#outer .current a:hover {
color: white;
background-color: inherit;
}
#clear {
clear: both;
}
#wrapper {
width: 960px;
margin: 0 auto;
}
#pic {
margin-top: 100px;
margin-left: 389px;
position: relative;
}
#content {
position: absolute;
top: 60px;
left: 90px;
}
#forms {
margin-top: 50px;
}
Now you may ask that how come i didn't noticed my menu not in placing during early stages of coding. Well the thing is that i was using borders on wrapper div during coding and everything was in place however as soon as i removed the border the whole thing fell apart.
I think it has something to do with the float not being cleared correctly resulting in pic div messing everything up. I would be really appreciative for your suggestions.
Thank you.
I don't know what you mean by "not in its place", but removing a border suggest you have a problem with collapsing margins.
If that's the case, you could solve it by adding overflow: auto or padding: 1px 0 to the rule where you removed the border.
Replace your
<div id="clear"></div>
with
<br id="clear">
or even better change it from and id to a class. That way you can use it multiple times.
For some reason it doesn't work with the div. But the "br" also shorter so I'd prefer that anyway.

I'm trying to display images horizontally using CSS

I have a series of images I would like to display horizontally on my website, using CSS and divs. I've looked at several posts about how to display images horizontally already and I wasn't able to get any of them to work.
Here's the code from the page I'm referring to:
<head>
<link rel=StyleSheet href="style.css" type="text/css" media=screen>
<style>
div.imageBox {
float: left;
margin-right: 10px;
}
div.imageBox p {
text-align: center;
}
</style>
</head>
<body>
<?php include 'header.php'; ?>
<div id="content">
<div id="container">
<div class="imageBox">
<img src="image" width="800" height ="600">
</div>
<div class="imageBox">
<img src="image" width="600" height ="450">
</div>
<div class="imageBox">
<img src="image" width="800" height ="450">
</div>
</div>
</div>
</body>
and here's the code for the style sheet:
body{
font-family: helvetica, arial, sans-serfif;
font-size: 14px;}
div#menu {
display: inline;
text-align: center;
width: auto;
top: 20px;
margin-left: 10%;
margin-right: 10%;
background: transparent;
height: 10px;
position: fixed;
}
div#menu ul {
margin: 0;
display: block;
}
div#menu ul li {
padding-right: 55px;
display: inline;
}
div#content {
padding-top: 40px;
text-align: center;
}
div#container{
float: left;
display: inline;
height: 800px;
width: 0px;
}
a:link {
text-decoration: none;
color: #000;
}
a:active {
text-decoration: none;
color: #000;
}
a:visited {
text-decoration: none;
color: #000;
}
a:hover {
text-decoration: underline;
color: #000;
}
Is there perhaps an easier way for me to do this?
Thanks for your help!
Try to change
div#container{
float: left;
display: inline;
height: 800px;
width: 0px;
}
into
div#container{
float: left;
display: inline;
height: 800px;
white-space: nowrap;
}
add:
div.imageBox {
float: left;
margin-right: 10px;
display:inline;
}
You're using divs which are block elements when you should be using something inline.
Images will align next to each other. If you need the divs, you can give them the style display: inline to make them behave in the same way. You can also use float: left, but that will make things harder in most cases.
You can apply styles like margins and borders to images too, so you may not need the divs. Although I'm not sure how you want it to look exactly.
Here is the example: http://jsfiddle.net/hobobne/d5sYM/
<html>
<head>
<style>
img {width: 100px; height: 100px;}
.imageBox {display: inline-block; border: 1px solid grey; background: silver; padding: 4px; text-align: center; font-size: 10px;}
</style>
</head>
<body>
<div class="imageBox">
<img src="http://screenshots.en.softonic.com/en/scrn/80000/80669/easter-icons-5.jpg"><br>
Image 1
</div>
<div class="imageBox">
<img src="http://l-userpic.livejournal.com/82523298/2181918"><br>
Image 2
</div>
<div class="imageBox">
<img src="http://www.jellymuffin.com/icons/emo/images/30.jpg"><br>
Image 3
</div>
</body>
</html>