Difficulties with div and id arrangement when making my own header bar - html

I'm trying to make a header bar that looks similar to Bootstrap. If you view this document now the problem is that 'Item 1' is displayed correctly but 'Item 2' is pushed below it, instead of being to the right of it. I thought that by setting "left:80px' to 'Item 2' it would go 80px right of item 1.
Please let me know how to fix this. I was also wondering if I'm doing this in a smart way or if stacking the elements (.items > #item_1) is better. Thanks!
CSS
/* header, logo, and items */
#header {
width: 100%;
height: 45px;
position: absolute;
top: 0px;
left: 0px;
background: #3b5998;
text-align: left;
box-shadow: 0px 1px 0px #888888;
}
.items {
top: 0px;
right: 0px;
width: 200px;
height: 23px;
padding-top: 11px;
padding-bottom: 11px;
position: absolute;
background: #3b5928;
text-align: center;
font-family: 'Calibri';
font-size: 18px;
color: #f7f7f7;
}
#item_1 {
width:80px;
background: fff;
}
#item_2 {
width: 80px;
left: 80px;
background: #3b7328;
}
HTML
<!--Header and Footer-->
<div id="header">
<div class="items">
<div id="item_1"> Item 1 </div>
<div id="item_2"> Item 2 </div>
</div>
</div>

One approach would be to change the display of the elements to inline-block. (example)
.items > div {
display:inline-block;
}
Alternatively, you could float the elements or use a flexbox layout.
It's worth noting that you can't position a static element (i.e., position:static - the default). If you wanted left: 80px to work, you could add position:relative or position:absolute - fixed would work too. (example) As the example demonstrates, this isn't really an effective way to line the elements up though. It would be better to either float them or make them inline.
#item_2 {
width: 80px;
left: 80px;
background: #3b7328;
position: relative;
}

Here is a JS Fiddle, next time you should set one up. It's very useful for other people to see your issue.
<!--Header and Footer-->
<div id="header">
<div class="items">
<div id="item_1"> Item 1 </div>
<div id="item_2"> Item 2 </div>
</div>
</div>
#item_1 {
width:80px;
color: black;
background: #fff;
position: relative;
float: left;
display: inline-block;
}
#item_2 {
float: left;
width: 80px;
background: #3b7328;
position: relative;
display: inline-block;
}
http://jsfiddle.net/ws5ew/

Related

Trying to add floating image and and description in a filled box

I am trying to get this to happen.
what I want
So far, I don't know how to overlap one img-div with another text-div and keep white space on the top of the text-div. You will see. What I have right now is:
<div id="some">
<img src="photos/some.png">
<div id="box">
<p>Proudly seeking</p>
<h2>some Cofefe</h2>
<button id="shopNow" class="button">Shop</button>
</div>
</div>
With some CSS that doesn't make it very appealing: what it looks like
#some{
margin-top: 20px;
background-color: red;
}
#some img{
width: 30%;
float: left;
}
#box{
padding-top: 220px;
margin-right: 40px;
font-family: "Eusthalia";
text-align: right;
}
#box p{
margin-right: 32%
}
h2 {
font-size: 2.6em;
}
button {
border: none;
font-family: "Eusthalia";
font-size: 15px;
background-color: #300c06;
color: #eadfc0;
padding: 2px 10px;
}
I am wondering if my whole approach with divs is wrong. I was researching and I found that right:0; doesn't work and stuff like that. How do I get a border to overlap behind the image? How do I give it a width and a height but make it push to the right?
Do I have to make the main div width 100% and then give the img a width 30% and the colored filled in text box 70%? But how would I have the box behind the img?
Drearo, I think you're doing fine with div tags. You just may need a bit more of them to help things along.
I would suggest the divs be position: absolute with the image in one of those. The box of text needs it too. Aside from that, a little CSS would get you the positioning you want. See here:
<div id="some">
<div class="my_img">
<img src="photos/some.jpg" />
</div>
<div id="box">
<p>Proudly seeking</p>
<h2>some Cofefe</h2>
<button id="shopNow" class="button">Shop</button>
</div>
</div>
css:
#some{
margin-top: 20px;
height: 100vh;
width: 100vw;
border: 1px solid #000;
position: relative;
}
.my_img {
position: absolute;
top: 5em;
left: 5em;
z-index: 200;
}
.my_img img {
width: 200px;
}
#box{
position: absolute;
top: 10em;
left: 10em;
transition: translate( -50%, -50%);
font-family: "Eusthalia";
text-align: right;
background: red;
min-width: 60%;
padding-right: 2em;
}
#box p{
margin-right: 32%
}
h2 {
font-size: 2.6em;
}
button {
border: none;
font-family: "Eusthalia";
font-size: 15px;
background-color: #300c06;
color: #eadfc0;
padding: 2px 10px;
}
https://jsfiddle.net/5k94j73p/

Applying pseudo class "before" and "after" to multi-line text

I am trying to add a pseudo before and after vertical line to a textfield for styling purposes. These elements need to be flush to the text -20px left and -20px right.
This works fine when the text is on one line as an inline-block, but as soon as the text spans multiple lines the width expands to that of the parent and the pseudo elements are no longer just 20px from the text.
Is there a way in which I can accomplish this using CSS?
.container {
width: 500px;
background-color: red;
text-align: center;
margin-left: 40px;
}
h2 {
position: relative;
display: inline-block;
margin: 0;
padding: 0;
background-color: blue;
color: white;
}
span {
position: relative;
background-color: green;
}
h2::before,
h2::after {
content: '';
position: absolute;
top: 0;
width: 4px;
height: 20px;
background: black;
}
h2::before {
left: -20px;
}
h2::after {
right: -20px;
}
<!-- Single line example works as the black bars are 20px away from the start/end of text-->
<div class="container">
<h2><span>This is a title</span></h2>
</div>
<br> <br>
<!-- double line doesn't work because the h2 is now the full width of the container -->
<div class="container">
<h2><span>This is loooonnggggggggggggggggggggggeeeeerrr</span></h2>
</div>
Edit: Here is a working version using tables, but if anyone has a better solution I'd love to hear it: https://codepen.io/anon/pen/MqveLQ
So from what i can see is the issue here is where you are applying the borders with before and after. You need to alter where you apply your borders. Remove them from the h2, and add in a new html element that wraps the h2 and apply there.
eg:
<div class="container">
<div class="headerwrap">
<h2><span>This is loooonnggggggggggggggggggggggeeeeerrr</span></h2>
</div>
</div>
<div class="container">
<div class="headerwrap">
<h2><span>This is a title</span></h2>
</div>
</div>
CSS
.headerwrap::before,
.headerwrap::after {
content: '';
position: absolute;
top: 0px;
bottom:0;
margin:auto;
width: 4px;
height: 20px;
background: black;
}
.headerwrap::before {
left: 10px;
}
.headerwrap::after {
right: 10px;
}
Here is a working example: https://codepen.io/FEARtheMoose/pen/VGbJjO?editors=1100#0
Edit: altered example after comments - https://codepen.io/FEARtheMoose/pen/VGbJjO
I have moved your code to this fiddle: https://jsfiddle.net/n2Lr6xy5/13/ and removed position: absolute along with stripping out some of the other styles as they seemed unnecessary and I think I have created what you're after.
Here is the updated CSS:
.container {
width: 500px;
background-color: red;
text-align: center;
margin-left: 40px;
}
h2{
display: inline-block;
}
h2:after,
h2:before {
content: "";
width: 4px;
height: 20px;
background: #000000;
display: inline-block;
margin: 0 10px;
}

Expand submenu item to fit header (first right then left)

I have the following structure:
<div class="wrap">
<div class="menu">
<div class="item">
Menu
<div class="submenu">
<div class="submenuitem">Submenu</div>
</div>
</div>
</div>
</div>
and so far the following CSS:
div.wrap {
background: #eee;
height: 80px;
}
div.menu {
margin-left: 50px;
background: #36e;
}
div.item {
background: #d00;
color: #fff;
line-height: 40px;
padding: 0px 20px;
font-size: 16px;
display: inline-block;
margin-left: 50px;
}
div.item:hover {
background: #b00;
}
div.submenu {
display: none;
background: #0b0;
height: 40px;
position: absolute;
right: 0%;
top: 50%;
min-width: 300px;
}
div.item:hover div.submenu {
display: inline-block;
}
div.submenuitem {
line-height: 40px;
padding: 0px 20px;
background: #b00;
color: #fff;
display: inline-block;
}
JSFiddle
The behaviour I'm after is that the width of submenuitem expands to fit its textual content, but that it can use at most the width of wrap for expanding. It should also be positioned directly under item unless the width of submenuitem will be larger than the distance from its original position to the right end of wrap. Thereafter it should expand to the left until it meets the left edge of wrap.
As you can see this succeeds perfectly when I can know the distance from submenuitem's original position to the right end of wrap by setting right: 0%; min-width: 300px; on submenuitem, but I want to do this in a way that doesn't require knowing that distance.
I have been trying to craft or find a solution to this for the past few days and have not managed to get any closer. Is it even possible with pure CSS to begin with?
Is this something you want? check this one nd let me know.
http://jsfiddle.net/zmcEC/9/
div.wrap {
width: 400px;
background: #eee;
position: relative;
height: 80px;
}
div.submenu {
display: none;
background: #0b0;
height: 40px;
position: absolute;
right: 0;
top: 40px;
left:0;
}

Scroll bar to nothing - how can I make my page shorter?

We have to support the last two revisions of IE, Chrome and Firefox and I have a feeling this isn't possible with IE 7/8, but perhaps I'm missing something
I have a footer that is moved up behind a content area by -280px. This content area is moved up over a header area by -230px. As a result I have a blank area at the bottom of my page of approx 320px. I can fill this and make it appear to be the bottom end of the gradient, but I'd really rather just cut it out, so there's no scroll bar to nothing.
In the example code below -
<div id = "page">
<div id = "topbar">
</div>
<div id = "header">
</div>
<div id = "content">
</div>
</div>
<div id = "footer">
I AM THA FOOTAH<br/> So much cooler than the header these days
</div>
body
{
/* background-color: #040e22; */
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
margin: 0px;
padding: 0px;
}
div
{
display: block;
}
#page
{
background-color: white;
margin: 0px auto;
padding: 0px;
position: relative;
}
#topbar
{
height: 60px;
background-color: #112247;
color: white;
position: static;
}
#header
{
background-color: navy;
color: yellow;
height: 240px;
position: relative;
}
#content
{
min-height: 280px;
background-color: green;
width: 480px;
margin: auto;
position: relative;
top: -230px;
z-index: 1;
height: 2000px;
}
#footer
{
/*background: url("footerGradient.png") repeat-x 50% 0%;*/
background-color: navy;
color: white;
text-align: center;
padding-top: 60px;
height: 220px;
top: -280px;
position: relative;
}
.inner
{
width: 940px;
margin: auto;
}
how do I get rid of the white under the navy footer?
just change in your #footer from top: -280px to margin-top: -280px voila!
position relative will move the element relatively to its original location but will perserve its space thus rendering empty area, negative margin will move your element including its bounding space and will do what you need.
You can change the footer position from relative to static like so:
#footer
{
/*background: url("footerGradient.png") repeat-x 50% 0%;*/
background-color: navy;
color: white;
text-align: center;
padding-top: 60px;
height: 220px;
bottom: 0px;
width: 100%;
position: fixed;
}
You might want to take a look at this sticky footer page-- you can modify that technique by NOT making the height of the footer and the negative margin of the previous element the same; you would want the negative margin to be greater.

z-index not working as expected (Chrome and Opera)

I have a div with class "opaque" and another with class "product-info", which are both on the same level.
The code is as follows:
<div class="opaque"></div>
<div class="product-info">
<img class="product-image" src="/Images/D3.jpg" />
fsdfdsfsdfs
</div>
.opaque
{
background-color: White;
-moz-opacity:.60; filter:alpha(opacity=60); opacity:.60;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
z-index: 1;
}
.product-info
{
padding: 5px;
text-align: center;
z-index: 2;
}
Note that product-info is set to z-index 2 and opaque has z-index 1. Therefore product-info should be displayed over opaque so should not be faded. However the image within product-info (and text) is faded. This happens in both Chrome and Opera, therefore I expect this is what should be happening since they are not IE!
There are lots of bits of HTML code as shown above, each nested in lis which are set to float left with width of 33%. When the page is fully loaded ($(window).load()) I use jQuery to detect the maximum height of all the products and apply that height to all the rest. I have tried removing all the jQuery in case this is affecting the z-index, but I get the same result only with an untidy look and feel.
I have tried using Google Chromes Inspect Element tool and the elements in question are showing the correct characteristics.
Can anyone see what I am doing wrong here? I have been trying to solve this for a couple of days now and would like to find out what is going on.
Thank you.
Regards,
Richard
Full code as requested:
I think this is all that is required. I will create a page with just this code in a few minutes, to see if it reproduces the problem.
<div id="BodyTag_ContentPanel">
<div class="overlay-background"></div>
<div class="scroll-pane">
<div>
<ul class="product-list">
<li class="product">
<div class="spacer">
<div class="opaque"></div>
<div class="product-info">
<img class="product-image" src="/Images/D3.jpg" />
<div class="enlarge">
<div class="image-enlargement">
<span class="close"><img src="/Images/close.jpg" /></span>
<div class="enlargement">
<div class="image-container"><img src="/Images/D3.jpg" /></div>
<div class="product-code"><span class="text-container">D3</span></div>
</div>
</div>
</div>
</div>
<div class="product-code">D3</div>
<div class="clear"></div>
</div>
</li>
</ul>
</div>
</div>
</body>
.product-list
{
list-style: none;
padding: 0;
width: 100%;
}
.product
{
width: 33%;
height: 25%;
float: left;
position: relative;
}
.product .spacer
{
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
margin: 10px;
padding: 5px;
border: 4px solid #C47F50;
position: relative;
}
.product .opaque
{
background-color: White;
-moz-opacity:.60; filter:alpha(opacity=60); opacity:.60;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
z-index: 1;
}
.product .product-info
{
padding: 5px;
text-align: center;
z-index: 2;
}
.product .product-info .product-image
{
max-width: 200px;
max-height: 200px;
min-width: 150px;
min-height: 150px;
z-index: 2;
}
.product .product-code
{
position: absolute;
bottom: -15px;
width: 50%;
margin-left: auto;
margin-right: auto;
background-color: White;
text-align: center;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
border: 4px solid #C47F50;
line-height: 20px;
z-index: 2;
}
.product .image-enlargement
{
position: fixed;
display: none;
padding: 5px;
background-color: White;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
border: 4px solid #C47F50;
z-index: 103;
}
.product .enlarge
{
float: right;
}
I have found the solution!! I simply added position: relative; to .product-info. I can't believe I was so stupid as to not try that in the first place! Thanks for both your efforts #Thomas & Lazycommit. #Lazycommit your link came in handy - it confirmed that my code should have been working if it weren't for missing out the position: relative;. I noticed that they had set the position attribute for all of the divs in the example - this is what made me try it.
This is a big theme. U may look this page from Firefox developers for better understanding browsers stacking.
Try setting the z-index of the item that is supposed to be in the back to a negative number, like -1 :)
I know this is old, but you can use rgba() instead of opacity and get rid of the "product-info" div. This does the same effect you want since rgba() uses a different method of causing transparency that does not make the children elements go transparent as well. Makes doing the effect you wanted much easier.
if there is outer div to set position: fixed; and you need position: fixed; there add z-index same as inner div.
ex:
.notifications {
position: fixed;
width: auto;
max-width: 70%;
z-index: 9999;
}
.notifications > div {
position: relative;
z-index: 9999;
/* margin: 5px 0px;*/ /*default value*/
margin: 65px 10px;
display: inline-block;
}
will work fine.
But if notifications class like below it does not set z-index in chrome correctly.
.notifications {
width: auto;
max-width: 70%;
z-index: 9999;
}