I'm trying to learn HTML/CSS and JavaScript+jQuery by using Codeacademy and working on my own little project to practice. However, I am stuck with a very simple problem:
I want a parent div to be displayed across the entire page. I can do this successfully (see fiddler). When I resize the browser screen, however; my parent div no longer fits across the entire page, which causes its right most child div to be displayed outside of the parent div (see fiddler). Basically, I want my parent div to always wrap its child divs, and to always be displayed across the entire screen.
Fiddler Links:
Link 1
Link 2
Relevant HTML:
<div id="topnav">
<a id="logo" class="navlink clearfix">DreamTeam</a>
<a id="logo" class="navlink clearfix">Strikers</a>
<a id="logo" class="navlink clearfix">Midfielders</a>
<a id="logo" class="navlink clearfix"><div class="navlink clearfix">Defenders</a>
<a id="logo" class="navlink clearfix">Goalkeepers</a>
</div>
Relevant CSS:
/* ID FOR PARENT DIV */
#topnav {
position: relative;
background-color: #EDEDED;
height: 70px;
width: 100%;
white-space: nowrap;
}
#logo {
width: 300px;
font-weight: bold;
margin-left: 5px;
font-size: 28px;
height: auto;
}
#lastlink {
border: none;
}
/* CLASS FOR CHILD DIVS */
.navlink {
position: relative;
font-family: Century Gothic;
height: auto;
font-size: 24px;
text-align: left;
line-height: 65px;
width: 175px;
border-right: 1px solid #bdbdbd;
}
Any help would be appreciated. I've gone through many Google searches and other stackoverflow posts, but nothing seems to work for me unless I completely missed an appropriate post. Using things like "overflow: hidden" or creating a wrapper div didn't really work for me. Thank you in advance for any suggestions. It would be great if anyone could point me to an appropriate post that I might have missed too.
How about get rid of the div tag inside a tag. Because you can use class with a tag.
Look at the following example provided by StackOverflow user starx
CSS
a.divlink {
display:block;
width:500px;
height:500px;
float:left;
}
HTML
<div>
<a class="divlink" href="yourlink.html">
The text or elements inside the elements
</a>
<a class="divlink" href="yourlink2.html">
Another text or element
</a>
</div>
Click here for more details
I managed to find a solution to my problem. The problem was not my invalid markup; it was actually the CSS for my #topnav ID. Here's how I changed that ID to get the result I wanted:
#topnav {
background-color: #EDEDED;
height: 50px;
min-width: 1050px;
position: absolute;
left: 0;
right: 0;
top: 0;
}
The key changes are below the height property.
Related
I am trying to make a header with a fixed title, horizontally and vertically centred. With a home icon that acts as a link.
The problem is that link is clickable on everything left of the Home icon.
I have tried to replicate the problem in this codepen;
body {
margin: 0;
background-color: #2d2d2d;
}
#header {
margin: 0;
background-color: rgb(171, 228, 250);
height: 10vh;
display: flex;
}
#homeIcon {
position: absolute;
padding-top: 2px;
padding-left: 30vw;
height: 10vh;
}
#headerTitle {
margin: auto;
font-size: 5vh;
color: #2d2d2d;
}
<div id="header">
<a href="">
<img id="homeIcon" src="https://image.flaticon.com/icons/svg/846/846551.svg" alt="homeicon" />
</a>
<h1 id="headerTitle"> title </h1>
</div>
Is there a way to have the link only on the content of the img tag?
In your CSS you are specifying the homeIcon has a padding-left of 30vw. If you change this to margin-left instead, it will no longer be clickable. This is because padding is included inside your element, while margin is outside. See https://www.w3schools.com/css/css_boxmodel.asp.
The css is your problem: you can solve it by changing padding-left: 30vw; to margin-left: 30vw;
This answer may be useful background reading: Difference between margin and padding?
the top attribute appears not to be working on a html. I am trying to use the top attribute on image to move an image to the top and place above a text but the top attribute of a css never moves the image Here is snippet
<div class="stl_02">
<div class="stl_03">
<img src=""
alt=""style="top: 4.4538em;" class="stl_04">
</div>
<div class="stl_view">
<div class="stl_05 stl_06">
//other texts here
here are the css rules
.stl_02 {
height: 46em;
font-size: 1em;
margin: 0em;
line-height: 0.0em;
display: block;
border-style: none;
width: 51em;
}
.stl_03 {
position: relative;
}
.stl_04 {
width: 100%;
clip: rect(-0.041667em,51.04167em,66.04166em,-0.041667em);
position: absolute;
pointer-events: none;
}
Please how can push the image to the top using this attribute style="top: 4.4538em;" is a challenge
Your element does have the top attribute applied. This can be seen in the following:
.stl_02 {
height: 46em;
font-size: 1em;
margin: 0em;
line-height: 0.0em;
display: block;
border-style: none;
width: 51em;
}
.stl_03 {
position: relative;
}
.stl_04 {
width: 100%;
clip: rect(-0.041667em, 51.04167em, 66.04166em, -0.041667em);
position: absolute;
pointer-events: none;
}
<div class="stl_02">
<div class="stl_03">
<img src="http://placehold.it/100" alt="" style="top: 4.4538em;" class="stl_04">
</div>
<div class="stl_view">
<div class="stl_05 stl_06">
</div>
</div>
</div>
If you are not seeing this effect, it is possible you have a rule with higher specificity overriding it, or you have cached the style before you applied this rule.
It's also worth noting that top only works on a positioned element. You need to have position: relative, position: absolute or similar on .stl-04 in order to position it with top.
Alternatively, you may be looking for margin-top, which positions vertically based on the containing element.
As an aside, basing margins off of font sizes (with em units) is generally bad practice; you should really use fixed units instead (preferably not going to so many decimal places).
I'm trying to align multiple images in a single div element which is <nav_bar>. These images will act as navigation links to the different webpages on the website. The problem is, initially I was able to position the (home) image using values I put into the css. However, when I put the second image (computericon), it went behind the (home) image. TO fix this, I gave them position: inline; property. Now I can't move either of the images to my liking. They're just stuck there. No amount of padding or margin adjustment is making them move. The fix to this is surely easy but I'm quite new to website design and development and so I cannot seem to get around this issue. Any sort of help would be greatly appreciated.
Thank You.
Here's the html code:
<div id = "nav_bar">
<img id= "home" src= "home.png" height= "50px" width= "70px">
<img id= "pc" src= "computericon.png" height= "50px" width= "70px">
</div>
And here's the CSS code:
#home {
left: 188px;
position: inline;
}
#MAIN_CONTENT_CONTAINER {
padding-top: 20px;
}
#nav_bar {
height: 60px;
background-color: #3c3c3c;
margin-left: 10px;
}
#home:hover {
background-color: #b4e1cf;
position: inline;
}
#computericon {
left: 80px;
position: inline;
}
Image is attached.The two icons are just stuck there.
Try:
position: relative;
float: left;
Add div with style display:block;clear:both right after these images (to clear floats).
Remove the left property.
There is no such thing as position: inline;.
You should use position: relative; but then the left coordinate will count from the box-side of the previous element.
Depending on what you need to achieve, it may be better to use
display: inline-block;
vertical-align: middle;
on them, use only margins or paddings and forget left and such.
I think that one of your problems is that you were calling your second image with the wrong id. I changed the id to what you were calling it as in the css and then added a class to both of the nav items. Is this what you were looking for?
#MAIN_CONTENT_CONTAINER {
padding-top: 20px;
}
#nav_bar {
height: 60px;
background-color: #3c3c3c;
margin-left: 10px;
}
.navItem {
margin-left: 20px;
margin-top: 5px
}
.navItem:hover {
background-color: #b4e1cf;
}
<div id = "nav_bar">
<img id="home" class="navItem" src= "home.png" height= "50px" width= "70px">
<img id="computericon" class="navItem" src= "computericon.png" height= "50px" width= "70px">
</div>
Hope this helped!
First off, it's always a good idea when you're asking something to include a JSFiddle instead of just pasting the whole code in the answer. I did it for you and took the liberty of changing the images to better show the result: https://jsfiddle.net/2fjg6g3r/
The right choice would be inline-block, what I used in the fiddle.
#nav_bar > img {
display: inline-block;
height: 100%;
width: auto;
margin-right: 5px;
}
I also noticed you're selecting a single element at a time, so I'd suggest taking a look at more CSS selectors to make your code cleaner and less repetitive.
I am trying to center some text within a banner (classic question I know). This banner is split into 12 columns, and there is a cross icon for closing the window in the left-most column. The text is centering in the available space between the cross icon and the end of the banner, rather than centering within the whole banner width. From the way the code is written I cannot see why it would be doing this. Here's the HTML:
<div class='col-xs-12 banner'>
<a class="navbar-brand cross-link" href="" ng-click="close()">
<img class="cross" src="/components/cross.png" alt="close">
</a>
<h1>{{title}}</h1>
</div>
with CSS:
.banner {
height: 70px;
background-color: red;
h1 {
color: white;
text-align: center;
}
.navbar-brand {
&.cross-link {
padding: 0px;
img.cross {
margin: auto;
width: 70px;
height: 70px;
padding: 29px 28px 27px 28px;
}
}
}
When I inspect this on Chrome, the h1 is quite happily sitting within a full-width container as expected, but the image appears to be shifting it across so that it doesn't center properly. Can you see how to resolve this?
Thanks
You could set the .cross-link to absolute position. Remember to set the container position property to a value different from "static":
.container{ position: relative; }
.cross-link{ position: absolute; left: xxxx; top: xxxx; }
What you are missing is a closing } at the end of your .banner block OR at the end of the css you shared.
I am building a Wordpress site with a fixed sidebar menu on the left hand side and the main content on the right within a container. I'm not sure what is best practise for coding the menu because I have currently coded the menu to sit outside of the main container div. I have also noticed for some reason if the desktop screen size is not wide enough my content overflows into the menu on the left and also some of my menu is being cut off at the bottom (some of the links are not visible) I'm not sure if this has anything to do with the height of the screen?
HTML for the menu looks like:
<div id="menuBar">
<img class="logo" src="wp-content/themes/starkers-master/images/lulu-logoi-01.png" width="180" height="250" alt="Lulu Plews Logo"/>
<ul>
<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>
</ul>
</div>
The css is:
.menu-menu-1-container {
position: fixed;
left: 8.8%;
top: 300px;
border-right: 1px solid #D1D2D4;
height: 10000px;
}
#menuBar {
position: fixed;
left: 8.15%;
top:0;
padding: 0 30px 0 0;
border-right: 1px solid #D1D2D4;
margin: 0 auto 0 auto;
}
I just want some advice as to how I should code this and whether or not there is something wrong with my code? I think it is missing something obvious but I don't know what it could be?
I figured out the solution to my problem; I did not realise at the time that using position: fixed; on an element takes it out of flow.
First of all unknowingly I added position:fixed; to the parent element > .menu-menu-1-container which was not required and did not help in anyway. I also removed all the other styles from this div as they were redundant.
Secondly I rectified the styles on #menuBar; I added a height of 100% to it and removed margin:0 auto; (which was doing nothing as I had not declared a width, also it did not need to be centered so this was redundant). I then added a width to #menuBar and removed the left style attribute.
Thirdly I found it to be correct to have the fixed sidebar outside of the main container div and styled the main container div with left margin and padding.
Here is a link to a fiddle with all my code and a snippet of my CSS is below:
#menuBar {
position: fixed;
width: 150px;
background-color: #DDD;
font-size: 18px;
top: 0;
bottom: 0;
border-right: 1px solid #D1D2D4;
}
#menuBar a {
display:block;
padding:15px;
color: #000;
text-decoration: none;
}
.content {
position: relative;
margin-left: 200px;
}
Hope it helps anyone else struggling with something as simple as a fixed position sidebar.