This question already has answers here:
Display a div width 100% with margins
(6 answers)
Closed 2 years ago.
I made a website, it looks good but the header goes out of bounds from the right, I followed a tutorial and then did exactly like he told but then also I couldn't fix it. Here is my CSS of header component:
As you can see, TESLA ACCOUNT going out of bounds.
Component code: (added just for divs info)
const Header = () => {
return (
<div className="header">
<div className="header__logo">
<img src={TeslaLogo} alt="Tesla Logo" />
</div>
<div className="header__models">
<p>MODEL S</p>
<p>MODEL 3</p>
<p>MODEL X</p>
<p>MODEL Y</p>
<p>SOLAR ROOF</p>
<p>SOLAR PANEL</p>
</div>
<div className="header__rightStuff">
<p>SHOP</p>
<p>TESLA ACCOUNT</p>
</div>
</div>
);
}
CSS code
.header {
display: flex;
background-color: transparent;
background: none;
justify-content: space-between;
align-items: center;
position: fixed;
width: 100vw;
top: 0;
margin: 1rem;
z-index: 100;
}
.header is the main div for HEADER.
(I added margin:0 padding:0 in index.css to reset website margin and padding, didn't work).
Width does not include margin, so you need to subtract the margin from your width.
.header {
display: flex;
background-color: transparent;
background: none;
justify-content: space-between;
align-items: center;
position: fixed;
width: calc(100vw - 2rem); /*Both left and right magin = 2rem*/
top: 0;
margin: 1rem;
z-index: 100;
}
More on calc
Related
so I'd like to put 2 texts around an image (one text on the left, the other right of the image) and center them vertically. Is there other ways than putting position:relative; in the container and position:absolute; to the text (and play with the pixels of top and left to put it in the position that I want) to make the text and image RESPONSIVE in any devices? I want something like this:
Here's my code:
HTML
<div class="socrates-words">
<p class="hello"><strong>Hello!</strong></p>
<p class="socrates-name"><strong>My name is Socrates.</strong></p>
</div>
<div class="container-gif">
<img src="/videos/socrates-video.gif" alt="" id="gif-socrates" />
</div>
CSS
.button-section .container-gif {
position: relative;
bottom: 80%;
}
.button-section .container-gif #gif-socrates {
transform: translate(-50%);
left: 50%;
width: 25%;
position: absolute;
display: none;
}
.button-section .socrates-words {
font-size: 32px;
position: relative;
}
.button-section .socrates-words .hello {
position: absolute;
left: 15%;
top: 17rem;
}
.button-section .socrates-words .socrates-name {
position: absolute;
left: 65%;
top: 17rem;
}
I'm doing my best and I'm a beginner so please no hate comments... Thank you in advance!
You do not have to change your HTML.
This snippet uses a CSS grid of 3 equal columns, tells it to use the contents of the first child rather than the child itself (i.e. the 2 p elements) using display: contents and defines the order of the second p element within the grid and the img containing element.
.container {
display: grid;
width: 100vw;
grid-template-columns: repeat(3, 1fr);
text-align: center;
}
.socrates-words {
display: contents;
}
.socrates-words>* {
display: flex;
align-items: center;
justify-content: center;
}
.socrates-words p:last-of-type {
order: 3;
}
.container-gif {
order: 2;
}
<div class="container">
<div class="socrates-words">
<p class="hello"><strong>Hello!</strong></p>
<p class="socrates-name"><strong>My name is Socrates.</strong></p>
</div>
<div class="container-gif">
<img src="https://picsum.photos/id/1015/50/100" alt="" id="gif-socrates" />
</div>
</div>
Obviously you can alter various dimensions to suit your particular requirement.
Here is an easy example of how you can do it.
just place your image in between the text container and then using display flex you can achieve the result you want.
It is also responsive.
Explanation
I used display: flex; so I can align all the image and text in a row.
I used justify-content: space-evenly; to make the space between the image and test even
I used align-items: center; to make all the text and the image to center from top and bottom.
.socrates-words{
display:flex;
justify-content: space-evenly;
align-items:center;
}
<div class="socrates-words">
<p class="hello"><strong>Hello!</strong></p>
<div class="container-gif">
<img src="https://images.unsplash.com/photo-1672002760123-4c6496489053?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwyfHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=400&q=60" alt="" id="gif-socrates" />
</div>
<p class="socrates-name"><strong>My name is Socrates.</strong></p>
</div>
I hope you got the answer and also got how it works!
I am trying to place 4 divs over a background image that would keep relative position and size as the browser is resized.
Here is the desired layout:
I have a big background (pink) that is placed with:
#screenFiller {
width: 100vw;
height: 100vh;
background-size: contain;
background-image: url("myimg.png");
background-repeat: no-repeat;
}
There are 4 main divs (red boxes). The two tops ones (side by side) contain a button each (blue boxes) with text (white squiggly lines) above the buttons but still in the red. I am positioning the divs next to each other using:
.flex {
display: flex;
justify-content: Left;
flex: none;
}
The main problem I am having is with the two top divs in that one is not keeping its height (it shrinks to content). I do understand that I will need to handle reducing the text size using a media query or something.
The stripped-down HTML looks like:
<div class="flex ">
<div class="boxme">
<div>
<p class="boldtext">Blha blah balh</p>
<div style="margin:10%;">
<button type="button" class="bigbut gborder5" onclick="window.location.href=''">Syart New</button>
</div>
</div>
</div>
<div class="boxme marl100">
<div>
<p class="boldtext">blah</p>
<div>
<button type="button" class="bigbut gborder5" onclick="window.location.href=''">Start New</button>
</div>
</div>
</div>
</div>
With boxme being:
.boxme {
background-color: white;
width: 25%;
height: 10%;
text-align: center;
}
Finally, all four divs are wrapped in a div with the following css
.relpos {
position: relative;
top: 36%;
left: 4%;
width: 85%
}
Please feel free to take me on an alternate path.
Bootstrap is available if that helps but currently, everything is just HTML and CSS.
Thank you in advanced for any consideration.
Normally, you never want to have fixed width, height, margin or paddings.
For your question, your flex values should be to put your content in the bottom left corner:
.container {
display: flex;
align-items: flex-end;
flex-direction: row; // default
}
Here is the full example that can be run as code snippet:
body {
height: 100vh;
}
body {
background-color: #fcf;
}
.container {
display: flex;
align-items: flex-end;
gap: 20px;
height: 100%;
padding: 20px;
}
.container>div {
background-color: #faa;
padding: 10px;
}
<div class="container">
<div>
<p>Text</p>
<button>Cick</button>
</div>
<div>
<p>Text</p>
<button>Cick</button>
</div>
</div>
I would strongly advise you to learn the fundamentals of html and css from other resources.
This question already has answers here:
My position: sticky element isn't sticky when using flexbox
(8 answers)
Closed 1 year ago.
I have a simple two column layout:
.container {
display: flex;
flex-direction: row;
}
.col {
padding: 30px;
width: 20%;
}
.col.right {
position: sticky;
}
<div class='container'>
<div class='col left'>Lorem<br/>ipsum<br/>dolor<br/>sit<br/>amet,<br/>consectetur<br/>adipiscing<br/>elit,<br/>sed<br/>do<br/>eiusmod<br/>tempor<br/>incididunt<br/>ut<br/>labore<br/>et<br/>dolore<br/>magna<br/>aliqua.<br/>Leo<br/>urna<br/>molestie<br/>at<br/>elementum<br/>eu<br/>facilisis.<br/>Quisque<br/>non<br/>tellus<br/>orci<br/>ac<br/>auctor<br/>augue.<br/>Ac<br/>turpis<br/>egestas<br/>sed<br/>tempus<br/>urna<br/>et<br/>pharetra<br/>pharetra<br/>massa.<br/>Elit<br/>eget<br/>gravida<br/>cum<br/>sociis<br/>natoque.<br/>Orci<br/>a<br/>scelerisque<br/>purus<br/>semper.<br/>Pellentesque<br/>dignissim<br/>enim<br/>sit<br/>amet.<br/>Eu<br/>tincidunt<br/>tortor<br/>aliquam<br/>nulla<br/>facilisi<br/>cras<br/>fermentum<br/>odio<br/>eu.<br/>Nunc<br/>mattis<br/>enim<br/>ut<br/>tellus.<br/>Convallis<br/>posuere<br/>morbi<br/>leo<br/>urna<br/>molestie.<br/>Egestas<br/>sed<br/>tempus<br/>urna<br/>et<br/>pharetra.<br/>Aliquet<br/>eget<br/>sit<br/>amet<br/>tellus.
</div>
<div class='col right'>Tincidunt<br/>praesent<br/>semper<br/>feugiat<br/>nibh<br/>sed<br/>pulvinar<br/>proin<br/>gravida.<br/>Phasellus<br/>vestibulum<br/>lorem<br/>sed<br/>risus<br/>ultricies<br/>tristique.<br/>Pellentesque<br/>pulvinar<br/>pellentesque<br/>habitant<br/>morbi<br/>tristique<br/>senectus<br/>et.<br/>Enim<br/>neque<br/>volutpat<br/>ac<br/>tincidunt<br/>vitae<br/>semper<br/>quis<br/>lectus<br/>nulla.<br/>Commodo<br/>viverra<br/>maecenas<br/>accumsan<br/>lacus<br/>vel<br/>facilisis<br/>volutpat<br/>est<br/>velit.
</div>
</div>
I'd like the right column to stick to the top of the viewport when the left column scrolls, but I'd also like the right column to be scrollable. Is this possible with CSS?
Scrolling need some height limit, something like this should work.
.container {
display: flex;
flex-direction: row;
overflow: auto; /* Scroll ability */
height: 500px; /* Need some height constraint */
}
.col {
padding: 30px;
width: 20%;
}
.col.right {
position: sticky;
top: 0; /* Stick limit */
overflow: auto; /* Scroll ability */
}
<div class='container'>
<div class='col left'>Lorem<br/>ipsum<br/>dolor<br/>sit<br/>amet,<br/>consectetur<br/>adipiscing<br/>elit,<br/>sed<br/>do<br/>eiusmod<br/>tempor<br/>incididunt<br/>ut<br/>labore<br/>et<br/>dolore<br/>magna<br/>aliqua.<br/>Leo<br/>urna<br/>molestie<br/>at<br/>elementum<br/>eu<br/>facilisis.<br/>Quisque<br/>non<br/>tellus<br/>orci<br/>ac<br/>auctor<br/>augue.<br/>Ac<br/>turpis<br/>egestas<br/>sed<br/>tempus<br/>urna<br/>et<br/>pharetra<br/>pharetra<br/>massa.<br/>Elit<br/>eget<br/>gravida<br/>cum<br/>sociis<br/>natoque.<br/>Orci<br/>a<br/>scelerisque<br/>purus<br/>semper.<br/>Pellentesque<br/>dignissim<br/>enim<br/>sit<br/>amet.<br/>Eu<br/>tincidunt<br/>tortor<br/>aliquam<br/>nulla<br/>facilisi<br/>cras<br/>fermentum<br/>odio<br/>eu.<br/>Nunc<br/>mattis<br/>enim<br/>ut<br/>tellus.<br/>Convallis<br/>posuere<br/>morbi<br/>leo<br/>urna<br/>molestie.<br/>Egestas<br/>sed<br/>tempus<br/>urna<br/>et<br/>pharetra.<br/>Aliquet<br/>eget<br/>sit<br/>amet<br/>tellus.
</div>
<div class='col right'>Tincidunt<br/>praesent<br/>semper<br/>feugiat<br/>nibh<br/>sed<br/>pulvinar<br/>proin<br/>gravida.<br/>Phasellus<br/>vestibulum<br/>lorem<br/>sed<br/>risus<br/>ultricies<br/>tristique.<br/>Pellentesque<br/>pulvinar<br/>pellentesque<br/>habitant<br/>morbi<br/>tristique<br/>senectus<br/>et.<br/>Enim<br/>neque<br/>volutpat<br/>ac<br/>tincidunt<br/>vitae<br/>semper<br/>quis<br/>lectus<br/>nulla.<br/>Commodo<br/>viverra<br/>maecenas<br/>accumsan<br/>lacus<br/>vel<br/>facilisis<br/>volutpat<br/>est<br/>velit.
</div>
</div>
This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 1 year ago.
I know this question came up many times but none of the suggested solutions is working for me so I opened up a code sandbox and made an example.
What I want
I want to place an item at the bottom of a div that already has some children. In my code sandbox the desired div to place at the bottom has a className of note__actions.
What I tried
Setting the container to position: relative and the item to position: absolute
Using flexbox and setting the container to display: flex and the item to align-self: flex-end
Multiple other things like vertical-align, and making empty items to fill up the space in-between
Div setup
<div className="flexBoxContainer" />
<div className="item">
<div>Header</div>
<div>Title</div>
<div>Paragraph</div>
<div className="bottom__item">Actions>/div> // Only this div should go to the bottom of the "item" div
</div>
</div>
Sandbox
https://codesandbox.io/s/infallible-sound-wf166?file=/src/App.js
Give the container display: flex; justify-content: space-between; - that will pull the content from top to bottom. If you want only the last item on the bottom and the rest on the top, wrap the rest with a div, like
<div className="flexBoxContainer" />
<div className="item">
<div>
<div>Header</div>
<div>Title</div>
<div>Paragraph</div>
</div>
<div className="bottom__item">Actions>/div>
</div>
</div>
You are already using flexbox which is great. Now, use margin-top:auto on note__actions to push the div to the bottom.
.note__actions {
margin-top: auto;
}
You could add a flex: 1; to the flexible content, for instance
.note {
background-color: rgb(255, 255, 211);
border-radius: 15px;
height: 400px;
width: 200px;
padding: 24px;
display: flex;
flex-direction: column;
}
.note-content {
flex: 1;
}
.note-action {
align-self: flex-end;
}
<div class="note">
<div>Peter Holbert - 09. Jul 21 23:28</div>
<div>Note Title</div>
<div class="note-content">Aldus PageMaker including versions of Lorem Ipsum.</div>
<div class="note-action">Action 1</div>
</div>
You can use a grid for that.
Change this in your Style on codesandbox.io:
.note {
background-color: rgb(255, 255, 211);
border-radius: 15px;
width: 200px;
padding: 24px;
display: grid;
/*flex-direction: column;*/
grid-template-rows: 50px 80px 1fr 80px; /* height: 100%; */
/* align-items: flex-end; */
/* position: relative; */
}
.note__actions_bot {
background-color: red;
height: 100px;
display: grid;
justify-content: center;
align-items: center;
}
I recommend this awesome Video to get a nice overview and some hint's how to do, and when to use which one of flex, grid and other solutions:
https://www.youtube.com/watch?v=qm0IfG1GyZU&feature=youtu.be
Add margin-top : auto to the note__actions element
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following situation, my left DIV contains my logo, which has a fixed width. My right DIV contains my menu, which does not have a fixed width. And finally, my center DIV contains a search field.
I would like my center DIV to use all the horizontal space that is left while the right DIV's width to be the minimum possible, so basically to have the menu's width. But since menu items can change, I can't set a fixed width for the right DIV.
Is there a nice CSS solution for this?
|--- Logo ---| |--- Search Bar ---| |--- Menu Items ---|
<div id="container">
<h1 id="logo">
<img src="logo.svg" alt="Logo">
</h1>
<div id="search">
<input type="text" name="search">
</div>
<div id="menu">
Home
Blog
Users
About Us
</div>
</div>
Using flex-box
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
margin: 10px auto;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
border: 1px solid grey;
}
.logo img {
display: block;
padding: 0;
margin: 0;
}
.container > * {
flex: 0 1 auto
}
.logo {
flex-grow: 0;
background: red;
margin: 0;
}
.search {
background: lightblue;
text-align: center;
align-items: center;
display: flex;
flex-grow: 1;
padding: 10px;
}
.search input {
width: 100%;
}
.menu {
display: flex;
flex-direction: row;
align-items: center;
text-align: center;
}
.menu a {
flex: ;
height: 35px;
line-height: 35px;
background: orange;
}
<div class="container">
<h1 class="logo">
<img src="http://lorempixel.com/output/city-q-c-200-150-8.jpg" alt="Logo">
</h1>
<div class="search">
<input type="text" name="search">
</div>
<div class="menu">
Home
Blog
Users
About Us
</div>
</div>
FlexBox - Codepen Demo
I you don't want to use flexbox (due to the compatibility or other reasons), you can try with display property, and table-cell value:
.row {
display: table-row;
}
.left {
display: table-cell;
width: 100px;
}
.center, right {
display: table-cell;
}
And html
<div class="row">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>