CSS border width changing layout [duplicate] - html

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
CSS: Width in percentage and Borders
(5 answers)
Closed 3 years ago.
I have an issue when I change border width from 1px to 0px.
That changes the layout of divs. The divs should be stacked step by step in the influence of margin, but when I set the border width as 0px, the top margin becomes 0px.
This is my code.
div {
height: 300px;
width: 50%;
margin: 10px;
border: 1px solid red;
}
.red {
background-color: #ffcccc;
}
.green {
background-color: #ccffcc;
}
.blue {
background-color: #ccccff;
}
.purple {
background-color: #cccccc
}
<div class="red">
<div class="green">
<div class="blue">
<div class="purple"></div>
</div>
</div>
</div>

In the standard html content box model the width is only that of the content of the box. If you add padding and/or borders to it, these will be added to the box width (in your case, 50% + 1px + 1px).
You can change this behavior by choosing to use a different box model, the border box model : in this case the width you specify will always include padding and border.
You can do that like this:
<style>
div {
box-sizing: border-box;
height: 300px;
width: 50%;
margin: 10px;
border: 1px solid red;
}
.red { background-color: #ffcccc; }
.green { background-color: #ccffcc; }
.blue { background-color: #ccccff; }
.purple { background-color: #cccccc}
</style>
See here and here for more details.

You can try this
div {
height: 300px;
width: 50%;
padding: 1px;
margin:10px;
border:0px solid red;
}
.red {
background-color: #ffcccc;
}
.green {
background-color: #ccffcc;
}
.blue {
background-color: #ccccff;
}
.purple {
background-color: #cccccc;
}
<div class="red">
<div class="green">
<div class="blue">
<div class="purple"></div>
</div>
</div>
</div>

You can use this code
body {
margin: 0;
padding: 0;
}
div {
height: 300px;
width: 50%;
margin: 10px;
border: 0px solid red;
float: left;
}
.red {
background-color: #ffcccc;
}
.green {
background-color: #ccffcc;
}
.blue {
background-color: #ccccff;
}
.purple {
background-color: #cccccc;
}
<div class="red">
<div class="green">
<div class="blue">
<div class="purple"></div>
</div>
</div>
</div>

This should do it!
<style>
div {
height: 300px;
width: 50%;
margin: 10px;
box-shadow:inset 0px 0px 0px 1px red;
}
.red { background-color: #ffcccc; }
.green { background-color: #ccffcc; }
.blue { background-color: #ccccff; }
.purple { background-color: #cccccc}
</style>
<div class="red">
<div class="green">
<div class="blue">
<div class="purple"></div>
</div>
</div>
</div>

Related

How to set dynamic div height [duplicate]

This question already has answers here:
Fill the remaining height or width in a flex container
(2 answers)
Fill remaining vertical space with CSS using display:flex
(6 answers)
Closed 1 year ago.
There is the following HTML:
body {
padding: 10px;
border: 2px solid black;
margin: 2px;
}
.div1 {
height: 50px;
border: 2px solid black;
}
.div2 {
/* height: ??? */
border: 2px solid black;
margin-top: 2px;
margin-bottom: 2px;
}
<body>
<div id="div1" class="div1"></div>
<div id="div2" class="div2"></div>
</body>
The height of div1 should be always static 50px, the rest of parent space should be filled with div2. For example,
if body.height == 700px then
div1.height = 50px
div2.height = 650px
How to define and set this dynamic height for the div2?
If the above is your exact case then you can use the css calc()
So, set your css like so:
#div1{
height:50px;
}
#div2{
height:calc(100% - 50px);
}
As long as there is a defined height on body, this should work
grid, padding and box-sizing will help you here:
* {
box-sizing: border-box;
margin: 0;
}
html {
padding: 2px;
height: 100%;
}
body {
padding: 10px;
border: 2px solid black;
/* added*/
min-height: 100%;
display: grid;
grid-template-rows: 50px 1fr;
gap: 2px;
}
.div1 {
border: 2px solid black;
}
.div2 {
border: 2px solid black;
margin-top: 2px;
margin-bottom: 2px;
}
<body>
<div id="div1" class="div1"></div>
<div id="div2" class="div2"></div>
</body>
You can use css flexbox
body {
min-height: 100vh;
display: flex;
flex-direction: column;
padding: 0;
margin: 0;
}
.div1 {
height: 50px;
border: 1px solid red;
}
.div2 {
flex-grow: 1;
border: 1px solid green;
}

why there is a gap space between DIV inline block boxes? [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 6 years ago.
when I use display = inline-block to my div, there is a gap between these 2 boxes. can anyone tell me why it is like this, and how can I remove the gap?
* {
margin: 0;
padding: 0;
}
.first {
height: 100px;
width: 100px;
border: solid 1px black;
display: inline-block;
}
.second {
height: 100px;
width: 100px;
border: solid 1px black;
display: inline-block;
}
<div class="first"></div>
<div class="second"></div>
Use float: left;
* {
margin: 0;
padding: 0;
}
.first {
height: 100px;
width: 100px;
border: solid 1px black;
display: inline-block; float:left
}
.second {
height: 100px;
width: 100px;
border: solid 1px black;
display: inline-block;float:left
}
set font-size: 0 for parent element.
.parent-element { /* apply to the parent element */
font-size: 0;
}
.first, .second {
font-size: 13px; /* default value, change as per your need */
}
Change your html like blow
other way add Comment like blow
* {
margin: 0;
padding: 0;
}
.first {
height: 100px;
width: 100px;
border: solid 1px black;
display: inline-block;
}
.second {
height: 100px;
width: 100px;
border: solid 1px black;
display: inline-block;
}
<div class="first"></div><div class="second"></div>
<div class="first"></div><!--
--><div class="second"></div>
#Chao Wang for removing that gap you have to use left float in bot the
div
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.first {
height: 100px;
width: 100px;
border: solid 1px black;
display: inline-block;
float:left;
}
.second {
height: 100px;
width: 100px;
border: solid 1px black;
display: inline-block;
float:left;
}
</style>
<div class="first"></div>
<div class="second"></div>
It will resolve your problem (y)
The "inline-block" elements has this space because of font size of the parent.
Here you can find more details and ways how to remove the space. One of the easiest is this to add a div parent with font-size=0:
<div style="font-size: 0;">
<div class="first"></div>
<div class="second"></div>
</div>
This happen beacause it use line height
.inline-parent{
display:inline-block;
width:100%;
line-height:0;
font-size:0;
}
.inline1{
display:inline-block;
width:50%;
background:#333;
line-height:1;
font-size:15px;
}
.inline2{
display:inline-block;
width:50%;
background:#999;
line-height:1;
font-size:15px;
}
<div class="inline-parent">
<div class="inline1">
text text
</div>
<div class="inline2">
text text
</div>
</div>
It is all because of line-height and font-size

Consistent spacing between neighboring elements inside a container

I have a container of certain height and width that holds a number of children (divs). I would like to have a 4px lightblue border around each div. Two neighboring divs should only have 4px space between them.
I'm able to accomplish this by manually setting the heights, widths, and margins/borders, but I'm sizing the children by percentage of the parent.
Here's a fiddle I have set up showing the divs in the parent, but without any spacing or border.
.container {
height: 300px;
width: 300px;
background-color: lightblue;
}
.left {
width: 30%;
height: 100%;
background-color: lightyellow;
float: left;
}
.top-right {
width: 70%;
height: 50%;
background-color: lightred;
float: right;
}
.bottom-middle {
width: 35%;
height: 50%;
background-color: lightpink;
float: left;
}
.bottom-right {
width: 35%;
height: 50%;
background-color: lightgreen;
float: right;
}
.border {
/* margin: 4px; */
}
<div class="container">
<div class="left border"></div>
<div class="top-right border"></div>
<div class="bottom-middle border"></div>
<div class="bottom-right border"></div>
</div>
http://jsfiddle.net/ymv0oave/
2px border for all divs, and 2px border for container.
.container {
...
border: 2px solid blue;
}
.container div{
box-sizing: border-box;
border: 2px solid blue;
}
https://jsfiddle.net/afelixj/mja5kfvw/
Putting the full answer here
.border {
/* margin: 10px; */
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
border:4px solid red;
padding: 4px;
}
.remove-right{
border-right: none;
}
Then put the class to your div class tag
<div class="container">
<div class="left border remove-right"></div>
<div class="top-right border remove-right "></div>
<div class="bottom-middle border"></div>
<div class="bottom-right border"></div>
</div>
You can use the calc() function is css to make use of % and still have an exact 4px border.
HTML:
<div class="container">
<div class="left border-right"></div>
<div class="top-right border-bottom"></div>
<div class="bottom-middle border-right"></div>
<div class="bottom-right"></div>
</div>
CSS:
.left {
width: 30% //Fallback for the 0.8% people still using IE7/IE8
width: calc(30%-4px); //HERE
height: 100%;
background-color: lightyellow;
float: left;
}
.top-right {
width: 70%;
height: 70% //Fallback for the 0.8% people still using IE7/IE8
height: calc(50% -4px); //HERE
background-color: lightred;
float: right;
}
.bottom-middle {
width: 35% //Fallback for the 0.8% people still using IE7/IE8
width: calc(35% -4px); //HERE
height: 50%;
background-color: lightpink;
float: left;
}
.bottom-right {
width: 35%;
height: 50%;
background-color: lightgreen;
float: right;
}
.border-right {
border-right: 4px solid lightblue;
}
.border-bottom {
border-bottom: 4px solid lightblue;
}

How can I centre each text element within each <div> both vertically and horizontally, so that the text is exactly in the middle

I am trying to create nine circles, with a picture of a person in each, with the name of the indidvidual directly in the middle of each picture. Below is my HTML and CSS file. I have tried using text-align: centre however it does not look accurate? Also, it only move the text horizontally and not vertically, i.e not to the center of the image, only to the center of the TOP of the image. Thank you.
<div class="friend">Stacey</div>
<div class="Sexy">Caroline</div>
<div class="friend"; id="best_friend">Adam</div></br>
<div class="boss">Paul</div>
<div class="friend">Phil</div>
<div class"colleague"; id="archnemesis">Luca</div>
<div class="friend">Ruth</div>
<div class="family">Mum</div>
<div class="enemy">Satan</div>
</body>
**My CSS file below:**
div {
display: inline-block;
margin-left: 5px;
border: 2px solid black;
border-radius: 100%;
height: 100px;
width: 100px;
text-align: center;
}
.friend {
border: 2px dashed #008000;
}
.family {
border: 2px dashed #0000FF;
}
.enemy {
border: 2px dashed #FF0000;
}
.colleague {
border: 2px solid brown;
}
.boss {
border: 5px solid pink;
}
.sexy {
border-color: purple;
}
#best_friend {
border: 4px solid #00C957;
}
#archnemesis {
border: 4px solid #CC0000;
}
<html>
<head>
<style>
div {
display: inline-block;
margin-left: 5px;
border: 2px solid black;
border-radius: 100%;
height: 100px;
width: 100px;
text-align: center;
}
span.center-content {
display: inline-block;
vertical-align: middle;
line-height:100px;
}
.friend {
border: 2px dashed #008000;
}
.family {
border: 2px dashed #0000FF;
}
.enemy {
border: 2px dashed #FF0000;
}
.colleague {
border: 2px solid brown;
}
.boss {
border: 5px solid pink;
}
.sexy {
border-color: purple;
}
#best_friend {
border: 4px solid #00C957;
}
#archnemesis {
border: 4px solid #CC0000;
}
</style>
</head>
<body>
<div class="friend"><span class="center-content ">Stacey</span></div>
<div class="Sexy"><span class="center-content ">Caroline</span></div>
<div class="friend"; id="best_friend"><span class="center-content ">Adam</span></div></br>
<div class="boss"><span class="center-content ">Paul</span></div>
<div class="friend"><span class="center-content ">Phil</span></div>
<div class="colleague"; id="archnemesis"><span class="center-content ">Luca</span></div>
<div class="friend"><span class="center-content ">Ruth</span></div>
<div class="family"><span class="center-content ">Mum</span></div>
<div class="enemy"><span class="center-content ">Satan</span></div>
</body>
</html>
Changes
Added a new span and common class to all spans - center-content
added new class in style - span.center-content
You will need to keep same height for div and span to get it in the middle of each div.
Either you can test the code above on your browser or else visit this demo url - https://jsfiddle.net/BRxKX/4962/
You probably want something along the lines of:
<div style="display:table;">
<div style="display:table-cell;vertical-align:middle;">
<div style="margin-left:auto;margin-right:auto;"></div>
</div>
</div>
This may be of help in the future http://howtocenterincss.com
You could use vertical-align:middlebut that requires to be a table and you can spoof it using display:table-cell.
I suggest to use line-height with the height of the circles, this works with only 1 line of text, if you one multiline you have to use internal divs or spans.
If if doesn't look accurate horizontally may be the padding.
div {
display: inline-block;
margin-left: 5px;
border: 2px solid black;
border-radius: 100%;
height: 100px;
width: 100px;
text-align: center;
padding:0;
line-height:100px;
}

How to Have the Div Aligned to the Middle, and That Relative to the Parent Div?

html:
<div class="parent">
<div>
First PageSecond PageThird Page
</div>
</div>
css:
div.parent > div > a {
margin-right: 2em;
}
div.parent {
border: 1px solid red;
height: 100px;
background-color: #bbb;
}
div.parent > div {
border: 1px solid lightblue;
background-color: #fff;
height: 40px;
width: 900px;
display: block;
float: default;
margin: 0 auto 0;
}
Example:
The result of the stylesheet can be seen on JSFiddle: http://jsfiddle.net/BMEXR/2/
Notice: The colors and spacings are set for better differentiating.
How can the div be aligned in the middle, and that relative to its parent?
So that the div will move relative to the height setting of the parent div?
If you don't mind some markup change:
http://jsfiddle.net/BMEXR/30/
HTML:
<div class="parent">
<div class="childContainer">
<div class="childContents">
First PageSecond PageThird Page
</div>
</div>
</div>
CSS:
div.childContents a {
margin-right: 2em;
}
div.childContents{
background-color:white;
height: 25px;
width: 900px;
}
div.parent {
border: 1px solid red;
height: 100px;
background-color: #bbb;
display:table-row;
}
div.childContainer {
border: 1px solid lightblue;
background-color: transparent;
vertical-align:middle;
display: table-cell;
}
Maybe you could try something like this using jQuery: http://jsfiddle.net/BMEXR/32/
HTML:
<div class="parent">
<div class="container">
<ul>
First Page | Second Page | Third Page
</ul>
</div>
</div>
​
CSS:
div.parent {
border: 1px solid red;
height: auto;
background-color: #bbb;
text-align: center;
padding: 5px;
}
div.container {
margin: 0 auto;
border: 1px solid lightblue;
}
​
JS:
$(function() {
var aSize = $('a').size();
var aTotalSize = 0;
for(var i = 0; i < aSize; i++) {
aTotalSize += $('a').eq(i).width()+10;
}
$('.container').width(aTotalSize);
});​