How do I prevent padding from throwing off my DIV widths? - html

I have two DIVs, which I would like to stack vertically …
<div id="searchContainer”>…</div>
<div id="searchResultsContainer”>…</div>
I have the following styles assigned to them …
#searchContainer {
padding: 10px;
font-family: "Calibre", "Helvetica Neue", "Helvetica", "Roboto", "Arial", sans-serif;
background-color: tan;
width: 100%;
}
#searchResultsContainer {
background-color:cyan;
font-family: "Calibre", "Helvetica Neue", "Helvetica", "Roboto", "Arial", sans-serif;
padding: 5px 0px 5px 0px;
width: 100%;
}
However it seems that adding padding to my top DIV causes it to be wider than the DIV below it. I would like both DIVs to be the same width, but I would like the top DIV to have the padding so that the elements don’t scrunch up to the border. How do I do that? Here is the Fiddle that illustrates my problem — https://jsfiddle.net/1zb5mqmo/ .

Use box-sizing: border-box; on the padded div:
#searchContainer {
padding: 10px;
font-family: "Calibre", "Helvetica Neue", "Helvetica", "Roboto", "Arial", sans-serif;
background-color: tan;
/* max-width: 1000px; */
width: 100%;
box-sizing: border-box;
}
box-sizing: border-box The width and height properties include the content padding and border
box-sizing: content-box The width and height properties include only content
Updated JSFiddle

In this case, simply remove the width and you can combine both padding and border without throwing it off
#searchContainer {
padding: 10px;
font-family: "Calibre", "Helvetica Neue", "Helvetica", "Roboto", "Arial", sans-serif;
background-color: tan;
}
#searchResultsContainer {
background-color:cyan;
font-family: "Calibre", "Helvetica Neue", "Helvetica", "Roboto", "Arial", sans-serif;
padding: 5px 0px 5px 0px;
border: 5px solid steelblue
}
<div id="searchContainer">...</div>
<div id="searchResultsContainer">...</div>

Related

Why my element uses 2 different font-sizes?

I set a font-size as global. That changes when browser's width change(kind of fluid typography). I setted that to body element.
But when size of browser changes, then it changes(font-size). Why? and How can I stop this?
*,
html {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
font-size: calc(16px + .75vw);
}
.container {
background-color: #d1c6c6;
padding: 10px;
min-height: 100vh;
}
.container__box {
font-weight: bold;
color: white;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
padding: 15px 10px;
background-color: #8b2be4;
width: 90vw;
font-size: 30px;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div class="container">
<div class="container__box">Gridfan Jessica</div>
</div>
But when size of browser changes, then it changes(font-size). Why?
Because you used vw font size changes when browser size changes.
How can I stop this?
Use px instead. If that's not eye comfortable, Use media query for different font sizes for different screen widths.

Heading 1 causing DIVs to part [duplicate]

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
Why does this CSS margin-top style not work?
(14 answers)
Closed 4 years ago.
I have an issue with my page its making a gap appear between the H1 and main page DIV section. After 2 hours I cant seem to fix it. Can anyone advise where its going wrong. I want the top banner and the #mainarea to be on top of each other. There is a small gap between the 2 at the moment.
body {
font-size: 15px;
font-family: 'Open Sans', sans-serif;
background-color: #f6f6f6;
padding: 0;
margin: 0;
border: 0;
}
#logo {
width: 100%;
height: 50px;
background-color: #000;
}
#mainarea {
width: 60%;
height: auto;
margin-top: 0px;
margin-right: auto;
margin-left: auto;
background-color: #E70A0E;
height: 250px;
}
h1 {
font-size: 1.5em;
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-weight: bolder;
font-style: normal;
padding-left: 15px;
}
<div id="logo">
</div>
<div style="clear: both;"></div>
<div id="mainarea">
<h1>This is a test </h1>
</div>
Set overflow:auto; in #mainarea and add margin:auto toh1 Tag.
See, what happens when you put div inside div: https://stackoverflow.com/a/8529773/9947640
body {
font-size: 15px;
font-family: 'Open Sans', sans-serif;
background-color: #f6f6f6;
padding: 0;
margin: 0;
border: 0;
}
#logo {
width: 100%;
height: 50px;
background-color: #000;
}
#mainarea {
width: 60%;
height: auto;
margin-top: 0px;
margin-right: auto;
margin-left: auto;
background-color: #E70A0E;
height: 250px;
overflow:auto;
}
h1 {
font-size: 1.5em;
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-weight: bolder;
font-style: normal;
padding-left: 15px;
margin:auto;
}
<div id="logo">
</div>
<div style="clear: both;"></div>
<div id="mainarea">
<h1>This is a test </h1>
</div>

Why i can't change the font for div in my css?

When I specify the font in body style in CSS it works ok. But when I do the same for div it has no effect.
.tel {
display: inline-block;
background-color:rgba(0,0,0,0.5);
height: 60px;
padding: 30px 30px 0 0;
font: 24pt/0.64 "Consolas", Arial, Helvetica, sans-serif;
font-size: 87.5%;
width: cover;
}
The problem was that i needed to use styling like this
.tel a{
font: 24pt/0.64 "Consolas", Arial, Helvetica, sans-serif;
color: white;
padding: 0 0 0 50px;
}
try with !important and see if it has an effect, if so, an other class is applied on your DIV.

Add boundary around the web page

I want to add boundaries around four sides of the web pages(black color 25px width). Now it is not lucky.
Please see my jsfiddle at demo.
The partial css:
body {
font-size: .85em;
font-family: "Segoe UI" , Verdana, Helvetica, Sans-Serif;
margin: 25px;
padding: 0;
background-color: #000000;
}
Take away the background and margin from the body element and add stuff to the HTML element instead:
* { /*this * selector selects every element*/
box-sizing: border-box;
}
html{
background: #000;
padding: 25px;
}
body {
font-size: .85em;
font-family: "Segoe UI" , Verdana, Helvetica, Sans-Serif;
}
(also use box-sizing: border-box; on everything to not screw stuff up with margins and padding)
http://jsfiddle.net/CenND/4/
I also removed position: absolute; from the footer. Now it behaves.

how to stretch the header background color across the web browser?

I am having a hard time to create make the background color stretch across the browser
here is some code
body {
font-size: 1.1em;
font-family: "Myriad Pro", "Gill Sans", "Gill Sans MT", "Calibri", sans-serif;
margin: 0px auto;
padding-bottom: 25px;
width: 980px;
}
div#black_bar{
background-color: #000000;
}
div#header {
height: 66px;
font-family: arial;
margin: 0 auto;
color: #FFFFFF;
margin-top: 5px;
}
of course as you can see i have specified the width to be 980px in the body and this apply to the header of course, any ideas how to solve this
and by the way this black bar is actually a div containing the header in it
Thanks in advance.
You will need the header wrapper to be inside the body with width 100%, and to specify width=980px both on the header and content below, like this :
CSS
body, html{
width:100%;
}
div#black_bar{
background-color: #000000;
width:100%;
}
div#header {
height: 66px;
font-family: arial;
color: #FFFFFF;
margin-top: 5px;
width:980px;
margin:0 auto;
}
#content{
font-size: 1.1em;
font-family: "Myriad Pro", "Gill Sans", "Gill Sans MT", "Calibri", sans-serif;
padding-bottom: 25px;
margin: 0 auto;
width: 980px;
}
HTML
<body>
<div id="black_bar">
<div id="header">
</div>
</div>
<div id="content">
</div>
</body>