Setting "overflow: auto;" somehow magically fixes layout issues [duplicate] - html

This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Align inline-block DIVs to top of container element
(5 answers)
Closed 2 years ago.
I have a bunch of divs that I laid out next to each other (depending upon window size there are 2-4 per "row"). Inside these divs, I added some wrappers and such, but at the center of each div there is an image that only has its width (not height) property set via CSS. The odd thing that was happening was that images with a greater width than height in the original png caused the outermost div containing it to shift down. I searched for a solution for a while and found that setting overflow: auto; in the divs with class "item-card-wrapper" (see below) somehow magically fixed the layout. I thought I understood how overflow works, but I am so confused as to how it seemed to magically fix the issue (and this isn't the only instance in which this has happened for me).
The only "correct" div here is the one that sticks out from the rest. When I added overflow: auto; it was fixed:
Here is the HTML (I clipped it early because it just starts repeating itself for each "square"):
<div id="shop">
<div id="shop-center-wrapper">
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="static/products/Three Kings Glow.png">
</div>
</div>
</div>
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="static/products/Amor Azul.png">
</div>
</div>
</div>
...
And here are the relevant CSS classes:
#shop {
margin-top: 40px;
margin-left: 50px;
margin-right: 50px;
overflow: auto;
}
#shop-center-wrapper {
text-align: center;
margin: 0px auto;
}
.item-card-wrapper {
margin-top: 20px;
margin-bottom: 20px;
display: inline-block;
overflow: auto;
}
.item-card {
width: 92%;
margin-left: 4%;
margin-right: 4%;;
background-color: white;
}
.image-wrapper {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.image {
width: 90%;
margin-left: 5%;
margin-right: 5%;
}
I really just want to understand how overflow: auto; was able to solve all of my problems.

The key property here should be inline-block in .item-card-wrapper. Using the inline-block display, the component will be aligned to the baseline, which same like vertical-align: baseline;.
Through my small experiment, you can see 3 images are attached on the baseline. You can remove the overflow: auto; in my snippet to create the same effect.
By adding overflow:auto, the item-card-wrapper's content should be clipped (if it is larger than its parent) to maintain the same height with the parent, therefore all item-card-wrapper component should be same height at last.
You might have question about, your picture have the same size, which shouldn't have this effect. I am guessing background-color: white; in .item-card is covered your actual picture size. Try change the color and see if my assumption is correct or not.
Ps. If you want to search for alternative from inline-block, flexbox with flex-direction might be an another good option.
#shop {
margin-top: 40px;
margin-left: 50px;
margin-right: 50px;
overflow: auto;
background: green; /* Edited */
padding: 10px; /* Edited */
}
#shop-center-wrapper {
text-align: center;
margin: 0px auto;
}
.item-card {
width: 92%;
margin-left: 4%;
margin-right: 4%;;
background-color: white;
}
.item-card-wrapper {
margin-top: 20px;
margin-bottom: 20px;
display: inline-block;
overflow: auto; /* Edited */
}
.image-wrapper {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: yellow; /* Edited */
}
.image {
width: 90%;
margin-left: 5%;
margin-right: 5%;
}
<div id="shop">
<div id="shop-center-wrapper">
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="https://www.designhill.com/design-blog/wp-content/uploads/2015/02/McDonald-Logo-1.jpg">
</div>
</div>
</div>
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="https://d1.awsstatic.com/case-studies/600x400_mcdonalds_logo.58256463615a3353933179883a8c58f593a00880.png">
</div>
</div>
</div>
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="https://media-exp1.licdn.com/dms/image/C4E0BAQHWxquJ9PJxvw/company-logo_200_200/0?e=2159024400&v=beta&t=95WVdd_Q6vNKUybW3mX2odTGxRJ30bwKjF9SkeSH96w">
</div>
</div>
</div>
</div>
</div>

Related

Is there a way I can alling some "a" tags that are inside some boxes to a logo (vertical-align: middle) [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 4 months ago.
I am building my own personal web page, and while doing the "header" I wasn't able to align my "a" tags to the logo... I've been trying everything, but there are no solutions.
This is my code. If I am wrong in some part, please explain me to not commit the same mistake.
HTML
`
<div class="mainBox">
<!--Logo-->
<div class="navBox">
<img src="Images/Logo3Final.png" alt="logo" id="logo">
</div>
<div class="navBox">
/*About Me*/
</div>
<div class="navBox">
/*Contact*/
</div>
<div class="navBox">
/*Expertise*/
</div>
<div class="navBox">
/*Projects*/
</div>
</div>
`
CSS
`#logo{
max-width: 200px;
max-height: 220px;
right: -100px;
}
/*MENU*/
.mainBox{
position: relative;
display: flex;
width: 95%;
height: 25vh;
justify-content: space-evenly;
align-items: center;
}
.navBox{
padding-top: 25px;
padding-left: 10px;
padding-right: 10px;
width: 20%;
height: 20vh;
text-align: center;
top: 50%;
background: transparent;
}
.navBox a:hover{
padding-top: 50%;
background: transparent;
color: var(--text-color);
text-decoration: none;
}
a:visited, a:active, a:link{
text-decoration: none;
color: var(--text-color);
}
.navBox a{
vertical-align: middle;
color: var(--text-color);
padding-top: 0.5rem;
text-align: initial;
}
I try changing position values, I tried giving padding-top, top, margin, nesting into antoher div. But I can not achieve what I will like to be.
Here is a ScreenShot of my page.
You can align .navBox to the center, like this:
.navBox{
/* ... */
display: flex;
align-items: center;
}
or you can remove the height you've specified for .navBox which I think is redundant. It will look like this:
#logo{
max-width: 200px;
max-height: 220px;
}
.mainBox{
position: relative;
display: flex;
width: 95%;
height: 25vh;
justify-content: space-evenly;
align-items: center;
}
.navBox{
padding-top: 25px;
padding-left: 10px;
padding-right: 10px;
width: 20%;
text-align: center;
}
<div class="mainBox">
<!--Logo-->
<div class="navBox">
<img src="https://placekitten.com/96/140" alt="logo" id="logo">
</div>
<div class="navBox">
/*About Me*/
</div>
<div class="navBox">
/*Contact*/
</div>
<div class="navBox">
/*Expertise*/
</div>
<div class="navBox">
/*Projects*/
</div>
</div>
The issue is that your logo is overflowing its allocated size, as defined by the .navBox class.
This is what your header looks like as is:
This is what it looks like when applying overflow: hidden to the navBox class. As you can see, the logo is oversized for its container. You need to limit its height and width OR remove the limitation from the container.
Removing the height attribute from navBox makes it look like this:
Looks centered to me.

Divs do not auto size

I have an MVC web site and the Div's are not auto-sizing
I've set overflow to auto, I've set a minimum height
<div class="content-wrapper">
<div class="jetdocs-header">
<img src="~/Content/images/logo_header.png" />
</div>
<div class="content-section">
<div class="content-menu">
</div>
<div class="content-body">
<div style="width: 900px; height: 30px;">
</div>
#RenderBody()
</div>
</div>
</div>
here is the CSS
.content-wrapper {
background-image: url('images/top.gif');
background-repeat: repeat-x;
width: 1155px;
position: relative;
margin: 0px auto 0 auto;
background-color: whitesmoke;
border-radius: 5px;
margin-top: 5px;
padding-bottom: 35px;
min-height: 600px;
overflow: auto;
}
.content-section {
top: 5px;
position: relative;
background-color: whitesmoke;
overflow: auto;
}
.content-menu {
position: relative;
width: 140px;
min-height: 700px;
height: 100%;
background-color: silver;
float: left;
overflow: auto;
}
.content-body {
position: relative;
width: 1000px;
float: left;
left: 2px;
background-color: whitesmoke;
overflow: auto;
}
The Content-Menu should auto-size to the same height as the content-section. and both should cause the content-wrapper to auto-size to match the height of the largest of the two...
Don't think this c# related, but have you tried setting the height of the parent divs? I think even if you specify min-height, it still needs height for the overflow to work correct.
I think use should use the css3 flexbox properties on the '.content-section'.
.content-section
{
display: flex;
align-items: stretch;
}
.content-body
{
min-height: 600px;
}
The Content-Menu will get the size of the Content-Body (or invers if the Content-Menu is higher), and the Content-Section will auto-size to match the height of the childs. More info about align-items here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Center fit images of any size to parent div

I've got an image slider, the selected image is set as the main image above. All the images are not same size. How do I make sure that each image is centered in the parent div and the entire parent div?
I'm now doing it with:
width: 100%;height: 100%;object-fit:fill;
but then my image loses the bottom of its content. I would like to center the image vertically.
My parent div has following class style:
.wrapper_image{
max-width:100%;
max-height:100%;
min-height:100%;
min-width: 100%;
margin: 0 auto;
}
code:
<div class="o-grid__col u-12/12" style="width: 400px;
height: 400px;
position: relative;
display: inline-block;
overflow: hidden;
text-align: center;
margin: 0;">
<div class="mySlides wrapper_image_horses">
<img src='url' style="width: 100%;height: 100%;object-fit:fill;">
</div>
</div>
I suggest you use display: flex; to achieve this. With flex it is much easier for you to align you images to center vertically and horizontally. Below is a test code I have done. Hope it helps with your problem
HTML
<div class="d-flex h-100">
<div class="img-slide">
<img src="https://via.placeholder.com/150">
</div>
</div>
CSS
.d-flex {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.h-100 {
height: -webkit-fill-available;
}
JS Fiddle Link : https://jsfiddle.net/SJ_KIllshot/y5fzvox8/
Here is a modern solution, hope it will help you
DEMO
<div class="o-grid__col u-12/12" style="width: 400px;
height: 400px;
position: relative;
display: inline-block;
overflow: hidden;
text-align: center;
margin: 0;
border: 1px solid #000;">
<div class="mySlides wrapper_image_horses" style="position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);">
<img src='https://picsum.photos/id/159/200/200' style="width: auto;height: auto;">
</div>

Scroll-x as overflow not working - shows scrollbar for y instead

I'm trying to understand why I cannot scroll horizontally to see the other pink thumbs. I added an overflow of scroll-x to the thumbs container and thought it would allow me to scroll horizontally to see the other thumbs; instead, it only scrolls vertically.
Can someone explain why it doesn't scroll horizontally? thanks a million
#content-wrap {
background: lightgreen;
width: 300px;
height: 300px;
}
#main-image {
background: cyan;
width: 300px;
height: 250px;
float: left;
}
#thumbs-wrap {
background: orange;
width: 300px;
height: 50px;
float: left;
overflow-x: scroll;
}
.thumb {
background: pink;
box-sizing: border-box;
width: 75px;
height: 50px;
border: 2px solid grey;
float: left;
}
<div id="content-wrap">
<div id="main-image"></div>
<div id="thumbs-wrap">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
</div>
It's because your #thumbs-wrap has fixed width, and it's not enough to keep all child elements in one row or add horizontal scrollbar. As an easy solution, you can wrap all child elements inside another one div and give extra-width to it. Here is an example:
#content-wrap {
background: lightgreen;
width: 300px;
height: 300px;
}
#main-image {
background: cyan;
width: 300px;
height: 250px;
float: left;
}
#thumbs-wrap {
background: orange;
width: 300px;
height: 50px;
float: left;
overflow-x: scroll;
}
#thumbs-inner-wrap {
width: 1000px;
}
.thumb {
background: pink;
box-sizing: border-box;
width: 75px;
height: 50px;
border: 2px solid grey;
float: left;
}
<div id="content-wrap">
<div id="main-image"></div>
<div id="thumbs-wrap">
<div id="thumbs-inner-wrap">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
</div>
</div>
HTML elements will usually wrap to new lines when there is no more horizontal space. That's why you get a vertical scrollbar even if you set overflow-x: auto. You can use CSS flexbox to override this behavior without adding any more elements. Add the following to your CSS:
#thumbs-wrap {
display: flex;
flex-wrap: nowrap;
}
.thumb {
flex: 0 0 auto;
}
How does this work?
flex-wrap:nowrap makes sure we force the child elements to stay on one line, and not wrap to new lines.
Now usually this might make the flex items shrink or grow to fit the horizontal space of their parent element. We can control this by controlling their sizes.
The flex property with three values is shorthand for flex: flex-grow flex-shrink flex-basis.
With flex-grow and flex-shrink set to 0, the flex items are not allowed to either grow/expand or shrink to fit the space of the container.
flex-basis:auto makes sure the flex items are sized exactly the way we have already sized them in our CSS.
Together these rulesets forces the flex items (.thumb) to be aligned horizontally, not wrap to a second line and to remain their original size. This will force a horizontal scrollbar to appear on the #thumbs-wrap element.
You can see how this works out together with your code:
#content-wrap {
background: lightgreen;
width: 300px;
height: 300px;
}
#main-image {
background: cyan;
width: 300px;
height: 250px;
float: left;
}
#thumbs-wrap {
background: orange;
width: 300px;
height: 50px;
float: left;
overflow-x: scroll;
display: flex;
flex-wrap: nowrap;
}
.thumb {
flex: 0 0 auto;
background: pink;
box-sizing: border-box;
width: 75px;
height: 50px;
border: 2px solid grey;
float: left;
}
<div id="content-wrap">
<div id="main-image"></div>
<div id="thumbs-wrap">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
</div>

CSS - center and rounded Img Inside Div

I have an Img inside a Div on my webpage, the image appears inside the div, but the alignment is off depending on the browser width.
this is my code:
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; height: 100%;">
<div>
<img class="" src="../images/Joanne.jpg" alt="Chania" style="height: 80px; display: block; margin-left: auto; margin-right: auto;">
</div>
</div>
<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>
</div>
I was wondering if there was a way I could vertically and horizontally align the Image inside that Div, and maybe also have the image with rounded corners.
I have searched around but nothing I try works, I thought by setting the margin left and right to 'auto' would solve my problem, but the image is still off when I resize the browser.
Any help or advice is appreciated.
Demo:https://jsfiddle.net/jjxbm7j7/
For a rounded image - use the border-radius property
For a vertically centered image - Use display:flex and align-items:center on the parent element of the image that has a specified height.
For a horizontally centered image - display:flex and justify-content:center on the parent element of the image that has a specified width.
However, for your example, I used margin:auto, because it is actually simpler.
For more information for flex stuff, click here: http://www.w3schools.com/css/css3_flexbox.asp
It should look like this:
.image {
border-radius: 50px;
}
.w3-col.w3-container.w3-green {
display: flex;
align-items: center;
margin: auto;
}
.w3-row {
border: 1px solid #aaa;
}
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; height: 100%;">
<div>
<img class="image" src="http://s33.postimg.org/vnc0xbztb/Joanne.jpg
" alt="Chania" style="height: 80px; display: block; margin-left: auto; margin-right: auto;">
</div>
</div>
<!--<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>-->
</div>
I added the 1px border property for clarity.
Perfectly Center your Image inside a DIV using transform: scale()
I have created a box inside that i have placed your image to show how to center the image inside the div(class="box") .
body{
width: 100%;
height: 100vh;
margin: 0;
}
.w3-row {
display: flex; //flexBox
align-items: center;
justify-content: center;
flex-direction: column; //Works as a Stack i.e Image at the Top and Description at the bottom
}
For Scaling Image using CSS Transformations
.image{
width: 100%;
height: auto;
transform: scale(.5); //change the scale value to change size of the Image 0<scale<1
}
For Rounded Image
.img-rounded{
border-radius: 50%
}
Working Fiddle: https://jsfiddle.net/rittamdebnath/jjxbm7j7/3/
before anything it's not good approach to write inline CSS but anyway you can use vertical align way on an sudo element (after) like so
.image {
border-radius: 50px; //adjust yourself
display: inline-block;
vertical-align: middle;
}
.w3-col.w3-container.w3-green{
text-align:center;
height:100px;
border:1px solid;
width:100%;
}
.w3-col.w3-container.w3-green:after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; margin:auto;">
<img class="image" src=" http://s33.postimg.org/vnc0xbztb/Joanne.jpg" alt="Chania" style="height: 80px; ">
</div>
<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>
</div>