简体   繁体   中英

CSS Help for a project. Page needs to have a 'form' element that is 600px when the window is wider than 600px and

CSS Help for a project. Page needs to have a 'form' element that is 600px when the window is wider than 600px and is the width of the screen when the screen is less than 600px wide.

It seems to work when the screen is more that 600px, but will not stay the width of the screen when the screen shrinks below 600px.

 .card { height: 335px; width: 344px; border: lightgrey; border-style: solid; margin-left: auto; margin-right: auto; font-family: Arial, Helvetica, sans-serif; transition: box-shadow; }.card:hover { box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.3); }.card-body { padding: 16px; font-size: 11px; color: #232f34; }.card_image { width: 100%; height: 194px; }.person { height: 50px; border-radius: 40px; float: left; margin: 0 1rem; }.title { display: inline; color: #000; font-size: 22px; padding: 16px; }.secondary { color: #232f34; display: inline; padding: 16px; }.myform { max-width: 600px; } @media screen { nav { position: static; height: auto; } } @media screen and (min-width: 600px) { form { display: grid; grid-template-columns: 600px; } }
 <form method="post" action="/pets"> <label for="name">Name</label> <input name="pet_name" id="name" type="text" /> <br> <label for="type">Type</label> <select name="pet_type" id="type"> <option value="Cat">Cat</option> <option value="Dog">Dog</option> <option value="Hamster">Hamster</option> <option value="Other">Other</option> <option value="Zebra">Zebra</option> </select> <br> <label for="bio">Biography</label> <textarea name="pet_bio" id="bio"></textarea> <br> <label for="owner-email">Owner's Email</label> <input name="pet_owner_email" id="owner-email" type="text" /> <br> <button class="new-pet-submit-button" type="submit"> Create new pet </button> <button class="Reset" type="reset">Reset</button> <form class='myform'> My form. </form> </form> <br> <br> <div class="card"> <img src="images/desert.jpg" id="desert" class="card_image"> <img src="images/person-avatar.jpg" class="person"> <h1 class="title">Title goes here</h1> <h3 class="secondary">Secondary text</h3> <p class="card-body"> Greyhound divisively hello coldly wonderfully marginally far upon excluding.</p> </div>

You don't need a media query for that. Just use max-width .

 body { margin: 0; }.myform { border: 1px solid blue; margin-inline: auto; padding: 1rem; /* here's where the magic happens */ max-width: 600px; }
 <form class = 'myform'> My form! </form>

Edited to add If the behaviour when the screen is greater than 600px is the one you want then set the grid element to max-width: 600px and set the grid-template-columns to 1fr as below. I also made the margins to the left and right auto to centre the form alongside your card.

 document.getElementsByTagName("h1")[0].style.fontSize = "6vw";
 .card { height: 335px; width: 344px; border: lightgrey; border-style: solid; margin-left: auto; margin-right: auto; font-family: Arial, Helvetica, sans-serif; transition: box-shadow; }.card:hover { box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.3); }.card-body { padding: 16px; font-size: 11px; color: #232f34; }.card_image { width: 100%; height: 194px; }.person { height: 50px; border-radius: 40px; float: left; margin: 0 1rem; }.title { display: inline; color: #000; font-size: 22px; padding: 16px; }.secondary { color: #232f34; display: inline; padding: 16px; } /* got rid of the media query and added this */ form { margin-inline: auto; display: grid; grid-template-columns: 1fr; max-width: 600px; }
 <form method="post" action="/pets"> <label for="name">Name</label> <input name="pet_name" id="name" type="text" /> <br> <label for="type">Type</label> <select name="pet_type" id="type"> <option value="Cat">Cat</option> <option value="Dog">Dog</option> <option value="Hamster">Hamster</option> <option value="Other">Other</option> <option value="Zebra">Zebra</option> </select> <br> <label for="bio">Biography</label> <textarea name="pet_bio" id="bio"></textarea> <br> <label for="owner-email">Owner's Email</label> <input name="pet_owner_email" id="owner-email" type="text" /> <br> <button class="new-pet-submit-button" type="submit"> Create new pet </button> <button class="Reset" type="reset">Reset</button> </form> <br> <br> <div class="card"> <img src="https://picsum.photos/id/46/200/300" id="desert" class="card_image"> <img src="https://picsum.photos/id/64/200/300" class="person"> <h1 class="title">Title goes here</h1> <h3 class="secondary">Secondary text</h3> <p class="card-body"> Greyhound divisively hello coldly wonderfully marginally far upon excluding.</p> </div>

To make a form element have a width of 600px when the window is wider than 600px, you can use a media query in your CSS: This media query will apply the styles inside it only when the viewport width is greater than or equal to 600px. To make the form element have a different width when the window is less than 600px wide, you can use another media query:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM