body {
background-color: #040081;
font-family: Arial, Helvetica, sans-serif;
padding: 50px;
box-sizing: border-box;
}
.header {
padding: 20px;
background-color: #1612AA;
text-align: center;
font-family: "Roboto" sans-serif;
font-size: 40px;
text-shadow: 2px 0 #F36502, -2px 0 #F36502, 0 2px #F36502, 1px 1px #F36502, -1px -1px #F36502, 1px -1px #F36502, -1px 1px #F36502;
position: relative;
height: 200px;
}
.part1 {
float: left;
width: 80%;
}
.part2 {
float: right;
width: 20%;
}
.img1 {
border-radius: 20px;
margin-top: 10px;
}
.row:after {
display: grid;
clear: both;
}
.columnLeft {
float: left;
width: 70%;
margin-top: 50px;
}
.columnRight {
float: right;
width: 30%;
margin-top: 50px;
margin-left: 50px;
}
.card {
background-color: #1612AA;
color: #F36502;
padding: 20px;
}
.img2 {
border-radius: 20px;
width: 80%;
padding-left: 20px;
}
<!DOCTYPE html>
<html>
<head>
<title>Fractal Forward</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="header">
<h1 class="part1">Fractal Forward</h1>
<img src="mandelImage.webp" class="part2 img1">
</div>
<div class="row">
<div class="columnLeft">
<div class="card">
<h2>Blog Entry Title</h2>
<img src="image1.jpeg" class="img2">
<p>This is the beginning of the blog Yay Yay Ya! The first couple sentences. Eventually I'm going to figure out how to code it so the first three sentences are automatically here and I don't have to type it out...</p>
</div>
</div>
<div class="columnRight">
<div class="card">
</div>
</div>
</div>
</body>
</html>
I tried adding ‘margin-left: 50px;’ to the .columnRight class so that there’d be space in-between the two columns, but every time I do that it shifts columnRight underneath columnLeft. Without the margin-left for the column there’s no separation between the two columns and it doesn’t look very good. Not sure how to fix this, or what I did wrong. This is my first attempt at creating a website and this has been my first real sticking point. Any help would be greatly appreciated – thanks!
3
Answers
The reason why adding
margin-left: 50px;
to the.columnRight
class shifts it underneath.columnLeft
is because both columns are floating. When an element floats, it is removed from the normal flow of the document and positioned next to its floated container. This means that the two columns will overlap unless you clear them.To clear the floats, you can add the following CSS to your
.row
class:The
:after
pseudo-element creates a new element after the.row
element. Thecontent
property is used to set the text content of the new element, but in this case we are setting it to an empty string. This means that the new element will be invisible, but it will still take up space in the document. Thedisplay: block
property makes the new element behave like a block element, and theclear: both
property clears both left and right floats.Once you have added the
:after
pseudo-element to your.row
class, you should be able to addmargin-left: 50px;
to the.columnRight
class without it shifting underneath.columnLeft
.Here is an example of how your CSS would look after making these changes:
I hope this helps!
Use the CSS function calc like this.
The problem is that 80% + 20% equals 100%, so 80% + 20% + 50px is greater than 100%.
Instead of specifying column sizes on the elements and using the older
float
layout algorithm, I suggest setting column sizes on the containing element and use CSS’s moderngrid
algorithm to handle the exact implementation details:Then you don’t need to use
float
,clear
, or::after
. Using the grid algorithm also allows you to add agap
property to the row div.