I’m working on a website and I want the majority of my elements to be center aligned. Each element is wrapped inside a section
so that I can smooth scroll from one page to the next. I have an h1
element that says "Build Your Future" and I’m trying to align it perfectly to the left, just above where it says "Purpose" however no matter what I do, I can’t seem to override my section
where it says align-items: center
for this particular element. I’ve tried changing the width and the text-align but it’s still stuck in the center.
section {
background-color: #fff;
height: 100vh;
height: fill-available;
min-height: -moz-available;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
overflow-x: hidden;
}
.box {
background-color: white;
display: flex;
justify-content: space-between;
width: 60vw;
}
.service h1 {
display: flex;
width: 100%;
align-items: left;
text-align: left;
}
<section class="service">
<div>
<h1>Build Your Vision</h1>
</div>
<div class="box">
<div>
<h2>Purpose</h2>
<p>Built with your vision</p>
</div>
<div>
<h2>Authentication</h2>
<p>From services you can trust</p>
</div>
<div>
<h2>User Interface</h2>
<p>Accessible from any device</p>
</div>
</div>
</section>
4
Answers
In fact by setting you section service in flex with justify content center the width of your parent h1 is basically set to fit content.
So you have two choices:
First one is to sort out the h1 div of the service section
Second one is to set à width to the parent div
or
You can use
align-self
property on your element and useflex-start
to move your element to the start of the container. I added a class on you title elementTo address the issue, you should use the parent container of the h1 element (which is a div in this case) instead of the
<section>
element. The problem you encountered is due to a limitation of the element.To solve this, you can apply a CSS rule by specifying the class. In this case, the
<section>
element has the class "service," so we can utilize that. Additionally, we want to target only the first div inside the section, which can be achieved using the:nth-child(1)
selector.Finally, we set the width of the selected div to be the same length as the box class. Here’s the updated code:
By applying this CSS rule, the width of the first div inside the section element with the "service" class will be set to 60% of the viewport width.
Hope this helps! Let me know if you have any further questions.
The easiest solution would probably just be to modify your markup a bit, but here is a dynamic solution that will work without hard-coding a
width
/height
value usinggrid
.The code is explained in the comments.