Why does this img div display in front of my other blocks? - html

I applied display: block to my other divs but my image is displayed in front. I thought display: block would force a line break. Why is my image in front?
https://codepen.io/El_Escandalo/pen/PoPzXPZ?editors=1100

That's because your .container div is limited to height: 200px;. Erase that to allow its height to adjust to the contents, and your image container will be below it.

You've got an extra closed after container C.
* {
padding: 0;
margin: 0;
}
.container {
height: 200px;
text-align: center;
padding: none;
}
.a {
height: 100px;
width: 33%;
background: red;
display: block;
padding: none;
border: 10px solid purple;
box-sizing: border-box;
}
.b {
height: 100px;
width: 33%;
background: green;
display: block;
padding: none;
border: 10px solid purple;
box-sizing: border-box;
}
.c {
height: 100px;
width: 33%;
background: blue;
display: block;
padding: none;
border: 10px solid purple;
box-sizing: border-box;
}
.d {
border: 25px solid pink;
margin: 0 auto;
width: 75vw;
height: auto;
text-align: center;
}
.d img {
width: 100%;
}
<div class="container">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
<div class="img-cont">
<div class="d">
<img src="https://cdn.pixabay.com/photo/2020/03/26/10/51/norway-4970019_1280.jpg" alt="view">
</div>
</div>

Related

Why does my inline overflowing even if I use border-box? [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 1 year ago.
I want to create three colored boxes inside a box, here is the HTML/CSS code. When I inspect my code in browser the width would fit the parent box however the last colored box still position itself below the other boxes which should not happen.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 1000px;
height: 500px;
border: 10px solid black;
margin: 0 auto;
}
.green {
display: inline-block;
box-sizing: border-box;
width: 33.3333%;
height: 400px;
padding: 20px;
background-color: green;
}
.red {
display: inline-block;
box-sizing: border-box;
width: 33.3333%;
height: 400px;
padding: 20px;
background-color: red;
}
.blue {
display: inline-block;
box-sizing: border-box;
width: 33.3333%;
height: 400px;
padding: 20px;
background-color: blue;
}
<div class="container">
<div class="box green"></div>
<div class="box red"></div>
<div class="box blue"></div>
</div>
Because inline elements are sensitive to the white space in your code. Just remove it:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 1000px;
height: 500px;
border: 10px solid black;
margin: 0 auto;
}
.green {
display: inline-block;
box-sizing: border-box;
width: 33.3333%;
height: 400px;
padding: 20px;
background-color: green;
}
.red {
display: inline-block;
box-sizing: border-box;
width: 33.3333%;
height: 400px;
padding: 20px;
background-color: red;
}
.blue {
display: inline-block;
box-sizing: border-box;
width: 33.3333%;
height: 400px;
padding: 20px;
background-color: blue;
}
<div class="container">
<div class="box green"></div><div class="box red"></div><div class="box blue"></div>
</div>

How to make child img respect parent when it has items above

I'm not able to make the img respect the parent's container when it has more items above it.
Html
<div class="box">
<h4>Some random text that will overflow the image</h4>
<div class="box2">
<img class="img" src="https://images-na.ssl-images-amazon.com/images/I/71Yd98vOJTL._AC_SY679_.jpg" alt="Poster not available"/>
</div>
</div>
CSS:
.box {
border: 1px solid;
border-radius: 3px;
margin: .3rem;
text-align: center;
padding: 2rem 0;
height: 300px;
}
.box2 {
border: 1px solid;
border-radius: 3px;
text-align: center;
height: 100%;
border-color: red;
display: inline-block;
box-sizing: content-box;
}
.img {
height: 100%;
width: 100%;
object-fit: contain;
box-sizing: content-box;
}
h4{
box-sizing: border-box
}
Fiddle: https://jsfiddle.net/dsxhy57k/1/
Image:
One way to achieve that is to use the CSS Flexible Box Layout combined with overflow: hidden
*{
box-sizing: border-box;
margin: 0;
}
html,
body {
height: 100%;
width: 100%;
}
.box {
display: flex;
flex-flow: column;
border: 1px solid;
border-radius: 3px;
margin: .3rem;
height: 300px;
}
.box2 {
flex: 2 ;
overflow: hidden;
background-color: green;
border: 1px solid red;
border-radius: 3px;
padding: 1rem;
}
.box2 img{
width: 100%;
height: 100%;
object-fit: contain;
}
<div class="box">
<h4>Some random text that will overflow the image</h4>
<div class="box2">
<img class="img" src="https://images-na.ssl-images-amazon.com/images/I/71Yd98vOJTL._AC_SY679_.jpg" alt="Poster not available"/>
</div>
</div>
An other would be using the calc function to set the height of your element .box2
.box {
border: 1px solid;
border-radius: 3px;
margin: .3rem;
text-align: center;
padding: 2rem 0;
height: 300px;
}
.box2 {
border: 1px solid;
border-radius: 3px;
text-align: center;
/*height: 100%;*/
height: calc(100% - 32px);/*here 32px is the space reserved for the h4, this cold well be the height of the h4*/
border-color: red;
display: inline-block;
box-sizing: content-box;
}
.img {
height: 100%;
width: 100%;
object-fit: contain;
box-sizing: content-box;
}
h4{
box-sizing: border-box
}
<div class="box">
<h4>Some random text that will overflow the image</h4>
<div class="box2">
<img class="img" src="https://images-na.ssl-images-amazon.com/images/I/71Yd98vOJTL._AC_SY679_.jpg" alt="Poster not available"/>
</div>
</div>

How to make CSS Sticky work with Flex issue [duplicate]

I have an HTML structure where I can't seem to get the CSS position sticky working.
I think it because it's within the aside container. If I make aside stick it works.
I want the .product-info div to be sticky and when it hits the div .content-other it unsticks.
Unless with flex I could move out .personal-info and .product-info from within the aside and have them sit to the right on top of each other? Like
content | Personal info
| Product info
Then not bother having the wrapping aside? Not sure how to stack these like this though with flex.
body {
padding: 20px;
}
.container {
margin-left: auto;
margin-right: auto;
position: relative;
padding-bottom: 16px;
padding-top: 16px;
width: 100%;
display: flex;
}
.content {
position: relative;
max-width: 100%;
flex-basis: 74%;
border: 1px solid black;
width: 300px;
margin-right: 20px;
height: 540px;
}
.right-side {
align-self: flex-start;
background-color: #ffffff;
border: 2px solid #e8e8e3;
border-radius: 0 4px 4px 4px;
flex: 1 1;
flex-basis: 40%;
min-width: 338px;
padding: 16px 16px 0;
display: block;
width: 400px;
}
.personal-info {
height: 250px;
}
.product-info {
position: sticky;
position: -webkit-sticky;
top: 24px;
border: 1px solid red;
}
.content-other {
width: 100%;
background: #f5f5f5;
height: 400px;
}
<div class="container">
<div class="content">content area here</div>
<aside class="right-side">
<div class="personal-info">some info</div>
<div class="product-info">sticky info</div>
</aside>
</div>
<div class="content-other">.product-info unsticks when it hits here</div>
Cheers
Simply remove align-self: flex-start;
body {
padding: 20px;
}
.container {
margin-left: auto;
margin-right: auto;
position: relative;
padding-bottom: 16px;
padding-top: 16px;
width: 100%;
display: flex;
}
.content {
position: relative;
max-width: 100%;
flex-basis: 74%;
border: 1px solid black;
width: 300px;
margin-right: 20px;
height: 540px;
}
.right-side {
/*align-self: flex-start;*/
background-color: #ffffff;
border: 2px solid #e8e8e3;
border-radius: 0 4px 4px 4px;
flex: 1 1;
flex-basis: 40%;
min-width: 338px;
padding: 16px 16px 0;
display: block;
width: 400px;
}
.personal-info {
height: 250px;
}
.product-info {
position: sticky;
position: -webkit-sticky;
top: 24px;
border: 1px solid red;
}
.content-other {
width: 100%;
background: #f5f5f5;
height: 400px;
}
<div class="container">
<div class="content">content area here</div>
<aside class="right-side">
<div class="personal-info">some info</div>
<div class="product-info">sticky info</div>
</aside>
</div>
<div class="content-other">.product-info unsticks when it hits here</div>

How to make CSS Sticky work with Flex issue

I have an HTML structure where I can't seem to get the CSS position sticky working.
I think it because it's within the aside container. If I make aside stick it works.
I want the .product-info div to be sticky and when it hits the div .content-other it unsticks.
Unless with flex I could move out .personal-info and .product-info from within the aside and have them sit to the right on top of each other? Like
content | Personal info
| Product info
Then not bother having the wrapping aside? Not sure how to stack these like this though with flex.
body {
padding: 20px;
}
.container {
margin-left: auto;
margin-right: auto;
position: relative;
padding-bottom: 16px;
padding-top: 16px;
width: 100%;
display: flex;
}
.content {
position: relative;
max-width: 100%;
flex-basis: 74%;
border: 1px solid black;
width: 300px;
margin-right: 20px;
height: 540px;
}
.right-side {
align-self: flex-start;
background-color: #ffffff;
border: 2px solid #e8e8e3;
border-radius: 0 4px 4px 4px;
flex: 1 1;
flex-basis: 40%;
min-width: 338px;
padding: 16px 16px 0;
display: block;
width: 400px;
}
.personal-info {
height: 250px;
}
.product-info {
position: sticky;
position: -webkit-sticky;
top: 24px;
border: 1px solid red;
}
.content-other {
width: 100%;
background: #f5f5f5;
height: 400px;
}
<div class="container">
<div class="content">content area here</div>
<aside class="right-side">
<div class="personal-info">some info</div>
<div class="product-info">sticky info</div>
</aside>
</div>
<div class="content-other">.product-info unsticks when it hits here</div>
Cheers
Simply remove align-self: flex-start;
body {
padding: 20px;
}
.container {
margin-left: auto;
margin-right: auto;
position: relative;
padding-bottom: 16px;
padding-top: 16px;
width: 100%;
display: flex;
}
.content {
position: relative;
max-width: 100%;
flex-basis: 74%;
border: 1px solid black;
width: 300px;
margin-right: 20px;
height: 540px;
}
.right-side {
/*align-self: flex-start;*/
background-color: #ffffff;
border: 2px solid #e8e8e3;
border-radius: 0 4px 4px 4px;
flex: 1 1;
flex-basis: 40%;
min-width: 338px;
padding: 16px 16px 0;
display: block;
width: 400px;
}
.personal-info {
height: 250px;
}
.product-info {
position: sticky;
position: -webkit-sticky;
top: 24px;
border: 1px solid red;
}
.content-other {
width: 100%;
background: #f5f5f5;
height: 400px;
}
<div class="container">
<div class="content">content area here</div>
<aside class="right-side">
<div class="personal-info">some info</div>
<div class="product-info">sticky info</div>
</aside>
</div>
<div class="content-other">.product-info unsticks when it hits here</div>

align full width search bar on left box and set a padding [duplicate]

This question already has answers here:
How to make an element width: 100% minus padding?
(15 answers)
Closed 3 years ago.
I need to place a search bar on the bottom left box of the page and it has to take full width with height of 40px. However, when I set a padding to the input to avoid the placeholder to be too close to the border, the search box doesn't respect the width of the parent box and occupies space out of it.
Codepen: https://codepen.io/gabrielmlinassi/pen/gObJQQQ?editors=1100
body,
html {
height: 100%;
width: 100%;
margin: 0;
}
.box {
height: 100%;
width: 100%;
}
.box .top {
height: 30%;
width: 100%;
background-color: #cccccc;
}
.box .bottom {
background-color: #ffffff;
height: 70%;
width: 100%;
display: flex;
justify-content: space-between;
}
.box .bottom .left {
background-color: #f7f7f7;
border: solid 1px #cccccc;
border-radius: 8px;
margin-top: -100px;
margin-left: 50px;
width: 59.5%;
height: 100%;
}
.box .bottom .right {
background-color: #f7f7f7;
border: solid 1px #cccccc;
border-radius: 8px;
margin-top: -100px;
margin-left: 15px;
margin-right: 50px;
width: 40%;
height: 100%;
}
.box .bottom .left .search-wrap {
display: flex;
width: 100%;
height: 40px;
}
.box .bottom .left .search-wrap .search-box {
width: 100%;
height: 100%;
margin: 15px;
}
.box .bottom .left .search-box input {
width: 100%;
height: 100%;
padding: 0 15px;
}
<div class="box">
<div class="top"></div>
<div class="bottom">
<div class="left">
<div class="search-wrap">
<div class="search-box">
<input type="text" placeholder="searh" />
</div>
</div>
</div>
<div class="right"></div>
</div>
</div>
How do I solve it?
Add box-sizing: border-box; to include the padding in the 100% width.
body,
html {
height: 100%;
width: 100%;
margin: 0;
}
.box {
height: 100%;
width: 100%;
}
.box .top {
height: 30%;
width: 100%;
background-color: #cccccc;
}
.box .bottom {
background-color: #ffffff;
height: 70%;
width: 100%;
display: flex;
justify-content: space-between;
}
.box .bottom .left {
background-color: #f7f7f7;
border: solid 1px #cccccc;
border-radius: 8px;
margin-top: -100px;
margin-left: 50px;
width: 59.5%;
height: 100%;
}
.box .bottom .right {
background-color: #f7f7f7;
border: solid 1px #cccccc;
border-radius: 8px;
margin-top: -100px;
margin-left: 15px;
margin-right: 50px;
width: 40%;
height: 100%;
}
.box .bottom .left .search-wrap {
display: flex;
width: 100%;
height: 40px;
}
.box .bottom .left .search-wrap .search-box {
width: 100%;
height: 100%;
margin: 15px;
}
.box .bottom .left .search-box input {
width: 100%;
height: 100%;
padding: 0 15px;
box-sizing: border-box;
}
<div class="box">
<div class="top"></div>
<div class="bottom">
<div class="left">
<div class="search-wrap">
<div class="search-box">
<input type="text" placeholder="searh" />
</div>
</div>
</div>
<div class="right"></div>
</div>
</div>
Note: In most cases, it's useful to include a general rule for all elements with this setting, like this:
* {
box-sizing: border-box;
}