Why are my heading elements overlapping on small screens? - html

I am new to frontend dev. When I reduce the browser window size the layout breaks. Elements overlap each other and height of section changes etc.
Here is my HTML code in which I have made a navbar and a section in which I created 3 items.
#import url('https://fonts.googleapis.com/css2?family=Dancing+Script&family=Lobster&display=swap');
#import url('https://fonts.googleapis.com/css2?family=Dancing+Script&family=Lobster&family=Varela+Round&display=swap');
* {
margin: 0;
padding: 0;
}
ul li {
list-style-type: none;
text-decoration: none;
}
#navbar {
display: flex;
align-items: center;
position: relative;
top: 0;
/* justify-content: center; */
}
#navbar::before {
content: "";
background-color: black;
position: absolute;
height: 100%;
width: 100%;
z-index: -1;
opacity: 0.4;
}
/* Logo */
#logo {
margin: 10px 34px;
}
/* Logo and Image */
#logo img {
margin: 3px 6px;
height: 56px;
}
#navbar ul {
display: flex;
font-size: 1.3rem;
}
#navbar ul li a {
text-decoration: none;
display: block;
padding: 3px 23px;
border-radius: 20px;
color: white;
}
#navbar ul li a:hover {
background-color: white;
color: black;
}
/* Home section */
#home {
display: flex;
flex-direction: column;
padding: 3px 200px;
height: 278px;
justify-content: center;
align-items: center;
font-family: 'Lobster', cursive;
}
#home::before {
content: "";
background: url('../bg1.jpg') no-repeat center center/cover;
position: absolute;
height: 56%;
width: 100%;
z-index: -1;
opacity: 0.89;
top: 0px;
left: 0px;
}
#home h1 {
color: white;
text-align: center;
}
#home p {
color: white;
text-align: center;
font-size: 1.5rem;
}
.h-primary {
font-size: 3.8rem;
padding: 12px;
font-family: 'Varela Round', sans-serif;
}
.h-secondary {
font-size: 2.2rem;
padding: 12px;
font-family: 'Varela Round', sans-serif;
}
.h-tertiary {
font-size: 0.9rem;
padding: 12px;
font-family: 'Varela Round', sans-serif;
}
#services {
margin: 110px 0;
margin-left: 20px;
margin-right: 20px;
display: flex;
}
#services .box {
border: 2px solid brown;
padding: 20px;
margin: 3px 6px;
border-radius: 20px;
background-color: rgb(236, 229, 229);
}
#services .box img {
display: block;
height: 171px;
margin: auto;
}
.btn {
padding: 6px 20px;
border: 2px solid white;
background-color: brown;
color: white;
margin: 17px;
font-size: 1.5rem;
border-radius: 10px;
cursor: pointer;
}
.center {
text-align: center;
}
<nav id="navbar">
<div id="logo">
<img src="logo.png" alt="">
</div>
<ul>
<li class="items">Home</li>
<li class="items">Service</li>
<li class="items">About</li>
<li class="items">Contact</li>
</ul>
</nav>
<section id="home">
<h1 class="h-primary">Welcome to Myonline meal</h1>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Voluptatem aperiam quaerat cumque consequatur quasi
</p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Debitis consequatur </p>
<button class="btn">Order Now</button>
</section>
<section class="services-container">
<h1 class="h-primary center">Our Service</h1>
<div id="services">
<div class="box">
<img src="img/1.png" alt="">
<h2 class="h-secondary center">Food Ordering</h2>
<p class="center h-tertiary">Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium quia atque, corrupti</p>
</div>
<div class="box">
<img src="img/2.png" alt="">
<h2 class="h-secondary center">Food Ordering</h2>
<p class="center h-tertiary">Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium quia atque, corruptir</p>
</div>
<div class="box">
<img src="img/3.png" alt="">
<h2 class="h-secondary center">Food Ordering</h2>
<p class="center h-tertiary">Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium quia atque, corruptir</p>
</div>
</div>
</section>

You should learn about responsiveness, when you make a website, it usually looks different on different screen sizes and devices, and in order to make it look better on smaller screens, you need to style your page differently according to the device's size, for example using media queries.
In your case you mostly use px unit to set sizes for margins, paddings and such in your css, but there are better units that are described as relative, such as %, em and rem.
You can learn more about all this just googling or watching tutorials, but in short, percentage unit is relative to the parent's size, em is relative to this element's font-size, and rem is relative to the root font-size (html tag) which is usually 16px, so for example if you set a font-size on a div of 3rem, that would mean 3 * 16px, which is 48px, but the difference between saying 3rem and just 48px, is that in some cases 3rem might become smaller or larger depending on the device or any other factor.
div {
display: flex;
flex-direction: column;
align-items: start;
margin: 1em 0;
}
.small button {
font-size: 16px;
}
.big button {
font-size: 48px;
}
.non-relative {
padding: 8px 16px;
}
.relative {
padding: 0.5em 1em;
}
<div class="small">
<button class="non-relative">Non-Relative</button>
<button class="relative">Relative</button>
</div>
<div class="big">
<button class="non-relative">Non-Relative</button>
<button class="relative">Relative</button>
</div>
Here you can see that, when I set the button's padding with px, it stays the same no matter the font size, but with the em unit, it will be relative to it, so proportionally it will be the same as the smaller version. Hope this is a good enough example, I am kind of sleepy and might've missed something, but hope this helps you!

Related

How to horizontally align rectangulars when they contain different amount of text

I've got a problem with aligning 1st rectangular with other two, apparently because it contains less amount of a text. As visible in css, I've set exact dimensions for width and height of rectangulars. Texts will be hyperlinks to different URLs. See screenshot below
(https://i.stack.imgur.com/Vyg8v.png)
.quote {
text-align: center;
margin-bottom: 30px;
}
.quote #quote-1,
#quote-2 {
font-size: large;
text-align: center;
width: 348.641px;
height: 84px;
border: 1px solid black;
display: inline-block;
padding: 30px;
margin-left: 15px;
margin-right: 15px;
margin-bottom: 0px;
}
.quote #quote-3 {
font-size: large;
text-align: center;
width: 348.641px;
height: 84px;
border: 1px solid black;
display: inline-block;
padding: 30px;
margin-left: 15px;
margin-right: 15px;
margin-bottom: 0px;
}
.quote a:link,
a:visited {
color: #000000;
background-color: transparent;
text-decoration: none;
}
.quote a:hover {
color: #6200ff;
background-color: transparent;
text-decoration: underline;
}
<main>
<div class="quote">
<div class="quote" id="quote-1">
<a href="https://www.youtube.com/watch?v=ONh37dDMJYk" target="_blank">
<i>"Nobody has done anything without trying,<br />
you have to try."</i>
</a>
</div>
<div class="quote" id="quote-2">
<a href="https://youtube.com/shorts/ExAkkAbwLHY?feature=share" target="_blank"><i
>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione
animi consequuntur sint</i>
</a>
</div>
<div class="quote" id="quote-3">
<a href="https://youtube.com/shorts/YAelHsS0p38?feature=share" target="_blank">
<i>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione
animi consequuntur sint
</i>
</a>
</div>
</div>
</main>
I´ve tried to set different display values, but still got no clue.
This is where display: flex comes in handy, specifically it's alignment capabilities
Update your .quote css to this:
.quote {
text-align: center;
margin-bottom: 30px;
display: flex;
align-items: center;
justify-content: center;
}
i fixed your code, just copy this and everything will work fine, i fixed content to be centered in the .quote container:
.quote {
text-align: center;
margin-bottom: 30px;
display: flex;
align-items: center;
justify-content: center;
}
.quote #quote-1,
#quote-2 {
font-size: large;
text-align: center;
width: 348.641px;
height: 84px;
border: 1px solid black;
padding: 30px;
margin-left: 15px;
margin-right: 15px;
margin-bottom: 0px;
}
.quote #quote-3 {
font-size: large;
text-align: center;
width: 348.641px;
height: 84px;
border: 1px solid black;
padding: 30px;
margin-left: 15px;
margin-right: 15px;
margin-bottom: 0px;
}
.quote a:link,
a:visited {
color: #000000;
background-color: transparent;
text-decoration: none;
}
.quote a:hover {
color: #6200ff;
background-color: transparent;
text-decoration: underline;
}
Flex is good but it's always worthwhile understanding why your first div was lower in the example you gave. Inline divs are, by default, aligned to the text baseline. As you can see from your screenshot, the bottom rows of text are all at the same height. The div has been moved down to ensure the text baseline across all three divs are at the same height.
To solve this, if you've fixed the height of the divs then just add vertical-align: top to your divs. See marked-up code below
/* added this class so you can see it's the children divs that need the vertical-alignment property set */
.vertical-top {
vertical-align:top;
}
.quote {
text-align: center;
margin-bottom: 30px;
}
.quote #quote-1,
#quote-2 {
font-size: large;
text-align: center;
width: 348.641px;
height: 84px;
border: 1px solid black;
display: inline-block;
padding: 30px;
margin-left: 15px;
margin-right: 15px;
margin-bottom: 0px;
}
.quote #quote-3 {
font-size: large;
text-align: center;
width: 348.641px;
height: 84px;
border: 1px solid black;
display: inline-block;
padding: 30px;
margin-left: 15px;
margin-right: 15px;
margin-bottom: 0px;
}
.quote a:link,
a:visited {
color: #000000;
background-color: transparent;
text-decoration: none;
}
.quote a:hover {
color: #6200ff;
background-color: transparent;
text-decoration: underline;
}
<main>
<div class="quote">
<div class="quote vertical-top" id="quote-1"><!-- added class vertical-top -->
<a href="https://www.youtube.com/watch?v=ONh37dDMJYk" target="_blank">
<i>"Nobody has done anything without trying,<br />
you have to try."</i>
</a>
</div>
<div class="quote vertical-top" id="quote-2"><!-- added class vertical-top -->
<a href="https://youtube.com/shorts/ExAkkAbwLHY?feature=share" target="_blank"><i
>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione
animi consequuntur sint</i>
</a>
</div>
<div class="quote vertical-top" id="quote-3"><!-- added class vertical-top -->
<a href="https://youtube.com/shorts/YAelHsS0p38?feature=share" target="_blank">
<i>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione
animi consequuntur sint
</i>
</a>
</div>
</div>
</main>

How to stop the overlay to cover the content and make it cover only the background?

I am using ::before as an overlay, but I face a problem where the overlay cover all the card including the content (texts), but I just want to cover the background of the card. I can't select any text because of this problem.
/* left-to-right effect box */
.ltr-effect {
position: relative;
}
.ltr-effect::before {
content: "";
display: block;
position: absolute;
top: 0px;
left: 0px;
width: 0%;
height: 100%;
background-color:rgba(215, 227, 250, 0.53);
transition: width 0.5s;
}
.ltr-effect:hover::before {
width: 100%;
}
/* card */
.service-content-box {
text-align: center;
border: 2px solid rgb(208, 228, 228);
background-color: #22beff;
border-radius: 10px;
padding: 30px;
font-size: 1.3em;
width: calc(90%/3);
margin-right: 5%;
position: relative;
}
/* content of services */
.service-content-box .service-title {
text-transform: capitalize;
margin-bottom: 10px;
font-size: 2em;
}
.service-content-box .service-desribtion {
line-height: 30px;
}
.service-content-box a {
margin: 7px 0px;
text-decoration: none;
text-transform: capitalize;
}
<section class="services">
<div class="services-content">
<div class="service-content-box ltr-effect">
<i class="fa fa-solid fa-book-open"></i>
<h4 class="service-title mg-b-10">fast reading</h4>
<p class="service-desribtion">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. A fugiat, ea et iure similique nesciunt Et accusantium magni
</p>
learn more
</div>
<div class="clear"></div>
</div><!-- /.services-content -->
</section><!-- /.services -->
You coud set a position: relative; z-index: 1; to elements in the card that you don't wanna cover with the overlay.
You need z-index to make them came up front, and as you may already know, z-index works only with non static positions. That's why I'm using that relative position.
/* left-to-right effect box */
.ltr-effect {
position: relative;
}
.ltr-effect::before {
content: "";
display: block;
position: absolute;
top: 0px;
left: 0px;
width: 0%;
height: 100%;
background-color: rgba(215, 227, 250, 0.53);
transition: width 0.5s;
}
.ltr-effect:hover::before {
width: 100%;
}
/* card */
.service-content-box {
text-align: center;
border: 2px solid rgb(208, 228, 228);
background-color: #22beff;
border-radius: 10px;
padding: 30px;
font-size: 1.3em;
width: calc(90% / 3);
margin-right: 5%;
position: relative;
}
/* content of services */
.service-content-box .service-title {
text-transform: capitalize;
margin-bottom: 10px;
font-size: 2em;
position:relative;
z-index:1;
}
.service-content-box .service-desribtion {
line-height: 30px;
position:relative;
z-index:1;
}
.service-content-box a {
margin: 7px 0px;
text-decoration: none;
text-transform: capitalize;
position:relative;
z-index:1;
}
<section class="services">
<div class="services-content">
<div class="service-content-box ltr-effect">
<i class="fa fa-solid fa-book-open"></i>
<h4 class="service-title mg-b-10">fast reading</h4>
<p class="service-desribtion">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. A fugiat, ea et iure similique nesciunt Et accusantium magni
</p>
learn more
</div>
<div class="clear"></div>
</div>
</section>

How can i make my page fullscreen and stretch it till the end

I'm relatively new to this, so someone help me understand how can I make my page content stretch to fill all the space and not just align in the center? I want to make it fullscreen. In which part of my code do I need changes?
#import url('https://fonts.googleapis.com/css?family=Roboto+Condensed');
body {
font-family: 'Roboto Condensed', sans-serif;
margin: 0;
background: rgb(255, 255, 255);
height: auto;
}
html {
scroll-behavior: smooth;
}
h1 {
font-weight: 400;
font-size: 2.5rem;
text-transform: uppercase;
margin: 0;
}
h2 {
font-weight: 400;
font-size: 1.2rem;
text-transform: capitalize;
margin: 0;
}
img {
display: block;
width: 100%;
}
main {
max-width: 900px;
margin: auto;
box-shadow: 30px 0px 40px rgba(0, 0, 0, 0.1), -30px 0px 40px rgba(0, 0, 0, 0.1);
}
#landing {
background: #fff;
}
#landing-text {
display: flex;
flex: 0 1 40vw;
height: 50vh;
justify-content: center;
align-items: center;
text-align: center;
padding-right: 1rem;
padding-left: 1rem;
}
#landing-text h2 {
color: #888;
}
#landing-image {
background: url(https://source.unsplash.com/dS-q7-zkD9c);
background-position: center;
background-size: cover;
background-repeat: no-repeat;
height: 50vh;
flex: 0 1 60vw;
margin: 0;
}
.btn {
padding: 0.5rem 2rem;
border: 1px #ccc solid;
display: inline-block;
margin: 2rem 0 0;
border-radius: 50px;
text-decoration: none;
color: #333;
transition: background 500ms ease;
}
.btn:hover {
background: #000000;
color: #fff;
transform: scale(1.11);
transition: all 0.6s;
}
#header {
padding: 1.5rem;
text-align: center;
background: #333;
color: #fff;
}
#header h2 {
border-left: dotted 1px #fff;
border-right: dotted 1px #fff;
display: inline-block;
padding-right: 1rem;
padding-left: 1rem;
}
.caption {
padding: 0.8rem;
text-align: center;
}
footer {
text-align: center;
padding: 2rem 1rem;
margin: auto;
color: #333;
}
footer h3 {
font-size: 3rem;
margin-bottom: 0;
}
.social-part .fa {
padding-right: 20px;
}
ul li a {
margin-right: 20px;
}
/* Screen Sizes 500px and Up */
#media (min-width: 500px) {
#landing {
display: flex;
height: 100%;
}
#landing-text {
height: 100vh;
}
#landing-image {
height: 100vh;
}
}
/* Screen Sizes 700px and Up */
#media (min-width: 700px) {
.btn {
padding: 1rem 3rem;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles.css">
<title>Acme Photography</title>
</head>
<body>
<main>
<!-- Landing Area -->
<div id="landing">
<div id="landing-text">
<div id="landing-text-inner">
<h1>Acme Photography</h1>
<h2>Beautiful Natural Photography</h2>
<a href="#images" class="btn" id="view-work">
View Work
</a>
</div>
</div>
<div id="landing-image"></div>
</div>
<div id="images">
<div id="header">
<h2>Our Work</h2>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,water" alt="">
<div class="caption">
<h3>Photo One</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,trees" alt="">
<div class="caption">
<h3>Photo Two</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,flowers" alt="">
<div class="caption">
<h3>Photo Three</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,animals" alt="">
<div class="caption">
<h3>Photo Four</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,clouds" alt="">
<div class="caption">
<h3>Photo Five</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
</div>
</main>
<footer>
<h3>Get In Touch</h3>
<p>Email or call us to set up a consult</p>
<p>Email:
<strong>contact#acmephotos.test</strong>
</p>
<p>Phone:
<strong>(617) 555-5555</strong>
</p>
</footer>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="main.js"></script>
</body>
</html>
Remove max-with on main
main {
/*max-width: 900px;*/
margin: auto;
box-shadow: 30px 0px 40px rgba(0, 0, 0, 0.1), -30px 0px 40px rgba(0, 0, 0, 0.1);
}
#import url('https://fonts.googleapis.com/css?family=Roboto+Condensed');
body {
font-family: 'Roboto Condensed', sans-serif;
margin: 0;
background: rgb(255, 255, 255);
height: auto;
}
html {
scroll-behavior: smooth;
}
h1 {
font-weight: 400;
font-size: 2.5rem;
text-transform: uppercase;
margin: 0;
}
h2 {
font-weight: 400;
font-size: 1.2rem;
text-transform: capitalize;
margin: 0;
}
img {
display: block;
width: 100%;
}
main {
/*max-width: 900px;*/
margin: auto;
box-shadow: 30px 0px 40px rgba(0, 0, 0, 0.1), -30px 0px 40px rgba(0, 0, 0, 0.1);
}
#landing {
background: #fff;
}
#landing-text {
display: flex;
flex: 0 1 40vw;
height: 50vh;
justify-content: center;
align-items: center;
text-align: center;
padding-right: 1rem;
padding-left: 1rem;
}
#landing-text h2 {
color: #888;
}
#landing-image {
background: url(https://source.unsplash.com/dS-q7-zkD9c);
background-position: center;
background-size: cover;
background-repeat: no-repeat;
height: 50vh;
flex: 0 1 60vw;
margin: 0;
}
.btn {
padding: 0.5rem 2rem;
border: 1px #ccc solid;
display: inline-block;
margin: 2rem 0 0;
border-radius: 50px;
text-decoration: none;
color: #333;
transition: background 500ms ease;
}
.btn:hover {
background: #000000;
color: #fff;
transform: scale(1.11);
transition: all 0.6s;
}
#header {
padding: 1.5rem;
text-align: center;
background: #333;
color: #fff;
}
#header h2 {
border-left: dotted 1px #fff;
border-right: dotted 1px #fff;
display: inline-block;
padding-right: 1rem;
padding-left: 1rem;
}
.caption {
padding: 0.8rem;
text-align: center;
}
footer {
text-align: center;
padding: 2rem 1rem;
margin: auto;
color: #333;
}
footer h3 {
font-size: 3rem;
margin-bottom: 0;
}
.social-part .fa {
padding-right: 20px;
}
ul li a {
margin-right: 20px;
}
/* Screen Sizes 500px and Up */
#media (min-width: 500px) {
#landing {
display: flex;
height: 100%;
}
#landing-text {
height: 100vh;
}
#landing-image {
height: 100vh;
}
}
/* Screen Sizes 700px and Up */
#media (min-width: 700px) {
.btn {
padding: 1rem 3rem;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles.css">
<title>Acme Photography</title>
</head>
<body>
<main>
<!-- Landing Area -->
<div id="landing">
<div id="landing-text">
<div id="landing-text-inner">
<h1>Acme Photography</h1>
<h2>Beautiful Natural Photography</h2>
<a href="#images" class="btn" id="view-work">
View Work
</a>
</div>
</div>
<div id="landing-image"></div>
</div>
<div id="images">
<div id="header">
<h2>Our Work</h2>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,water" alt="">
<div class="caption">
<h3>Photo One</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,trees" alt="">
<div class="caption">
<h3>Photo Two</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,flowers" alt="">
<div class="caption">
<h3>Photo Three</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,animals" alt="">
<div class="caption">
<h3>Photo Four</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
<img src="https://source.unsplash.com/1600x900/?nature,clouds" alt="">
<div class="caption">
<h3>Photo Five</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo, tenetur.</p>
</div>
</div>
</main>
<footer>
<h3>Get In Touch</h3>
<p>Email or call us to set up a consult</p>
<p>Email:
<strong>contact#acmephotos.test</strong>
</p>
<p>Phone:
<strong>(617) 555-5555</strong>
</p>
</footer>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="main.js"></script>
</body>
</html>
I suggest you always use this code at the top of your CSS document.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
The star points to all the elements!
Your page is simply responsive. You can use a framework like Bootstrap. And put the writings and boxes together and the page will be stretched.
You can try width: 100%; in your CSS if you'd like to stretch the content from the left edge to the right edge of your webpage.
As said by Lalji Tadhani above, remove the max-width in the main html tag's css because it will only let it stay till 900px and will not allow its contents to stretch till both of the sides.

Center div under section [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I don't know what i'm missing here but i can't seem to center my div under a section. Whenever i try to put a margin on the div alone, it also applies a margin to the section tag.
What i want to do is very simple. Put <div class="container"> on the center of the <section id="main">, so the <div class="container"> has space at every side.
Here is my HTML code:
<body>
<section id="main">
<div class="container">
<nav><h2>ALEX\ALBARAN</h2><img src="icon_back.png"></nav><br><br><br>
<div class="box-1">
<h1> HELLO! I'M ALEX</h1>
</div> <br><br>
<div class="box-2">
<h3> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique inventore ipsa optio velit, neque, itaque quasi fugiat impedit libero, vitae temporibus? Quisquam libero voluptas, excepturi non fugiat, perferendis laborum magni!</h3><br><br>
</div>
<div class=box-3>
<i class="fas fa-arrow-circle-down"></i>
</div>
</div>
</section>
and here is my CSS code:
nav {
display: flex;
justify-content: space-between;
}
h2 {
font-family: 'Questrial', sans-serif;
font-weight: 200;
font-size: 15px;
color: white;
}
img {
width:40px;
height: 40px;
align-items: flex-end;
}
#main {
background-color: aqua;
font-family: 'Roboto', sans-serif;
font-weight: 100;
}
.container {
background-image:url(bg1.png);
background-repeat: no-repeat;
background-size: cover;
width: 80%;
justify-content: center;
text-align: center;
margin: 10% 10%;
}
.box-1 {
border: 3px solid white;
background-color: transparent;
color: white;
width: 50%;
padding: auto;
margin: auto;
text-align: center;
}
.box-2 {
font-size: 10px;
font-weight: 100;
color: white;
background-color: transparent;
width: 50%;
padding:auto;
margin:auto;
text-align: center;
}
.box-3 {
text-align: center;
color: white;
padding-bottom: 20px;
}
You can use display:flex on main div. Then center .container.
Here is a simplified snippet:
#main {
background-color: aqua;
font-family: 'Roboto', sans-serif;
font-weight: 100;
display: flex;
align-items: center;
justify-content: center;
height: 300px;
}
.container {
border: 1px solid red;
width: 80%;
height: 80px
}
<section id="main">
<div class="container">
</div>
</section>
To center your div, here are the attributes you have to add in the CSS:
.centered-div {
margin: 0 auto;
width: 80%; // specifies a custom width
}
The problem was the margin: 10% 10%. Replace it with the auto margin and the result will be this (run the snippet to see your HTML page displayed):
nav {
display: flex;
justify-content: space-between;
}
h2 {
font-family: 'Questrial', sans-serif;
font-weight: 200;
font-size: 15px;
color: white;
}
img {
width:40px;
height: 40px;
align-items: flex-end;
}
#main {
background-color: aqua;
font-family: 'Roboto', sans-serif;
font-weight: 100;
}
.container {
background-image:url(bg1.png);
background-repeat: no-repeat;
background-size: cover;
width: 80%;
justify-content: center;
text-align: center;
margin: 0 auto; ############# HERE IS THE MODIFIED LINE #############
}
.box-1 {
border: 3px solid white;
background-color: transparent;
color: white;
width: 50%;
padding: auto;
margin: auto;
text-align: center;
}
.box-2 {
font-size: 10px;
font-weight: 100;
color: white;
background-color: transparent;
width: 50%;
padding:auto;
margin:auto;
text-align: center;
}
.box-3 {
text-align: center;
color: white;
padding-bottom: 20px;
}
<body>
<section id="main">
<div class="container">
<nav><h2>ALEX\ALBARAN</h2><img src="icon_back.png"></nav><br><br><br>
<div class="box-1">
<h1> HELLO! I'M ALEX</h1>
</div> <br><br>
<div class="box-2">
<h3> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique inventore ipsa optio velit, neque, itaque quasi fugiat impedit libero, vitae temporibus? Quisquam libero voluptas, excepturi non fugiat, perferendis laborum magni!</h3><br><br>
</div>
<div class=box-3>
<i class="fas fa-arrow-circle-down"></i>
</div>
</div>
</section>
By the way, you forgot to close your <body>.

How To Create Equal Height Columns with CSS only and without flexbox? [duplicate]

This question already has answers here:
CSS - Equal Height Columns?
(11 answers)
Closed 2 years ago.
I have 3 columns in my footer (f-box). I want them to have equal height. I don't want to use flexbox for this one. I am trying to achieve the result using - display:table on my container of the boxes and make display:table-cell for every box. But it doesn't work. Why? How to do it with this display: table method
#import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght#300;400;600;700;800&family=Quantico:wght#400;700&display=swap");
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: "Open Sans", sans-serif;
line-height: 1.5;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-family: "Quantico", sans-serif;
margin-bottom: 20px;
}
p {
font-size: 15px;
color: #666;
line-height: 26px;
margin-bottom: 15px;
}
a {
font-family: "Quantico", sans-serif;
text-decoration: none;
color: #111;
}
ul {
list-style: none;
}
img {
width: 100%;
display: block;
}
/* Theme */
.container {
margin: auto;
max-width: 1500px;
padding: 0 15px;
}
.logo {
font-size: 30px;
padding: 20px 0;
float: left;
margin: 0;
}
.main-color {
color: #009603;
}
.btn {
display: inline-block;
text-transform: uppercase;
background: #009603;
color: #fff;
padding: 14px 30px;
font-weight: 700;
}
/* Nav */
nav {
overflow: hidden;
text-transform: uppercase;
}
nav ul {
float: right;
}
nav ul li {
float: left;
margin-right: 32px;
position: relative;
}
nav ul li a {
display: block;
padding: 31px 8px;
font-weight: 700;
}
nav ul li a:after {
position: absolute;
left: 0;
bottom: 0;
height: 3px;
width: 100%;
background: #009603;
content: "";
opacity: 0;
transition: all 0.3s;
}
nav ul li.active a:after {
opacity: 1;
}
nav ul li:hover > a:after {
opacity: 1;
}
/* Showcase */
#showcase {
background: url("../img/bg.jpg") no-repeat center center/cover;
height: 900px;
}
#showcase .showcase-content {
color: #fff;
text-align: center;
padding-top: 300px;
}
#showcase .showcase-content h2 {
font-size: 60px;
font-weight: 700;
text-transform: uppercase;
}
#showcase .showcase-content p {
margin-bottom: 30px;
line-height: 30px;
color: #fff;
}
/* Features */
#features {
padding-bottom: 60px;
}
#features .container {
margin-top: -70px;
max-width: 1400px;
overflow: hidden;
}
#features .box {
background: #fff;
float: left;
max-width: 30%;
margin: 0 15px 30px 15px;
padding: 10px 10px 18px 10px;
box-shadow: 0px 10px 25px rgba(206, 206, 206, 0.5);
text-align: center;
}
#features .box img {
margin-bottom: 28px;
}
#features .box h3 {
color: #191039;
font-weight: 700;
text-transform: uppercase;
margin-bottom: 10px;
font-size: 30px;
}
/* Footer */
footer {
padding-top: 70px;
overflow: hidden;
background-color: #000;
}
footer .container {
max-width: 1400px;
display: table;
}
footer .f-box {
display: table-cell;
float: left;
max-width: 33.3%;
margin-bottom: 30px;
padding: 0 15px;
}
footer .logo {
padding-top: 0;
color: #fff;
text-transform: uppercase;
float: none;
}
footer p {
color: #c4c4c4;
margin-bottom: 20px;
}
footer .social i {
color: #fff;
margin-right: 20px;
}
footer h5 {
color: #fff;
text-transform: uppercase;
font-size: 20px;
font-weight: 700px;
margin-bottom: 35px;
padding-top: 5px;
}
footer img {
float: left;
width: calc(33.33% - 5px);
margin-right: 5px;
}
footer form {
position: relative;
}
footer input {
width: 100%;
height: 50px;
font-size: 15px;
color: #c4c4c4;
padding-left: 20px;
border: 1px solid #009603;
background: transparent;
}
footer form button {
font-size: 18px;
color: #fff;
background: #009603;
height: 50px;
width: 50px;
border: none;
position: absolute;
right: 0;
top: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pacocha | Garden Projects</title>
<link rel="stylesheet" href="css/style.css" />
<script
src="https://kit.fontawesome.com/1685e275a4.js"
crossorigin="anonymous"
></script>
</head>
<body>
<header>
<nav>
<div class="container">
<a href="index.html">
<h1 class="logo"><i class="fas fa-leaf main-color"></i> Pacocha</h1>
</a>
<ul>
<li class="active">Home</li>
<li>About</li>
<li>Services</li>
</ul>
</div>
</nav>
<div id="showcase">
<div class="container">
<div class="showcase-content">
<h2>Garden Projects</h2>
<p>
We have the best home improvement projects, expert advice, and DIY
home improvement ideas for your home. <br />
You can create your dream home with smart planning and the right
home improvement contractors.
</p>
About Us
</div>
</div>
</div>
</header>
<!-- Features -->
<section id="features">
<div class="container">
<div class="box">
<img src="img/feat1.jpeg" alt="" />
<h3>Gardening</h3>
<p>
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Maiores
magnam reprehenderit aspernatur neque nam ipsum enim, vitae minus
totam voluptates.
</p>
</div>
<div class="box">
<img src="img/feat2.jpg" alt="" />
<h3>Decorating</h3>
<p>
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Maiores
magnam reprehenderit aspernatur neque nam ipsum enim, vitae minus
totam voluptates.
</p>
</div>
<div class="box">
<img src="img/feat3.jpg" alt="" />
<h3>Ideas</h3>
<p>
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Maiores
magnam reprehenderit aspernatur neque nam ipsum enim, vitae minus
totam voluptates.
</p>
</div>
</div>
</section>
<footer>
<div class="container">
<div class="f-box">
<h1 class="logo"><i class="fas fa-leaf main-color"></i> Pacocha</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione
corporis nostrum ex perferendis! Adipisci, molestias.
</p>
<div class="social">
<i class="fab fa-facebook-f"></i>
<i class="fab fa-twitter"></i>
<i class="fab fa-youtube"></i>
<i class="fab fa-instagram"></i>
</div>
</div>
<div class="f-box">
<h5>Instagram</h5>
<img src="img/insta1.jpg" alt="" />
<img src="img/insta2.jpg" alt="" />
<img src="img/insta3.jpg" alt="" />
</div>
<div class="f-box">
<h5>Subscribe</h5>
<p>
Lorem ipsum, dolor sit amet consectetur adipisicing elit.
Necessitatibus, vel?
</p>
<form action="">
<input type="email" placeholder="Email" />
<button type="submit"><i class="fa fa-send"></i></button>
</form>
</div>
</div>
</footer>
</body>
</html>
you just need this css
footer {
padding: 70px 0;
}
footer .f-box {
display: table-cell;
float: none;
width: 33.3%;
margin-bottom: 30px; /*the margin property is not applicable to display:table-cell elements.*/
padding: 0 15px;
}
and it will behave like this - equal column height