I am trying to get an Input and a Link element to sit side by side inside a Div element.
I wanted to have the Div and the Link to completely fill the Div element so you cannot see the containing Div around the edges of the Input and Link elements.
However if I set the width of my Link element any wider than 27px is falls to the next line even though I should have up to 31px left of the container Div to fit the Link element in. Relevent HTML and CSS below...
HTML
<section>
<img src="images/logo11w.png">
<div name="search">
<input>
</div>
</section>
CSS
div[name="search"]{
display: block;
margin: auto;
height: 27px;
width: 572px;
background-color: green;}
input{
display: inline-block;
width: 541px;
height: 27px;
border-style: none;
background-color: yellow;
padding: 0;
margin: 0;}
div[name="search"]>a{
display: inline-block;
width: 27px;
height: 27px;
background-color: red;
margin: 0;}
Could someone explain why my link element cannot be wider that 27px without falling to the next line? I am OK with having the width be only 27px I just wanted to understand why this is happening from a technical standpoint.
inline-block displays the whitespace in the actual layout in the rendered html... one whitespace is 4px . so if you want your link to have 31px width, you need to give margin-left:-4px or remove the whitespace in the actual layout.
<section>
<img src="images/logo11w.png">
<div name="search">
<input>
</div>
</section>
updated fiddle with no white space:
http://jsfiddle.net/x6Mq5/
updated fiddle with margin-left:-4px;
http://jsfiddle.net/x6Mq5/1/
Related
The given css code gives the bottom border line to my header in html page. I want to add a similar vertical line which starts from the border line in header till bottom of the page such that I can get a sidebar panel from left. Please help me.
header {
border-bottom: #bfa161 solid 3px;
height: 60px;
background-color: #fff;
position: relative;
z-index: 9999;
}
Yeah, do what Hash said. You should have your site organized into wrappers and "sub" wrappers using the "div" tag. W3Schools has some really well written documentation on it. I strongly advise you read it. It's not very long. Just google it.
Here's a link explaining the basic concept if you aren't familiar.
What is the correct way to do a CSS Wrapper?
You should make a div as the first thing inside your body tag, and the last thing before the closing tag. Assign an ID to that tag. Most people call that wrapper. Here's what the HTML would look like.
<body>
<div id=wrapper>
#content for your page
</div>
</body>
Then when you do your CSS, add something like this.
NOTEIt's a good idea to define a "max-width". You can actually customize different widths for different resolution ranges when you get comfortable doing more complicated things in CSS.
#wrappper{
max-width: 900px;
max-height: 1750px;
border: #bfa161 solid 3px;
margin: 0 auto; <---"This is so the wrapper centers itself on your screen"
}
Now if you wanted to have a left navigation menu that didn't interfere with any of your page content, you could make two more div tags inside your wrapper div. Give one an id of something like navigationdiv and the other one contentdiv. Float the content div right and float the navigation div left. Obviously, you should place your nav in the navigationdiv and everything else in the contentdiv. Make sure you make both div tags inline-block. It never hurts to define a width either.
navigationdiv{
display: inline-block;
width: 200px;
border-right: #bfa161 solid 3px; <---"This will make the line you wanted"
float: left;
}
contentdiv{
display: inline-block;
max-width: 700px;
float: right;
}
I hope this was clear and thorough.
Simple as this, you just need to give a border color to the div. You can size the div to match the requirement.
header {
border-bottom: #bfa161 solid 3px;
height: 60px;
background-color: #fff;
position: relative;
z-index: 9999;
}
footer {
border-top: #bfa161 solid 3px;
background-color: #fff;
}
#mainContent {
display: inline-flex;
}
#vertical_line {
border-left: 3px solid #bfa161;
width: 60px;
}
<header>
MY HEADER
</header>
<body>
<div id="mainContent">
<div>
<p>THIS IS SIDEBAR</p>
</div>
<div id="vertical_line">
<p>THIS IS MY CONTENT</p>
</div>
</div>
</body>
<footer>
MY FOOTER
</footer>
I have a footer that has three rows. Row one is two divs floated left. Row two is a 'divider' line that is 100 width of the footer. Row three will be 3 more divs floated left.
The problem is on the first row. I have a margin-top:40px; for the middle line. The first floated element sits on top as it should but the second floated element ( which is going to be a text box and has padding inside ) sits on top fine WITHOUT padding, but when I put the 10px padding in, it sits 40px above as it should, but adds extra margin to the elements around it.
.footer {
background-color: #172135;
padding: 40px;
}
.footer-links {
margin: 0px auto 0px auto;
float: left;
}
.middle-line {
width: 100%;
border: 1px solid #1889b4;
padding: 0;
margin-top: 40px;
}
.newsletter {
padding: 10px;
border: 1px solid #188ab4;
width: 300px;
font-family: 'rBblack';
font-size: 12px;
color: white;
text-transform: uppercase;
float: left;
}
<footer class="footer clear" role="contentinfo">
<div class="footer-row-1 clear">
<div class="footer-links">
stuff
</div>
<div class="newsletter">
Sign Up For Our Newsletter
</div>
</div>
<div class="footer-row-2 clear">
<div class="middle-line"></div>
</div>
<div class="footer-row=3 clear">
more stuff
</div>
</footer>
**** PLEASE NOTE ***** The code snippet is not an accurate representation as css reset and clearfix is missing so not correct. Someone else edited this and put it there....
Unless you tell it to, the browser will make the element the width you specify, and then add on the padding etc
If you set the border-sizing property this will prevent it from happening;
box-sizing: border-box;
Try adding that to your CSS declaration
You can compensate for the shifting by of the padding by adding either margin-top:-10px; or position: relative; top: -10px; to .newsletter.
.footer {
background-color: #172135;
padding: 40px;
}
.footer-links {
margin: 0px auto 0px auto;
float: left;
}
.middle-line {
width: 100%;
border: 1px solid #1889b4;
padding: 0;
margin-top: 40px;
}
.newsletter {
padding: 10px;
margin-top: -10px; /* negative or padding value - readjusts position back up */
border: 1px solid #188ab4;
width: 300px;
font-family: 'rBblack';
font-size: 12px;
color: white;
text-transform: uppercase;
float: left;
}
<footer class="footer clear" role="contentinfo">
<div class="footer-row-1 clear">
<div class="footer-links">
stuff
</div>
<div class="newsletter">
Sign Up For Our Newsletter
</div>
</div>
<div class="footer-row-2 clear">
<div class="middle-line"></div>
</div>
<div class="footer-row=3 clear">
more stuff
</div>
</footer>
After reviewing what you said about my old, now competently irrelevant answer, i think i found what your issue is.
padding:10px;
adds padding to ALL 4 sides. it is functionally equivalent to
padding: 10px 10px 10px 10px;
the newsletter div is now significantly Taller than the other stuff in the same div, and the browser is forced to compensate by making the container div bigger. the container div gains 20 pixels in height when you do this, which would appear to add additional margin to the other elements.
to remove this, you would instead want to use either of these
padding: 0 10px;
padding: 0 10px 0 10px;
as per http://www.w3schools.com/cssref/pr_padding.asp
either will add padding to the LEFT and RIGHT sides equal to 10px, but the top and bottom will remain 0. the newsletter div will no longer be over-sized, making the container div bigger, which will make it appear there is margin for the others.
Edit (additional options):
however, if you want to keep the top and bottom padding, your have 3 main options.
1) add the padding to the other div inside the parent as well as newsletter. they will line up with newsletter, and have the extra space above and below. you would likely want to shrink the middle div's height to compensate for the increase.
2) to completely remove the newsletter from its parent div. set the width of newsletter and its parent div so that they add up to 100% including padding and borders, or use box-sizing:border box, and float both left so that they line up horizontally. now you can make newsletter as big as you want, and it will not affect the others.
3) you fix the height of the parent div,so that newsletter can be bigger than its parent div, however this tends to cause problems with layouts if your not careful, as it may overlap.
I'm using foundation as a basic template for my Website. Now i have a span-element inside many divs.
The parent element is a div with width: 100%, what i need, but the span has text now, with a background color.
So i just want the span-width adjusting by the text, and not by the
parent element. How to do this?
width: initial or auto is not working.
EDIT:
CSS looks like:
span.error, small.error {
background: #B86566;
margin: 0px 10px 10px 10px;
padding: 0.375rem 0.375rem 0.375rem;
text-align: left;
}
HTML:
<span ng-if="method_here" class="error">
<span class="arrow_up"></span>
{{validation_message}}
</span>
All of this is inside an div which has a width: 100%.
Set the span to display: inline-block;.
Is it possible to stack in-line-block elements?
I have a DIV which I want the elements inside it (h1 and P) to be centred. So I set the DIV to text-align centre and initally set the H1 and P tag to inline-blocks.respectively.
The idea was to display the two elements (H1 and P) as in-line-block elements so content is centred and a transparent png shows in the background for the length of the text.
But the problem I have is that having elements as inline-blocks means they will appears next to each other (I don't want this to happen), so I set the P tag as block element but it's resulting in the transparent png being as wide.
HTML:
<div id="hero">
<div class="container">
<div class="row">
<div class="span12" id="hero-text">
<h2>Heading line</h2>
<p>Paragraph line goes here</p>
</div>
</div>
</div>
</div>
CSS
#hero {
height: 435px;
width: 100%;
background: url(../img/hero-image.png) 0 0 no-repeat;
background-color: #999;
position: relative;
color: #FFF;
border-bottom: 3px solid #E6E6E6;
}
#hero-text {
position: absolute;
top: 33%;
text-align: center;
}
#hero h2 {
font-size: 4em;
font-style: normal;
line-height: 50px;
padding-top: 10px;
background: url(../img/bg-heading.png) repeat;
display: inline-block;
padding: 10px 20px;
}
#hero p {
font-size: 2em;
line-height: 30px;
display: block;
background: url(../img/bg-heading.png) repeat;
padding: 10px 20px;
}
Any help is appreciated.
This was actually tougher to solve than I originally thought. I could find two options for you. If you don't want to change your markup:
Give both #hero h2 and #hero p display:inline-block, and give them widths so that their combined width is greater than 100%. They both can be width:51%, or one can be wider than the other, just as long as their total is more than the width of the parent. This will cause the p to break to a new line. See http://codepen.io/anon/pen/cjDiH OR
2.If you want their widths to be fluid, I'd add an element in between the h2 and p that is display:block. I added hr, then took away its margin, padding and border to make it not visible other than to cause the line break. See http://codepen.io/anon/pen/AGDti
I see you figured out out to get them to stack like in your screenshot.
Now,
try adding width: auto; to #hero p in your css.
I'm having some trouble with this code:
CSS:
div#header
{
width: 100%;
background-color: #252525;
padding: 10px 0px 10px 15px;
position: relative;
}
div#login
{
float: right;
position: absolute;
right: 10px;
top: 5px;
}
HTML:
<div id="header">
<img src="./img/logo.jpg" />
<div id="login">
<form id="header-login" action="#">
<input type="text" /> <input type="text" /> <input type="submit" value="LOGIN" />
</form>
</div>
</div>
The div id=header tag has a left padding of 15px. Because of this, the div itself stretches the width of the page, plus an extra 15px to the right, causing me to have a horizontal scrollbar. I've tried putting the header div inside a container div, with relative positioning, but the padding caused the header div to just overflow 15px over the container, still leaving me with the sidebar. Can someone help me with a better understanding? Thanks.
Try removing the 100% width of the header. Since divs are line elements, thats not needed.
Try the div header with this.
div#header { width: 100%; height:15px; background-color: #252525; padding: 10px 0px 10px; position: relative; }
I'm not really sure what you're trying to accomplish: but here's a starter:
block elements (like a div, for instance) always expand to the width of your container, unless you're using quirks mode in IE.
there is no point in using position absolute and float right on the same element. use margin to get the appropriate distances. A floated element do need some sizes, a width for instance.
If you want to floated element to be "at top", it need to be specified first in it's parent element. Meaning, put the div before the img.
div#header {
background-color: #252525;
padding: 10px 0px 10px 15px;
}
div#login {
width: 100px; /* use preferred size here. */
float: right;
margin-right: 10px;
margin-top: 5px;
}
Would 'overflow:hidden' on a properly sized container div work?