This question already has answers here:
What is the default padding and/or margin for a p element (reset css)?
(5 answers)
Closed 1 year ago.
I'm trying to put the div right below hr tag but it doesn't seem to work. I tried adding padding: 0; margin: 0; Instead nothing changes. Right now I have this:
But instead I want something like the image below:
HTML:
<hr class="hr">
<div class="alttxt">
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis knostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum. </p>
</div>
CSS:
.hr {
border: 3px solid #ffffff;
margin: 0 5% 0 5%;
padding-bottom: 0 !important;
}
.alttxt {
background-color: rgba(1,1,1,0.5);
margin-top: 0 !important;
margin: 0 5% 0 5%;
}
.alttxt p {
text-align: justify;
padding: 3% 3% 3% 3%;
color: white;
font-size: 140%;
}
Remove the default margin from the paragraph
.hr {
border: 3px solid red;
margin: 0 5% 0 5%;
}
.alttxt {
background-color: rgba(1, 1, 1, 0.5);
margin: 0 5% 0 5%;
}
.alttxt p {
text-align: justify;
padding: 3% 3% 3% 3%;
color: white;
font-size: 140%;
margin: 0;
}
<hr class="hr">
<div class="alttxt">
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis knostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum. </p>
</div>
Play with the .alttxt margins
I chose 7%
That's the only change I made to your css
.hr
{
border: 3px solid #0000ff;
margin: 0 5% 0 5%;
padding-bottom: 0 !important;
}
.alttxt
{
background-color: rgba(1,1,1,0.5);
margin-top: 0 !important;
margin: 0 7% 0 7%;
p
{
text-align: justify;
padding: 3% 3% 3% 3%;
color: white;
font-size: 140%;
}
}
Related
I am trying to solve the issue with a floating sidebar.
I created blog-styled site, which has sidebar on the right of the text. The first page worked well and when changing the post of the page 2, the sidebar is not floating to the right anymore. Can somebody tell why it happens? Thanks.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 1148px;
margin: 20px auto;
}
.blog-post {
width: 80%;
/*background-color: rgba(100,134,134,.2);*/
float: left;
padding-right: 30px;
position: relative;
}
.other-posts {
display: inline-block;
width: 20%;
/*background-color: rgba(100,10,134,.2);*/
float: left;
}
.clearfix:after {
content: ".";
visibility: hidden;
display: block;
height: 0;
clear: both;
}
h1,
h2 {
margin: 20px 0;
}
p {
text-align: justify;
margin-bottom: 15px;
line-height: 130%;
}
.author-box {
border-top: 1px solid #808080;
padding-top: 20px;
padding-bottom: 20px;
}
.other {
margin-bottom: 30px;
}
.author-text {
font-size: 22px;
float: left;
margin: 30px 10px;
}
.author-box img {
height: 100px;
width: 100px;
border-radius: 50%;
float: left;
margin-bottom: 20px;
}
.date {
position: absolute;
top: 30px;
right: 30px;
}
.other a {
text-decoration-line: none;
}
<!DOCTYPE html>
<html>
<head>
<title>Test with CSS/a blog</title>
<link rel="stylesheet" type="text/css" href="styles2.css">
</head>
<body>
<div class="container">
<div class="blog-post">
<h1>This is my second blog build</h1>
<p class="date">April 9th, 2019</p>
<h2>Yeah, we talk about delphins again.</h2>
<p class="main-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse<br> cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="other-posts">
<div class="other">
The first post. What is the future of the TV.
</div>
<div class="other">
The second post. Yeah, we talk about delphins again.
</div>
<div class="other">
The third post. What should you know about CSS grid.
</div>
</div>
<div class="clearfix"></div>
<div class="author-box">
<img src="https://randomuser.me/api/portraits/women/66.jpg">
<p class="author-text">mrs Janet Gonzales</p>
</div>
</div>
<!-- container ending div -->
</body>
</html>
Looks like you're missing a closing </div> tag for <div class="blog-post">. Insert a closing tag and I think you'll get what you're after.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 1148px;
margin: 20px auto;
}
.blog-post {
width: 80%;
/*background-color: rgba(100,134,134,.2);*/
float:left;
padding-right: 30px;
position: relative;
}
.other-posts {
display: inline-block;
width: 20%;
/*background-color: rgba(100,10,134,.2);*/
float: left;
}
.clearfix:after {
content: ".";
visibility: hidden;
display: block;
height: 0;
clear: both;
}
h1,h2 {
margin: 20px 0;
}
p {
text-align: justify;
margin-bottom: 15px;
line-height: 130%;
}
.author-box {
border-top: 1px solid #808080;
padding-top: 20px;
padding-bottom: 20px;
}
.other {
margin-bottom: 30px;
}
.author-text {
font-size: 22px;
float: left;
margin: 30px 10px;
}
.author-box img {
height: 100px;
width: 100px;
border-radius: 50%;
float: left;
margin-bottom: 20px;
}
.date {
position: absolute;
top: 30px;
right: 30px;
}
.other a {
text-decoration-line: none;
}
<div class="container">
<div class="blog-post">
<h1>This is my second blog build</h1>
<p class="date">April 9th, 2019</p>
<h2>Yeah, we talk about delphins again.</h2>
<p class="main-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse<br>
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="other-posts">
<div class="other">
The first post. What is the future of the TV.
</div>
<div class="other">
The second post. Yeah, we talk about delphins again.
</div>
<div class="other">
The third post. What should you know about CSS grid.
</div>
</div>
<div class="clearfix"></div>
<div class="author-box">
<img src="https://randomuser.me/api/portraits/women/66.jpg">
<p class="author-text">mrs Janet Gonzales</p>
</div>
</div> <!-- container ending div -->
I am trying to make a newspaper type footer.
I've got all the things fine.
The problem is that i have 2 <div>s inside the newspaper <div class = "newspaper" , each having 1 <p> .
But, the first <p> is split across the two sides of the vertical line.
U can see it here, but if U dont, then:
Copy the code and paste it in notepad.
And open it with a browser.
Hover on "FeedBack".
It would be outlined
Now U should see a part pf the outline box on the other side of the vertical line.
If U dont, then try changing the size of the browser.
Is there any way to avoid this??
p {
font-size: 20px;
font-family: Comic Sans MS;
}
footer {
position: relative;
top: 10px;
background-color: #316441;
border: solid black 2px;
}
.con {
text-align: left;
margin-left: 10px;
margin-right: 10px;
}
.contact {
color: white;
font-weight: 5px;
font-size: 40px;
}
.fol {
margin-left: 10px;
margin-right: 10px;
text-align: left;
}
.follow {
color: white;
font-weight: 5px;
font-size: 40px;
}
a.email {
position: relative;
top: 5px;
padding: 5px;
color: white;
text-decoration: none;
}
a.feedback {
position: relative;
top: 17px;
padding: 5px;
color: white;
text-decoration: none;
}
a.feedback:hover,
a.email:hover {
outline: 2px white solid;
padding: 5px;
}
.sm {
padding: 8px;
font-size: 30px !important;
text-align: center;
text-decoration: none;
border: white 2px solid;
border-radius: 5px;
background-color: black;
color: white;
}
.newspaper {
-webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
-webkit-column-rule: 2px #d3d3d3 solid;
-moz-column-rule: 2px #d3d3d3 solid;
column-rule: 2px #d3d3d3 solid;
-webkit-column-gap: 0;
-moz-column-gap: 0;
column-gap: 0;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<footer>
<div class="newspaper">
<div class="con">
<p><span class="contact">Contact Us: </span><br><i class="fa fa-envelope-o" aria-hidden="true"></i> Email: HyperMathsMail<span style="font-family: 'New Times Roman'">#</span>gmail.com<br>
<i class="fa fa-comments-o" aria-hidden="true"></i> FeedBack</p>
</div>
<div class="fol">
<p><span class="follow">Follow Us: </span><span style="position: relative;left: 5px;"><br> <a href = "javascript:void(0)" class="fa fa-youtube-play sm fa-fw" ></a></span></p>
</div>
</div>
</footer>
</body>
</html>
Replacing column-count with display: flex appears to give you the same result and fixes the issue?
p {
font-size: 20px;
font-family: Comic Sans MS;
}
footer {
position: relative;
top: 10px;
background-color: #316441;
border: solid black 2px;
}
.con {
text-align: left;
margin-left: 10px;
margin-right: 10px;
border-right: 3px solid white;
}
.contact {
color: white;
font-weight: 5px;
font-size: 40px;
}
.fol {
margin-left: 10px;
margin-right: 10px;
text-align: left;
}
.follow {
color: white;
font-weight: 5px;
font-size: 40px;
}
a.email {
position: relative;
top: 5px;
padding: 5px;
color: white;
text-decoration: none;
}
a.feedback {
position: relative;
top: 17px;
padding: 5px;
color: white;
text-decoration: none;
}
a.feedback:hover,
a.email:hover {
outline: 2px white solid;
padding: 5px;
}
.sm {
padding: 8px;
font-size: 30px !important;
text-align: center;
text-decoration: none;
border: white 2px solid;
border-radius: 5px;
background-color: black;
color: white;
}
.newspaper {
display: flex;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<footer>
<div class="newspaper">
<div class="con">
<p><span class="contact">Contact Us: </span><br><i class="fa fa-envelope-o" aria-hidden="true"></i> Email: HyperMathsMail<span style="font-family: 'New Times Roman'">#</span>gmail.com<br>
<i class="fa fa-comments-o" aria-hidden="true"></i> FeedBack</p>
</div>
<div class="fol">
<p><span class="follow">Follow Us: </span><span style="position: relative;left: 5px;"><br> <a href = "javascript:void(0)" class="fa fa-youtube-play sm fa-fw" ></a></span></p>
</div>
</div>
</footer>
</body>
</html>
You don't have to use column-count as you know exactly what there is in each column. column-count is usefull for large text, but is not what you need here i think. You can use display:table instead.
p {
font-size: 20px;
font-family: Comic Sans MS;
}
footer {
position: relative;
top: 10px;
background-color: #316441;
border: solid black 2px;
display:table ;
width:100% ;
}
.newspaper {
display:table-row ;
}
.newspaper>div {
display:table-cell ;
padding: 0 10px;
}
.newspaper>div.con {
border-right:3px solid white ;
}
.contact {
color: white;
font-weight: 5px;
font-size: 40px;
}
.follow {
color: white;
font-weight: 5px;
font-size: 40px;
}
a.email {
position: relative;
top: 5px;
padding: 5px;
color: white;
text-decoration: none;
}
a.feedback {
position: relative;
top: 17px;
padding: 5px;
color: white;
text-decoration: none;
}
a.feedback:hover,
a.email:hover {
outline: 2px white solid;
padding: 5px;
}
.sm {
padding: 8px;
font-size: 30px !important;
text-align: center;
text-decoration: none;
border: white 2px solid;
border-radius: 5px;
background-color: black;
color: white;
}
.newspaper div:first-child() {
border-right:1px solid white ;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<footer>
<div class="newspaper">
<div class="con">
<p><span class="contact">Contact Us: </span><br><i class="fa fa-envelope-o" aria-hidden="true"></i> Email: HyperMathsMail<span style="font-family: 'New Times Roman'">#</span>gmail.com<br>
<i class="fa fa-comments-o" aria-hidden="true"></i> FeedBack</p>
</div>
<div class="fol">
<p><span class="follow">Follow Us: </span><span style="position: relative;left: 5px;"><br> <a href = "javascript:void(0)" class="fa fa-youtube-play sm fa-fw" ></a></span></p>
</div>
</div>
</footer>
</body>
</html>
I suggest that you do not use the top property on your a.email and a.feedback CSS selectors. Elements positioned like this become difficult to maintain, and leads to weird issues, like the issue you're currently having.
Remove the top and position: relative CSS properties, add display: block to both elements and, finally, you can use padding as appropriate to create spacing:
a.email {
padding: 10px 5px;
display: block;
color: white;
text-decoration: none;
}
a.feedback {
padding: 10px 5px;
display: block;
color: white;
text-decoration: none;
}
This answer provides more info on why to avoid position: relative top.
I am stuck here, i want to make fix nav with full height section with content but second section is not going good after scroll, need help.
Below my tried code:
*,
*::after,
*::before {
margin: 0px;
padding: 0px;
box-sizing: inherit;
-webkit-box-sizing: inherit;
}
body,
html {
margin: 0px;
padding: 0px;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
nav {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
text-align: center;
padding: 10px 0px;
background: tomato;
}
nav ul {
margin: 0px;
padding: 0px;
list-style: none;
text-align: center;
}
nav ul li {
display: inline-block;
margin: 3px;
}
nav ul li a {
display: block;
padding: 5px;
background: #000;
color: #ddd;
text-decoration: none;
}
nav ul li a:hover {
background: #ddd;
color: #333;
}
section {
height: 100vh;
background: #303030;
margin: 0px;
padding: 0px;
}
section p {
margin-top: 54px;
color: #fff;
}
section.green {
background: green;
height: 100vh;
margin: 0px;
padding: 0px;
}
section.green p {
color: #ddd;
}
<section>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Service</li>
<li>Contact</li>
</ul>
</nav>
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</section>
<section class="green">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</section>
</section>
Can anyone help me out and provide me a solution to my problem?
You have nested <section> which is not recommended, a better approach should replace your outer <section> with <div class="wrapper">...</div>
Then use a wrapper class to set the margin-top: 54px which is the height of nav.
.wrapper {
margin-top: 54px;
height: 100vh;
background: #303030;
padding: 0px;
}
For the overlap, use calc to set the height dynamically height: calc(100vh - 52px);
*,
*::after,
*::before {
margin: 0px;
padding: 0px;
box-sizing: inherit;
-webkit-box-sizing: inherit;
}
body,
html {
margin: 0px;
padding: 0px;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
nav {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
text-align: center;
padding: 10px 0px;
background: tomato;
}
nav ul {
margin: 0px;
padding: 0px;
list-style: none;
text-align: center;
}
nav ul li {
display: inline-block;
margin: 3px;
}
nav ul li a {
display: block;
padding: 5px;
background: #000;
color: #ddd;
text-decoration: none;
}
nav ul li a:hover {
background: #ddd;
color: #333;
}
section {
height: 100vh;
background: #303030;
margin: 0px;
padding: 0px;
}
.wrapper {
margin-top: 54px;
height: 100vh;
background: #303030;
padding: 0px;
}
section p {
color: #fff;
}
section.green {
background: green;
height: calc(100vh - 52px);
margin: 0px;
padding: 0px;
}
section.green p {
color: #ddd;
}
<div class="wrapper">
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Service</li>
<li>Contact</li>
</ul>
</nav>
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</section>
<section class="green">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</section>
</div>
You can use calc() to calculate the height of the sections
height: calc(100vh - 54px); /*54px is the height of the nav*/
So this will make the height of each section have the height of the screen minus the height of the nav.
And to remove the space between the sections you need to remove margin-top from the p tag and add padding-top:54px;to the container, again 54px is the height of the nav, and that for the sections to start right after the nav
*,
*::after,
*::before {
margin: 0px;
padding: 0px;
box-sizing: inherit;
-webkit-box-sizing: inherit;
}
body,
html {
margin: 0px;
padding: 0px;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
.container {
height: 100vh;
padding-top:54px;
}
nav {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
text-align: center;
padding: 10px 0px;
background: tomato;
}
nav ul {
margin: 0px;
padding: 0px;
list-style: none;
text-align: center;
}
nav ul li {
display: inline-block;
margin: 3px;
}
nav ul li a {
display: block;
padding: 5px;
background: #000;
color: #ddd;
text-decoration: none;
}
nav ul li a:hover {
background: #ddd;
color: #333;
}
section {
height: calc(100vh - 54px);
background: #303030;
margin: 0px;
padding: 0px;
}
section p {
color: #fff;
}
section.green {
background: green;
margin: 0px;
padding: 0px;
}
section.green p {
color: #ddd;
}
<div class="container">
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Service</li>
<li>Contact</li>
</ul>
</nav>
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</section>
<section class="green">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</section>
</div>
So I want to achieve this using css and html
So I wrote this code that sets the width of each box to 33.33%
/* Base style */
h1 {
text-align: center;
}
.row {
width: 100%;
height: auto;
padding: 10px;
}
* {
box-sizing: border-box;
font-family: sans-serif;
margin: 0px;
}
div > div {
background-color: gray;
border: 1px solid;
float: left;
}
.dummy_text {
clear: right;
padding: 10px;
}
/* Top right paragraphs*/
#chiken {
float: right;
background-color: pink;
border: 2px solid;
width: 150px;
text-align: center;
font-weight: bold;
position: relative;
left: 1px;
padding: 5px;
}
#beef {
float: right;
background-color: indianred;
color: white;
border: 2px solid black;
width: 150px;
text-align: center;
font-weight: bold;
left: 1px;
padding: 5px;
}
#sushi {
float: right;
background-color: lightgoldenrodyellow;
border: 2px solid;
width: 150px;
text-align: center;
font-weight: bold;
left: 1px;
padding: 5px;
}
/* Desktop */
#media (min-width: 992px) {
.col-dsk-3 {
float: left;
width: 33.33%;
}
}
<h1>Our menu</h1>
<div class="row">
<div class="col-dsk-3 col-tbl-2 col-mbl-1">
<p id="chiken">Chicken
<p>
<p class="dummy_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="col-dsk-3 col-tbl-2 col-mbl-1">
<p id="beef">Beef
<p>
<p class="dummy_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="col-dsk-3 col-tbl-1 col-mbl-1">
<p id="sushi">Sushi
<p>
<p class="dummy_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
And the result is this:
The thing is that I need spacing between paragraphs, so what I though was to add some margin-left to the boxes, the thing is that when I add 10px, the result is that one of the three boxes goes to a new line, but I need the three in the same line.
This is what I did to add the margin, I modify the div > div part like this:
div > div {
background-color: gray;
border: 1px solid;
float: left;
margin-left: 10px;
}
And then result:
Use % values for all your layout widths.
Use :last-child to set the margin of the right div to zero.
div > div {
background-color: gray;
border: 1px solid;
float: left;
margin-right: 2%
}
div > div:last-child {
margin-right: 0;
}
/* Desktop */
#media (min-width: 992px) {
.col-dsk-3 {
float: left;
width: 32%;
}
}
Here's a codepen:http://codepen.io/prime8/pen/LRympm
Use calc for with
.col-dsk-3 {
float: left;
width: calc(33.33% - 20px);
margin-right: 10px;
}
.col-dsk-3:last-child {
margin-right: 0px;
}
But I suggest you use flexbox instead
When adding the margin-left: 10px to each paragraph you are making their widths larger than 33.33% which results in being greater than 100% pushing the last paragraph down.
Like all things in CSS, there is a couple of different ways you can solve this, but the easiest and most direct answer to your question is using the calc CSS function. The calc function calculates a numerical value in CSS using basic math operations.
Using the calc function you can then set the width of each paragraph to be:
width: calc(33.33% - 10px);
Which will result in a 100% fit.
There are a number of things you can do about this.
1 - Use calc to reduce the width of elements currently set to 33.33% by 10px, and use 10px margin:
.col-dsk-3 {
float: left;
width: calc(33.33% - 10px);
margin-right: 10px;
}
2 - Wrap the content of your columns in another element, and apply a padding to your columns:
<div class="col-dsk-3 col-tbl-2 col-mbl-1">
<div class="column-content">
<p id="chiken">Chicken<p>
<p class="dummy_text">Lorem ipsum dolor sit....</p>
</div>
</div>
.column-content {
background-color: gray;
}
.col-dsk-3 {
float: left;
width: 33.33%;
padding: 10px;
background: none;
}
3 - use flexbox instead of floats for your columns. Remove the floats and the width: 33.33%, and add:
.row {
display: flex;
flex-direction: row;
}
.col-dsk-3 {
margin: 10px;
}
You use percentages to define the width, but add absolute values to the margins. Your widths add up to (almost) 100%, yet you add more margins, resulting in more than 100%, therefore to a value that is bigger than the space that is available.
Adjust your margins to use percentages as well and make sure you end up with 100% or less.
Try this. To use additional div wrapper in HTML.
This way has a good compatibility.
/* Base style */
h1 {
text-align: center;
}
.row {
width: 100%;
height: auto;
padding: 10px;
}
* {
box-sizing: border-box;
font-family: sans-serif;
margin: 0px;
}
/* NOTE: `.row > div > div` is better than `div > div > div` */
div > div > div { /* changed */
background-color: gray;
border: 1px solid;
float: left;
}
.row > div > div { /* changed */
margin: 0 10px;
}
.dummy_text {
clear:right;
padding: 10px;
}
/* Top right paragraphs*/
#chiken {
float: right;
background-color: pink;
border: 2px solid;
width: 150px;
text-align: center;
font-weight: bold;
position: relative;
left: 1px;
padding: 5px;
}
#beef {
float: right;
background-color: indianred;
color: white;
border: 2px solid black;
width: 150px;
text-align: center;
font-weight: bold;
left: 1px;
padding: 5px;
}
#sushi {
float: right;
background-color: lightgoldenrodyellow;
border: 2px solid;
width: 150px;
text-align: center;
font-weight: bold;
left: 1px;
padding: 5px;
}
/* Desktop */
#media (min-width: 992px) {
.col-dsk-3 {
float: left;
width: 33.33%;
}
}
<h1>Our menu</h1>
<div class="row">
<div class="col-dsk-3 col-tbl-2 col-mbl-1">
<div>
<p id="chiken">Chicken<p>
<p class="dummy_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
<div class="col-dsk-3 col-tbl-2 col-mbl-1">
<div>
<p id="beef">Beef<p>
<p class="dummy_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
<div class="col-dsk-3 col-tbl-1 col-mbl-1">
<div>
<p id="sushi">Sushi<p>
<p class="dummy_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</div>
I'm just now trying to learn the flexbox layout as it seems like it could simplify many things.
I'm trying to get 2 columns side by side with 1 60% width and the other 30% width.
Even though I specify the width in the flex-basis property they stay the same size, evenly spaced on the page.
http://codepen.io/anon/pen/VLrWZY
HTML:
<article class="fullCol">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</article>
<section class="flexWrap">
<article class="halfCol">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</article>
<article class="halfCol">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</article>
<!-- Put top and bottom drop shadow on navbar -->
</section>
</section>
CSS:
article, section, nav, aside {
display: block;
}
#wrapper {
}
.flexWrap {
display: flex;
-webkit-flex-flow: row || wrap;
flex-flow: row || wrap;
-webkit-justify-content: center;
justify-content: center;
}
.fullCol {
width: 100%;
border-bottom: 1px solid black;
margin: 0px 0px 15px 0px;
padding: 10px;
box-shadow: 0 4px 2px -2px gray;
}
.halfCol {
box-shadow: 0 3px 5px #888, 0 -3px 5px #888;
width: 45%;
border: 1px solid black;
margin: 0px 5px 0px 5px;
padding: 10px;
}
.mainCol {
box-shadow: 0 3px 5px #888, 0 -3px 5px #888;
border: 1px solid black;
margin: 0px 5px 0px 5px;
padding: 10px;
-webkit-flex-basis: auto | 60%; /* Safari */
flex-basis: auto | 60%;
}
.sideCol {
box-shadow: 0 3px 5px #888, 0 -3px 5px #888;
width: 30%;
border: 1px solid black;
margin: 0px 5px 0px 5px;
padding: 10px;
-webkit-flex-basis: auto | 30%; /* Safari */
flex-basis: auto | 30%;
}
you need to use -webkit-flex: initial; and flex: initial; on the flex child you are trying to define a width on.