How to prevent overflowing of an element in css? - html

So I am trying to create a logo and a menu icon in the header but for some reason, they are always overflowing the height of the header which I have strictly specified! Why is that ?
And I know I can hide out the overflowing items by using overflow:hidden; property but it is not always a good case.
For example, I tried to create a hamburger icon but I could not because of this overflow issue. The menu lines were working as if the entire element is shown but I had to hide it out so that it could fit into the header.
Here is the code -
<header>
<div class="logo">
Elvis
</div>
<div class="menu">
Hamburger Menu
</div>
</header>
In CSS -
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
border: 2px solid red;
}
.logo {
font-size: 33px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
color: white;
}
.menu {
height: 100px;
width: 100px;
background-color: #bd4439;
}
Here is the codepen link -
https://codepen.io/raghav-sharma333/pen/eYeZYGO
Here is the image of the issue -
Overflowing content
So I just want to know :
Why is it happening?
&
How can it be prevented?

Basically you are forcing your elements to be higher than the header itself by giving them static heights (height 100px on the menu and padding-top/bottom 30px on the logo)
I updated your pen: https://codepen.io/penmasterx/pen/wvPGaGz
Using height 100%, so the elements adapt to the header.
Let me know if this solves your problem. If not, let me know in more detail what you're trying to accomplish.
What I added to the pen:
.logo {
height: 100%;
display: flex;
align-items: center;
/* removed padding top/bottom */
}
.menu {
height: 100%;
}

In such cases, it is better to use the position to manage the inheritance of the elements
I modified your code:
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
align-items: center;
border: 2px solid red;
position: relative;
}
.wrapper{
display: flex;
justify-content: space-between;
width: 100%;
height: 100%;
position: absolute;
}
.logo {
font-size: 30px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
max-height: 100%;
color: white;
}
.menu {
height: 100%;
width: 100px;
background-color: #bd4439;
}
<header>
<div class="wrapper">
<div class="logo">Elvis</div>
<div class="menu">Hamburger Menu</div>
</div>
</header>

First: the reason you use a 33px font which adds padding, then you use a height:100px on the menu while on your header you put a height:60px
you also need to add align-self: center on your flex-box
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
align-self: center;
border: 2px solid red;
}
.logo {
font-size: 17px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
color: white;
}
.menu {
height: 60px;
width: 100px;
background-color: #bd4439;
}

I did it like 'Ali Memar' answer but the difference is the position of the texts. they are now in the middle of the div.
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
align-items: center;
border: 2px solid red;
position: relative;
}
.wrapper{
display: flex;
justify-content: space-between;
width: 100%;
height: 100%;
position: absolute;
}
.logo {
font-size: 30px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
max-height: 100%;
color: white;
text-align: center;
display: flex;
align-items: center
}
.menu {
height: 100%;
width: 100px;
background-color: #bd4439;
text-align: center;
display: flex;
align-items: center
}
<header>
<div class="wrapper">
<div class="logo">Elvis</div>
<div class="menu">Hamburger Menu</div>
</div>
</header>

Related

image/content not contained inside div

I'm trying to contain an image inside the first div of this page and it's outside of it for some reason. All the assets are in a flexbox. It works completely fine when I put text inside that div but not an image.
I've circled in red which image and div I'm talking about.
The code snippet won't show you what I'm talking about since it isn't the full code.
.landing {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.introWrapper {
width: 1000px;
}
.intro1 {
font-size: 25px;
text-align: center;
font-family: 'Yapari';
align-items: center;
justify-content: center;
margin-top: 23px;
}
.intro2 {
font-size: 25px;
text-align: center;
font-family: 'Yapari';
/* font-weight: 72; */
margin-top: 50px;
}
#introCircle {
border: 2px solid black;
border-radius: 50%;
width: 230px;
height: 70px;
margin: auto;
}
.icons {
border: 2px solid black;
height: 50px;
}
.icon1 {
width: 30px;
}
<div id='landing' className={styles.landing}>
<div className={styles.introWrapper}>
<div className={styles.icons}>
<img src={icon1} className={styles.icon1}/>
</div>
<div id={styles.introCircle}>
<h1 className={styles.intro1}>HELLO I'M</h1>
</div>
<LastName />
<h1 className={styles.intro2}>A FULL STACK DEVELOPER</h1>
</div>
</div>
From what I understand you have provided a height to the div "icons" . change the height to min-height
.icons {
border: 2px solid black;
min-height: 50px;
}

images and buttons are not horizontally aligned

I have met some problems while doing a image-viewer project. The problem is that my buttons and the image are not following justify-content property, which they don't distributed equally inside my div block, how could it be solved? Also the image is not centered as the title does despite I set the align item property. I dow know how to fix that. I've searched over the website for solutions but none of them seems working.
Could anyone help me, please? Thanks in advance.
Here are the html and css code:
<div class="image-viewer__container">
<div class="image-viewer__title">Image Viewer</div>
<div class="image-viewer__main">
<div class="image-viewer__button"><img src="./images/back.png" id="previous" /></div>
<div class="image-viewer__display" style="background-image: url(./images/loading.gif);background-repeat: no-repeat; background-position: center;">
<img src="https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF" id="display">
<div class="image-viewer__display-source-wrapper">
<span><a href="https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF" target="_blank">
https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF</a>
</span>
</div>
</div>
<div class="image-viewer__button"><img src="./images/next.png" id="next" /></div>
</div>
</div>
.image-viewer__container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-evenly;
}
.image-viewer__title {
font-size: 5rem;
font-weight: 600;
color: #615dec;
margin: 0;
margin-top: 2rem;
}
.image-viewer__main {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
margin: auto;
}
.image-viewer__button {
display: inline;
background: none;
border: none;
border-radius: 50%;
}
.image-viewer__button img {
width: 80px;
height: 80px;
border: 1px solid transparent;
border-radius: 50%;
cursor: pointer;
}
.image-viewer__display {
position: relative;
padding: 15px;
margin: 3rem;
max-width: 80rem;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
font-size: 0.6rem;
}
.image-viewer__display-source-wrapper {
position: absolute;
font-size: 12px;
left: 50%;
margin-right: 50%;
transform: translate(-50%, -50%);
min-width: 100em;
text-align: center;
bottom: 0;
}
#display {
object-fit: contain;
width: 50rem;
height: 30rem;
margin-bottom: 1rem;
}
#source {
display: inline;
color: black;
}
This is because you've set a fixed width to your image. By setting the main image to 100% the image will fit and fill up the remaining space so the 3 elements are always distributed equally.
main image size = full width - both your arrows
current
#display {
object-fit: contain;
width: 50rem; /*fixed width*/
height: 30rem; /*fixed width*/
margin-bottom: 1rem;
}
amended
#display {
margin-bottom: 1rem;
width: 100%; /*was added*/
height: auto; /*was added*/
}
jsFiddle
Add css float:"right" in css button.

getting images and Divs to be the same size with flexbox

I know this is probably a basic question but so far I have not been able to resolve it via google:
I want to have a navbar with an Image (a logo) and 3 links on its right. I want them all to have the same height and to be on the same height, a little bit like this:
however all I manage is to make it look like this:
#nav-bar {
position: fixed;
top: 0px;
width: 100%;
background: #b7b7b7;
border: 1px #4c4c4c solid;
padding: 1em;
}
#flex {
display: flex;
justify-content: start;
flex-wrap: wrap;
align-content: center;
}
img {
margin-top: 20px;
height: auto;
width: 15%;
}
.nav-link {
border: 1px solid #4c4c4c;
padding: 0.5em;
background-color: #b7b7b7;
color: black;
text-decoration: none;
}
<nav id="nav-bar">
<div id="flex">
<div><img src="https://i.pinimg.com/originals/58/c8/82/58c88275c1a3389a7260baf05bf34e9a.jpg" alt="violin logo" id="header-img"></div>
Products
Demo
Newsletter
</div>
</nav>
Maybe something like this:
#nav-bar {
position: fixed;
top: 0px;
width: 100%;
background: #b7b7b7;
border: 1px #4c4c4c solid;
padding: 1em;
}
#flex{
display: flex;
justify-content: space-between;
flex-wrap: wrap;
align-content: center;
}
img {
height: 50px;
width: auto;
}
.nav-links {
padding-right: 50px;
}
.nav-links .nav-link {
border: 1px solid #4c4c4c;
background-color: #b7b7b7;
color: black;
height: 50px;
display: inline-block;
text-decoration: none;
}
<nav id="nav-bar">
<div id="flex">
<img src="https://i.pinimg.com/originals/58/c8/82/58c88275c1a3389a7260baf05bf34e9a.jpg" alt="violin logo" id="header-img">
<div class="nav-links">
Products
Demo
Newsletter
</div>
</div>
</nav>
Just wrap links into a div, then set justify-content to 'space-between' and set the same height of both image and div with links. Hope it will help
You almost did it, the only thing you needed was basically to size the <div> around the <img> appropriately, so the <img> could just fill it:
#nav-bar {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: #b7b7b7;
border: 1px #4c4c4c solid;
padding: 1em;
box-sizing: border-box; /* to exclude paddings&borders from width instead of adding them */
}
#flex{
display: flex;
justify-content: start;
align-items: stretch;
}
/* sizing the div with the img */
#flex > div {
width: 15%;
flex: 1 0 auto;
}
/* making the img filling this div */
img{
display: block;
width: 100%;
}
.nav-link{
border: 1px solid #4c4c4c;
padding: 0.5em;
background-color: #b7b7b7;
color: black;
text-decoration: none;
/* making links fill all the space, except some gaps between them */
flex: 1 1 auto;
margin-left: .5em;
/* centering the text in the links and making it responsive */
display: flex;
justify-content: center;
align-items: center;
font-size: calc(10px + 2vw);
}
<nav id="nav-bar">
<div id="flex">
<div>
<img src="https://i.pinimg.com/originals/58/c8/82/58c88275c1a3389a7260baf05bf34e9a.jpg" alt="violin logo" id="header-img">
</div>
Products
Demo
Newsletter
</div>
</nav>

How can i center image in any div in HTML & CSS?

How can i center image in any div in HTML & CSS ?
I have a nav bar that uses images as links for other pages, the nav bar is multiple divs each has an image and anther div contains a words, how can I center this img in the div
The CSS:
#Nav
{
height: 150px;
margin-top: 50px;
}
#Nav ul
{
list-style-type: none;
padding: 50px;
padding-top: 0px;
width: 100%;
}
#Nav li
{
float: left;
font-size: 12px;
font-weight: bold;
letter-spacing: 0;
margin: 30px;
}
#Nav a
{
display: inline-block;
padding: 5px;
width: 70px;
height: 100px;
color: black;
text-decoration: none;
}
#Nav a:hover
{
border: 2px solid #ddd;
border-radius: 4px;
box-shadow: 0 0 2px 1px rgba (0, 140, 186, 0.5);
}
#img img
{
align-self: middle;
width: 50px;
height: 50px;
}
#desc
{
margin-top: 15px;
text-align: center;
}
The html:
<li> <a target="_blank" href="#">
<div id="img"> <img src="C:/Users/hp1/Desktop/Website/Pictures/Accessories.jpg" alt="Accessories">
<div id="desc"> Accessories </div>
</div>
</a> </li>
You need to change your CSS for image like below:-
#img img
{
display: block;
margin: 0 auto;
}
You can see Many Demos Here-
Note that align-self is for elements inside a flexbox container which is not your case, you can try with text-align: center; on img.
Or if you wish to go full flexbox, set the img container to:
.containerClass {
display: flex;
flex-flow: column wrap;
justify-content: center;
}
With this setup align-self on the img is not need since justify-content on it's container will do.
Flexbox to the rescue:
.parent {
width:200px; height:200px;
border:2px solid #000;
display: flex; /* Make it flex */
flex-direction: column; /* wrap children elements to columns */
align-items: center; /* Center children horizontally */
justify-content: center; /* Center children vertically */
}
<div class="parent ">
<img src="http://placehold.it/100x100/cf5">
<p>Green-ish</p>
</div>
<div class="parent ">
<img src="http://placehold.it/100x100/5fc">
<p>Blue-ish</p>
</div>
align-self: center; is valid, align-self: middle; is not valid.
However, you can probably achieve what you're wanting with text-align: center;. Despite it being an image, you can still center using text-align as images are, by default, inline elements just like text.
#img img
{
align-self: center;
width: 50px;
height: 50px;
}
OR
#img img
{
text-align: center;
width: 50px;
height: 50px;
}
First you have to change the nature of div to table then make its align to center like this
#img img
{
display:table;
align: center;
width: 50px;
height: 50px;
}

How to make a rectangle centered AND responsive with Bootstrap?

I'm trying to make a board-game adoption for web.
I want to use Bootstrap to make the elements responsive.
The main element of the game is a rectangle (the game board). This shall appear centered in all display-sizes and with a bit of margin to all sides.
Which attributes and CSS-rules do I have to apply?
Shall I use a normal container or container-fluid?
Would it be enough to make one column within the container / row and give it a class of "col-xs-12"?
As far as I know this would be applied to all devices beginning from the smallest to the largest upwards.
What I have tried so far:
html, body {
height: 100%;
}
.container {
margin: 10px auto;
height: 100%;
display: flex;
justify-content: center;
background-color: #ababab;
}
.row {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.board {
margin: 10px;
height: 400px;
width: calc(100% - 40px);
background-color: teal;
border: 1px solid black;
font-size: 2.5em;
font-weight: bold;
color: white;
text-align: center;
line-height: 400px;
}
<div class="container">
<div class="row">
<div class="col-xs-12 board">The game-board</div>
</div>
</div>
Here you can see an example : you dont need col-xs-12 . You have only to set contaier-fluid in the parent and some padding in div wrapper with box-sizing:border-box . Row is for reset the standard padding of container and col classes with negative margin. It's your decision if you want to .
html,body{height:100%;margin:0;padding:0;}
.container-fluid,.row{height:100%;background-color:grey}
.board-container{padding:40px;box-sizing:border-box}
.board{background-color:teal;height:100%;padding:40px;}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div class="container-fluid">
<div class="row board-container">
<div class="board">Board Game</div>
</div>
</div>
html, body {
height: 100%;
}
.container {
margin: 10px auto;
height: 100%;
display: flex;
justify-content: center;
background-color: #ababab;
}
.row {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.board {
margin: 10px;
height: calc(100% - 40px);;
width: calc(100% - 40px);
background-color: teal;
border: 1px solid black;
font-size: 2.5em;
font-weight: bold;
color: white;
text-align: center;
}
<div class="container">
<div class="row">
<div class="col-xs-12 board">The game-board</div>
</div>
</div>
html,body{
height:100%;
background:#FF3366;
}
.container {
width:100%;
height:100%;
}
.row{
height:100%;
margin:30px 30px 30px 30px;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
background-color: teal;
border: 1px solid black;
font-size: 2.5em;
font-weight: bold;
color: white;
}
<div class="container">
<div class="row">
<div class="col-xs-12">The game-board</div>
</div>
</div>
Try this one.