I'm kind of knew (some days) to this so this might be a dumb question.
I'm trying to make a navbar with three links/buttons. Two of them, when hovered, simply change the background to a darker one, the third one, opens a small menu with more links/buttons.
This is what I did (probably not the best method, but the first that came into my mind):
http://jsbin.com/woxodovoxo/1/edit?html,css
My problem is that on the button in the middle, the positions are not right (you can see that the button flickers a little when you hover your mouse over it, although this doesn't happen on the other buttons).
This is happening because I wasn't able to put #text-dropdown and #dropdown-div on the same starting position, since I couldn't find the right value for the "margin-top; padding-top" on #dropdown-div
From what I see on the code, .headertext is 0.5em (+ font-size) underneath the top of the page.
The problem comes with #dropdown-div, since I don't know its original position (without any tweaking) relative to the top of the page. If I were to know that, I could simply make a calculation to know which values to put in "margin-top; padding-top" on #dropdown-div.
Could you help me with that?
Also, percentages and em's don't seem to go well together, specially on the navbar when zooming in/out. What alternative could I use? Everything with em's? Everything with percentages? Or something else?
Thanks
Would this solve your problem? I didn't copy over your exact code because it was exhausting to read, but hopefully it gets you in the write direction. If you set an objects position to be absolute with its parent set to relative you can move it around relative to its parent.
Also, the box-sizing attribute may help you in the future. Makes it so that the padding/borders are included in the size calculations.
div#navbar {
width: 100%;
height: 2em;
}
div#navbar *,
div#navbar {
padding: 0;
margin: 0;
background-color: #3ff;
}
div#navbar div {
display: inline-block;
position: relative;
box-sizing: border-box;
height: 100%;
width: 150px;
}
div#navbar div>div {
display: none;
position: absolute;
top: 100%;
left: 0;
height: auto;
}
div#navbar div:hover>div {
display: block;
}
<div id="navbar">
<div>Something 1</div>
<div>Submenu
<div>
Other text
<br />Yup
</div>
</div>
<div>
hello
</div>
</div>
.headerlink{
height: 3em;
line-height: 3em;
width: 25%;
z-index: 1;
position: fixed;
}
.headertext{
text-align: center;
font-size: 1.3em;
font-family: 'Verdana';
color: #E3E3E3;
font-weight: bold;
}
#dropdown-div{
position: absolute;
top:100%;
width:100%;
background-color: #57a58a;
opacity: 0;
}
Modify the above styles and delete the below one
#hd2:hover #text-dropdown{
opacity: 0;
}
Edit:
Add this too..
#hd2:hover{
background-color: #57a58a;
}
and remove this extra option:
<div class="header-dropdown">Page2</div>
Related
I don't know how to make the div display in front of the h1 text, so that the blue box is in front of the text? I have been stuck on this for the past 30 mins and cant resolve it in my head. I am a beginner so please have patience.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.wrapper2 {
position: absolute;
z-index: 2;
width: 500px;
height: 250px;
background: blue;
}
h1 {
position: relative;
z-index: 0;
}
<div class="wrapper2">
<h1>Welcome</h1>
</div>
As mentioned above, you can simply put the <h1> element above your wrapper in HTML. If you want your <h1> to stay inside, you could use this:
display: none; or visibility: hidden; opacity: 0;
CSS:
h1 {
position: relative;
/* either of these */
display: none;
visibility: hidden;
opacity: 0;
}
You could try this:
.wrapper2 {
width: 500px;
height: 250px;
background: blue;
}
<div class="wrapper2"></div>
<h1>Welcome</h1>
Although then it's not really a "wrapper" anymore, so maybe you'll need to create an element just for the blue square.
You've placed your h1 inside the wrapper2, so you can try to move it after it if you want to style them separately. I would also suggest to not use absolute position unless you really want this, the consequences of doing this will quickly become apparent as you build out a larger page.
You can make each element "inline" rather than "block", this will make them follow the text flow of the page: display: inline;
I'm writing here because I've got a problem with CSS. I have a .container div that contains another div set to position:absolute, top:0, left:0 and width:100%; height:100%. However I keep seeing these kind of white spaces, that when I zoom in the page disappear. Any solution?
.loop {
display: block;
position: absolute;
height: 36px;
background: white;
border: 2px solid black;
box-sizing: border-box;
width: 250px;
top: 7px;
left: 50%;
transform: translateX(-50%);
border-radius: 5px;
overflow: hidden;
transition: background 0.2s;
}
.goPrev,
.goNext {
position: absolute;
width: 36px;
height: 100%;
box-sizing: border-box;
top: 0;
display: flex;
justify-content: center;
}
.goMid {
position: absolute;
top: 0;
left: 36px;
width: calc(100% - 72px);
height: 100%;
font-family: "Poppins";
padding-top: 9px;
text-align: center;
box-sizing: border-box;
}
.goMid:hover,
.goPrev:hover,
.goNext:hover {
background: rgba(0, 0, 0, 0.3);
}
<body>
<div class="loop">
<div class="goPrev">
</div>
<div class="goMid">
Help me.
</div>
<div class="goNext">
</div>
</div>
</body>
That is just a draw.
Here you have the screenshot
Well, I'm not totally sure what to do, but the following changes seem to fix the problem for me. I changed:
Set .loop overflow from hidden to visible
Set .goMid top from 0 to -1px
The .goMid height from 100% to calc(100% + 2px)
When I moved the inner div underneath the border using top: -5px I still saw the whitespace until I changed the outer div overflow property to visible. Then if you stretch the inner div a little it seems to solve the problem. It helps that your outer div has a thick border.
.loop {
display: block;
position: absolute;
height: 36px;
background: white;
border: 2px solid black;
box-sizing: border-box;
width: 250px;
top: 7px;
left: 50%;
transform: translateX(-50%);
border-radius: 5px;
/* HERE */
overflow: visible;
transition: background 0.2s;
}
.goPrev,
.goNext {
position: absolute;
width: 36px;
height: 100%;
box-sizing: border-box;
top: 0;
display: flex;
justify-content: center;
}
.goMid {
position: absolute;
/* HERE */
top: -1px;
left: 36px;
width: calc(100% - 72px);
/* HERE */
height: calc(100% + 2px);
font-family: "Poppins";
padding-top: 9px;
text-align: center;
box-sizing: border-box;
}
.goMid:hover,
.goPrev:hover,
.goNext:hover {
background: rgba(0, 0, 0, 0.3);
}
<body>
<div class="loop">
<div class="goPrev">
</div>
<div class="goMid">
Help me.
</div>
<div class="goNext">
</div>
</div>
</body>
For what it's worth, I think #MarkP may have a good point. Combining absolute positioning and flexbox does feel like maybe a code smell. But, I don't know what the context is in your code and my flexbox-fu is a little shaky.
ACTUAL GENERATED VIEW:
I added text to all 3 nested divs and got the following centered display.
You can see all your text is bunched together. I am going to review your code and the offer a way forward. You may find you need to re-word your problem to allow us to help you better. My assumption is you are trying to set-up a tool to navigate through some type of media, such as images or pages.
CODE REVIEW
In review of your Code i can see you are trying to use a 3 part display using flexbox. Except you have also included absolute positions which prevents relative display of the divs alongside each other. Now i know you are concerned about white space but i am going to suggest a way to better use flex-box as well as debugging the whitespace, although it would be better to start again with a appropriate flexbox structure.
WHITE SPACE DEGUGGING
My suggestion first would be to remove CSS that could be causing this and then re-introduce the CSS progressively. If you are using Google Chrome you can use the insight tool to adjust the live CSS. Simply right-click on the area you wish to inspect and the CSS being used there will be displayed. You can edit directly in the CSS display and it will change the page behaviour, this is great for debugging and seeing what CSS improves your layout. Once you find the CSS you need you can replicate that in your code.
I would start with removing the following and see how you go:
Remove overflow:hidden;
When you look closer you can see the style code allows for 36px for each div on the left and the right. There may be an image missing from the .goPrev and .goNext divs, where your white space is now. Not sure if you copied your code from somewhere or wrote this from scratch?
TRY STARTING WITH A NEW FLEX-BOX STRUCTURE
I recommend creating your divs from scratch using one of the approaches found here: Common CSS Flexbox Layout Patterns with Example Code . Flexbox is super simple and a great way to build mobile responsive layouts.
I've encountered a strange issue with text positioning in Safari for buttons on a site I've been working on.
1. Is it possible to keep the text center aligned on the buttons while using left: ...; ? Would this fix the issue?
2. Would placing span in a relatively placed div .text-pos with a sub-class .text-pos span ... position: absolute; be bad form? Would it fix the issue?
Code:
.button a span {
z-index: 2;
width: 100%;
position: absolute;
top: 12%;
color: #000000;
text-align: center;
font-size: 4vmin;
}
.button a img {
height: 100%;
display: flex;
}
<div class="button antiques">
<a href="/landing/gallery/antiques/antiques.html">
<img alt="antiques" src="/assets/img_style/plank.png">
<span>ANTIQUES</span>
</a>
</div>
Did not include left: ...; as the text needs to be center aligned on the button.
Result(too new to post images):
http://i.imgur.com/3E55EMH.png
My first thought was that the issue was with vmin, but:
1- Text scales appropriately with browser adjustments.
2- Text on the hover(upper left image frame) also uses vmin, but is
appropriately positioned.
In reference to point two, the text is placed in a relatively positioned div container to force aspect ratio like so:
<div id="wide-container"> /* position: relative; */
<div id="content"> /* position: absolute; */
...
</div>
</div>
I don't have ready access to an OSX machine so any input would be appreciated!
Open minded to any other approaches you may have to offer. Thank you (:
SOLVED
.button {
height: 6vmin;
margin-top:1.5vmin;
margin-bottom:1.5vmin;
position: relative;
}
.button a {
height: 100%;
}
.button:before a {
content: "";
display: block;
}
.button a span {
z-index: 2;
position: absolute;
top: 9%;
bottom: 0;
left: 0;
right: 0;
text-align:center;
width: 100%;
height: 100%;
color: #000000;
font-size: 4vmin;
}
Found the solution by setting the button to be relatively positioned while leaving the text position as absolute. Solution outlined in more detail in the edited question.
The problem came from my misunderstanding of how browsers treat the box model differently. Safari seemed to be taking the contained elements and floating them left individually since the the image had no positioning attributes.
This solution displays more or less the same on Chrome, Firefox, and Safari.
This is my first time using the service, so I'll try to be specific.
I'm trying to create a land page for my domain, but when I place the logo for the top menu, and add another element, the element does not respect the space of the logo, and it stays in front of the logo.
Here's the CSS I'm using:
#header {
width: 100%;
padding: 5px 0;
background: #b20000;
}
#header .hwrap {
display: block;
width: 980px;
height: 40px;
margin: 0 auto;
}
#header .menuLogo {
display: block;
width: 205px;
height: 70px;
background: url(menu_logo-70px.png);
text-indent: -9999px;
margin-bottom: -30px;
}
And here is an excerpt of the HTML I'm using:
<body>
<div id="header">
<div class="hwrap"><h1 class="menuLogo">fhtgames.com</h1>Random text</div>
</div>
</body>
Fairly simple.
EDIT: What I want is the logo to overflow the menu bar, and add the menu options to the right of the logo, still inside the .hwrap element. I used the logo with an <h1> element and placed the image as a background to avoid the image to be right-clicked and be saved.
But when I try to add the menu and the link to the logo, I notice that Google Chrome renders the page with the logo link for the full width of the .hwrap element, and adding anything else, makes the logo to stay behind the new elements.
Here's a link of the screen: http://img.fhtcentral.com/stack/screen001.png
I am using an HTML5 Reset stylesheet (found here) and I'm pulling the latest jQuery library from Google servers.
I've done this lots of times before, without any problems whatsoever, so I really don't know what am I doing wrong. I am sorry if this looks completely noobie question, but I just can't see the mistake.
Thank you for you time.
EDIT: The problem has been solved. The answer is right below. Thank you all for your elp :D
The text appears above the logo, because you have set the logo image as a background. So html intends that you want, as the word says, the image as background!
To avoid this I guess you have set the display: block to your h1.menuLogo. The right way would be display: inline-block.
#header .menuLogo {
display: inline-block;
width: 205px;
height: 70px;
background: url(menu_logo-70px.png);
text-indent: -9999px;
margin-bottom: -30px;
}
You can find a working fiddle right here.
The rest is about adjusting with margin and padding.
For further information about your problem you can read about the difference of block/inline-block here.
If you need other suggestions please let me know!
Best regards, Marian.
Hope this help you.
#header {
width: 100%;
padding: 5px 0;
background: #b20000;
overflow:hidden;
position:relative;
}
#header .hwrap {
display: block;
width: 980px;
margin: 0 auto;
color:#fff;
}
#header .menuLogo {
display: block;
width: 205px;
height: 70px;
background: url('http://jsfiddle.net/img/logo.png') no-repeat rgb(249, 153, 5);
text-indent: -9999px;
overflow: hidden;
line-height: 1;
margin: 0;
padding: 0;
}
JSFiddle Link.
I have a problem understanding z-index properly.
Please have a look at this fiddle I created for you: http://jsfiddle.net/df3EL/
<div id="content">
1. Content
<div id="popup">
3. PopUp
</div>
</div>
<div id="footer">
2. Footer
</div>
I'm aware of positioning and opacity influencing z-index. But with this markup, no matter what I try, the footer is above 1 & 3 or below - never in between.
Is there any way to make the order (1, 2, 3) work, without changing the html markup?
z-index inherits from the parent element
So if your 1 element has a z-index of 100, your 3 element cannot exceed that value in the global scope. In the local scope (within the #content element), the z-index will essentially "reset"
So to make your thing work, you'll need to change the HTML markup to make each element independent (so they can have sequential z-index in the global scope)
if you want popup be hover footer, just set index for footer and popup : http://jsfiddle.net/df3EL/1/
div {
font-family: Verdana;
font-size: 11px;
padding: 20px;
}
div#content {
display: block;
height: 150px;
width: 250px;
background: #eee;
position: relative;
}
div#footer {
display: block;
height: 50px;
width: 250px;
background: #eeefc0;
position: relative;
left: 25px;
top: -25px;
z-index: 1;
}
div#popup {
display: block;
height: 140px;
width: 100px;
background: #C0C0EF;
position: relative;
left: 220px;
top: -5px;
z-index: 2;
}
[http://jsfiddle.net/df3EL/3/][1]
Remove all z-index property except div#popup
It should work in modern browser (suppose ie9+, chrome, opera, FF)
But more logical way move #popup after #footer (may be it should do with javascript when needed to show popup)
You have used position:relative for div#footer and div#popup. Change this to position: absolute and change the vaules top, bottom, left,right to get the desired result.By this you won't have to change your html structure.