DIV borders not lining up [duplicate] - html

This question already has answers here:
How to make borders collapse (on a div)?
(8 answers)
Closed 2 years ago.
I am trying to create a table with 3 divs using flex box, but when I add a border to the div's the border isn't lining up on top of the next div. Anyone with an eagle eye spot what I'm missing?
Thanks!
.content div {
width: 350px;
border: solid;
padding: 6px;
}
p {
font-size: medium;
font-weight: 700;
color: #000;
margin-left: 10px;
}
.content {
height: 50px;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.red {
display: flex;
align-items: center;
justify-content: center;
}
.green {
display: flex;
align-items: center;
justify-content: center;
}
.blue {
display: flex;
align-items: center;
justify-content: center;
}
<div class="content">
<div class="red"><img src="https://i.ibb.co/7YJB7QC/flag.png" alt="flag" border="0" width="40" height="40">
<p>100% Made in the USA</p>
</div>
<div class="green"><img src="https://i.ibb.co/dmDyGKH/100.png" alt="100" border="0" width="40" height="40">
<p>100% Happiness Guaranteed</p>
</div>
<div class="blue"><img src="https://i.ibb.co/zmK0Jk0/check.png" alt="check" border="0" width="40" height="40">
<p>100% Secure Checkout</p>
</div>
<table>
</table>
</div>

If you are referring to the thick borders its because of the box-model, the borders from each element are right underneath each other making it look like the border is 2px. A good trick is to have no top or bottom borders on each element and then adjust either the first or last one.
This was my solution, I applied a border-bottom of none to all the items and the added the border back to the bottom of the last element.
.content div {
width: 350px;
border: solid;
padding: 6px;
border-bottom: none;
}
.content div:last-child {
border-bottom: solid;
}
.content div {
width: 350px;
border: solid;
padding: 6px;
border-bottom: none;
}
.content div:last-child {
border-bottom: solid;
}
p {
font-size: medium;
font-weight: 700;
color: #000;
margin-left: 10px;
}
.content {
height: 50px;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.red {
display: flex;
align-items: center;
justify-content: center;
}
.green {
display: flex;
align-items: center;
justify-content: center;
}
.blue {
display: flex;
align-items: center;
justify-content: center;
}
<div class="content">
<div class="red"><img src="https://i.ibb.co/7YJB7QC/flag.png" alt="flag" border="0" width="40" height="40">
<p>100% Made in the USA</p>
</div>
<div class="green"><img src="https://i.ibb.co/dmDyGKH/100.png" alt="100" border="0" width="40" height="40">
<p>100% Happiness Guaranteed</p>
</div>
<div class="blue"><img src="https://i.ibb.co/zmK0Jk0/check.png" alt="check" border="0" width="40" height="40">
<p>100% Secure Checkout</p>
</div>
</div>

Flexbox items don't collapse borders.
If you want the borders to operate like table borders, display them like table, table-row, and table-cell:
div.content {
width: 350px;
display: table;
text-align: center;
border-spacing: 0;
border-collapse: collapse;
}
div.content div.row {
border: solid 2px black;
display: table-row;
}
div.row p {
display: table-cell;
}
div.content div.row img {
height: 40px;
margin-right: 10px;
vertical-align: middle;
}
<div class="content">
<div class="red row">
<p>
<img src="https://i.ibb.co/7YJB7QC/flag.png" alt="flag"></img>
100% Made in the USA
</p>
</div>
<div class="green row">
<p>
<img src="https://i.ibb.co/dmDyGKH/100.png" alt="100"></img>
100% Happiness Guaranteed
</p>
</div>
<div class="blue row">
<p>
<img src="https://i.ibb.co/zmK0Jk0/check.png" alt="check"></img>
100% Secure Checkout
</p>
</div>
</div>

Try setting a thickness for the border, such as 1px:
.content div {
width: 350px;
border: 1px solid;
padding: 6px;
}

Related

Why does the image within the box shrink upwards when the window shrinks?

I really don't know what I'm doing wrong here. I want the image inside the box to stay centered when the window shrinks. Furthermore, I would have thought that align-items: center; would work, but apparently not. The colors are only relevant for me, so I understand what's going on. I don't know if there is a solution for this either, but I hope so. And please ignore the naming and order of the individual classes, I couldn't do better ...:)
.megadiv {
max-width: 1600px;
margin: auto;
text-align: center;
}
.centerbox {
display: flex;
justify-content: space-between;
}
.left {
width: 64%;
background-color: red;
justify-content: space-between;
border: 2px solid gray;
display: flex;
}
.insideleft {
width: 20%;
background-color: yellow;
align-items: center;
text-align: center;
align-content: center;
}
.insideright {
width: 78%;
background-color: purple;
float: right;
padding-top: 2%;
text-align: left;
border-left: 2px ridge #ffa54f;
padding-left: 2%;
padding-bottom: 1%;
}
.picture {
width: 80%;
border-radius: 1%;
margin-top: 10%;
margin-bottom: 8%;
}
.right {
width: 34%;
border: 2px solid gray;
height: 20px;
}
h7 {
color: rgb(0, 153, 158);
font-size: large;
font-family: sans-serif;
}
.textpart {
margin-bottom: 0.5%;
}
<div class="megadiv">
<div class="centerbox">
<div class="left">
<div class="insideleft">
<h20>
<a href="">
<img class="picture" src="https://images-na.ssl-images-amazon.com/images/I/71hi8fWdX2L.jpg"> </a>
</h20>
</div>
<div class="insideright">
<h7>Headline</h7><br>
<h4>
<div class="textpart">Authors</div>
<div class="textpart">Views <a class="" href="">Chapter 2</a></div>
<div class="textpart">Genres: Action - Adventure - Comedy</div>
<div class="textpart">Rating: ⭐⭐⭐⭐⭐</div>
</h4>
</div>
<div class="right">
wawaeaweew
</div>
</div>
</div>
h4 and h20 are empty
You're pretty close to getting the image vertically aligned as you wanted. Try this out, and see if this works the way you would like:
.megadiv {
max-width: 1600px;
margin: auto;
text-align: center;
}
.centerbox {
display: flex;
justify-content: space-between;
}
.left {
width: 64%;
background-color: red;
justify-content: space-between;
border: 2px solid gray;
display: flex;
}
.insideleft {
display: flex;
width: 20%;
background-color: yellow;
align-items: center;
text-align: center;
align-content: center;
}
.insideright {
width: 78%;
background-color: purple;
float: right;
padding-top: 2%;
text-align: left;
border-left: 2px ridge #ffa54f;
padding-left: 2%;
padding-bottom: 1%;
}
.picture {
width: 80%;
border-radius: 1%;
margin-top: 10%;
margin-bottom: 8%;
}
.right {
width: 34%;
border: 2px solid gray;
height: 20px;
}
h7 {
color: rgb(0, 153, 158);
font-size: large;
font-family: sans-serif;
}
.textpart {
margin-bottom: 0.5%;
}
<div class="megadiv">
<div class="centerbox">
<div class="left">
<div class="insideleft">
<a href="">
<img class="picture" src="https://images-na.ssl-images-amazon.com/images/I/71hi8fWdX2L.jpg"> </a>
</div>
<div class="insideright">
<h7>Headline</h7><br>
<h4>
<div class="textpart">Authors</div>
<div class="textpart">Views <a class="" href="">Chapter 2</a></div>
<div class="textpart">Genres: Action - Adventure - Comedy</div>
<div class="textpart">Rating: ⭐⭐⭐⭐⭐</div>
</h4>
</div>
<div class="right">
wawaeaweew
</div>
</div>
</div>
I saw you used align-items: center; in the .insideleft CSS selector which is for aligning a container's children to the center like you want, you'll just want to make this a flexbox to make this work. To do this, simply add display: flex; to the .insideleft selector like in the example. I also removed the <h20> tag from the HTML as this is not valid or necessary.
As for the image shrinking down when the screen width is shrinked - this is because you're using percentages for the widths for all the containers and the image. If you want the image to stop shrinking after a certain point, you can add min-width: 80px; /* (this can be any number of pixels) */ to your .picture selector to make the image stop shrinking once it gets to a certain width of pixels.
Flexbox is super useful for position elements in CSS and I'd recommend looking into this more to have a better understanding. Check out this link here if you'd like an overview of the different flexbox CSS properties.
I am not 100% sure on your intent - Here I changed the class names a bit for clarity and adjusted the markup for a left-middle-right
Not a huge fan of % for padding and margin sizing myself (em feels more clear since it is based on the current font size)
Not strictly needed but I added the containing element class in a few places in CSS for clarity example: .left-pane .picture-container
.page-container {
max-width: 1600px;
text-align: center;
}
.container-box {
display: flex;
align-content: space-between;
}
.container-box .left-pane {
width: 20em;
display: flex;
align-items: center;
justify-content: center;
background-color: #FF0000;
border: 2px solid gray;
}
.left-pane .picture-container {
width: 30%;
background-color: yellow;
align-items: center; /* vertical */
align-content: center; /* horizontal */
}
.left-pane .picture-container .picture {
width: 80%;
border-radius: 1%;
margin-top: 10%;
margin-bottom: 8%;
}
.container-box .middle-pane {
width: 70em;
background-color: #FFDDDD;
padding-top: 2%;
padding-left: 2%;
padding-bottom: 1%;
border-left: 2px ridge #ffa54f;
}
.middle-pane .headline {
color: rgb(0, 153, 158);
font-size: 1.5em;
font-family: sans-serif;
margin-bottom: 1em;
background-color: #eeeeee;
}
.middle-pane .textpart {
margin-bottom: 0.5em;
}
.container-box .right-pane {
height: 20px;
border: 2px solid gray;
}
<div class="page-container">
<div class="container-box">
<div class="left-pane">
<div class="picture-container">
<div>
<a href="">
<img class="picture" src="https://images-na.ssl-images-amazon.com/images/I/71hi8fWdX2L.jpg"> </a>
</div>
</div>
</div>
<div class="middle-pane">
<div class="headline">Headline</div>
<h4>
<div class="textpart">Authors</div>
<div class="textpart">Views <a class="" href="">Chapter 2</a></div>
<div class="textpart">Genres: Action - Adventure - Comedy</div>
<div class="textpart">Rating: ⭐⭐⭐⭐⭐</div>
</h4>
</div>
<div class="right-pane">
wawaeaweew
</div>
</div>
</div>

Centering a div to the viewport with other divs on both sides

I'm currently designing a header bar for a site I'm working on. I would like the header to show a logo at the left, the title in the center, and the user account on the right.
This mockup is what I'm envisioning. Note that the dotted boxes denote a div.
I've made some progress on creating it in HTML/CSS, but I can't seem to get the title to center to the viewport.
As you can see, the title is centering itself between the logo and the account info divs, instead of centering itself on the viewport. This ends up making the page just look a little bit off, and so I would really like to center the title to the viewport.
Here's my code:
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display: inline-block;
text-align: center;
}
.logoArea {
display: inline;
text-align: center;
float: left;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
display: inline-block;
text-align: center;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.siteTitle {
color: white;
}
.pageTitle {
color: white;
}
.userAccountArea {
display: inline;
text-align: center;
float: right;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
<div className="headerBarArea">
<div className="logoArea">
<img src="assets/mines_logo_stacked.png" className="minesLogo" />
</div>
<div className="titleArea">
<h2 className="siteTitle">This is my site Title</h2>
<h3 className="pageTitle">Page Title</h3>
</div>
<div className="userAccountArea">
<img src="assets/user_account.png" className="userAccountIcon" />
<span className="UserAccountText">John Smith (Student)</span>
</div>
</div>
Any ideas on what I could do to make the title div centered to the viewport, instead of centering between the two divs?
html code
<div class="flex-container">
<div class="flex-item">1</div>
<div class="align-self-center">
<span class="siteTitle">This is my site Title</span>
<br>
<div class="text-center ">Page Title</div>
</div>
<div class="flex-item align-self-end third-item">3</div>
</div>
CSS code
.flex-container {
/* We first create a flex layout context */
display: flex;
/* Then we define the flow direction
and if we allow the items to wrap
* Remember this is the same as:
* flex-direction: row;
* flex-wrap: wrap;
*/
flex-flow: row wrap;
/* Then we define how is distributed the remaining space */
justify-content: space-between;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bold;
text-align: center;
}
.third-item {
height: 100px;
}
.text-center{
text-align: center;
}
.align-self-end{
align-self:end;
}
.align-self-center{
align-self:center;
}
code output
code solution
used flex to place the items used .flex-container as parent div where flex items are placed in .justify-content: space-between; is used to place space in between the items. align-self:center; is used to place Page Title at center
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
codepen
.headerBarArea{
display:flex;
justify-content: space-between;
align-items: center
}
It is an easy way to the layout.
you can try it.
Try adding margin-left:auto; and margin-right:auto; to the .titleArea class .
I would suggest using a flex-box though.
Replace your css code with this to make title div centered in all the viewport.
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display:flex;
justify-content: space-between;
}
.logoArea , .titleArea , .userAccountArea {
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
text-align: center;
}
.siteTitle , .pageTitle{
color: white;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}

Header content alignment / positioning

I have a project and i struggle with positioning elements in CSS. i want to create a header with a contact number and email to the right and in image at the centre.
The below is the code. suggestions to overcome this problem i'm facing are very welcome.
header {
display: block;
text-align: center;
}
#cw-logo-trans {
width: 200px;
display: inline-block;
align-items: right;
font-size: 24px;
}
#contacts {
margin-left: 5px;
float: left;
color: white;
font-size: 1.5em;
/* 40px/16=2.5em */
color: white;
}
.home {
width: 70px;
height: auto;
background-color: white;
padding: 10px;
margin: 10px;
float: left;
text-align: center;
}
<header>
<span id="cw-logo-trans" alt="cw-logo-trans">
<img src="images/cw_logo.png" alt="cw-logo-" > </span>
<span id="contacts">0800 111 111</br>email#emailyou.com</span>
<div class="home" alt="Home-button">Home</div>
</br>
</header>
Or you can use Flex as well.
.outer {
display: flex;
background-color: lightgray;
}
.outer div {
flex: 1;
align-items: center;
}
.logo {
display: flex;
justify-content: center;
}
<div class="outer">
<div></div>
<div class="logo">
<image src="https://facebookbrand.com/wp-content/uploads/2019/04/f_logo_RGB-Hex-Blue_512.png" height="40px" width="40px" />
</div>
<div>
<p>markzuckerberg#twitter.com</p>
<p>1234567890</p>
</div>
</div>

Truncate text in flex item

I have a header with a left and right side. The left side contains some text which I want to keep on a single line, truncating with ellipsis if needed. However, when I apply white-space: nowrap to it then the whole header goes outside its container. Here's what I mean:
.container {
width: 300px;
height: 400px;
border: 1px solid black;
}
.header {
height: 80px;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
border: 1px solid red;
}
.header-right,
.header-left {
display: flex;
align-items: center;
justify-content: center;
border: 1px solid blue;
}
.title {
text-overflow: ellipsis;
white-space: nowrap;
}
img {
height: 20px;
width: 20px;
margin: 10px;
}
<div class="container">
<div class="header">
<div class="header-left">
<img src="https://image.flaticon.com/icons/png/128/181/181548.png">
<span class="title">Title: Keep me on a single line</span>
</div>
<div class="header-right">
<img src="https://image.flaticon.com/icons/png/128/181/181548.png">
<img src="https://image.flaticon.com/icons/png/128/181/181548.png">
<img src="https://image.flaticon.com/icons/png/128/181/181548.png">
</div>
</div>
</div>
Does anyone know how to keep the title on a single line but truncate it so the header doesn't go outside the bounds?
You need to disable the default minimun width by adding min-width:0 and disable the shriking for the images and their container by adding flex-shrink:0; and add overflow:hidden
.container {
width: 300px;
height: 400px;
border: 1px solid black;
}
.header {
height: 80px;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
border: 1px solid red;
}
.header-right,
.header-left {
display: flex;
align-items: center;
justify-content: center;
border: 1px solid blue;
min-width:0; /* Added */
}
.title {
text-overflow: ellipsis;
white-space: nowrap;
overflow:hidden; /* Added */
}
img {
height: 20px;
width: 20px;
margin: 10px;
flex-shrink:0; /* Added */
}
.header-right {
flex-shrink:0; /* Added */
}
<div class="container">
<div class="header">
<div class="header-left">
<img src="https://image.flaticon.com/icons/png/128/181/181548.png">
<span class="title">Title: Keep me on a single line</span>
</div>
<div class="header-right">
<img src="https://image.flaticon.com/icons/png/128/181/181548.png">
<img src="https://image.flaticon.com/icons/png/128/181/181548.png">
<img src="https://image.flaticon.com/icons/png/128/181/181548.png">
</div>
</div>
</div>
Related:
Why don't flex items shrink past content size? (To understand the min-width)
Why is a flex-child limited to parent size? (To understand the flex-shrink)

floating div elements right

I really have a hard time placing a div tag. I need to get my form-box with the yellow border out to the right, and my text Inbound Marketing and subtext in the middle. Can anybody help me through this one?
The text is laying in the div with a green border.
The background picture is laying in the div with a red border
Here is my code what I tried until now.
My code looks like this:
HTML:
<div class="top-area" style="border: 4px solid red;">
<div class="container" style="border: 4px solid green;">
<div class="form-container" style="border: 4px solid yellow;">
<div>
#Umbraco.RenderMacro("Ebook")
</div>
</div>
<h1 class="header-xl center">
#Html.Raw(headerText)
</h1>
<p class="sub-header center">
#Html.Raw(CurrentPage.imageTeaserText)
</p>
</div>
</div>
CSS
.top-area {
position: relative;
text-align: center;
}
.top-area, .top-area > .container {
min-height: 500px;
display: -ms-flexbox;
display: flex;
}
.top-area > .container {
-ms-flex-align: center;
align-items:center;
-ms-flex-pack: center;
justify-content: center;
-ms-flex-direction: column;
flex-direction: column;
}
.top-area p {
margin: 0;
color: #fff;
width: 100%;
}
.top-area .header-xl {
margin-top: 20px;
padding: 0;
}
.top-area .sub-header {
font-weight: 300;
margin-top: 5px;
font-size: 2.8em !important;
}
.form-container {
display: block;
float: right;
float: right;
padding: 20px 0;
width: 320px;
}
I'm not entirely sure that I'm clear on what you're trying to do here, but you can use a display: block; on your .top-area > .container item to move it to the right within the green container. If you wanted it OUTSIDE of the green container and to the right in the red container, then you'd need to move it in the HTML. Right now it's contained in the div with the green border, rather than the red border.